1 package com.quantum.util.xml;
3 import org.w3c.dom.Attr;
4 import org.w3c.dom.NamedNodeMap;
5 import org.w3c.dom.Node;
6 import org.w3c.dom.NodeList;
11 public class XMLRenderer {
13 protected boolean insertNewLine = false;
14 protected int numberOfTabs;
16 protected final static String LINE_SEPARATOR = System
17 .getProperty("line.separator");
19 protected final static String INDENT_STRING = "\t";
21 /** Returns a sorted list of attributes. */
22 protected Attr[] sortAttributes(NamedNodeMap attrs) {
24 int len = (attrs != null) ? attrs.getLength() : 0;
25 Attr array[] = new Attr[len];
26 for (int i = 0; i < len; i++) {
27 array[i] = (Attr) attrs.item(i);
29 for (int i = 0; i < len - 1; i++) {
30 String name = array[i].getNodeName();
32 for (int j = i + 1; j < len; j++) {
33 String curName = array[j].getNodeName();
34 if (curName.compareTo(name) < 0) {
41 array[i] = array[index];
50 protected XMLRenderer() {
51 this.numberOfTabs = 0;
52 this.insertNewLine = true;
55 protected void newLine(StringBuffer buffer) {
56 if (this.insertNewLine) {
57 buffer.append(LINE_SEPARATOR);
58 for (int i = 0; i < this.numberOfTabs; i++) {
59 buffer.append(INDENT_STRING);
64 /** Prints the specified node, recursively. */
65 protected void print(Node node, StringBuffer buffer) {
66 // is there anything to do?
68 int type = node.getNodeType();
71 case Node.DOCUMENT_NODE:
72 printDocumentNode(node, buffer);
75 // print element with attributes
76 case Node.ELEMENT_NODE:
77 printElementNode(node, buffer);
79 // handle entity reference nodes
80 case Node.ENTITY_REFERENCE_NODE:
81 printEntityReferenceNode(node, buffer);
84 // print cdata sections
85 case Node.CDATA_SECTION_NODE:
86 printCDataSectionNode(node, buffer);
91 printTextNode(node, buffer);
94 // print processing instruction
95 case Node.PROCESSING_INSTRUCTION_NODE:
96 printProcessingInstructionNode(node, buffer);
102 protected void printProcessingInstructionNode(Node node, StringBuffer buffer) {
104 buffer.append(node.getNodeName());
105 String data = node.getNodeValue();
106 if (data != null && data.length() > 0) {
113 protected void printTextNode(Node node, StringBuffer buffer) {
114 printString(node.getNodeValue(), buffer);
115 this.insertNewLine = false;
118 protected void printCDataSectionNode(Node node, StringBuffer buffer) {
119 buffer.append("<![CDATA[");
120 buffer.append(node.getNodeValue());
121 buffer.append("]]>");
124 protected void printEntityReferenceNode(Node node, StringBuffer buffer) {
126 buffer.append(node.getNodeName());
130 protected void printElementNode(Node node, StringBuffer buffer) {
134 buffer.append(node.getNodeName());
135 Attr attrs[] = sortAttributes(node.getAttributes());
136 for (int i = 0; i < attrs.length; i++) {
137 Attr attr = attrs[i];
139 buffer.append(attr.getNodeName());
140 buffer.append("=\"");
141 printString(attr.getNodeValue(), buffer);
145 if (!node.hasChildNodes()) {
146 buffer.append(" />");
150 printAllChildNodes(node, buffer);
155 buffer.append(node.getNodeName());
158 this.insertNewLine = true;
161 protected void printDocumentNode(Node node, StringBuffer buffer) {
162 buffer.append("<?xml version=\"1.0\" ?>");
164 printAllChildNodes(node, buffer);
167 protected void printAllChildNodes(Node node, StringBuffer buffer) {
168 NodeList children = node.getChildNodes();
169 for (int i = 0; i < children.getLength(); i++) {
170 print(children.item(i), buffer);
174 /** Normalizes the given string. */
175 protected void printString(String s, StringBuffer buffer) {
177 int len = (s != null) ? s.length() : 0;
178 for (int i = 0; i < len; i++) {
179 char ch = s.charAt(i);
182 buffer.append("<");
186 buffer.append(">");
190 buffer.append("&");
194 buffer.append(""");
205 public static String render(Node node) {
206 XMLRenderer renderer = new XMLRenderer();
207 StringBuffer buffer = new StringBuffer();
208 renderer.print(node, buffer);
210 return buffer.toString();
215 * Renders a String in a format that it would appear in a text node. That is
216 * to say, special characters (such as &) are converted into entity
217 * references (&amp;).
219 public static String render(String string) {
220 XMLRenderer renderer = new XMLRenderer();
221 StringBuffer buffer = new StringBuffer();
222 renderer.printString(string, buffer);
223 return buffer.toString();