1 /*
2  * Copyright (C) 2015 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 android.support.v7.mms;
18 
19 import org.xmlpull.v1.XmlPullParser;
20 import org.xmlpull.v1.XmlPullParserException;
21 
22 import java.io.IOException;
23 
24 /**
25  * XML parser for carrier config (i.e. mms_config)
26  */
27 class CarrierConfigXmlParser extends MmsXmlResourceParser {
28     interface KeyValueProcessor {
process(String type, String key, String value)29         void process(String type, String key, String value);
30     }
31 
32     private static final String TAG_MMS_CONFIG = "mms_config";
33 
34     private final KeyValueProcessor mKeyValueProcessor;
35 
CarrierConfigXmlParser(final XmlPullParser parser, final KeyValueProcessor keyValueProcessor)36     CarrierConfigXmlParser(final XmlPullParser parser, final KeyValueProcessor keyValueProcessor) {
37         super(parser);
38         mKeyValueProcessor = keyValueProcessor;
39     }
40 
41     // Parse one key/value
42     @Override
parseRecord()43     protected void parseRecord() throws IOException, XmlPullParserException {
44         final String key = mInputParser.getAttributeValue(null, "name");
45         // We are at the start tag, the name of the tag is the type
46         // e.g. <int name="key">value</int>
47         final String type = mInputParser.getName();
48         int nextEvent = mInputParser.next();
49         String value = null;
50         if (nextEvent == XmlPullParser.TEXT) {
51             value = mInputParser.getText();
52             nextEvent = mInputParser.next();
53         }
54         if (nextEvent != XmlPullParser.END_TAG) {
55             throw new XmlPullParserException("Expecting end tag @" + xmlParserDebugContext());
56         }
57         // We are done parsing one mms_config key/value, call the handler
58         if (mKeyValueProcessor != null) {
59             mKeyValueProcessor.process(type, key, value);
60         }
61     }
62 
63     @Override
getRootTag()64     protected String getRootTag() {
65         return TAG_MMS_CONFIG;
66     }
67 
68 }
69