New C# port from Suraj Supekar
[zxing.git] / csharp / client / result / ProductResultParser.cs
1 /*\r
2 * Copyright 2007 ZXing authors\r
3 *\r
4 * Licensed under the Apache License, Version 2.0 (the "License");\r
5 * you may not use this file except in compliance with the License.\r
6 * You may obtain a copy of the License at\r
7 *\r
8 *      http://www.apache.org/licenses/LICENSE-2.0\r
9 *\r
10 * Unless required by applicable law or agreed to in writing, software\r
11 * distributed under the License is distributed on an "AS IS" BASIS,\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13 * See the License for the specific language governing permissions and\r
14 * limitations under the License.\r
15 */\r
16 using System;\r
17 using BarcodeFormat = com.google.zxing.BarcodeFormat;\r
18 using Result = com.google.zxing.Result;\r
19 using UPCEReader = com.google.zxing.oned.UPCEReader;\r
20 namespace com.google.zxing.client.result\r
21 {\r
22         \r
23         /// <summary> Parses strings of digits that represent a UPC code.\r
24         /// \r
25         /// </summary>\r
26         /// <author>  dswitkin@google.com (Daniel Switkin)\r
27         /// </author>\r
28         /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source \r
29         /// </author>\r
30         sealed class ProductResultParser:ResultParser\r
31         {\r
32                 \r
33                 private ProductResultParser()\r
34                 {\r
35                 }\r
36                 \r
37                 // Treat all UPC and EAN variants as UPCs, in the sense that they are all product barcodes.\r
38                 public static ProductParsedResult parse(Result result)\r
39                 {\r
40                         BarcodeFormat format = result.BarcodeFormat;\r
41                         if (!(BarcodeFormat.UPC_A.Equals(format) || BarcodeFormat.UPC_E.Equals(format) || BarcodeFormat.EAN_8.Equals(format) || BarcodeFormat.EAN_13.Equals(format)))\r
42                         {\r
43                                 return null;\r
44                         }\r
45                         // Really neither of these should happen:\r
46                         System.String rawText = result.Text;\r
47                         if (rawText == null)\r
48                         {\r
49                                 return null;\r
50                         }\r
51                         \r
52                         int length = rawText.Length;\r
53                         for (int x = 0; x < length; x++)\r
54                         {\r
55                                 char c = rawText[x];\r
56                                 if (c < '0' || c > '9')\r
57                                 {\r
58                                         return null;\r
59                                 }\r
60                         }\r
61                         // Not actually checking the checksum again here    \r
62                         \r
63                         System.String normalizedProductID;\r
64                         // Expand UPC-E for purposes of searching\r
65                         if (BarcodeFormat.UPC_E.Equals(format))\r
66                         {\r
67                                 normalizedProductID = UPCEReader.convertUPCEtoUPCA(rawText);\r
68                         }\r
69                         else\r
70                         {\r
71                                 normalizedProductID = rawText;\r
72                         }\r
73                         \r
74                         return new ProductParsedResult(rawText, normalizedProductID);\r
75                 }\r
76         }\r
77 }