• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
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.android.bluetooth.pbapclient.utils;
18 
19 import android.util.Log;
20 
21 import java.text.ParseException;
22 import java.util.regex.Matcher;
23 import java.util.regex.Pattern;
24 
25 public final class BmsgTokenizer {
26 
27     private final String mStr;
28 
29     private final Matcher mMatcher;
30 
31     private int mPos = 0;
32 
33     private final int mOffset;
34 
35     static public class Property {
36         public final String name;
37         public final String value;
38 
Property(String name, String value)39         public Property(String name, String value) {
40             if (name == null || value == null) {
41                 throw new IllegalArgumentException();
42             }
43 
44             this.name = name;
45             this.value = value;
46 
47             Log.v("BMSG >> ", toString());
48         }
49 
50         @Override
toString()51         public String toString() {
52             return name + ":" + value;
53         }
54 
55         @Override
equals(Object o)56         public boolean equals(Object o) {
57             return ((o instanceof Property) && ((Property) o).name.equals(name) && ((Property) o).value
58                     .equals(value));
59         }
60     };
61 
BmsgTokenizer(String str)62     public BmsgTokenizer(String str) {
63         this(str, 0);
64     }
65 
BmsgTokenizer(String str, int offset)66     public BmsgTokenizer(String str, int offset) {
67         mStr = str;
68         mOffset = offset;
69         mMatcher = Pattern.compile("(([^:]*):(.*))?\r\n").matcher(str);
70         mPos = mMatcher.regionStart();
71     }
72 
next(boolean alwaysReturn)73     public Property next(boolean alwaysReturn) throws ParseException {
74         boolean found = false;
75 
76         do {
77             mMatcher.region(mPos, mMatcher.regionEnd());
78 
79             if (!mMatcher.lookingAt()) {
80                 if (alwaysReturn) {
81                     return null;
82                 }
83 
84                 throw new ParseException("Property or empty line expected", pos());
85             }
86 
87             mPos = mMatcher.end();
88 
89             if (mMatcher.group(1) != null) {
90                 found = true;
91             }
92         } while (!found);
93 
94         return new Property(mMatcher.group(2), mMatcher.group(3));
95     }
96 
next()97     public Property next() throws ParseException {
98         return next(false);
99     }
100 
remaining()101     public String remaining() {
102         return mStr.substring(mPos);
103     }
104 
pos()105     public int pos() {
106         return mPos + mOffset;
107     }
108 }
109