Changeset 3970


Ignore:
Timestamp:
2003-03-25T09:44:12+12:00 (21 years ago)
Author:
kjdon
Message:

now have getString() - for plain output, and getPrettyString() for nocely indented output - only use the pretty one for printing - when its read back in it introduces extra text nodes

File:
1 edited

Legend:

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

    r3941 r3970  
    106106    {
    107107    outputEscaping = true;
    108     return getString(xmlNode, 0);
    109     }
    110 
    111 
    112     private String getString(Node xmlNode, int depth)
     108    return getString(xmlNode, 0, false);
     109    }
     110
     111    /** returns the node as a nicely formatted String - this introduces extra
     112     * text nodes if the String is read back in as a DOM, so should only be
     113     * used for printing */
     114    public String getPrettyString(Node xmlNode) {
     115   
     116    outputEscaping = true;
     117    return getString(xmlNode, 0, true);
     118    }
     119
     120    private String getString(Node xmlNode, int depth, boolean pretty)
    113121    {
    114122    String xmlRepresentation = "";
     
    122130    // Handle Element nodes
    123131    if (nodeType == Node.ELEMENT_NODE) {
    124         xmlRepresentation += "\n";
    125         for (int i = 0; i < depth; i++)
    126         xmlRepresentation += "  ";
     132        if (pretty) {
     133        xmlRepresentation += "\n";
     134        for (int i = 0; i < depth; i++) {
     135            xmlRepresentation += "  ";
     136        }
     137        }
    127138
    128139        // Write opening tag
    129140        xmlRepresentation += "<" + nodeName;
    130 
     141       
    131142        // Write the node attributes
    132143        NamedNodeMap nodeAttributes = xmlNode.getAttributes();
     
    148159        return xmlRepresentation;
    149160        }
    150 
     161       
    151162        // Close the opening tag
    152163        xmlRepresentation += ">";
    153 
     164       
    154165        // Apply recursively to the children of this node
    155166        NodeList children = xmlNode.getChildNodes();
    156167        for (int i = 0; i < children.getLength(); i++) {
    157         xmlRepresentation += getString(children.item(i), depth + 1);
    158         }
    159 
     168        xmlRepresentation += getString(children.item(i), depth + 1, pretty);
     169        }
     170       
    160171        // Write closing tag
    161         if (xmlRepresentation.endsWith("\n")) {
    162         for (int i = 0; i < depth; i++)
    163             xmlRepresentation += "  ";
    164         }
    165         xmlRepresentation += "</" + nodeName + ">\n";
    166     }
    167 
     172        if (pretty) {
     173        if (xmlRepresentation.endsWith("\n")) {
     174            for (int i = 0; i < depth; i++)
     175            xmlRepresentation += "  ";
     176        }
     177        }
     178        xmlRepresentation += "</" + nodeName + ">";
     179        if (pretty) {
     180        xmlRepresentation += "\n";
     181        }
     182    }
     183   
    168184    // Handle Text nodes
    169185    else if (nodeType == Node.TEXT_NODE) {
Note: See TracChangeset for help on using the changeset viewer.