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>
32 * @author srowen@google.com (Sean Owen)
34 public 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("mms:")) {
47 } else if (rawText.startsWith("smsto:") || rawText.startsWith("SMSTO:") ||
48 rawText.startsWith("mmsto:") || rawText.startsWith("MMSTO:")) {
53 // Drop sms, query portion
54 int queryStart = rawText.indexOf('?', prefixLength);
55 String smsURIWithoutQuery;
57 smsURIWithoutQuery = rawText.substring(prefixLength);
59 smsURIWithoutQuery = rawText.substring(prefixLength, queryStart);
61 int numberEnd = smsURIWithoutQuery.indexOf(';');
65 number = smsURIWithoutQuery;
68 number = smsURIWithoutQuery.substring(0, numberEnd);
69 String maybeVia = smsURIWithoutQuery.substring(numberEnd + 1);
70 if (maybeVia.startsWith("via=")) {
71 via = maybeVia.substring(4);
76 Hashtable nameValuePairs = parseNameValuePairs(rawText);
77 String subject = null;
79 if (nameValuePairs != null) {
80 subject = (String) nameValuePairs.get("subject");
81 body = (String) nameValuePairs.get("body");
83 return new SMSParsedResult("sms:" + number, number, via, subject, body, null);