1 /* 2 * Copyright (C) 2009 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.sdklib.internal.repository; 18 19 import org.w3c.dom.Node; 20 21 /** 22 * Misc utilities to help extracting elements and attributes out of an XML document. 23 */ 24 class XmlParserUtils { 25 26 /** 27 * Returns the first child element with the given XML local name. 28 * If xmlLocalName is null, returns the very first child element. 29 */ getFirstChild(Node node, String xmlLocalName)30 public static Node getFirstChild(Node node, String xmlLocalName) { 31 32 String nsUri = node.getNamespaceURI(); 33 for(Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { 34 if (child.getNodeType() == Node.ELEMENT_NODE && 35 nsUri.equals(child.getNamespaceURI())) { 36 if (xmlLocalName == null || xmlLocalName.equals(child.getLocalName())) { 37 return child; 38 } 39 } 40 } 41 42 return null; 43 } 44 45 /** 46 * Retrieves the value of that XML element as a string. 47 * Returns an empty string when the element is missing. 48 */ getXmlString(Node node, String xmlLocalName)49 public static String getXmlString(Node node, String xmlLocalName) { 50 Node child = getFirstChild(node, xmlLocalName); 51 52 return child == null ? "" : child.getTextContent(); //$NON-NLS-1$ 53 } 54 55 /** 56 * Retrieves the value of that XML element as an integer. 57 * Returns the default value when the element is missing or is not an integer. 58 */ getXmlInt(Node node, String xmlLocalName, int defaultValue)59 public static int getXmlInt(Node node, String xmlLocalName, int defaultValue) { 60 String s = getXmlString(node, xmlLocalName); 61 try { 62 return Integer.parseInt(s); 63 } catch (NumberFormatException e) { 64 return defaultValue; 65 } 66 } 67 68 /** 69 * Retrieves the value of that XML element as a long. 70 * Returns the default value when the element is missing or is not an integer. 71 */ getXmlLong(Node node, String xmlLocalName, long defaultValue)72 public static long getXmlLong(Node node, String xmlLocalName, long defaultValue) { 73 String s = getXmlString(node, xmlLocalName); 74 try { 75 return Long.parseLong(s); 76 } catch (NumberFormatException e) { 77 return defaultValue; 78 } 79 } 80 81 /** 82 * Retrieve an attribute which value must match one of the given enums using a 83 * case-insensitive name match. 84 * 85 * Returns defaultValue if the attribute does not exist or its value does not match 86 * the given enum values. 87 */ getEnumAttribute( Node archiveNode, String attrName, Object[] values, Object defaultValue)88 public static Object getEnumAttribute( 89 Node archiveNode, 90 String attrName, 91 Object[] values, 92 Object defaultValue) { 93 94 Node attr = archiveNode.getAttributes().getNamedItem(attrName); 95 if (attr != null) { 96 String found = attr.getNodeValue(); 97 for (Object value : values) { 98 if (value.toString().equalsIgnoreCase(found)) { 99 return value; 100 } 101 } 102 } 103 104 return defaultValue; 105 } 106 107 } 108