2 * Copyright 2007 Google Inc.
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 java.util.ArrayList;
20 import java.util.List;
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.
27 * @author srowen@google.com (Sean Owen)
29 abstract class AbstractDoCoMoResult extends ParsedReaderResult {
31 AbstractDoCoMoResult(ParsedReaderResultType type) {
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.
38 static String[] matchPrefixedField(String prefix, String rawText) {
39 List<String> matches = null;
41 int max = rawText.length();
43 i = rawText.indexOf(prefix, i);
47 i += prefix.length(); // Skip past this prefix we found to start
48 int start = i; // Found the start of a match here
51 i = rawText.indexOf((int) ';', i);
53 // No terminating semicolon? uh, done. Set i such that loop terminates and break
56 } else if (rawText.charAt(i-1) == '\\') {
57 // semicolon was escaped so continue
61 if (matches == null) {
62 matches = new ArrayList<String>(3); // lazy init
64 matches.add(unescape(rawText.substring(start, i)));
70 if (matches == null) {
73 return matches.toArray(new String[matches.size()]);
76 static String matchSinglePrefixedField(String prefix, String rawText) {
77 String[] matches = matchPrefixedField(prefix, rawText);
78 return matches == null ? null : matches[0];
81 static String[] matchRequiredPrefixedField(String prefix, String rawText) {
82 String[] result = matchPrefixedField(prefix, rawText);
84 throw new IllegalArgumentException("Did not match prefix " + prefix);
89 private static String unescape(String escaped) {
90 if (escaped != null) {
91 int backslash = escaped.indexOf((int) '\\');
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 != '\\') {
101 nextIsEscaped = false;
103 nextIsEscaped = true;
106 return unescaped.toString();
112 static void maybeAppend(String value, StringBuilder result) {
115 result.append(value);