• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 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 package com.android.cts;
17 
18 import java.io.File;
19 import java.io.FileNotFoundException;
20 import java.io.FileOutputStream;
21 
22 import javax.xml.transform.Transformer;
23 import javax.xml.transform.TransformerException;
24 import javax.xml.transform.TransformerFactory;
25 import javax.xml.transform.TransformerFactoryConfigurationError;
26 import javax.xml.transform.dom.DOMSource;
27 import javax.xml.transform.stream.StreamResult;
28 
29 import org.w3c.dom.Attr;
30 import org.w3c.dom.Document;
31 import org.w3c.dom.Node;
32 import org.w3c.dom.NodeList;
33 
34 /**
35  * An abstract class to deal with the XML information using DOM.
36  */
37 public abstract class XMLResourceHandler {
38 
getElementContent(Node elem)39     protected String getElementContent(Node elem) {
40         return elem.getChildNodes().item(0).getNodeValue().trim();
41     }
42 
43     /**
44      * Get string from DOM node by attribute name.
45      *
46      * @param elem a node from DOM tree.
47      * @param attrName the attribute name.
48      * @return string value of the attribute name from the DOM node.
49      */
getStringAttributeValue(Node elem, String attrName)50     static public String getStringAttributeValue(Node elem, String attrName) {
51         Node node = elem.getAttributes().getNamedItem(attrName);
52         if (node == null) {
53             return null;
54         }
55         return node.getNodeValue().trim();
56     }
57 
58     /**
59      * Get integer attribute value.
60      *
61      * @param elem The element node.
62      * @param attrName The attribute name.
63      * @return The attribute value in integer.
64      */
getAttributeValue(Node elem, String attrName)65     protected int getAttributeValue(Node elem, String attrName) {
66         return Integer.parseInt(getStringAttributeValue(elem, attrName));
67     }
68 
69     /**
70      * Get child by attribute.
71      *
72      * @param parent The parent node.
73      * @param attrName The attribute name.
74      * @param attrValue The attribute value.
75      * @return The child node.
76      */
getChildByAttribute(Node parent, String attrName, String attrValue)77     protected Node getChildByAttribute(Node parent, String attrName, String attrValue) {
78         if (parent == null || attrName == null || attrValue == null) {
79             return null;
80         }
81         NodeList children = parent.getChildNodes();
82         for (int i = 0; i < children.getLength(); i++) {
83             Node child = children.item(i);
84             if (child.getNodeType() == Node.ELEMENT_NODE) {
85                 if (attrValue.equals(getStringAttributeValue(child, attrName))) {
86                     return child;
87                 }
88             }
89         }
90 
91         return null;
92     }
93 
94     /**
95      * Set the attribute value.
96      *
97      * @param doc The document.
98      * @param elem The element node.
99      * @param name The attribute name.
100      * @param value The attribute value in integer.
101      */
setAttribute(Document doc, Node elem, String name, int value)102     protected void setAttribute(Document doc, Node elem, String name, int value) {
103         setAttribute(doc, elem, name, Integer.toString(value));
104     }
105 
106     /**
107      * Set the attribute value.
108      *
109      * @param doc The document.
110      * @param elem The element node.
111      * @param name The attribute name.
112      * @param value The attribute value in string.
113      */
setAttribute(Document doc, Node elem, String name, String value)114     protected void setAttribute(Document doc, Node elem, String name, String value) {
115         Attr attrNode = doc.createAttribute(name);
116         attrNode.setNodeValue(value);
117 
118         elem.getAttributes().setNamedItem(attrNode);
119     }
120 
121     /**
122      * Write a DOM Document object into a file.
123      *
124      * @param file XML file to be written
125      * @param doc DOM Document
126      */
writeToFile(File file, Document doc)127     protected static void writeToFile(File file, Document doc) throws FileNotFoundException,
128             TransformerFactoryConfigurationError, TransformerException {
129         Transformer t = TransformerFactory.newInstance().newTransformer();
130         // enable indent in result file
131         t.setOutputProperty("indent", "yes");
132         t.transform(new DOMSource(doc),
133                 new StreamResult(new FileOutputStream(file)));
134     }
135 }
136