Changeset 28978
- Timestamp:
- 2014-04-16T12:10:50+12:00 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/XMLConverter.java
r28962 r28978 72 72 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.XMLConverter.class.getName()); 73 73 74 //protected EntityResolver resolver = null;75 76 private static boolean outputEscaping = true;77 78 74 /** the no-args constructor */ 79 75 public XMLConverter() … … 81 77 82 78 } 83 84 // /** sets the entity resolver. pass in null to unset it */85 // public void setEntityResolver(EntityResolver er)86 // {87 // this.resolver = er;88 // }89 79 90 80 /** returns a DOM Document */ … … 242 232 public static String getString(Node xmlNode) 243 233 { 244 outputEscaping = true;245 234 StringBuffer xmlRepresentation = new StringBuffer(); 246 235 getString(xmlNode, xmlRepresentation, 0, false); … … 255 244 public static String getPrettyString(Node xmlNode) 256 245 { 257 258 outputEscaping = true;259 246 StringBuffer xmlRepresentation = new StringBuffer(); 260 247 getString(xmlNode, xmlRepresentation, 0, true); … … 356 343 xmlRepresentation.append(">"); 357 344 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 362 350 NodeList children = xmlNode.getChildNodes(); 363 351 boolean do_pretty = pretty; 352 boolean output_escaping = true; // record if we have encountered a disable-output-escaping instruction 364 353 for (int i = 0; i < children.getLength(); i++) 365 354 { 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, "&", "&"); 377 text = StringUtils.replace(text, "<", "<"); 378 text = StringUtils.replace(text, ">", ">"); 379 text = StringUtils.replace(text, "'", "'"); 380 text = StringUtils.replace(text, "\"", """); 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 372 393 373 394 // Write closing tag … … 387 408 xmlRepresentation.append("\n"); 388 409 } 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, "&", "&"); 402 text = StringUtils.replace(text, "<", "<"); 403 text = StringUtils.replace(text, ">", ">"); 404 text = StringUtils.replace(text, "'", "'"); 405 text = StringUtils.replace(text, "\"", """); 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 430 411 431 412 else if (nodeType == Node.COMMENT_NODE) … … 437 418 } 438 419 420 // TEXT and PROCESSING_INSTRUCTION nodes are handled inside their containing element node 439 421 // A type of node that is not handled yet 440 422 else
Note:
See TracChangeset
for help on using the changeset viewer.