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