MCPcopy Create free account
hub / github.com/apache/freemarker / simplify

Method simplify

src/main/java/freemarker/ext/dom/NodeModel.java:469–504  ·  view source on GitHub ↗

Removes comments and processing instruction, and then unites adjacent text nodes. Note that CDATA sections count as text nodes.

(Node node)

Source from the content-addressed store, hash-verified

467 * Note that CDATA sections count as text nodes.
468 */
469 static public void simplify(Node node) {
470 NodeList children = node.getChildNodes();
471 int i = 0;
472 int len = children.getLength();
473 Node prevTextChild = null;
474 while (i < len) {
475 Node child = children.item(i);
476 if (child.hasChildNodes()) {
477 simplify(child);
478 prevTextChild = null;
479 i++;
480 } else {
481 int type = child.getNodeType();
482 if (type == Node.PROCESSING_INSTRUCTION_NODE) {
483 node.removeChild(child);
484 len--;
485 } else if (type == Node.COMMENT_NODE) {
486 node.removeChild(child);
487 len--;
488 } else if (type == Node.TEXT_NODE || type == Node.CDATA_SECTION_NODE ) {
489 if (prevTextChild != null) {
490 CharacterData ptc = (CharacterData) prevTextChild;
491 ptc.setData(ptc.getNodeValue() + child.getNodeValue());
492 node.removeChild(child);
493 len--;
494 } else {
495 prevTextChild = child;
496 i++;
497 }
498 } else {
499 prevTextChild = null;
500 i++;
501 }
502 }
503 }
504 }
505
506 NodeModel getDocumentNodeModel() {
507 if (node instanceof Document) {

Callers 1

parseMethod · 0.95

Calls 2

getChildNodesMethod · 0.65
getNodeTypeMethod · 0.65

Tested by

no test coverage detected