/silverlight/client/result/SMSMMSResultParser.cs

https://bitbucket.org/jrasanen/silverlightzxing · C# · 123 lines · 83 code · 6 blank · 34 comment · 22 complexity · 02787de5395d71774385cfeae2954e04 MD5 · raw file

  1. /*
  2. * Copyright 2008 ZXing authors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. using System;
  17. using Result = com.google.zxing.Result;
  18. namespace com.google.zxing.client.result
  19. {
  20. /// <summary> <p>Parses an "sms:" URI result, which specifies a number to SMS and optional
  21. /// "via" number. See <a href="http://gbiv.com/protocols/uri/drafts/draft-antti-gsm-sms-url-04.txt">
  22. /// the IETF draft</a> on this.</p>
  23. ///
  24. /// <p>This actually also parses URIs starting with "mms:", "smsto:", "mmsto:", "SMSTO:", and
  25. /// "MMSTO:", and treats them all the same way, and effectively converts them to an "sms:" URI
  26. /// for purposes of forwarding to the platform.</p>
  27. ///
  28. /// </summary>
  29. /// <author> Sean Owen
  30. /// </author>
  31. /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source
  32. /// </author>
  33. sealed class SMSMMSResultParser:ResultParser
  34. {
  35. private SMSMMSResultParser()
  36. {
  37. }
  38. public static SMSParsedResult parse(Result result)
  39. {
  40. System.String rawText = result.Text;
  41. if (rawText == null)
  42. {
  43. return null;
  44. }
  45. int prefixLength;
  46. if (rawText.StartsWith("sms:") || rawText.StartsWith("SMS:") || rawText.StartsWith("mms:") || rawText.StartsWith("MMS:"))
  47. {
  48. prefixLength = 4;
  49. }
  50. else if (rawText.StartsWith("smsto:") || rawText.StartsWith("SMSTO:") || rawText.StartsWith("mmsto:") || rawText.StartsWith("MMSTO:"))
  51. {
  52. prefixLength = 6;
  53. }
  54. else
  55. {
  56. return null;
  57. }
  58. // Check up front if this is a URI syntax string with query arguments
  59. System.Collections.Hashtable nameValuePairs = parseNameValuePairs(rawText);
  60. System.String subject = null;
  61. System.String body = null;
  62. bool querySyntax = false;
  63. if (nameValuePairs != null && !(nameValuePairs.Count == 0))
  64. {
  65. subject = ((System.String) nameValuePairs["subject"]);
  66. body = ((System.String) nameValuePairs["body"]);
  67. querySyntax = true;
  68. }
  69. // Drop sms, query portion
  70. //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'"
  71. int queryStart = rawText.IndexOf('?', prefixLength);
  72. System.String smsURIWithoutQuery;
  73. // If it's not query syntax, the question mark is part of the subject or message
  74. if (queryStart < 0 || !querySyntax)
  75. {
  76. smsURIWithoutQuery = rawText.Substring(prefixLength);
  77. }
  78. else
  79. {
  80. smsURIWithoutQuery = rawText.Substring(prefixLength, (queryStart) - (prefixLength));
  81. }
  82. int numberEnd = smsURIWithoutQuery.IndexOf(';');
  83. System.String number;
  84. System.String via;
  85. if (numberEnd < 0)
  86. {
  87. number = smsURIWithoutQuery;
  88. via = null;
  89. }
  90. else
  91. {
  92. number = smsURIWithoutQuery.Substring(0, (numberEnd) - (0));
  93. System.String maybeVia = smsURIWithoutQuery.Substring(numberEnd + 1);
  94. if (maybeVia.StartsWith("via="))
  95. {
  96. via = maybeVia.Substring(4);
  97. }
  98. else
  99. {
  100. via = null;
  101. }
  102. }
  103. // Thanks to dominik.wild for suggesting this enhancement to support
  104. // smsto:number:body URIs
  105. if (body == null)
  106. {
  107. int bodyStart = number.IndexOf(':');
  108. if (bodyStart >= 0)
  109. {
  110. body = number.Substring(bodyStart + 1);
  111. number = number.Substring(0, (bodyStart) - (0));
  112. }
  113. }
  114. return new SMSParsedResult("sms:" + number, number, via, subject, body, null);
  115. }
  116. }
  117. }