Standardize and update all copyright statements to name "ZXing authors" as suggested...
[zxing.git] / core / src / com / google / zxing / client / result / SMSParsedResult.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  * Represents a "sms:" URI result, which specifies a number to SMS and optional
23  * "via" number. See <a href="http://gbiv.com/protocols/uri/drafts/draft-antti-gsm-sms-url-04.txt">
24  * the IETF draft</a> on this.
25  *
26  * @author srowen@google.com (Sean Owen)
27  */
28 public final class SMSParsedResult extends ParsedReaderResult {
29
30   private final String smsURI;
31   private final String number;
32   private final String via;
33
34   private SMSParsedResult(String smsURI, String number, String via) {
35     super(ParsedReaderResultType.SMS);
36     this.smsURI = smsURI;
37     this.number = number;
38     this.via = via;
39   }
40
41   public static SMSParsedResult parse(Result result) {
42     String rawText = result.getText();
43     if (rawText == null || !rawText.startsWith("sms:")) {
44       return null;
45     }
46     // Drop sms, query portion
47     int queryStart = rawText.indexOf('?', 4);
48     String smsURIWithoutQuery;
49     if (queryStart < 0) {
50       smsURIWithoutQuery = rawText.substring(4);
51     } else {
52       smsURIWithoutQuery = rawText.substring(4, queryStart);
53     }
54     int numberEnd = smsURIWithoutQuery.indexOf(';');
55     String number;
56     String via;
57     if (numberEnd < 0) {
58       number = smsURIWithoutQuery;
59       via = null;
60     } else {
61       number = smsURIWithoutQuery.substring(0, numberEnd);
62       String maybeVia = smsURIWithoutQuery.substring(numberEnd + 1);
63       if (maybeVia.startsWith("via=")) {
64         via = maybeVia.substring(4);
65       } else {
66         via = null;
67       }
68     }
69     return new SMSParsedResult(rawText, number, via);
70   }
71
72   public String getSMSURI() {
73     return smsURI;
74   }
75
76   public String getNumber() {
77     return number;
78   }
79
80   public String getVia() {
81     return via;
82   }
83
84   public String getDisplayResult() {
85     return number;
86   }
87
88 }