Biiig standardization of whitespace. 2 space indents now, no tabs.
[zxing.git] / core / src / com / google / zxing / client / result / AbstractDoCoMoResult.java
1 /*
2  * Copyright 2007 Google Inc.
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 java.util.Vector;
20
21 /**
22  * See
23  * <a href="http://www.nttdocomo.co.jp/english/service/imode/make/content/barcode/about/s2.html">
24  * DoCoMo's documentation</a> about the result types represented by subclasses of this class.
25  *
26  * @author srowen@google.com (Sean Owen)
27  */
28 abstract class AbstractDoCoMoResult extends ParsedReaderResult {
29
30   AbstractDoCoMoResult(ParsedReaderResultType type) {
31     super(type);
32   }
33
34   // This could as well be implemented with java.util.regex. It was already implemented partially
35   // to run in a J2ME enviroment, where this unavailable.
36
37   static String[] matchPrefixedField(String prefix, String rawText) {
38     Vector matches = null;
39     int i = 0;
40     int max = rawText.length();
41     while (i < max) {
42       i = rawText.indexOf(prefix, i);
43       if (i < 0) {
44         break;
45       }
46       i += prefix.length(); // Skip past this prefix we found to start
47       int start = i; // Found the start of a match here
48       boolean done = false;
49       while (!done) {
50         i = rawText.indexOf((int) ';', i);
51         if (i < 0) {
52           // No terminating semicolon? uh, done. Set i such that loop terminates and break
53           i = rawText.length();
54           done = true;
55         } else if (rawText.charAt(i - 1) == '\\') {
56           // semicolon was escaped so continue
57           i++;
58         } else {
59           // found a match
60           if (matches == null) {
61             matches = new Vector(3); // lazy init
62           }
63           matches.addElement(unescape(rawText.substring(start, i)));
64           i++;
65           done = true;
66         }
67       }
68     }
69     if (matches == null) {
70       return null;
71     }
72     int size = matches.size();
73     String[] result = new String[size];
74     for (int j = 0; j < size; j++) {
75       result[j] = (String) matches.elementAt(j);
76     }
77     return result;
78   }
79
80   static String matchSinglePrefixedField(String prefix, String rawText) {
81     String[] matches = matchPrefixedField(prefix, rawText);
82     return matches == null ? null : matches[0];
83   }
84
85   static String[] matchRequiredPrefixedField(String prefix, String rawText) {
86     String[] result = matchPrefixedField(prefix, rawText);
87     if (result == null) {
88       throw new IllegalArgumentException("Did not match prefix " + prefix);
89     }
90     return result;
91   }
92
93   private static String unescape(String escaped) {
94     if (escaped != null) {
95       int backslash = escaped.indexOf((int) '\\');
96       if (backslash >= 0) {
97         int max = escaped.length();
98         StringBuffer unescaped = new StringBuffer(max - 1);
99         unescaped.append(escaped.toCharArray(), 0, backslash);
100         boolean nextIsEscaped = false;
101         for (int i = backslash; i < max; i++) {
102           char c = escaped.charAt(i);
103           if (nextIsEscaped || c != '\\') {
104             unescaped.append(c);
105             nextIsEscaped = false;
106           } else {
107             nextIsEscaped = true;
108           }
109         }
110         return unescaped.toString();
111       }
112     }
113     return escaped;
114   }
115
116   static void maybeAppend(String value, StringBuffer result) {
117     if (value != null) {
118       result.append('\n');
119       result.append(value);
120     }
121   }
122
123 }