One more change to accommodate differences in jpeg libraries. Some machines will...
[zxing.git] / csharp / client / result / GeoResultParser.cs
1 /*\r
2 * Copyright 2008 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 Result = com.google.zxing.Result;\r
18 namespace com.google.zxing.client.result\r
19 {\r
20         \r
21         /// <summary> Parses a "geo:" URI result, which specifies a location on the surface of\r
22         /// the Earth as well as an optional altitude above the surface. See\r
23         /// <a href="http://tools.ietf.org/html/draft-mayrhofer-geo-uri-00">\r
24         /// http://tools.ietf.org/html/draft-mayrhofer-geo-uri-00</a>.\r
25         /// \r
26         /// </summary>\r
27         /// <author>  Sean Owen\r
28         /// </author>\r
29         /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source \r
30         /// </author>\r
31         sealed class GeoResultParser:ResultParser\r
32         {\r
33                 \r
34                 private GeoResultParser()\r
35                 {\r
36                 }\r
37                 \r
38                 public static GeoParsedResult parse(Result result)\r
39                 {\r
40                         System.String rawText = result.Text;\r
41                         if (rawText == null || (!rawText.StartsWith("geo:") && !rawText.StartsWith("GEO:")))\r
42                         {\r
43                                 return null;\r
44                         }\r
45                         // Drop geo, query portion\r
46                         //UPGRADE_WARNING: Method 'java.lang.String.indexOf' was converted to 'System.String.IndexOf' which may throw an exception. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1101'"\r
47                         int queryStart = rawText.IndexOf('?', 4);\r
48                         System.String geoURIWithoutQuery = queryStart < 0?rawText.Substring(4):rawText.Substring(4, (queryStart) - (4));\r
49                         int latitudeEnd = geoURIWithoutQuery.IndexOf(',');\r
50                         if (latitudeEnd < 0)\r
51                         {\r
52                                 return null;\r
53                         }\r
54                         //UPGRADE_WARNING: Method 'java.lang.String.indexOf' was converted to 'System.String.IndexOf' which may throw an exception. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1101'"\r
55                         int longitudeEnd = geoURIWithoutQuery.IndexOf(',', latitudeEnd + 1);\r
56                         double latitude, longitude, altitude;\r
57                         try\r
58                         {\r
59                                 latitude = System.Double.Parse(geoURIWithoutQuery.Substring(0, (latitudeEnd) - (0)));\r
60                                 if (longitudeEnd < 0)\r
61                                 {\r
62                                         longitude = System.Double.Parse(geoURIWithoutQuery.Substring(latitudeEnd + 1));\r
63                                         altitude = 0.0;\r
64                                 }\r
65                                 else\r
66                                 {\r
67                                         longitude = System.Double.Parse(geoURIWithoutQuery.Substring(latitudeEnd + 1, (longitudeEnd) - (latitudeEnd + 1)));\r
68                                         altitude = System.Double.Parse(geoURIWithoutQuery.Substring(longitudeEnd + 1));\r
69                                 }\r
70                         }\r
71                         catch (System.FormatException nfe)\r
72                         {\r
73                                 return null;\r
74                         }\r
75                         return new GeoParsedResult(rawText.StartsWith("GEO:")?"geo:" + rawText.Substring(4):rawText, latitude, longitude, altitude);\r
76                 }\r
77         }\r
78 }