Big refactoring of ParsedResult: now split into ResultParser and ParsedResult classes...
[zxing.git] / core / src / com / google / zxing / client / result / AbstractDoCoMoResultParser.java
1 /*
2  * Copyright 2007 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 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 AbstractDoCoMoResultParser extends ResultParser {
32
33   // This could as well be implemented with java.util.regex. It was already implemented partially
34   // to run in a J2ME enviroment, where this unavailable.
35
36   static String[] matchPrefixedField(String prefix, String rawText) {
37     return matchPrefixedField(prefix, rawText, ';');
38   }
39
40   static String[] matchPrefixedField(String prefix, String rawText, char endChar) {
41     Vector matches = null;
42     int i = 0;
43     int max = rawText.length();
44     while (i < max) {
45       i = rawText.indexOf(prefix, i);
46       if (i < 0) {
47         break;
48       }
49       i += prefix.length(); // Skip past this prefix we found to start
50       int start = i; // Found the start of a match here
51       boolean done = false;
52       while (!done) {
53         i = rawText.indexOf((int) endChar, i);
54         if (i < 0) {
55           // No terminating end character? uh, done. Set i such that loop terminates and break
56           i = rawText.length();
57           done = true;
58         } else if (rawText.charAt(i - 1) == '\\') {
59           // semicolon was escaped so continue
60           i++;
61         } else {
62           // found a match
63           if (matches == null) {
64             matches = new Vector(3); // lazy init
65           }
66           matches.addElement(unescapeBackslash(rawText.substring(start, i)));
67           i++;
68           done = true;
69         }
70       }
71     }
72     if (matches == null || matches.isEmpty()) {
73       return null;
74     }
75     int size = matches.size();
76     String[] result = new String[size];
77     for (int j = 0; j < size; j++) {
78       result[j] = (String) matches.elementAt(j);
79     }
80     return result;
81   }
82
83   static String matchSinglePrefixedField(String prefix, String rawText) {
84     return matchSinglePrefixedField(prefix, rawText, ';');
85   }
86
87   static String matchSinglePrefixedField(String prefix, String rawText, char endChar) {
88     String[] matches = matchPrefixedField(prefix, rawText, endChar);
89     return matches == null ? null : matches[0];
90   }
91
92
93 }