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