Per dominik.wild, added support for "sms:number:body" format URIs
[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
23 /**
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>
27  *
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>
31  *
32  * @author srowen@google.com (Sean Owen)
33  */
34 final class SMSMMSResultParser extends ResultParser {
35
36   private SMSMMSResultParser() {
37   }
38
39   public static SMSParsedResult parse(Result result) {
40     String rawText = result.getText();
41     if (rawText == null) {
42       return null;
43     }
44     int prefixLength;
45     if (rawText.startsWith("sms:") || rawText.startsWith("mms:")) {
46       prefixLength = 4;
47     } else if (rawText.startsWith("smsto:") || rawText.startsWith("SMSTO:") ||
48                rawText.startsWith("mmsto:") || rawText.startsWith("MMSTO:")) {
49       prefixLength = 6;
50     } else {
51       return null;
52     }
53     // Drop sms, query portion
54     int queryStart = rawText.indexOf('?', prefixLength);
55     String smsURIWithoutQuery;
56     if (queryStart < 0) {
57       smsURIWithoutQuery = rawText.substring(prefixLength);
58     } else {
59       smsURIWithoutQuery = rawText.substring(prefixLength, queryStart);
60     }
61     int numberEnd = smsURIWithoutQuery.indexOf(';');
62     String number;
63     String via;
64     if (numberEnd < 0) {
65       number = smsURIWithoutQuery;
66       via = null;
67     } else {
68       number = smsURIWithoutQuery.substring(0, numberEnd);
69       String maybeVia = smsURIWithoutQuery.substring(numberEnd + 1);
70       if (maybeVia.startsWith("via=")) {
71         via = maybeVia.substring(4);
72       } else {
73         via = null;
74       }
75     }
76     Hashtable nameValuePairs = parseNameValuePairs(rawText);
77     String subject = null;
78     String body = null;
79     if (nameValuePairs != null) {
80       subject = (String) nameValuePairs.get("subject");
81       body = (String) nameValuePairs.get("body");
82     }
83     // Thanks to dominik.wild for suggesting this enhancement to support
84     // smsto:number:body URIs
85     if (body == null) {
86       int bodyStart = number.indexOf(':');
87       if (bodyStart >= 0) {
88         body = number.substring(bodyStart + 1);
89         number = number.substring(0, bodyStart);
90       }
91     }
92     return new SMSParsedResult("sms:" + number, number, via, subject, body, null);
93   }
94
95 }