Prints the specified node, recursively. @param node The node to output
(Node node)
| 50 | * @param node The node to output |
| 51 | */ |
| 52 | public void print(Node node) { |
| 53 | |
| 54 | // is there anything to do? |
| 55 | if (node == null) { |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | int type = node.getNodeType(); |
| 60 | switch (type) { |
| 61 | // print document |
| 62 | case Node.DOCUMENT_NODE: |
| 63 | print(((Document) node).getDocumentElement()); |
| 64 | out.flush(); |
| 65 | break; |
| 66 | |
| 67 | // print element with attributes |
| 68 | case Node.ELEMENT_NODE: |
| 69 | out.print('<'); |
| 70 | out.print(node.getLocalName()); |
| 71 | Attr[] attrs = sortAttributes(node.getAttributes()); |
| 72 | boolean xmlns = false; |
| 73 | for (Attr attr : attrs) { |
| 74 | if ("xmlns".equals(attr.getPrefix())) { |
| 75 | // Skip namespace prefixes as they are removed |
| 76 | continue; |
| 77 | } |
| 78 | out.print(' '); |
| 79 | out.print(attr.getLocalName()); |
| 80 | if ("xmlns".equals(attr.getLocalName())) { |
| 81 | xmlns = true; |
| 82 | } |
| 83 | out.print("=\""); |
| 84 | out.print(Escape.xml("", true, attr.getNodeValue())); |
| 85 | out.print('"'); |
| 86 | } |
| 87 | if (!xmlns && node.getNamespaceURI() != null) { |
| 88 | out.print(" xmlns=\""); |
| 89 | out.print(Escape.xml(node.getNamespaceURI())); |
| 90 | out.print('"'); |
| 91 | } |
| 92 | out.print('>'); |
| 93 | printChildren(node); |
| 94 | break; |
| 95 | |
| 96 | // handle entity reference nodes |
| 97 | case Node.ENTITY_REFERENCE_NODE: |
| 98 | printChildren(node); |
| 99 | break; |
| 100 | |
| 101 | // print cdata sections |
| 102 | case Node.CDATA_SECTION_NODE: |
| 103 | out.print(Escape.xml("", true, node.getNodeValue())); |
| 104 | break; |
| 105 | |
| 106 | // print text |
| 107 | case Node.TEXT_NODE: |
| 108 | out.print(Escape.xml("", false, node.getNodeValue())); |
| 109 | break; |
no test coverage detected