[3235] | 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 | */
|
---|
[3222] | 19 | package org.greenstone.gsdl3.util;
|
---|
| 20 |
|
---|
| 21 | // XML classes
|
---|
| 22 | import org.w3c.dom.Document;
|
---|
[16374] | 23 | import org.w3c.dom.DocumentType;
|
---|
[16688] | 24 | import org.w3c.dom.Element;
|
---|
[3222] | 25 | import org.w3c.dom.Node;
|
---|
[3768] | 26 | import org.w3c.dom.NodeList;
|
---|
| 27 | import org.w3c.dom.NamedNodeMap;
|
---|
[3222] | 28 | import org.xml.sax.InputSource;
|
---|
[5187] | 29 | import org.xml.sax.EntityResolver;
|
---|
[3222] | 30 | import org.apache.xerces.parsers.DOMParser;
|
---|
[3849] | 31 | import org.apache.xerces.dom.DocumentImpl; // for new Documents
|
---|
[3222] | 32 |
|
---|
| 33 | // other java classes
|
---|
| 34 | import java.io.Reader;
|
---|
[4087] | 35 | import java.io.InputStreamReader;
|
---|
[3222] | 36 | import java.io.StringReader;
|
---|
| 37 | import java.io.File;
|
---|
[4087] | 38 | import java.io.FileInputStream;
|
---|
[3222] | 39 | import java.io.FileReader;
|
---|
| 40 |
|
---|
[13124] | 41 | import org.apache.log4j.*;
|
---|
| 42 |
|
---|
[5187] | 43 | /** XMLConverter - utility class for greenstone
|
---|
[3222] | 44 | *
|
---|
| 45 | * parses XML Strings into Documents, converts Nodes to Strings
|
---|
| 46 | * different parsers have different behaviour - can experiment in here
|
---|
[3941] | 47 | * now we only use xerces
|
---|
[3222] | 48 | *
|
---|
[3235] | 49 | * @author <a href="mailto:[email protected]">Katherine Don</a>
|
---|
| 50 | * @version $Revision: 16999 $
|
---|
| 51 | *
|
---|
[3222] | 52 | */
|
---|
| 53 | public class XMLConverter {
|
---|
| 54 |
|
---|
[13210] | 55 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.XMLConverter.class.getName());
|
---|
[13124] | 56 |
|
---|
[3222] | 57 | /** xerces parser */
|
---|
[5187] | 58 | protected DOMParser parser = null;
|
---|
[3222] | 59 |
|
---|
[3768] | 60 | private boolean outputEscaping = true;
|
---|
| 61 |
|
---|
| 62 |
|
---|
[3235] | 63 | /** the no-args constructor */
|
---|
[3222] | 64 | public XMLConverter() {
|
---|
| 65 | try {
|
---|
[5187] | 66 | this.parser = new DOMParser();
|
---|
| 67 | this.parser.setFeature("http://xml.org/sax/features/validation", false);
|
---|
[13210] | 68 | // don't try and load external DTD - no need if we are not validating, and may cause connection errors if a proxy is not set up.
|
---|
| 69 | this.parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
|
---|
[10067] | 70 | // a performance test showed that having this on lead to increased
|
---|
| 71 | // memory use for small-medium docs, and not much gain for large
|
---|
| 72 | // docs.
|
---|
| 73 | // http://www.sosnoski.com/opensrc/xmlbench/conclusions.html
|
---|
| 74 | this.parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", false);
|
---|
[3222] | 75 | } catch (Exception e) {
|
---|
[13210] | 76 | logger.error(e.getMessage());
|
---|
[3222] | 77 | }
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[5187] | 80 | /** sets the entity resolver. pass in null to unset it */
|
---|
| 81 | public void setEntityResolver(EntityResolver er) {
|
---|
| 82 | this.parser.setEntityResolver(er);
|
---|
| 83 | }
|
---|
[16688] | 84 |
|
---|
[16780] | 85 | /** Given a Node representing an Element or Document, will return the
|
---|
| 86 | * Element/docroot Element. Returns null if the Node was not an element. */
|
---|
| 87 | public static Element nodeToElement(Node node)
|
---|
[16688] | 88 | {
|
---|
[16999] | 89 | if(node == null) {
|
---|
| 90 | return null;
|
---|
| 91 | }
|
---|
[16688] | 92 | short nodeType = node.getNodeType();
|
---|
| 93 |
|
---|
| 94 | if (nodeType == Node.DOCUMENT_NODE) {
|
---|
| 95 | Document docNode = (Document)node;
|
---|
| 96 | return docNode.getDocumentElement() ;
|
---|
| 97 | }
|
---|
| 98 | else if (nodeType == Node.ELEMENT_NODE) {
|
---|
| 99 | return (Element)node;
|
---|
| 100 | }
|
---|
| 101 | else {
|
---|
[16780] | 102 | String message = "Expecting Document or Element node type but got "
|
---|
| 103 | + node.getNodeName() + "\nReturning null";
|
---|
| 104 | System.err.println(message);
|
---|
| 105 | logger.warn(message);
|
---|
[16688] | 106 | return null;
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 |
|
---|
[3222] | 110 | /** returns a DOM Document */
|
---|
| 111 | public Document getDOM(String in) {
|
---|
| 112 |
|
---|
| 113 | try {
|
---|
| 114 | Reader reader = new StringReader(in);
|
---|
| 115 | InputSource xml_source = new InputSource(reader);
|
---|
| 116 |
|
---|
[5187] | 117 | this.parser.parse(xml_source);
|
---|
| 118 | Document doc = this.parser.getDocument();
|
---|
[10202] | 119 |
|
---|
[3222] | 120 | return doc;
|
---|
[4087] | 121 |
|
---|
[3222] | 122 | } catch (Exception e) {
|
---|
[13210] | 123 | logger.error(e.getMessage());
|
---|
[3222] | 124 | }
|
---|
| 125 | return null;
|
---|
| 126 | }
|
---|
[3235] | 127 |
|
---|
| 128 | /** returns a DOM Document */
|
---|
[3222] | 129 | public Document getDOM(File in) {
|
---|
| 130 | try {
|
---|
[4087] | 131 | FileReader reader = new FileReader(in);
|
---|
[3222] | 132 | InputSource xml_source = new InputSource(reader);
|
---|
[5187] | 133 | this.parser.parse(xml_source);
|
---|
| 134 | Document doc = this.parser.getDocument();
|
---|
[4087] | 135 | return doc;
|
---|
| 136 |
|
---|
| 137 | } catch (Exception e) {
|
---|
[13210] | 138 | logger.error(e.getMessage(), e);
|
---|
| 139 |
|
---|
[4087] | 140 | }
|
---|
| 141 | return null;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | /** returns a DOM document */
|
---|
| 145 | public Document getDOM(File in, String encoding) {
|
---|
| 146 | try {
|
---|
| 147 |
|
---|
| 148 | InputStreamReader isr = new InputStreamReader(new FileInputStream(in), encoding);
|
---|
| 149 | InputSource xml_source = new InputSource(isr);
|
---|
| 150 |
|
---|
[5187] | 151 | this.parser.parse(xml_source);
|
---|
| 152 | Document doc = this.parser.getDocument();
|
---|
[3222] | 153 |
|
---|
| 154 | return doc;
|
---|
| 155 |
|
---|
| 156 | } catch (Exception e) {
|
---|
[13210] | 157 | logger.error(e.getMessage());
|
---|
[3222] | 158 | }
|
---|
| 159 | return null;
|
---|
| 160 | }
|
---|
[4087] | 161 |
|
---|
[3222] | 162 |
|
---|
| 163 | /** creates a new empty DOM Document */
|
---|
| 164 | public Document newDOM() {
|
---|
[3941] | 165 | Document doc = new DocumentImpl();
|
---|
[3222] | 166 | return doc;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | /** returns the Node as a String */
|
---|
[3768] | 170 | public String getString(Node xmlNode)
|
---|
| 171 | {
|
---|
| 172 | outputEscaping = true;
|
---|
[10202] | 173 | StringBuffer xmlRepresentation = new StringBuffer();
|
---|
| 174 | getString(xmlNode, xmlRepresentation, 0, false);
|
---|
| 175 | return xmlRepresentation.toString();
|
---|
[3768] | 176 | }
|
---|
| 177 |
|
---|
[3970] | 178 | /** returns the node as a nicely formatted String - this introduces extra
|
---|
| 179 | * text nodes if the String is read back in as a DOM, so should only be
|
---|
| 180 | * used for printing */
|
---|
| 181 | public String getPrettyString(Node xmlNode) {
|
---|
| 182 |
|
---|
| 183 | outputEscaping = true;
|
---|
[10202] | 184 | StringBuffer xmlRepresentation = new StringBuffer();
|
---|
| 185 | getString(xmlNode, xmlRepresentation, 0, true);
|
---|
| 186 | return xmlRepresentation.toString();
|
---|
[3970] | 187 | }
|
---|
[3768] | 188 |
|
---|
[10202] | 189 | private void getString(Node xmlNode, StringBuffer xmlRepresentation,
|
---|
| 190 | int depth, boolean pretty)
|
---|
[3768] | 191 | {
|
---|
[10202] | 192 |
|
---|
| 193 | if (xmlNode == null) {
|
---|
| 194 | xmlRepresentation.append("<null>");
|
---|
| 195 | return;
|
---|
| 196 | }
|
---|
[3768] | 197 |
|
---|
| 198 | short nodeType = xmlNode.getNodeType();
|
---|
| 199 | String nodeName = xmlNode.getNodeName();
|
---|
| 200 |
|
---|
[9874] | 201 | if (nodeType == Node.DOCUMENT_NODE) {
|
---|
[16374] | 202 | Document xmlDocNode = (Document)xmlNode;
|
---|
| 203 |
|
---|
| 204 | if (xmlDocNode.getDoctype() == null) {
|
---|
| 205 | System.err.println("Doctype is null.");
|
---|
| 206 | }
|
---|
| 207 | else {
|
---|
| 208 |
|
---|
| 209 | DocumentType dt = xmlDocNode.getDoctype();
|
---|
| 210 |
|
---|
| 211 | String name = dt.getName();
|
---|
| 212 | String pid = dt.getPublicId();
|
---|
| 213 | String sid = dt.getSystemId();
|
---|
| 214 |
|
---|
[16690] | 215 | String doctype_str = "<!DOCTYPE " + dt.getName() + " PUBLIC \"" + pid + "\" \"" + sid + "\">\n";
|
---|
[16374] | 216 |
|
---|
| 217 | xmlRepresentation.append(doctype_str);
|
---|
| 218 | }
|
---|
| 219 | getString(xmlDocNode.getDocumentElement(), xmlRepresentation, depth, pretty);
|
---|
[10202] | 220 | return;
|
---|
[9874] | 221 | }
|
---|
[3768] | 222 | // Handle Element nodes
|
---|
| 223 | if (nodeType == Node.ELEMENT_NODE) {
|
---|
[3970] | 224 | if (pretty) {
|
---|
[10202] | 225 | xmlRepresentation.append("\n");
|
---|
[3970] | 226 | for (int i = 0; i < depth; i++) {
|
---|
[10202] | 227 | xmlRepresentation.append(" ");
|
---|
[3970] | 228 | }
|
---|
| 229 | }
|
---|
[3768] | 230 |
|
---|
| 231 | // Write opening tag
|
---|
[10202] | 232 | xmlRepresentation.append("<");
|
---|
| 233 | xmlRepresentation.append(nodeName);
|
---|
[3970] | 234 |
|
---|
[3768] | 235 | // Write the node attributes
|
---|
| 236 | NamedNodeMap nodeAttributes = xmlNode.getAttributes();
|
---|
| 237 | for (int i = 0; i < nodeAttributes.getLength(); i++) {
|
---|
| 238 | Node attribute = nodeAttributes.item(i);
|
---|
[10202] | 239 | xmlRepresentation.append(" ");
|
---|
| 240 | xmlRepresentation.append(attribute.getNodeName());
|
---|
| 241 | xmlRepresentation.append("=\"");
|
---|
| 242 | xmlRepresentation.append(attribute.getNodeValue());
|
---|
| 243 | xmlRepresentation.append("\"");
|
---|
[3768] | 244 | }
|
---|
| 245 |
|
---|
| 246 | // If the node has no children, close the opening tag and return
|
---|
| 247 | if (xmlNode.hasChildNodes() == false) {
|
---|
| 248 | // This produces somewhat ugly output, but it is necessary to compensate
|
---|
| 249 | // for display bugs in Netscape. Firstly, the space is needed before the
|
---|
| 250 | // closing bracket otherwise Netscape will ignore some tags (<br/>, for
|
---|
| 251 | // example). Also, a newline character would be expected after the tag,
|
---|
| 252 | // but this causes problems with the display of links (the link text
|
---|
| 253 | // will contain a newline character, which is displayed badly).
|
---|
[10202] | 254 | xmlRepresentation.append(" />");
|
---|
| 255 | return;
|
---|
[3768] | 256 | }
|
---|
[3970] | 257 |
|
---|
[3768] | 258 | // Close the opening tag
|
---|
[10202] | 259 | xmlRepresentation.append(">");
|
---|
[3970] | 260 |
|
---|
[3768] | 261 | // Apply recursively to the children of this node
|
---|
[4285] | 262 | // hack for nodes next to text nodes - dont make them pretty
|
---|
| 263 | // this is needed for text inside a <pre> element - any new lines
|
---|
| 264 | // or spaces around the span elements show up in the text
|
---|
[3768] | 265 | NodeList children = xmlNode.getChildNodes();
|
---|
[4285] | 266 | boolean do_pretty = pretty;
|
---|
[3768] | 267 | for (int i = 0; i < children.getLength(); i++) {
|
---|
[4285] | 268 | if (children.item(i).getNodeType()==Node.TEXT_NODE) {
|
---|
| 269 | do_pretty=false; // if there is a text node amongst the children, do teh following nodes in non-pretty mode - hope this doesn't stuff up something else
|
---|
| 270 | }
|
---|
[10202] | 271 | getString(children.item(i), xmlRepresentation, depth + 1, do_pretty);
|
---|
[3768] | 272 | }
|
---|
[3970] | 273 |
|
---|
[3768] | 274 | // Write closing tag
|
---|
[3970] | 275 | if (pretty) {
|
---|
[10202] | 276 | if (xmlRepresentation.charAt(xmlRepresentation.length()-1) == '\n') {
|
---|
[3970] | 277 | for (int i = 0; i < depth; i++)
|
---|
[10202] | 278 | xmlRepresentation.append(" ");
|
---|
[3970] | 279 | }
|
---|
[3768] | 280 | }
|
---|
[10202] | 281 | xmlRepresentation.append("</");
|
---|
| 282 | xmlRepresentation.append(nodeName);
|
---|
| 283 | xmlRepresentation.append(">");
|
---|
[3970] | 284 | if (pretty) {
|
---|
[10202] | 285 | xmlRepresentation.append("\n");
|
---|
[3970] | 286 | }
|
---|
[3501] | 287 | }
|
---|
[3970] | 288 |
|
---|
[3768] | 289 | // Handle Text nodes
|
---|
| 290 | else if (nodeType == Node.TEXT_NODE) {
|
---|
| 291 | String text = xmlNode.getNodeValue();
|
---|
| 292 |
|
---|
| 293 | // Perform output escaping, if required
|
---|
| 294 | if (outputEscaping) {
|
---|
| 295 | text = text.replaceAll("&", "&"); // Must be done first!!
|
---|
| 296 | text = text.replaceAll("<", "<");
|
---|
| 297 | text = text.replaceAll(">", ">");
|
---|
| 298 | text = text.replaceAll("\"", """);
|
---|
| 299 | text = text.replaceAll("\'", "'");
|
---|
| 300 | }
|
---|
| 301 |
|
---|
| 302 | // Remove any control-C characters
|
---|
| 303 | text = text.replaceAll("" + (char) 3, "");
|
---|
[10202] | 304 | xmlRepresentation.append(text);
|
---|
[3768] | 305 | }
|
---|
| 306 |
|
---|
| 307 | // Handle Processing Instruction nodes
|
---|
| 308 | else if (nodeType == Node.PROCESSING_INSTRUCTION_NODE) {
|
---|
[4013] | 309 | if (nodeName.equals("javax.xml.transform.disable-output-escaping")) {
|
---|
[3768] | 310 | outputEscaping = false;
|
---|
| 311 | }
|
---|
[4013] | 312 | else if (nodeName.equals("javax.xml.transform.enable-output-escaping")) {
|
---|
[3768] | 313 | outputEscaping = true;
|
---|
| 314 | }
|
---|
| 315 | else {
|
---|
[13210] | 316 | logger.warn("Unhandled processing instruction " + nodeName);
|
---|
[3768] | 317 | }
|
---|
| 318 | }
|
---|
[8923] | 319 |
|
---|
| 320 | else if (nodeType == Node.COMMENT_NODE) {
|
---|
| 321 | String text = xmlNode.getNodeValue();
|
---|
[10202] | 322 | xmlRepresentation.append("<!-- ");
|
---|
| 323 | xmlRepresentation.append(text);
|
---|
| 324 | xmlRepresentation.append(" -->");
|
---|
[8923] | 325 | }
|
---|
| 326 |
|
---|
| 327 |
|
---|
| 328 |
|
---|
[3768] | 329 | // A type of node that is not handled yet
|
---|
| 330 | else {
|
---|
[13210] | 331 | logger.warn("Unknown node type: " + nodeType+" "+getNodeTypeString(nodeType));
|
---|
[3768] | 332 | }
|
---|
| 333 |
|
---|
[10202] | 334 | return;
|
---|
[3222] | 335 | }
|
---|
[3908] | 336 |
|
---|
| 337 | protected static String getNodeTypeString(short node_type) {
|
---|
| 338 |
|
---|
| 339 | String type = "";
|
---|
| 340 | switch(node_type) {
|
---|
| 341 | case Node.ATTRIBUTE_NODE:
|
---|
| 342 | type="ATTRIBUTE_NODE";
|
---|
| 343 | break;
|
---|
| 344 | case Node.CDATA_SECTION_NODE:
|
---|
| 345 | type="CDATA_SECTION_NODE";
|
---|
| 346 | break;
|
---|
| 347 | case Node.COMMENT_NODE:
|
---|
| 348 | type="COMMENT_NODE";
|
---|
| 349 | break;
|
---|
| 350 | case Node.DOCUMENT_FRAGMENT_NODE:
|
---|
| 351 | type="DOCUMENT_FRAGMENT_NODE";
|
---|
| 352 | break;
|
---|
| 353 | case Node.DOCUMENT_NODE:
|
---|
| 354 | type="DOCUMENT_NODE";
|
---|
| 355 | break;
|
---|
| 356 | case Node.DOCUMENT_TYPE_NODE:
|
---|
| 357 | type="DOCUMENT_TYPE_NODE";
|
---|
| 358 | break;
|
---|
| 359 | case Node.ELEMENT_NODE:
|
---|
| 360 | type="ELEMENT_NODE";
|
---|
| 361 | break;
|
---|
| 362 | case Node.ENTITY_NODE:
|
---|
| 363 | type="ENTITY_NODE";
|
---|
| 364 | break;
|
---|
| 365 | case Node.ENTITY_REFERENCE_NODE:
|
---|
| 366 | type="ENTITY_REFERENCE_NODE";
|
---|
| 367 | break;
|
---|
| 368 | case Node.NOTATION_NODE:
|
---|
| 369 | type="NOTATION_NODE";
|
---|
| 370 | break;
|
---|
| 371 | case Node.PROCESSING_INSTRUCTION_NODE:
|
---|
| 372 | type="PROCESSING_INSTRUCTION_NODE";
|
---|
| 373 | break;
|
---|
| 374 | case Node.TEXT_NODE:
|
---|
| 375 | type="TEXT_NODE";
|
---|
| 376 | break;
|
---|
| 377 | default:
|
---|
| 378 | type="UNKNOWN";
|
---|
| 379 | }
|
---|
| 380 |
|
---|
| 381 | return type;
|
---|
| 382 | }
|
---|
[3222] | 383 | }
|
---|