Update sms: and geo: handling to better respect new RFCs
[zxing.git] / core / src / com / google / zxing / client / result / SMSTOMMSTOResultParser.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 /**
22  * <p>Parses an "smsto:" URI result, whose format is not standardized but appears to be like:
23  * <code>smsto:number(:body)</code>.</p>
24  *
25  * <p>This actually also parses URIs starting with "smsto:", "mmsto:", "SMSTO:", and
26  * "MMSTO:", and treats them all the same way, and effectively converts them to an "sms:" URI
27  * for purposes of forwarding to the platform.</p>
28  *
29  * @author Sean Owen
30  */
31 final class SMSTOMMSTOResultParser extends ResultParser {
32
33   private SMSTOMMSTOResultParser() {
34   }
35
36   public static SMSParsedResult parse(Result result) {
37     String rawText = result.getText();
38     if (rawText == null) {
39       return null;
40     }
41     if (!(rawText.startsWith("smsto:") || rawText.startsWith("SMSTO:") ||
42           rawText.startsWith("mmsto:") || rawText.startsWith("MMSTO:"))) {
43       return null;
44     }
45     // Thanks to dominik.wild for suggesting this enhancement to support
46     // smsto:number:body URIs
47     String number = rawText.substring(6);
48     String body = null;
49     int bodyStart = number.indexOf(':');
50     if (bodyStart >= 0) {
51       body = number.substring(bodyStart + 1);
52       number = number.substring(0, bodyStart);
53     }
54     return new SMSParsedResult(number, null, null, body);
55   }
56
57 }