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