Home | History | Annotate | Download | only in repository
      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      */
     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 whether the element is missing or empty,
     48      * so you can't tell the difference.
     49      * <p/>
     50      * Note: use {@link #getOptionalXmlString(Node, String)} if you need to know when the
     51      * element is missing versus empty.
     52      *
     53      * @param node The XML <em>parent</em> node to parse.
     54      * @param xmlLocalName The XML local name to find in the parent node.
     55      * @return The text content of the element. Returns an empty string whether the element
     56      *         is missing or empty, so you can't tell the difference.
     57      */
     58     public static String getXmlString(Node node, String xmlLocalName) {
     59         Node child = getFirstChild(node, xmlLocalName);
     60 
     61         return child == null ? "" : child.getTextContent();  //$NON-NLS-1$
     62     }
     63 
     64     /**
     65      * Retrieves the value of that XML element as a string.
     66      * Returns null when the element is missing, so you can tell between a missing element
     67      * and an empty one.
     68      * <p/>
     69      * Note: use {@link #getXmlString(Node, String)} if you don't need to know when the
     70      * element is missing versus empty.
     71      *
     72      * @param node The XML <em>parent</em> node to parse.
     73      * @param xmlLocalName The XML local name to find in the parent node.
     74      * @return The text content of the element. Returns null when the element is missing.
     75      *         Returns an empty string whether the element is present but empty.
     76      */
     77     public static String getOptionalXmlString(Node node, String xmlLocalName) {
     78         Node child = getFirstChild(node, xmlLocalName);
     79 
     80         return child == null ? null : child.getTextContent();  //$NON-NLS-1$
     81     }
     82 
     83     /**
     84      * Retrieves the value of that XML element as an integer.
     85      * Returns the default value when the element is missing or is not an integer.
     86      */
     87     public static int getXmlInt(Node node, String xmlLocalName, int defaultValue) {
     88         String s = getXmlString(node, xmlLocalName);
     89         try {
     90             return Integer.parseInt(s);
     91         } catch (NumberFormatException e) {
     92             return defaultValue;
     93         }
     94     }
     95 
     96     /**
     97      * Retrieves the value of that XML element as a long.
     98      * Returns the default value when the element is missing or is not an integer.
     99      */
    100     public static long getXmlLong(Node node, String xmlLocalName, long defaultValue) {
    101         String s = getXmlString(node, xmlLocalName);
    102         try {
    103             return Long.parseLong(s);
    104         } catch (NumberFormatException e) {
    105             return defaultValue;
    106         }
    107     }
    108 
    109     /**
    110      * Retrieve an attribute which value must match one of the given enums using a
    111      * case-insensitive name match.
    112      *
    113      * Returns defaultValue if the attribute does not exist or its value does not match
    114      * the given enum values.
    115      */
    116     public static Object getEnumAttribute(
    117             Node archiveNode,
    118             String attrName,
    119             Object[] values,
    120             Object defaultValue) {
    121 
    122         Node attr = archiveNode.getAttributes().getNamedItem(attrName);
    123         if (attr != null) {
    124             String found = attr.getNodeValue();
    125             for (Object value : values) {
    126                 if (value.toString().equalsIgnoreCase(found)) {
    127                     return value;
    128                 }
    129             }
    130         }
    131 
    132         return defaultValue;
    133     }
    134 
    135 }
    136