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;
22 import java.util.Vector;
25 * <p>Parses an "sms:" URI result, which specifies a number to SMS.
26 * See <a href="http://tools.ietf.org/html/rfc5724"> RFC 5724</a> on this.</p>
28 * <p>This class supports "via" syntax for numbers, which is not part of the spec.
29 * For example "+12125551212;via=+12124440101" may appear as a number.
30 * It also supports a "subject" query parameter, which is not mentioned in the spec.
31 * These are included since they were mentioned in earlier IETF drafts and might be
34 * <p>This actually also parses URIs starting with "mms:" and treats them all the same way,
35 * and effectively converts them to an "sms:" URI for purposes of forwarding to the platform.</p>
39 final class SMSMMSResultParser extends ResultParser {
41 private SMSMMSResultParser() {
44 public static SMSParsedResult parse(Result result) {
45 String rawText = result.getText();
46 if (rawText == null) {
49 if (!(rawText.startsWith("sms:") || rawText.startsWith("SMS:") ||
50 rawText.startsWith("mms:") || rawText.startsWith("MMS:"))) {
54 // Check up front if this is a URI syntax string with query arguments
55 Hashtable nameValuePairs = parseNameValuePairs(rawText);
56 String subject = null;
58 boolean querySyntax = false;
59 if (nameValuePairs != null && !nameValuePairs.isEmpty()) {
60 subject = (String) nameValuePairs.get("subject");
61 body = (String) nameValuePairs.get("body");
65 // Drop sms, query portion
66 int queryStart = rawText.indexOf('?', 4);
67 String smsURIWithoutQuery;
68 // If it's not query syntax, the question mark is part of the subject or message
69 if (queryStart < 0 || !querySyntax) {
70 smsURIWithoutQuery = rawText.substring(4);
72 smsURIWithoutQuery = rawText.substring(4, queryStart);
77 Vector numbers = new Vector(1);
78 Vector vias = new Vector(1);
79 while ((comma = smsURIWithoutQuery.indexOf(',', lastComma + 1)) > lastComma) {
80 String numberPart = smsURIWithoutQuery.substring(lastComma + 1, comma);
81 addNumberVia(numbers, vias, numberPart);
84 addNumberVia(numbers, vias, smsURIWithoutQuery.substring(lastComma + 1));
86 return new SMSParsedResult(toStringArray(numbers), toStringArray(vias), subject, body);
89 private static void addNumberVia(Vector numbers, Vector vias, String numberPart) {
90 int numberEnd = numberPart.indexOf(';');
92 numbers.addElement(numberPart);
93 vias.addElement(null);
95 numbers.addElement(numberPart.substring(0, numberEnd));
96 String maybeVia = numberPart.substring(numberEnd + 1);
98 if (maybeVia.startsWith("via=")) {
99 via = maybeVia.substring(4);
103 vias.addElement(via);