OSDN Git Service

original
[gb-231r1-is01/GB_2.3_IS01.git] / sdk / sdkmanager / libs / sdklib / src / com / android / sdklib / internal / repository / XmlParserUtils.java
diff --git a/sdk/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/repository/XmlParserUtils.java b/sdk/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/repository/XmlParserUtils.java
new file mode 100644 (file)
index 0000000..61b5b24
--- /dev/null
@@ -0,0 +1,135 @@
+/*\r
+ * Copyright (C) 2009 The Android Open Source Project\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *      http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+package com.android.sdklib.internal.repository;\r
+\r
+import org.w3c.dom.Node;\r
+\r
+/**\r
+ * Misc utilities to help extracting elements and attributes out of an XML document.\r
+ */\r
+class XmlParserUtils {\r
+\r
+    /**\r
+     * Returns the first child element with the given XML local name.\r
+     * If xmlLocalName is null, returns the very first child element.\r
+     */\r
+    public static Node getFirstChild(Node node, String xmlLocalName) {\r
+\r
+        String nsUri = node.getNamespaceURI();\r
+        for(Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {\r
+            if (child.getNodeType() == Node.ELEMENT_NODE &&\r
+                    nsUri.equals(child.getNamespaceURI())) {\r
+                if (xmlLocalName == null || xmlLocalName.equals(child.getLocalName())) {\r
+                    return child;\r
+                }\r
+            }\r
+        }\r
+\r
+        return null;\r
+    }\r
+\r
+    /**\r
+     * Retrieves the value of that XML element as a string.\r
+     * Returns an empty string whether the element is missing or empty,\r
+     * so you can't tell the difference.\r
+     * <p/>\r
+     * Note: use {@link #getOptionalXmlString(Node, String)} if you need to know when the\r
+     * element is missing versus empty.\r
+     *\r
+     * @param node The XML <em>parent</em> node to parse.\r
+     * @param xmlLocalName The XML local name to find in the parent node.\r
+     * @return The text content of the element. Returns an empty string whether the element\r
+     *         is missing or empty, so you can't tell the difference.\r
+     */\r
+    public static String getXmlString(Node node, String xmlLocalName) {\r
+        Node child = getFirstChild(node, xmlLocalName);\r
+\r
+        return child == null ? "" : child.getTextContent();  //$NON-NLS-1$\r
+    }\r
+\r
+    /**\r
+     * Retrieves the value of that XML element as a string.\r
+     * Returns null when the element is missing, so you can tell between a missing element\r
+     * and an empty one.\r
+     * <p/>\r
+     * Note: use {@link #getXmlString(Node, String)} if you don't need to know when the\r
+     * element is missing versus empty.\r
+     *\r
+     * @param node The XML <em>parent</em> node to parse.\r
+     * @param xmlLocalName The XML local name to find in the parent node.\r
+     * @return The text content of the element. Returns null when the element is missing.\r
+     *         Returns an empty string whether the element is present but empty.\r
+     */\r
+    public static String getOptionalXmlString(Node node, String xmlLocalName) {\r
+        Node child = getFirstChild(node, xmlLocalName);\r
+\r
+        return child == null ? null : child.getTextContent();  //$NON-NLS-1$\r
+    }\r
+\r
+    /**\r
+     * Retrieves the value of that XML element as an integer.\r
+     * Returns the default value when the element is missing or is not an integer.\r
+     */\r
+    public static int getXmlInt(Node node, String xmlLocalName, int defaultValue) {\r
+        String s = getXmlString(node, xmlLocalName);\r
+        try {\r
+            return Integer.parseInt(s);\r
+        } catch (NumberFormatException e) {\r
+            return defaultValue;\r
+        }\r
+    }\r
+\r
+    /**\r
+     * Retrieves the value of that XML element as a long.\r
+     * Returns the default value when the element is missing or is not an integer.\r
+     */\r
+    public static long getXmlLong(Node node, String xmlLocalName, long defaultValue) {\r
+        String s = getXmlString(node, xmlLocalName);\r
+        try {\r
+            return Long.parseLong(s);\r
+        } catch (NumberFormatException e) {\r
+            return defaultValue;\r
+        }\r
+    }\r
+\r
+    /**\r
+     * Retrieve an attribute which value must match one of the given enums using a\r
+     * case-insensitive name match.\r
+     *\r
+     * Returns defaultValue if the attribute does not exist or its value does not match\r
+     * the given enum values.\r
+     */\r
+    public static Object getEnumAttribute(\r
+            Node archiveNode,\r
+            String attrName,\r
+            Object[] values,\r
+            Object defaultValue) {\r
+\r
+        Node attr = archiveNode.getAttributes().getNamedItem(attrName);\r
+        if (attr != null) {\r
+            String found = attr.getNodeValue();\r
+            for (Object value : values) {\r
+                if (value.toString().equalsIgnoreCase(found)) {\r
+                    return value;\r
+                }\r
+            }\r
+        }\r
+\r
+        return defaultValue;\r
+    }\r
+\r
+}\r