Small fixes for 1.6 release
[zxing.git] / csharp / client / result / SMSMMSResultParser.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> <p>Parses an "sms:" URI result, which specifies a number to SMS and optional\r
22         /// "via" number. See <a href="http://gbiv.com/protocols/uri/drafts/draft-antti-gsm-sms-url-04.txt">\r
23         /// the IETF draft</a> on this.</p>\r
24         /// \r
25         /// <p>This actually also parses URIs starting with "mms:", "smsto:", "mmsto:", "SMSTO:", and\r
26         /// "MMSTO:", and treats them all the same way, and effectively converts them to an "sms:" URI\r
27         /// for purposes of forwarding to the platform.</p>\r
28         /// \r
29         /// </summary>\r
30         /// <author>  Sean Owen\r
31         /// </author>\r
32         /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source \r
33         /// </author>\r
34         sealed class SMSMMSResultParser:ResultParser\r
35         {\r
36                 \r
37                 private SMSMMSResultParser()\r
38                 {\r
39                 }\r
40                 \r
41                 public static SMSParsedResult parse(Result result)\r
42                 {\r
43                         System.String rawText = result.Text;\r
44                         if (rawText == null)\r
45                         {\r
46                                 return null;\r
47                         }\r
48                         int prefixLength;\r
49                         if (rawText.StartsWith("sms:") || rawText.StartsWith("SMS:") || rawText.StartsWith("mms:") || rawText.StartsWith("MMS:"))\r
50                         {\r
51                                 prefixLength = 4;\r
52                         }\r
53                         else if (rawText.StartsWith("smsto:") || rawText.StartsWith("SMSTO:") || rawText.StartsWith("mmsto:") || rawText.StartsWith("MMSTO:"))\r
54                         {\r
55                                 prefixLength = 6;\r
56                         }\r
57                         else\r
58                         {\r
59                                 return null;\r
60                         }\r
61                         \r
62                         // Check up front if this is a URI syntax string with query arguments\r
63                         System.Collections.Hashtable nameValuePairs = parseNameValuePairs(rawText);\r
64                         System.String subject = null;\r
65                         System.String body = null;\r
66                         bool querySyntax = false;\r
67                         if (nameValuePairs != null && !(nameValuePairs.Count == 0))\r
68                         {\r
69                                 subject = ((System.String) nameValuePairs["subject"]);\r
70                                 body = ((System.String) nameValuePairs["body"]);\r
71                                 querySyntax = true;\r
72                         }\r
73                         \r
74                         // Drop sms, query portion\r
75                         //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
76                         int queryStart = rawText.IndexOf('?', prefixLength);\r
77                         System.String smsURIWithoutQuery;\r
78                         // If it's not query syntax, the question mark is part of the subject or message\r
79                         if (queryStart < 0 || !querySyntax)\r
80                         {\r
81                                 smsURIWithoutQuery = rawText.Substring(prefixLength);\r
82                         }\r
83                         else\r
84                         {\r
85                                 smsURIWithoutQuery = rawText.Substring(prefixLength, (queryStart) - (prefixLength));\r
86                         }\r
87                         int numberEnd = smsURIWithoutQuery.IndexOf(';');\r
88                         System.String number;\r
89                         System.String via;\r
90                         if (numberEnd < 0)\r
91                         {\r
92                                 number = smsURIWithoutQuery;\r
93                                 via = null;\r
94                         }\r
95                         else\r
96                         {\r
97                                 number = smsURIWithoutQuery.Substring(0, (numberEnd) - (0));\r
98                                 System.String maybeVia = smsURIWithoutQuery.Substring(numberEnd + 1);\r
99                                 if (maybeVia.StartsWith("via="))\r
100                                 {\r
101                                         via = maybeVia.Substring(4);\r
102                                 }\r
103                                 else\r
104                                 {\r
105                                         via = null;\r
106                                 }\r
107                         }\r
108                         \r
109                         // Thanks to dominik.wild for suggesting this enhancement to support\r
110                         // smsto:number:body URIs\r
111                         if (body == null)\r
112                         {\r
113                                 int bodyStart = number.IndexOf(':');\r
114                                 if (bodyStart >= 0)\r
115                                 {\r
116                                         body = number.Substring(bodyStart + 1);\r
117                                         number = number.Substring(0, (bodyStart) - (0));\r
118                                 }\r
119                         }\r
120                         return new SMSParsedResult("sms:" + number, number, via, subject, body, null);\r
121                 }\r
122         }\r
123 }