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 androidx.appcompat.mms;
18 
19 import android.content.ContentValues;
20 
21 import org.xmlpull.v1.XmlPullParser;
22 import org.xmlpull.v1.XmlPullParserException;
23 
24 import java.io.IOException;
25 
26 /**
27  * Parser for built-in XML resource file for APN list
28  */
29 class ApnsXmlParser extends MmsXmlResourceParser {
30     interface ApnProcessor {
process(ContentValues apnValues)31         void process(ContentValues apnValues);
32     }
33 
34     private static final String TAG_APNS = "apns";
35     private static final String TAG_APN = "apn";
36 
37     private final ApnProcessor mApnProcessor;
38 
39     private final ContentValues mValues = new ContentValues();
40 
ApnsXmlParser(final XmlPullParser parser, final ApnProcessor apnProcessor)41     ApnsXmlParser(final XmlPullParser parser, final ApnProcessor apnProcessor) {
42         super(parser);
43         mApnProcessor = apnProcessor;
44     }
45 
46     // Parse one APN
47     @Override
parseRecord()48     protected void parseRecord() throws IOException, XmlPullParserException {
49         if (TAG_APN.equals(mInputParser.getName())) {
50             mValues.clear();
51             // Collect all the attributes
52             for (int i = 0; i < mInputParser.getAttributeCount(); i++) {
53                 final String key = mInputParser.getAttributeName(i);
54                 if (key != null) {
55                     mValues.put(key, mInputParser.getAttributeValue(i));
56                 }
57             }
58             // We are done parsing one APN, call the handler
59             if (mApnProcessor != null) {
60                 mApnProcessor.process(mValues);
61             }
62         }
63         // We are at the end tag
64         if (mInputParser.next() != XmlPullParser.END_TAG) {
65             throw new XmlPullParserException("Expecting end tag @" + xmlParserDebugContext());
66         }
67     }
68 
69     @Override
getRootTag()70     protected String getRootTag() {
71         return TAG_APNS;
72     }
73 }
74