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 | /** Parse an XML document from a given file */
|
---|
48 | static public Document parseXMLFile(File xml_file)
|
---|
49 | {
|
---|
50 | Document document = null;
|
---|
51 |
|
---|
52 | try {
|
---|
53 | FileInputStream fis = new FileInputStream(xml_file);
|
---|
54 | InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
|
---|
55 | Reader r = new BufferedReader(isr);
|
---|
56 | InputSource isc = new InputSource(r);
|
---|
57 | DOMParser parser = new DOMParser();
|
---|
58 | parser.setFeature("http://xml.org/sax/features/validation", false);
|
---|
59 | parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
|
---|
60 | // May or may not be ignored, the documentation for Xerces is contradictory. If it works then parsing -should- be faster.
|
---|
61 | parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", true);
|
---|
62 | parser.setFeature("http://apache.org/xml/features/dom/include-ignorable-whitespace", false);
|
---|
63 | parser.parse(isc);
|
---|
64 | document = parser.getDocument();
|
---|
65 | isr.close();
|
---|
66 | fis.close();
|
---|
67 | }
|
---|
68 | catch (Exception exception) {
|
---|
69 | exception.printStackTrace();
|
---|
70 | }
|
---|
71 |
|
---|
72 | return document;
|
---|
73 | }
|
---|
74 |
|
---|
75 |
|
---|
76 | /** Write an XML document to a given file */
|
---|
77 | static public void writeXMLFile(File xml_file, Document document)
|
---|
78 | {
|
---|
79 | try {
|
---|
80 | OutputStream os = new FileOutputStream(xml_file);
|
---|
81 | // Create an output format for our document.
|
---|
82 | OutputFormat f = new OutputFormat(document);
|
---|
83 | f.setEncoding("UTF-8");
|
---|
84 | f.setIndenting(true);
|
---|
85 | f.setLineWidth(0); // Why isn't this working!
|
---|
86 | f.setPreserveSpace(false);
|
---|
87 | // Create the necessary writer stream for serialization.
|
---|
88 | OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
|
---|
89 | Writer w = new BufferedWriter(osw);
|
---|
90 | // Generate a new serializer from the above.
|
---|
91 | XMLSerializer s = new XMLSerializer(w, f);
|
---|
92 | s.asDOMSerializer();
|
---|
93 | // Finally serialize the document to file.
|
---|
94 | s.serialize(document);
|
---|
95 | // And close.
|
---|
96 | os.close();
|
---|
97 | }
|
---|
98 | catch (Exception exception) {
|
---|
99 | exception.printStackTrace();
|
---|
100 | }
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|