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