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