Changeset 3768
- Timestamp:
- 2003-02-26T10:33:44+13:00 (21 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gsdl3/src/java/org/greenstone/gsdl3/util/XMLConverter.java
r3501 r3768 22 22 import org.w3c.dom.Document; 23 23 import org.w3c.dom.Node; 24 import org.w3c.dom.NodeList; 25 import org.w3c.dom.NamedNodeMap; 24 26 import org.xml.sax.InputSource; 25 27 import javax.xml.parsers.*; 26 import org.apache.xindice.xml.TextWriter;27 28 import org.apache.xerces.dom.TextImpl; 28 29 import org.apache.xerces.parsers.DOMParser; … … 53 54 protected DOMParser parser_ = null; 54 55 56 private boolean outputEscaping = true; 57 58 55 59 /** the no-args constructor */ 56 60 public XMLConverter() { … … 109 113 110 114 /** returns the Node as a String */ 111 public String getString(Node doc) { 112 if (doc==null) { 113 return ""; 114 } 115 TextWriter t = new TextWriter(doc); 116 return t.toString(); 115 public String getString(Node xmlNode) 116 { 117 outputEscaping = true; 118 return getString(xmlNode, 0); 119 } 120 121 122 private String getString(Node xmlNode, int depth) 123 { 124 String xmlRepresentation = ""; 125 126 short nodeType = xmlNode.getNodeType(); 127 String nodeName = xmlNode.getNodeName(); 128 129 // Handle Element nodes 130 if (nodeType == Node.ELEMENT_NODE) { 131 xmlRepresentation += "\n"; 132 for (int i = 0; i < depth; i++) 133 xmlRepresentation += " "; 134 135 // Write opening tag 136 xmlRepresentation += "<" + nodeName; 137 138 // Write the node attributes 139 NamedNodeMap nodeAttributes = xmlNode.getAttributes(); 140 for (int i = 0; i < nodeAttributes.getLength(); i++) { 141 Node attribute = nodeAttributes.item(i); 142 xmlRepresentation += " " + attribute.getNodeName() + "="; 143 xmlRepresentation += "\"" + attribute.getNodeValue() + "\""; 144 } 145 146 // If the node has no children, close the opening tag and return 147 if (xmlNode.hasChildNodes() == false) { 148 // This produces somewhat ugly output, but it is necessary to compensate 149 // for display bugs in Netscape. Firstly, the space is needed before the 150 // closing bracket otherwise Netscape will ignore some tags (<br/>, for 151 // example). Also, a newline character would be expected after the tag, 152 // but this causes problems with the display of links (the link text 153 // will contain a newline character, which is displayed badly). 154 xmlRepresentation += " />"; 155 return xmlRepresentation; 156 } 157 158 // Close the opening tag 159 xmlRepresentation += ">"; 160 161 // Apply recursively to the children of this node 162 NodeList children = xmlNode.getChildNodes(); 163 for (int i = 0; i < children.getLength(); i++) { 164 xmlRepresentation += getString(children.item(i), depth + 1); 165 } 166 167 // Write closing tag 168 if (xmlRepresentation.endsWith("\n")) { 169 for (int i = 0; i < depth; i++) 170 xmlRepresentation += " "; 171 } 172 xmlRepresentation += "</" + nodeName + ">\n"; 173 } 174 175 // Handle Text nodes 176 else if (nodeType == Node.TEXT_NODE) { 177 String text = xmlNode.getNodeValue(); 178 179 // Perform output escaping, if required 180 if (outputEscaping) { 181 text = text.replaceAll("&", "&"); // Must be done first!! 182 text = text.replaceAll("<", "<"); 183 text = text.replaceAll(">", ">"); 184 text = text.replaceAll("\"", """); 185 text = text.replaceAll("\'", "'"); 186 } 187 188 // Remove any control-C characters 189 text = text.replaceAll("" + (char) 3, ""); 190 xmlRepresentation += text; 191 } 192 193 // Handle Processing Instruction nodes 194 else if (nodeType == Node.PROCESSING_INSTRUCTION_NODE) { 195 if (nodeName == "javax.xml.transform.disable-output-escaping") { 196 outputEscaping = false; 197 } 198 else if (nodeName == "javax.xml.transform.enable-output-escaping") { 199 outputEscaping = true; 200 } 201 else { 202 System.err.println("Warning: Unhandled processing instruction " + nodeName); 203 } 204 } 205 206 // A type of node that is not handled yet 207 else { 208 System.err.println("Warning: Unknown node type: " + nodeType); 209 } 210 211 return xmlRepresentation; 117 212 } 118 213 }
Note:
See TracChangeset
for help on using the changeset viewer.