OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.test.omaha; |
| 6 |
| 7 import android.util.Log; |
| 8 |
| 9 import org.xml.sax.Attributes; |
| 10 import org.xml.sax.InputSource; |
| 11 import org.xml.sax.SAXException; |
| 12 import org.xml.sax.SAXParseException; |
| 13 import org.xml.sax.helpers.DefaultHandler; |
| 14 |
| 15 import java.io.IOException; |
| 16 import java.io.StringReader; |
| 17 |
| 18 import javax.xml.parsers.ParserConfigurationException; |
| 19 import javax.xml.parsers.SAXParser; |
| 20 import javax.xml.parsers.SAXParserFactory; |
| 21 |
| 22 /** |
| 23 * Pulls out a given tag attribute's value from the XML. |
| 24 * Assumes that the same tag doesn't appear twice with the same attribute. |
| 25 */ |
| 26 public class AttributeFinder extends DefaultHandler { |
| 27 private static final String TAG = "AttributeFinder"; |
| 28 |
| 29 private final String mDesiredTag; |
| 30 private final String mDesiredAttribute; |
| 31 private boolean mTagFound; |
| 32 private String mValue; |
| 33 |
| 34 public AttributeFinder(String xml, String tag, String attribute) { |
| 35 mDesiredTag = tag; |
| 36 mDesiredAttribute = attribute; |
| 37 |
| 38 try { |
| 39 SAXParserFactory factory = SAXParserFactory.newInstance(); |
| 40 SAXParser saxParser = factory.newSAXParser(); |
| 41 saxParser.parse(new InputSource(new StringReader(xml)), this); |
| 42 } catch (IOException e) { |
| 43 Log.e(TAG, "Hit IOException", e); |
| 44 } catch (ParserConfigurationException e) { |
| 45 Log.e(TAG, "Hit ParserConfigurationException", e); |
| 46 } catch (SAXParseException e) { |
| 47 Log.e(TAG, "Hit SAXParseException", e); |
| 48 } catch (SAXException e) { |
| 49 Log.e(TAG, "Hit SAXException", e); |
| 50 } |
| 51 } |
| 52 |
| 53 @Override |
| 54 public void startElement(String uri, String localName, String tag, Attribute
s attributes) |
| 55 throws SAXException { |
| 56 if (tag.equals(mDesiredTag)) { |
| 57 mTagFound = true; |
| 58 mValue = mDesiredAttribute != null ? attributes.getValue(mDesiredAtt
ribute) : null; |
| 59 } |
| 60 } |
| 61 |
| 62 public boolean isTagFound() { |
| 63 return mTagFound; |
| 64 } |
| 65 |
| 66 public String getValue() { |
| 67 return mValue; |
| 68 } |
| 69 } |
OLD | NEW |