Support SMTP URLs
[zxing.git] / core / src / com / google / zxing / client / result / SMSMMSResultParser.java
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
17 package com.google.zxing.client.result;
18
19 import com.google.zxing.Result;
20
21 import java.util.Hashtable;
22 import java.util.Vector;
23
24 /**
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>
27  *
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
32  * used.</p>
33  *
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>
36  *
37  * @author Sean Owen
38  */
39 final class SMSMMSResultParser extends ResultParser {
40
41   private SMSMMSResultParser() {
42   }
43
44   public static SMSParsedResult parse(Result result) {
45     String rawText = result.getText();
46     if (rawText == null) {
47       return null;
48     }
49     if (!(rawText.startsWith("sms:") || rawText.startsWith("SMS:") ||
50           rawText.startsWith("mms:") || rawText.startsWith("MMS:"))) {
51       return null;
52     }
53
54     // Check up front if this is a URI syntax string with query arguments
55     Hashtable nameValuePairs = parseNameValuePairs(rawText);
56     String subject = null;
57     String body = null;
58     boolean querySyntax = false;
59     if (nameValuePairs != null && !nameValuePairs.isEmpty()) {
60       subject = (String) nameValuePairs.get("subject");
61       body = (String) nameValuePairs.get("body");
62       querySyntax = true;
63     }
64
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);
71     } else {
72       smsURIWithoutQuery = rawText.substring(4, queryStart);
73     }
74
75     int lastComma = -1;
76     int comma;
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);
82       lastComma = comma;
83     }
84     addNumberVia(numbers, vias, smsURIWithoutQuery.substring(lastComma + 1));    
85
86     return new SMSParsedResult(toStringArray(numbers), toStringArray(vias), subject, body);
87   }
88
89   private static void addNumberVia(Vector numbers, Vector vias, String numberPart) {
90     int numberEnd = numberPart.indexOf(';');
91     if (numberEnd < 0) {
92       numbers.addElement(numberPart);
93       vias.addElement(null);
94     } else {
95       numbers.addElement(numberPart.substring(0, numberEnd));
96       String maybeVia = numberPart.substring(numberEnd + 1);
97       String via;
98       if (maybeVia.startsWith("via=")) {
99         via = maybeVia.substring(4);
100       } else {
101         via = null;
102       }
103       vias.addElement(via);
104     }
105   }
106
107 }