Changeset 3768


Ignore:
Timestamp:
2003-02-26T10:33:44+13:00 (21 years ago)
Author:
mdewsnip
Message:

Replaced usage of TextWriter in Xindice package with a custom-built function for displaying a DOM model as text. The new function respects the 'java.xml.transform.disable-output-escaping' and 'java.xml.transform.enable-output-escaping' processing instructions, formats the text much better (as an indented hierarchy), and works around some Netscape display issues.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/util/XMLConverter.java

    r3501 r3768  
    2222import org.w3c.dom.Document;
    2323import org.w3c.dom.Node;
     24import org.w3c.dom.NodeList;
     25import org.w3c.dom.NamedNodeMap;
    2426import org.xml.sax.InputSource;
    2527import javax.xml.parsers.*;
    26 import org.apache.xindice.xml.TextWriter;
    2728import org.apache.xerces.dom.TextImpl;
    2829import org.apache.xerces.parsers.DOMParser;
     
    5354    protected DOMParser parser_ = null;
    5455
     56    private boolean outputEscaping = true;
     57
     58
    5559     /** the no-args constructor */
    5660    public XMLConverter() {
     
    109113
    110114    /** returns the Node as a String */
    111     public String getString(Node doc) {
    112     if (doc==null) {
    113         return "";
    114     }
    115     TextWriter t = new TextWriter(doc);
    116     return t.toString();
     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    short nodeType = xmlNode.getNodeType();
     127    String nodeName = xmlNode.getNodeName();
     128
     129    // Handle Element nodes
     130    if (nodeType == Node.ELEMENT_NODE) {
     131        xmlRepresentation += "\n";
     132        for (int i = 0; i < depth; i++)
     133        xmlRepresentation += "  ";
     134
     135        // Write opening tag
     136        xmlRepresentation += "<" + nodeName;
     137
     138        // Write the node attributes
     139        NamedNodeMap nodeAttributes = xmlNode.getAttributes();
     140        for (int i = 0; i < nodeAttributes.getLength(); i++) {
     141        Node attribute = nodeAttributes.item(i);
     142        xmlRepresentation += " " + attribute.getNodeName() + "=";
     143        xmlRepresentation += "\"" + attribute.getNodeValue() + "\"";
     144        }
     145
     146        // If the node has no children, close the opening tag and return
     147        if (xmlNode.hasChildNodes() == false) {
     148        // This produces somewhat ugly output, but it is necessary to compensate
     149        // for display bugs in Netscape. Firstly, the space is needed before the
     150        // closing bracket otherwise Netscape will ignore some tags (<br/>, for
     151        // example). Also, a newline character would be expected after the tag,
     152        // but this causes problems with the display of links (the link text
     153        // will contain a newline character, which is displayed badly).
     154        xmlRepresentation += " />";
     155        return xmlRepresentation;
     156        }
     157
     158        // Close the opening tag
     159        xmlRepresentation += ">";
     160
     161        // Apply recursively to the children of this node
     162        NodeList children = xmlNode.getChildNodes();
     163        for (int i = 0; i < children.getLength(); i++) {
     164        xmlRepresentation += getString(children.item(i), depth + 1);
     165        }
     166
     167        // Write closing tag
     168        if (xmlRepresentation.endsWith("\n")) {
     169        for (int i = 0; i < depth; i++)
     170            xmlRepresentation += "  ";
     171        }
     172        xmlRepresentation += "</" + nodeName + ">\n";
     173    }
     174
     175    // Handle Text nodes
     176    else if (nodeType == Node.TEXT_NODE) {
     177        String text = xmlNode.getNodeValue();
     178
     179        // Perform output escaping, if required
     180        if (outputEscaping) {
     181        text = text.replaceAll("&", "&amp;");  // Must be done first!!
     182        text = text.replaceAll("<", "&lt;");
     183        text = text.replaceAll(">", "&gt;");
     184        text = text.replaceAll("\"", "&quot;");
     185        text = text.replaceAll("\'", "&apos;");
     186        }
     187
     188        // Remove any control-C characters
     189        text = text.replaceAll("" + (char) 3, "");
     190        xmlRepresentation += text;
     191    }
     192
     193    // Handle Processing Instruction nodes
     194    else if (nodeType == Node.PROCESSING_INSTRUCTION_NODE) {
     195        if (nodeName == "javax.xml.transform.disable-output-escaping") {
     196        outputEscaping = false;
     197        }
     198        else if (nodeName == "javax.xml.transform.enable-output-escaping") {
     199        outputEscaping = true;
     200        }
     201        else {
     202        System.err.println("Warning: Unhandled processing instruction " + nodeName);
     203        }
     204    }
     205
     206    // A type of node that is not handled yet
     207    else {
     208        System.err.println("Warning: Unknown node type: " + nodeType);
     209    }
     210
     211    return xmlRepresentation;
    117212    }
    118213}
Note: See TracChangeset for help on using the changeset viewer.