2 * Copyright 2008 ZXing authors
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.zxing.client.result;
19 import com.google.zxing.Result;
21 import java.util.Hashtable;
24 * <p>Parses an "sms:" URI result, which specifies a number to SMS and optional
25 * "via" number. See <a href="http://gbiv.com/protocols/uri/drafts/draft-antti-gsm-sms-url-04.txt">
26 * the IETF draft</a> on this.</p>
28 * <p>This actually also parses URIs starting with "mms:", "smsto:", "mmsto:", "SMSTO:", and
29 * "MMSTO:", and treats them all the same way, and effectively converts them to an "sms:" URI
30 * for purposes of forwarding to the platform.</p>
34 final class SMSMMSResultParser extends ResultParser {
36 private SMSMMSResultParser() {
39 public static SMSParsedResult parse(Result result) {
40 String rawText = result.getText();
41 if (rawText == null) {
45 if (rawText.startsWith("sms:") || rawText.startsWith("SMS:") ||
46 rawText.startsWith("mms:") || rawText.startsWith("MMS:")) {
48 } else if (rawText.startsWith("smsto:") || rawText.startsWith("SMSTO:") ||
49 rawText.startsWith("mmsto:") || rawText.startsWith("MMSTO:")) {
55 // Check up front if this is a URI syntax string with query arguments
56 Hashtable nameValuePairs = parseNameValuePairs(rawText);
57 String subject = null;
59 boolean querySyntax = false;
60 if (nameValuePairs != null && nameValuePairs.size() > 0) {
61 subject = (String) nameValuePairs.get("subject");
62 body = (String) nameValuePairs.get("body");
66 // Drop sms, query portion
67 int queryStart = rawText.indexOf('?', prefixLength);
68 String smsURIWithoutQuery;
69 // If it's not query syntax, the question mark is part of the subject or message
70 if (queryStart < 0 || !querySyntax) {
71 smsURIWithoutQuery = rawText.substring(prefixLength);
73 smsURIWithoutQuery = rawText.substring(prefixLength, queryStart);
75 int numberEnd = smsURIWithoutQuery.indexOf(';');
79 number = smsURIWithoutQuery;
82 number = smsURIWithoutQuery.substring(0, numberEnd);
83 String maybeVia = smsURIWithoutQuery.substring(numberEnd + 1);
84 if (maybeVia.startsWith("via=")) {
85 via = maybeVia.substring(4);
91 // Thanks to dominik.wild for suggesting this enhancement to support
92 // smsto:number:body URIs
94 int bodyStart = number.indexOf(':');
96 body = number.substring(bodyStart + 1);
97 number = number.substring(0, bodyStart);
100 return new SMSParsedResult("sms:" + number, number, via, subject, body, null);