Changeset 28978


Ignore:
Timestamp:
2014-04-16T12:10:50+12:00 (10 years ago)
Author:
kjdon
Message:

removed commented out code. removed static variable outputEscaping. what will happen if two threads are doing pretty printing???. Now we process text and processing instruction nodes in a line, not recursively, so can use the processing instructions when processing the text nods in the for loop.

File:
1 edited

Legend:

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

    r28962 r28978  
    7272    static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.XMLConverter.class.getName());
    7373
    74   //protected EntityResolver resolver = null;
    75 
    76     private static boolean outputEscaping = true;
    77 
    7874    /** the no-args constructor */
    7975    public XMLConverter()
     
    8177
    8278    }
    83 
    84     // /** sets the entity resolver. pass in null to unset it */
    85     // public void setEntityResolver(EntityResolver er)
    86     // {
    87     //  this.resolver = er;
    88     // }
    8979
    9080    /** returns a DOM Document */
     
    242232    public static String getString(Node xmlNode)
    243233    {
    244         outputEscaping = true;
    245234        StringBuffer xmlRepresentation = new StringBuffer();
    246235        getString(xmlNode, xmlRepresentation, 0, false);
     
    255244    public static String getPrettyString(Node xmlNode)
    256245    {
    257 
    258         outputEscaping = true;
    259246        StringBuffer xmlRepresentation = new StringBuffer();
    260247        getString(xmlNode, xmlRepresentation, 0, true);
     
    356343            xmlRepresentation.append(">");
    357344
    358             // Apply recursively to the children of this node
    359             // hack for nodes next to text nodes - dont make them pretty
    360             // this is needed for text inside a <pre> element - any new lines
    361             // or spaces around the span elements show up in the text
     345            // Process the children. We process text nodes here, but recursively process other nodes.
     346            // hack for nodes next to text nodes - dont make them pretty, ie don'e do any new lines or indenting
     347            // Usually text nodes will be inside their own element. Sometimes we have eg span tags next to text nodes - don't want those indented.
     348            // also if these are inside a pre tag then the space shows up in the page.
     349           
    362350            NodeList children = xmlNode.getChildNodes();
    363351            boolean do_pretty = pretty;
     352            boolean output_escaping = true; // record if we have encountered a disable-output-escaping instruction
    364353            for (int i = 0; i < children.getLength(); i++)
    365354            {
    366                 if (children.item(i).getNodeType() == Node.TEXT_NODE)
    367                 {
    368                     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
    369                 }
    370                 getString(children.item(i), xmlRepresentation, depth + 1, do_pretty);
    371             }
     355              Node child = children.item(i);
     356              short child_type = child.getNodeType();
     357              if (child_type == Node.PROCESSING_INSTRUCTION_NODE) {
     358                if (child.getNodeName().equals("javax.xml.transform.disable-output-escaping")) {
     359                  output_escaping = false;
     360                }
     361                else if (child.getNodeName().equals("javax.xml.transform.enable-output-escaping")) {
     362                  output_escaping = true;
     363                }
     364                else {
     365                  logger.warn("Unhandled processing instruction " + child.getNodeName());
     366                }
     367              }
     368              else if (child_type == Node.TEXT_NODE) {
     369               do_pretty = false; // if there is a text node amongst the children, do all the following nodes in non-pretty mode - hope this doesn't stuff up something else
     370                // output the text
     371                String text = child.getNodeValue();
     372
     373                // Perform output escaping, if required
     374                // Apache Commons replace method is far superior to String.replaceAll - very fast!
     375                if (output_escaping) {
     376                  text = StringUtils.replace(text, "&", "&amp;");
     377                  text = StringUtils.replace(text, "<", "&lt;");
     378                  text = StringUtils.replace(text, ">", "&gt;");
     379                  text = StringUtils.replace(text, "'", "&apos;");
     380                  text = StringUtils.replace(text, "\"", "&quot;");
     381                }
     382                // Remove any control-C characters
     383                text = StringUtils.replace(text, "" + (char) 3, "");
     384               
     385                xmlRepresentation.append(text);
     386               
     387              }
     388              else {
     389                // recursively call getString
     390                getString(child, xmlRepresentation, depth + 1, do_pretty);
     391              }
     392            } // foreach child of the element
    372393
    373394            // Write closing tag
     
    387408                xmlRepresentation.append("\n");
    388409            }
    389         }
    390 
    391         // Handle Text nodes
    392         else if (nodeType == Node.TEXT_NODE)
    393         {
    394             String text = xmlNode.getNodeValue();
    395 
    396             // Perform output escaping, if required
    397             // Apache Commons replace method is far superior to String.replaceAll - very fast!
    398             if (outputEscaping)
    399             {
    400 
    401                 text = StringUtils.replace(text, "&", "&amp;");
    402                 text = StringUtils.replace(text, "<", "&lt;");
    403                 text = StringUtils.replace(text, ">", "&gt;");
    404                 text = StringUtils.replace(text, "'", "&apos;");
    405                 text = StringUtils.replace(text, "\"", "&quot;");
    406             }
    407 
    408             // Remove any control-C characters
    409             text = StringUtils.replace(text, "" + (char) 3, "");
    410 
    411             xmlRepresentation.append(text);
    412         }
    413 
    414         // Handle Processing Instruction nodes
    415         else if (nodeType == Node.PROCESSING_INSTRUCTION_NODE)
    416         {
    417             if (nodeName.equals("javax.xml.transform.disable-output-escaping"))
    418             {
    419                 outputEscaping = false;
    420             }
    421             else if (nodeName.equals("javax.xml.transform.enable-output-escaping"))
    422             {
    423                 outputEscaping = true;
    424             }
    425             else
    426             {
    427                 logger.warn("Unhandled processing instruction " + nodeName);
    428             }
    429         }
     410        } // ELEMENT_NODE
    430411
    431412        else if (nodeType == Node.COMMENT_NODE)
     
    437418        }
    438419
     420        // TEXT and PROCESSING_INSTRUCTION nodes are handled inside their containing element node
    439421        // A type of node that is not handled yet
    440422        else
Note: See TracChangeset for help on using the changeset viewer.