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 android.bluetooth.client.map; 18 19import android.util.Log; 20 21import org.xmlpull.v1.XmlPullParser; 22import org.xmlpull.v1.XmlPullParserException; 23import org.xmlpull.v1.XmlPullParserFactory; 24 25import java.io.IOException; 26import java.io.InputStream; 27import java.util.ArrayList; 28import java.util.HashMap; 29 30class BluetoothMapMessagesListing { 31 32 private static final String TAG = "BluetoothMapMessagesListing"; 33 34 private final ArrayList<BluetoothMapMessage> mMessages; 35 36 public BluetoothMapMessagesListing(InputStream in) { 37 mMessages = new ArrayList<BluetoothMapMessage>(); 38 39 parse(in); 40 } 41 42 public void parse(InputStream in) { 43 44 try { 45 XmlPullParser xpp = XmlPullParserFactory.newInstance().newPullParser(); 46 xpp.setInput(in, "utf-8"); 47 48 int event = xpp.getEventType(); 49 while (event != XmlPullParser.END_DOCUMENT) { 50 switch (event) { 51 case XmlPullParser.START_TAG: 52 if (xpp.getName().equals("msg")) { 53 54 HashMap<String, String> attrs = new HashMap<String, String>(); 55 56 for (int i = 0; i < xpp.getAttributeCount(); i++) { 57 attrs.put(xpp.getAttributeName(i), xpp.getAttributeValue(i)); 58 } 59 60 try { 61 BluetoothMapMessage msg = new BluetoothMapMessage(attrs); 62 mMessages.add(msg); 63 } catch (IllegalArgumentException e) { 64 /* TODO: provide something more useful here */ 65 Log.w(TAG, "Invalid <msg/>"); 66 } 67 } 68 break; 69 } 70 71 event = xpp.next(); 72 } 73 74 } catch (XmlPullParserException e) { 75 Log.e(TAG, "XML parser error when parsing XML", e); 76 } catch (IOException e) { 77 Log.e(TAG, "I/O error when parsing XML", e); 78 } 79 } 80 81 public ArrayList<BluetoothMapMessage> getList() { 82 return mMessages; 83 } 84} 85