1 /* 2 * Copyright (C) 2011 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.cts.tradefed.result; 18 19 import com.android.tradefed.util.xml.AbstractXmlParser.ParseException; 20 21 import org.xmlpull.v1.XmlPullParser; 22 import org.xmlpull.v1.XmlPullParserException; 23 import org.xmlpull.v1.XmlPullParserFactory; 24 25 import java.io.IOException; 26 import java.io.Reader; 27 28 /** 29 * Helper abstract class for XmlPullParser 30 * 31 * TODO: move to com.android.tradefed.util.xml 32 */ 33 public abstract class AbstractXmlPullParser { 34 35 /** 36 * Parse the summary data from the given input data. 37 * 38 * @param xmlReader the input XML 39 * @throws ParseException if failed to parse the summary data. 40 */ parse(Reader xmlReader)41 public void parse(Reader xmlReader) throws ParseException { 42 try { 43 XmlPullParserFactory fact = org.xmlpull.v1.XmlPullParserFactory.newInstance(); 44 XmlPullParser parser = fact.newPullParser(); 45 parser.setInput (xmlReader); 46 parse(parser); 47 } catch (XmlPullParserException e) { 48 throw new ParseException(e); 49 } catch (IOException e) { 50 throw new ParseException(e); 51 } 52 } 53 parse(XmlPullParser parser)54 abstract void parse(XmlPullParser parser) throws XmlPullParserException, IOException; 55 56 /** 57 * Parse an integer value from an XML attribute 58 * 59 * @param parser the {@link XmlPullParser} 60 * @param name the attribute name 61 * @return the parsed value or 0 if it could not be parsed 62 */ parseIntAttr(XmlPullParser parser, String name)63 protected int parseIntAttr(XmlPullParser parser, String name) { 64 try { 65 String value = parser.getAttributeValue(null, name); 66 if (value != null) { 67 return Integer.parseInt(value); 68 } 69 } catch (NumberFormatException e) { 70 // ignore 71 } 72 return 0; 73 } 74 75 /** 76 * Parse a boolean attribute value 77 */ parseBooleanAttr(XmlPullParser parser, String name)78 protected boolean parseBooleanAttr(XmlPullParser parser, String name) { 79 String stringValue = parser.getAttributeValue(null, name); 80 return stringValue != null && 81 Boolean.parseBoolean(stringValue); 82 } 83 84 /** 85 * Helper method for retrieving attribute value with given name 86 * 87 * @param parser the XmlPullParser 88 * @param name the attribute name 89 * @return the attribute value 90 */ getAttribute(XmlPullParser parser, String name)91 protected String getAttribute(XmlPullParser parser, String name) { 92 return parser.getAttributeValue(null, name); 93 } 94 } 95