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