View Javadoc

1   package org.jmage.util;
2   
3   import org.apache.xml.serialize.OutputFormat;
4   import org.apache.xml.serialize.XMLSerializer;
5   import org.w3c.dom.*;
6   import org.xml.sax.SAXException;
7   
8   import javax.naming.directory.NoSuchAttributeException;
9   import javax.xml.parsers.DocumentBuilder;
10  import javax.xml.parsers.DocumentBuilderFactory;
11  import javax.xml.parsers.ParserConfigurationException;
12  import java.io.File;
13  import java.io.FileOutputStream;
14  import java.io.IOException;
15  import java.io.InputStream;
16  import java.util.NoSuchElementException;
17  
18  /***
19   * Xml util class
20   */
21  public class XmlUtil {
22  
23      /***
24       * Writes an xml dom document to a file.
25       *
26       * @param document
27       * @param destFile
28       * @throws IOException
29       */
30      public void write(Document document, File destFile) throws IOException {
31          FileOutputStream fos = new FileOutputStream(destFile);
32  
33          OutputFormat format = new OutputFormat(document);
34          format.setLineWidth(80);
35          format.setIndenting(true);
36          format.setIndent(4);
37          format.setEncoding("ISO-8859-1");
38          format.setMediaType("application/xml");
39          format.setOmitComments(false);
40          format.setOmitXMLDeclaration(false);
41          format.setVersion("1.0");
42          format.setStandalone(true);
43  
44          XMLSerializer serializer = new XMLSerializer(fos, format);
45          serializer.serialize(document);
46      }
47  
48      /***
49       * Read an xml document from file.
50       *
51       * @param file the file
52       * @return the xml document
53       * @throws IOException
54       * @throws SAXException
55       * @throws ParserConfigurationException
56       */
57      public Document read(File file) throws IOException, SAXException, ParserConfigurationException {
58          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
59          DocumentBuilder builder = null;
60          builder = factory.newDocumentBuilder();
61          assert(file.exists()) : "unable to find xml file: " + file.getAbsolutePath();
62          Document document = builder.parse(file);
63          return document;
64      }
65  
66      /***
67       * Read an xml document from input stream.
68       *
69       * @param inputStream the input stream.
70       * @return the xml document
71       * @throws IOException
72       * @throws SAXException
73       * @throws ParserConfigurationException
74       */
75      public Document read(InputStream inputStream) throws IOException, SAXException, ParserConfigurationException {
76          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
77          DocumentBuilder builder = null;
78          builder = factory.newDocumentBuilder();
79          assert (inputStream.available() > -1) : "unable to read from input stream: " + inputStream;
80          Document document = builder.parse(inputStream);
81          return document;
82      }
83  
84      /***
85       * Sets an attribute in an xml dom node
86       *
87       * @param node  the node
88       * @param key   the attribute's name
89       * @param value the attribute's value
90       */
91      public void setAttribute(Node node, String key, String value) {
92          NamedNodeMap map = node.getAttributes();
93          Attr attr = node.getOwnerDocument().createAttribute(key);
94          attr.setValue(value);
95          map.setNamedItem(attr);
96      }
97  
98      /***
99       * Gets a named child node under a parent node. If multiple nodes with that name exist, method will return the
100      * first matching node.
101      *
102      * @param parentNode
103      * @param nodeName
104      * @return the child node
105      * @throws NoSuchElementException
106      */
107     public Node getNamedChildNode(Node parentNode, String nodeName) throws NoSuchElementException {
108         try {
109             Node childNode = null;
110             NodeList childNodes = parentNode.getChildNodes();
111             for (int i = 0; i <= childNodes.getLength() - 1; i++) {
112                 Node node = childNodes.item(i);
113                 //dispatcher only bracket nodes, skip cdata
114                 if (nodeName.equals(node.getNodeName())) {
115                     childNode = node;
116                 }
117             }
118             if (childNode == null) {
119                 throw new NoSuchElementException("node for nodeName cannot be found under parent node: " + nodeName);
120             }
121             return childNode;
122         } catch (Exception e) {
123             throw new NoSuchElementException("node for nodeName cannot be found under parent node: " + nodeName);
124         }
125     }
126 
127     /***
128      * Gets an attribute's value in an xml dom node
129      *
130      * @param node the node
131      * @param key  the attribute's name
132      * @return the attribute's value
133      */
134     public String getAttribute(Node node, String key) throws NoSuchAttributeException {
135         try {
136             NamedNodeMap map = node.getAttributes();
137             Attr attr = (Attr) map.getNamedItem(key);
138             return attr.getValue();
139         } catch (Exception e) {
140             throw new NoSuchAttributeException("attribute for key does not exist: " + key);
141         }
142     }
143 }