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