2 * Copyright 2007 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>Abstract class representing the result of decoding a barcode, as more than
26 * a String -- as some type of structured data. This might be a subclass which represents
27 * a URL, or an e-mail address. {@link #parseResult(com.google.zxing.Result)} will turn a raw
28 * decoded string into the most appropriate type of structured representation.</p>
30 * <p>Thanks to Jeff Griffin for proposing rewrite of these classes that relies less
31 * on exception-based mechanisms during parsing.</p>
33 * @author srowen@google.com (Sean Owen)
35 public abstract class ResultParser {
37 public static ParsedResult parseResult(Result theResult) {
38 // This is a bit messy, but given limited options in MIDP / CLDC, this may well be the simplest
39 // way to go about this. For example, we have no reflection available, really.
40 // Order is important here.
42 if ((result = BookmarkDoCoMoResultParser.parse(theResult)) != null) {
44 } else if ((result = AddressBookDoCoMoResultParser.parse(theResult)) != null) {
46 } else if ((result = EmailDoCoMoResultParser.parse(theResult)) != null) {
48 } else if ((result = EmailAddressResultParser.parse(theResult)) != null) {
50 } else if ((result = AddressBookAUResultParser.parse(theResult)) != null) {
52 } else if ((result = VCardResultParser.parse(theResult)) != null) {
54 } else if ((result = BizcardResultParser.parse(theResult)) != null) {
56 } else if ((result = TelResultParser.parse(theResult)) != null) {
58 } else if ((result = SMSMMSResultParser.parse(theResult)) != null) {
60 } else if ((result = GeoResultParser.parse(theResult)) != null) {
62 } else if ((result = URLTOResultParser.parse(theResult)) != null) {
64 } else if ((result = URIResultParser.parse(theResult)) != null) {
66 } else if ((result = UPCResultParser.parse(theResult)) != null) {
69 return new TextParsedResult(theResult.getText(), null);
72 protected static void maybeAppend(String value, StringBuffer result) {
79 protected static void maybeAppend(String[] value, StringBuffer result) {
81 for (int i = 0; i < value.length; i++) {
83 result.append(value[i]);
88 protected static String unescapeBackslash(String escaped) {
89 if (escaped != null) {
90 int backslash = escaped.indexOf((int) '\\');
92 int max = escaped.length();
93 StringBuffer unescaped = new StringBuffer(max - 1);
94 unescaped.append(escaped.toCharArray(), 0, backslash);
95 boolean nextIsEscaped = false;
96 for (int i = backslash; i < max; i++) {
97 char c = escaped.charAt(i);
98 if (nextIsEscaped || c != '\\') {
100 nextIsEscaped = false;
102 nextIsEscaped = true;
105 return unescaped.toString();
111 protected static String urlDecode(String escaped) {
113 // No we can't use java.net.URLDecoder here. JavaME doesn't have it.
114 if (escaped == null) {
117 char[] escapedArray = escaped.toCharArray();
119 int first = findFirstEscape(escapedArray);
124 int max = escapedArray.length;
125 // final length is at most 2 less than original due to at least 1 unescaping
126 StringBuffer unescaped = new StringBuffer(max - 2);
127 // Can append everything up to first escape character
128 unescaped.append(escapedArray, 0, first);
130 for (int i = first; i < max; i++) {
131 char c = escapedArray[i];
133 // + is translated directly into a space
134 unescaped.append(' ');
135 } else if (c == '%') {
136 // Are there even two more chars? if not we will just copy the escaped sequence and be done
138 unescaped.append('%'); // append that % and move on
140 int firstDigitValue = parseHexDigit(escapedArray[++i]);
141 int secondDigitValue = parseHexDigit(escapedArray[++i]);
142 if (firstDigitValue < 0 || secondDigitValue < 0) {
143 // bad digit, just move on
144 unescaped.append('%');
145 unescaped.append(escapedArray[i-1]);
146 unescaped.append(escapedArray[i]);
148 unescaped.append((char) ((firstDigitValue << 4) + secondDigitValue));
154 return unescaped.toString();
157 private static int findFirstEscape(char[] escapedArray) {
158 int max = escapedArray.length;
159 for (int i = 0; i < max; i++) {
160 char c = escapedArray[i];
161 if (c == '+' || c == '%') {
168 private static int parseHexDigit(char c) {
171 return 10 + (c - 'a');
173 } else if (c >= 'A') {
175 return 10 + (c - 'A');
177 } else if (c >= '0') {
185 protected static boolean isStringOfDigits(String value, int length) {
189 int stringLength = value.length();
190 if (length != stringLength) {
193 for (int i = 0; i < length; i++) {
194 char c = value.charAt(i);
195 if (c < '0' || c > '9') {
202 protected static Hashtable parseNameValuePairs(String uri) {
203 int paramStart = uri.indexOf('?');
204 if (paramStart < 0) {
207 Hashtable result = new Hashtable(3);
210 while ((paramEnd = uri.indexOf('&', paramStart)) >= 0) {
211 appendKeyValue(uri, paramStart, paramEnd, result);
212 paramStart = paramEnd + 1;
214 appendKeyValue(uri, paramStart, uri.length(), result);
218 private static void appendKeyValue(String uri, int paramStart, int paramEnd, Hashtable result) {
219 int separator = uri.indexOf('=', paramStart);
220 if (separator >= 0) {
222 String key = uri.substring(paramStart, separator);
223 String value = uri.substring(separator + 1, paramEnd);
224 value = urlDecode(value);
225 result.put(key, value);
228 String key = uri.substring(paramStart, paramEnd);
229 result.put(key, null);
233 static String[] matchPrefixedField(String prefix, String rawText, char endChar) {
234 Vector matches = null;
236 int max = rawText.length();
238 i = rawText.indexOf(prefix, i);
242 i += prefix.length(); // Skip past this prefix we found to start
243 int start = i; // Found the start of a match here
244 boolean done = false;
246 i = rawText.indexOf((int) endChar, i);
248 // No terminating end character? uh, done. Set i such that loop terminates and break
249 i = rawText.length();
251 } else if (rawText.charAt(i - 1) == '\\') {
252 // semicolon was escaped so continue
256 if (matches == null) {
257 matches = new Vector(3); // lazy init
259 matches.addElement(unescapeBackslash(rawText.substring(start, i)));
265 if (matches == null || matches.isEmpty()) {
268 return toStringArray(matches);
271 static String matchSinglePrefixedField(String prefix, String rawText, char endChar) {
272 String[] matches = matchPrefixedField(prefix, rawText, endChar);
273 return matches == null ? null : matches[0];
276 static String[] toStringArray(Vector strings) {
277 int size = strings.size();
278 String[] result = new String[size];
279 for (int j = 0; j < size; j++) {
280 result[j] = (String) strings.elementAt(j);