Changeset 10202 for trunk/gsdl3


Ignore:
Timestamp:
2005-07-01T16:24:07+12:00 (19 years ago)
Author:
kjdon
Message:

getString now uses a StringBuffer instead of concating lots of Strings

File:
1 edited

Legend:

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

    r10067 r10202  
    8585        this.parser.parse(xml_source);
    8686        Document doc = this.parser.getDocument();
     87       
    8788        return doc;
    8889       
     
    141142    {
    142143    outputEscaping = true;
    143     return getString(xmlNode, 0, false);
     144    StringBuffer xmlRepresentation = new StringBuffer();
     145    getString(xmlNode, xmlRepresentation, 0, false);
     146    return xmlRepresentation.toString();
    144147    }
    145148
     
    150153   
    151154    outputEscaping = true;
    152     return getString(xmlNode, 0, true);
    153     }
    154 
    155     private String getString(Node xmlNode, int depth, boolean pretty)
     155    StringBuffer xmlRepresentation = new StringBuffer();
     156    getString(xmlNode, xmlRepresentation, 0, true);
     157    return xmlRepresentation.toString();
     158    }
     159
     160    private void getString(Node xmlNode, StringBuffer xmlRepresentation,
     161                 int depth, boolean pretty)
    156162    {
    157     String xmlRepresentation = "";
    158 
    159     if (xmlNode == null)
    160         return "<null>";
     163   
     164    if (xmlNode == null) {
     165        xmlRepresentation.append("<null>");
     166        return;
     167    }
    161168
    162169    short nodeType = xmlNode.getNodeType();
     
    164171
    165172    if (nodeType == Node.DOCUMENT_NODE) {
    166         return getString(((Document)xmlNode).getDocumentElement(), depth, pretty);
     173        getString(((Document)xmlNode).getDocumentElement(), xmlRepresentation, depth, pretty);
     174        return;
    167175    }
    168176    // Handle Element nodes
    169177    if (nodeType == Node.ELEMENT_NODE) {
    170178        if (pretty) {
    171         xmlRepresentation += "\n";
     179        xmlRepresentation.append("\n");
    172180        for (int i = 0; i < depth; i++) {
    173             xmlRepresentation += "  ";
     181            xmlRepresentation.append("  ");
    174182        }
    175183        }
    176184
    177185        // Write opening tag
    178         xmlRepresentation += "<" + nodeName;
     186        xmlRepresentation.append("<");
     187        xmlRepresentation.append(nodeName);
    179188       
    180189        // Write the node attributes
     
    182191        for (int i = 0; i < nodeAttributes.getLength(); i++) {
    183192        Node attribute = nodeAttributes.item(i);
    184         xmlRepresentation += " " + attribute.getNodeName() + "=";
    185         xmlRepresentation += "\"" + attribute.getNodeValue() + "\"";
     193        xmlRepresentation.append(" ");
     194        xmlRepresentation.append(attribute.getNodeName());
     195        xmlRepresentation.append("=\"");
     196        xmlRepresentation.append(attribute.getNodeValue());
     197        xmlRepresentation.append("\"");
    186198        }
    187199
     
    194206        // but this causes problems with the display of links (the link text
    195207        // will contain a newline character, which is displayed badly).
    196         xmlRepresentation += " />";
    197         return xmlRepresentation;
     208        xmlRepresentation.append(" />");
     209        return;
    198210        }
    199211       
    200212        // Close the opening tag
    201         xmlRepresentation += ">";
     213        xmlRepresentation.append(">");
    202214       
    203215        // Apply recursively to the children of this node
     
    211223            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
    212224        }
    213         xmlRepresentation += getString(children.item(i), depth + 1, do_pretty);
     225        getString(children.item(i), xmlRepresentation, depth + 1, do_pretty);
    214226        }
    215227       
    216228        // Write closing tag
    217229        if (pretty) {
    218         if (xmlRepresentation.endsWith("\n")) {
     230        if (xmlRepresentation.charAt(xmlRepresentation.length()-1) == '\n') {
    219231            for (int i = 0; i < depth; i++)
    220             xmlRepresentation += "  ";
     232            xmlRepresentation.append("  ");
    221233        }
    222234        }
    223         xmlRepresentation += "</" + nodeName + ">";
     235        xmlRepresentation.append("</");
     236        xmlRepresentation.append(nodeName);
     237        xmlRepresentation.append(">");
    224238        if (pretty) {
    225         xmlRepresentation += "\n";
     239        xmlRepresentation.append("\n");
    226240        }
    227241    }
     
    242256        // Remove any control-C characters
    243257        text = text.replaceAll("" + (char) 3, "");
    244         xmlRepresentation += text;
     258        xmlRepresentation.append(text);
    245259    }
    246260
     
    260274    else if (nodeType == Node.COMMENT_NODE) {
    261275        String text = xmlNode.getNodeValue();
    262         xmlRepresentation += "<!-- "+text+" -->";
     276        xmlRepresentation.append("<!-- ");
     277        xmlRepresentation.append(text);
     278        xmlRepresentation.append(" -->");
    263279    }
    264280       
     
    270286    }
    271287
    272     return xmlRepresentation;
     288    return;
    273289    }
    274290
Note: See TracChangeset for help on using the changeset viewer.