source: trunk/gsdl3/src/java/org/greenstone/gsdl3/util/XMLConverter.java@ 3773

Last change on this file since 3773 was 3773, checked in by mdewsnip, 21 years ago

Added check for null Nodes.

  • Property svn:keywords set to Author Date Id Revision
File size: 6.3 KB
Line 
1/*
2 * XMLConverter.java
3 * Copyright (C) 2002 New Zealand Digital Library, http://www.nzdl.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19package org.greenstone.gsdl3.util;
20
21// XML classes
22import org.w3c.dom.Document;
23import org.w3c.dom.Node;
24import org.w3c.dom.NodeList;
25import org.w3c.dom.NamedNodeMap;
26import org.xml.sax.InputSource;
27import javax.xml.parsers.*;
28import org.apache.xerces.dom.TextImpl;
29import org.apache.xerces.parsers.DOMParser;
30
31// other java classes
32import java.io.Reader;
33import java.io.StringReader;
34import java.io.File;
35import java.io.FileReader;
36
37/** XMLConverter - utitlity class for greenstone
38 *
39 * parses XML Strings into Documents, converts Nodes to Strings
40 * different parsers have different behaviour - can experiment in here
41 *
42 * @author <a href="mailto:[email protected]">Katherine Don</a>
43 * @version $Revision: 3773 $
44 *
45 */
46public class XMLConverter {
47
48 /** JAXP parser factory */
49 protected DocumentBuilderFactory doc_build_fact_=null;
50 /** JAXP parser */
51 protected DocumentBuilder doc_builder_=null;
52
53 /** xerces parser */
54 protected DOMParser parser_ = null;
55
56 private boolean outputEscaping = true;
57
58
59 /** the no-args constructor */
60 public XMLConverter() {
61 try {
62 doc_build_fact_ = DocumentBuilderFactory.newInstance();
63
64 doc_builder_ = doc_build_fact_.newDocumentBuilder();
65 parser_ = new DOMParser();
66 } catch (Exception e) {
67 System.out.println("XMLConverter:exception "+e.getMessage());
68 }
69 }
70
71 /** returns a DOM Document */
72 public Document getDOM(String in) {
73
74 try {
75 Reader reader = new StringReader(in);
76 InputSource xml_source = new InputSource(reader);
77
78 // Document doc = doc_builder_.parse(xml_source);
79 parser_.parse(xml_source);
80 Document doc = parser_.getDocument();
81 return doc;
82
83 } catch (Exception e) {
84 System.out.println("XMLConverter.getDOM(String): exception "+e.getMessage());
85 }
86 return null;
87 }
88
89 /** returns a DOM Document */
90 public Document getDOM(File in) {
91 try {
92
93 Reader reader = new FileReader(in);
94 InputSource xml_source = new InputSource(reader);
95
96 //Document doc = doc_builder_.parse(xml_source);
97 parser_.parse(xml_source);
98 Document doc = parser_.getDocument();
99
100 return doc;
101
102 } catch (Exception e) {
103 System.out.println("XMLConverter.getDOM(File): exception "+e.getMessage());
104 }
105 return null;
106 }
107
108 /** creates a new empty DOM Document */
109 public Document newDOM() {
110 Document doc = doc_builder_.newDocument();
111 return doc;
112 }
113
114 /** returns the Node as a String */
115 public String getString(Node xmlNode)
116 {
117 outputEscaping = true;
118 return getString(xmlNode, 0);
119 }
120
121
122 private String getString(Node xmlNode, int depth)
123 {
124 String xmlRepresentation = "";
125
126 if (xmlNode == null)
127 return "<null>";
128
129 short nodeType = xmlNode.getNodeType();
130 String nodeName = xmlNode.getNodeName();
131
132 // Handle Element nodes
133 if (nodeType == Node.ELEMENT_NODE) {
134 xmlRepresentation += "\n";
135 for (int i = 0; i < depth; i++)
136 xmlRepresentation += " ";
137
138 // Write opening tag
139 xmlRepresentation += "<" + nodeName;
140
141 // Write the node attributes
142 NamedNodeMap nodeAttributes = xmlNode.getAttributes();
143 for (int i = 0; i < nodeAttributes.getLength(); i++) {
144 Node attribute = nodeAttributes.item(i);
145 xmlRepresentation += " " + attribute.getNodeName() + "=";
146 xmlRepresentation += "\"" + attribute.getNodeValue() + "\"";
147 }
148
149 // If the node has no children, close the opening tag and return
150 if (xmlNode.hasChildNodes() == false) {
151 // This produces somewhat ugly output, but it is necessary to compensate
152 // for display bugs in Netscape. Firstly, the space is needed before the
153 // closing bracket otherwise Netscape will ignore some tags (<br/>, for
154 // example). Also, a newline character would be expected after the tag,
155 // but this causes problems with the display of links (the link text
156 // will contain a newline character, which is displayed badly).
157 xmlRepresentation += " />";
158 return xmlRepresentation;
159 }
160
161 // Close the opening tag
162 xmlRepresentation += ">";
163
164 // Apply recursively to the children of this node
165 NodeList children = xmlNode.getChildNodes();
166 for (int i = 0; i < children.getLength(); i++) {
167 xmlRepresentation += getString(children.item(i), depth + 1);
168 }
169
170 // Write closing tag
171 if (xmlRepresentation.endsWith("\n")) {
172 for (int i = 0; i < depth; i++)
173 xmlRepresentation += " ";
174 }
175 xmlRepresentation += "</" + nodeName + ">\n";
176 }
177
178 // Handle Text nodes
179 else if (nodeType == Node.TEXT_NODE) {
180 String text = xmlNode.getNodeValue();
181
182 // Perform output escaping, if required
183 if (outputEscaping) {
184 text = text.replaceAll("&", "&amp;"); // Must be done first!!
185 text = text.replaceAll("<", "&lt;");
186 text = text.replaceAll(">", "&gt;");
187 text = text.replaceAll("\"", "&quot;");
188 text = text.replaceAll("\'", "&apos;");
189 }
190
191 // Remove any control-C characters
192 text = text.replaceAll("" + (char) 3, "");
193 xmlRepresentation += text;
194 }
195
196 // Handle Processing Instruction nodes
197 else if (nodeType == Node.PROCESSING_INSTRUCTION_NODE) {
198 if (nodeName == "javax.xml.transform.disable-output-escaping") {
199 outputEscaping = false;
200 }
201 else if (nodeName == "javax.xml.transform.enable-output-escaping") {
202 outputEscaping = true;
203 }
204 else {
205 System.err.println("Warning: Unhandled processing instruction " + nodeName);
206 }
207 }
208
209 // A type of node that is not handled yet
210 else {
211 System.err.println("Warning: Unknown node type: " + nodeType);
212 }
213
214 return xmlRepresentation;
215 }
216}
Note: See TracBrowser for help on using the repository browser.