git-svn-id: http://zxing.googlecode.com/svn/trunk@290 59b500cc-1b3d-0410-9834-0bbf25f...
[zxing.git] / core / src / com / google / zxing / client / result / AbstractDoCoMoParsedResult.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  * <p>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.</p>
25  *
26  * <p>Thanks to Jeff Griffin for proposing rewrite of these classes that relies less
27  * on exception-based mechanisms during parsing.</p>
28  *
29  * @author srowen@google.com (Sean Owen)
30  */
31 abstract class AbstractDoCoMoParsedResult extends ParsedReaderResult {
32
33   AbstractDoCoMoParsedResult(ParsedReaderResultType type) {
34     super(type);
35   }
36
37   // This could as well be implemented with java.util.regex. It was already implemented partially
38   // to run in a J2ME enviroment, where this unavailable.
39
40   static String[] matchPrefixedField(String prefix, String rawText) {
41     return matchPrefixedField(prefix, rawText, ';');
42   }
43
44   static String[] matchPrefixedField(String prefix, String rawText, char endChar) {
45     Vector matches = null;
46     int i = 0;
47     int max = rawText.length();
48     while (i < max) {
49       i = rawText.indexOf(prefix, i);
50       if (i < 0) {
51         break;
52       }
53       i += prefix.length(); // Skip past this prefix we found to start
54       int start = i; // Found the start of a match here
55       boolean done = false;
56       while (!done) {
57         i = rawText.indexOf((int) endChar, i);
58         if (i < 0) {
59           // No terminating end character? uh, done. Set i such that loop terminates and break
60           i = rawText.length();
61           done = true;
62         } else if (rawText.charAt(i - 1) == '\\') {
63           // semicolon was escaped so continue
64           i++;
65         } else {
66           // found a match
67           if (matches == null) {
68             matches = new Vector(3); // lazy init
69           }
70           matches.addElement(unescape(rawText.substring(start, i)));
71           i++;
72           done = true;
73         }
74       }
75     }
76     if (matches == null || matches.isEmpty()) {
77       return null;
78     }
79     int size = matches.size();
80     String[] result = new String[size];
81     for (int j = 0; j < size; j++) {
82       result[j] = (String) matches.elementAt(j);
83     }
84     return result;
85   }
86
87   static String matchSinglePrefixedField(String prefix, String rawText) {
88     return matchSinglePrefixedField(prefix, rawText, ';');
89   }
90
91   static String matchSinglePrefixedField(String prefix, String rawText, char endChar) {
92     String[] matches = matchPrefixedField(prefix, rawText, endChar);
93     return matches == null ? null : matches[0];
94   }
95
96   private static String unescape(String escaped) {
97     if (escaped != null) {
98       int backslash = escaped.indexOf((int) '\\');
99       if (backslash >= 0) {
100         int max = escaped.length();
101         StringBuffer unescaped = new StringBuffer(max - 1);
102         unescaped.append(escaped.toCharArray(), 0, backslash);
103         boolean nextIsEscaped = false;
104         for (int i = backslash; i < max; i++) {
105           char c = escaped.charAt(i);
106           if (nextIsEscaped || c != '\\') {
107             unescaped.append(c);
108             nextIsEscaped = false;
109           } else {
110             nextIsEscaped = true;
111           }
112         }
113         return unescaped.toString();
114       }
115     }
116     return escaped;
117   }
118
119   static void maybeAppend(String value, StringBuffer result) {
120     if (value != null) {
121       result.append('\n');
122       result.append(value);
123     }
124   }
125
126   static void maybeAppend(String[] value, StringBuffer result) {
127     if (value != null) {
128       for (int i = 0; i < value.length; i++) {
129         result.append('\n');
130         result.append(value[i]);
131       }
132     }
133   }
134
135 }