Standardize and update all copyright statements to name "ZXing authors" as suggested...
[zxing.git] / core / src / com / google / zxing / client / result / optional / AbstractMobileTagParsedResult.java
1 /*
2  * Copyright 2008 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.optional;
18
19 import com.google.zxing.client.result.ParsedReaderResult;
20 import com.google.zxing.client.result.ParsedReaderResultType;
21
22 /**
23  * <p>Superclass for classes encapsulating reader results encoded according
24  * to the MobileTag Reader International Specification.</p>
25  * 
26  * @author srowen@google.com (Sean Owen)
27  */
28 abstract class AbstractMobileTagParsedResult extends ParsedReaderResult {
29
30   public static final int ACTION_DO = 1;
31   public static final int ACTION_EDIT = 2;
32   public static final int ACTION_SAVE = 4;
33
34   AbstractMobileTagParsedResult(ParsedReaderResultType type) {
35     super(type);
36   }
37
38   static String[] matchDelimitedFields(String rawText, int maxItems) {
39     String[] result = new String[maxItems];
40     int item = 0;
41     int i = 0;
42     int max = rawText.length();
43     while (item < maxItems && i < max) {
44       int start = i; // Found the start of a match here
45       boolean done = false;
46       while (!done) {
47         i = rawText.indexOf((int) '|', i);
48         if (i < 0) {
49           // No terminating end character? done. Set i such that loop terminates and break
50           i = rawText.length();
51           done = true;
52         } else if (rawText.charAt(i - 1) == '\\') {
53           // semicolon was escaped so continue
54           i++;
55         } else {
56           // found a match
57           if (start != i) {
58             result[item] = unescapeBackslash(rawText.substring(start, i));
59           }
60           item++;
61           i++;
62           done = true;
63         }
64       }
65     }
66     if (item < maxItems) {
67       return null;
68     }
69     return result;
70   }
71
72   static boolean isDigits(String s, int expectedLength) {
73     if (s == null) {
74       return true;
75     }
76     if (s.length() != expectedLength) {
77       return false;
78     }
79     for (int i = 0; i < expectedLength; i++) {
80       if (!Character.isDigit(s.charAt(i))) {
81         return false;
82       }
83     }
84     return true;
85   }
86
87 }