1 | package org.greenstone.gatherer.util;
|
---|
2 |
|
---|
3 |
|
---|
4 | import java.io.*;
|
---|
5 | import java.util.*;
|
---|
6 | import org.apache.xerces.parsers.*;
|
---|
7 | import org.apache.xml.serialize.*;
|
---|
8 | import org.w3c.dom.*;
|
---|
9 | import org.xml.sax.*;
|
---|
10 |
|
---|
11 |
|
---|
12 | /** This class is a static class containing useful XML functions */
|
---|
13 | public class XMLTools
|
---|
14 | {
|
---|
15 | /** Remove all of the child nodes from a certain node. */
|
---|
16 | static final public void clear(Node node)
|
---|
17 | {
|
---|
18 | while (node.hasChildNodes()) {
|
---|
19 | node.removeChild(node.getFirstChild());
|
---|
20 | }
|
---|
21 | }
|
---|
22 |
|
---|
23 |
|
---|
24 | static public ArrayList getChildElementsByTagName(Element parent_element, String element_name)
|
---|
25 | {
|
---|
26 | ArrayList child_elements = new ArrayList();
|
---|
27 |
|
---|
28 | NodeList children_nodelist = parent_element.getChildNodes();
|
---|
29 | for (int i = 0; i < children_nodelist.getLength(); i++) {
|
---|
30 | Node child_node = children_nodelist.item(i);
|
---|
31 | if (child_node.getNodeType() == Node.ELEMENT_NODE && child_node.getNodeName().equals(element_name)) {
|
---|
32 | child_elements.add(child_node);
|
---|
33 | }
|
---|
34 | }
|
---|
35 |
|
---|
36 | return child_elements;
|
---|
37 | }
|
---|
38 |
|
---|
39 |
|
---|
40 | static public String getElementTextValue(Element element)
|
---|
41 | {
|
---|
42 | // Find the first text node child
|
---|
43 | NodeList children_nodelist = element.getChildNodes();
|
---|
44 | for (int i = 0; i < children_nodelist.getLength(); i++) {
|
---|
45 | Node child_node = children_nodelist.item(i);
|
---|
46 | if (child_node.getNodeType() == Node.TEXT_NODE) {
|
---|
47 | return child_node.getNodeValue();
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | // None found
|
---|
52 | return "";
|
---|
53 | }
|
---|
54 |
|
---|
55 |
|
---|
56 | /** Method to retrieve the value of a given node.
|
---|
57 | * @param element The <strong>Element</strong> whose value we wish to find.
|
---|
58 | * Soon to be deprecated!
|
---|
59 | */
|
---|
60 | static final public String getValue(Node element) {
|
---|
61 | // If we've been given a subject node first retrieve its value node.
|
---|
62 | if(element.getNodeName().equals("Subject")) {
|
---|
63 | element = getNodeFromNamed(element, "Value");
|
---|
64 | }
|
---|
65 | // If we've got a value node, then reconstruct the text. Remember that DOM will split text over 256 characters into several text nodes
|
---|
66 | if(element != null && element.hasChildNodes()) {
|
---|
67 | StringBuffer text_buffer = new StringBuffer();
|
---|
68 | NodeList text_nodes = element.getChildNodes();
|
---|
69 | for(int i = 0; i < text_nodes.getLength(); i++) {
|
---|
70 | Node possible_text = text_nodes.item(i);
|
---|
71 | if(possible_text.getNodeName().equals(StaticStrings.TEXT_NODE)) {
|
---|
72 | text_buffer.append(possible_text.getNodeValue());
|
---|
73 | }
|
---|
74 | }
|
---|
75 | return text_buffer.toString();
|
---|
76 | }
|
---|
77 | return "";
|
---|
78 | }
|
---|
79 |
|
---|
80 |
|
---|
81 | /** Method to retrieve from the node given, a certain child node with the specified name.
|
---|
82 | * @param parent The <strong>Node</strong> whose children should be searched.
|
---|
83 | * @param name The required nodes name as a <strong>String</strong>.
|
---|
84 | * @return The requested <strong>Node</strong> if it is found, <i>null</i> otherwise.
|
---|
85 | * Soon to be deprecated!
|
---|
86 | */
|
---|
87 | static final public Node getNodeFromNamed(Node parent, String name) {
|
---|
88 | Node child = null;
|
---|
89 | for(Node i = parent.getFirstChild(); i != null && child == null;
|
---|
90 | i = i.getNextSibling()) {
|
---|
91 | if(i.getNodeName().equals(name)) {
|
---|
92 | child = i;
|
---|
93 | }
|
---|
94 | }
|
---|
95 | return child;
|
---|
96 | }
|
---|
97 |
|
---|
98 |
|
---|
99 | /** Set the #text node value of some element.
|
---|
100 | * @param element the Element whose value we wish to set
|
---|
101 | * @param value the new value for the element as a String
|
---|
102 | * Soon to be deprecated!
|
---|
103 | */
|
---|
104 | static final public void setValue(Element element, String value) {
|
---|
105 | // Remove any existing child node(s)
|
---|
106 | clear(element);
|
---|
107 | // Add new text node.
|
---|
108 | if (value != null) {
|
---|
109 | element.appendChild(element.getOwnerDocument().createTextNode(value));
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 | /** Parse an XML document from a given file */
|
---|
115 | static public Document parseXMLFile(File xml_file)
|
---|
116 | {
|
---|
117 | Document document = null;
|
---|
118 |
|
---|
119 | try {
|
---|
120 | FileInputStream fis = new FileInputStream(xml_file);
|
---|
121 | InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
|
---|
122 | Reader r = new BufferedReader(isr);
|
---|
123 | InputSource isc = new InputSource(r);
|
---|
124 | DOMParser parser = new DOMParser();
|
---|
125 | parser.setFeature("http://xml.org/sax/features/validation", false);
|
---|
126 | parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
|
---|
127 | // May or may not be ignored, the documentation for Xerces is contradictory. If it works then parsing -should- be faster.
|
---|
128 | parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", true);
|
---|
129 | parser.setFeature("http://apache.org/xml/features/dom/include-ignorable-whitespace", false);
|
---|
130 | parser.parse(isc);
|
---|
131 | document = parser.getDocument();
|
---|
132 | isr.close();
|
---|
133 | fis.close();
|
---|
134 | }
|
---|
135 | catch (Exception exception) {
|
---|
136 | exception.printStackTrace();
|
---|
137 | }
|
---|
138 |
|
---|
139 | return document;
|
---|
140 | }
|
---|
141 |
|
---|
142 |
|
---|
143 | /** Write an XML document to a given file */
|
---|
144 | static public void writeXMLFile(File xml_file, Document document)
|
---|
145 | {
|
---|
146 | try {
|
---|
147 | OutputStream os = new FileOutputStream(xml_file);
|
---|
148 | // Create an output format for our document.
|
---|
149 | OutputFormat f = new OutputFormat(document);
|
---|
150 | f.setEncoding("UTF-8");
|
---|
151 | f.setIndenting(true);
|
---|
152 | f.setLineWidth(0); // Why isn't this working!
|
---|
153 | f.setPreserveSpace(false);
|
---|
154 | // Create the necessary writer stream for serialization.
|
---|
155 | OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
|
---|
156 | Writer w = new BufferedWriter(osw);
|
---|
157 | // Generate a new serializer from the above.
|
---|
158 | XMLSerializer s = new XMLSerializer(w, f);
|
---|
159 | s.asDOMSerializer();
|
---|
160 | // Finally serialize the document to file.
|
---|
161 | s.serialize(document);
|
---|
162 | // And close.
|
---|
163 | os.close();
|
---|
164 | }
|
---|
165 | catch (Exception exception) {
|
---|
166 | exception.printStackTrace();
|
---|
167 | }
|
---|
168 | }
|
---|
169 | }
|
---|