Changeset 10202
- Timestamp:
- 2005-07-01T16:24:07+12:00 (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gsdl3/src/java/org/greenstone/gsdl3/util/XMLConverter.java
r10067 r10202 85 85 this.parser.parse(xml_source); 86 86 Document doc = this.parser.getDocument(); 87 87 88 return doc; 88 89 … … 141 142 { 142 143 outputEscaping = true; 143 return getString(xmlNode, 0, false); 144 StringBuffer xmlRepresentation = new StringBuffer(); 145 getString(xmlNode, xmlRepresentation, 0, false); 146 return xmlRepresentation.toString(); 144 147 } 145 148 … … 150 153 151 154 outputEscaping = true; 152 return getString(xmlNode, 0, true); 153 } 154 155 private String getString(Node xmlNode, int depth, boolean pretty) 155 StringBuffer xmlRepresentation = new StringBuffer(); 156 getString(xmlNode, xmlRepresentation, 0, true); 157 return xmlRepresentation.toString(); 158 } 159 160 private void getString(Node xmlNode, StringBuffer xmlRepresentation, 161 int depth, boolean pretty) 156 162 { 157 String xmlRepresentation = ""; 158 159 if (xmlNode == null) 160 return "<null>"; 163 164 if (xmlNode == null) { 165 xmlRepresentation.append("<null>"); 166 return; 167 } 161 168 162 169 short nodeType = xmlNode.getNodeType(); … … 164 171 165 172 if (nodeType == Node.DOCUMENT_NODE) { 166 return getString(((Document)xmlNode).getDocumentElement(), depth, pretty); 173 getString(((Document)xmlNode).getDocumentElement(), xmlRepresentation, depth, pretty); 174 return; 167 175 } 168 176 // Handle Element nodes 169 177 if (nodeType == Node.ELEMENT_NODE) { 170 178 if (pretty) { 171 xmlRepresentation += "\n";179 xmlRepresentation.append("\n"); 172 180 for (int i = 0; i < depth; i++) { 173 xmlRepresentation += " ";181 xmlRepresentation.append(" "); 174 182 } 175 183 } 176 184 177 185 // Write opening tag 178 xmlRepresentation += "<" + nodeName; 186 xmlRepresentation.append("<"); 187 xmlRepresentation.append(nodeName); 179 188 180 189 // Write the node attributes … … 182 191 for (int i = 0; i < nodeAttributes.getLength(); i++) { 183 192 Node attribute = nodeAttributes.item(i); 184 xmlRepresentation += " " + attribute.getNodeName() + "="; 185 xmlRepresentation += "\"" + attribute.getNodeValue() + "\""; 193 xmlRepresentation.append(" "); 194 xmlRepresentation.append(attribute.getNodeName()); 195 xmlRepresentation.append("=\""); 196 xmlRepresentation.append(attribute.getNodeValue()); 197 xmlRepresentation.append("\""); 186 198 } 187 199 … … 194 206 // but this causes problems with the display of links (the link text 195 207 // will contain a newline character, which is displayed badly). 196 xmlRepresentation += " />";197 return xmlRepresentation;208 xmlRepresentation.append(" />"); 209 return; 198 210 } 199 211 200 212 // Close the opening tag 201 xmlRepresentation += ">";213 xmlRepresentation.append(">"); 202 214 203 215 // Apply recursively to the children of this node … … 211 223 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 212 224 } 213 xmlRepresentation += getString(children.item(i), depth + 1, do_pretty);225 getString(children.item(i), xmlRepresentation, depth + 1, do_pretty); 214 226 } 215 227 216 228 // Write closing tag 217 229 if (pretty) { 218 if (xmlRepresentation. endsWith("\n")) {230 if (xmlRepresentation.charAt(xmlRepresentation.length()-1) == '\n') { 219 231 for (int i = 0; i < depth; i++) 220 xmlRepresentation += " ";232 xmlRepresentation.append(" "); 221 233 } 222 234 } 223 xmlRepresentation += "</" + nodeName + ">"; 235 xmlRepresentation.append("</"); 236 xmlRepresentation.append(nodeName); 237 xmlRepresentation.append(">"); 224 238 if (pretty) { 225 xmlRepresentation += "\n";239 xmlRepresentation.append("\n"); 226 240 } 227 241 } … … 242 256 // Remove any control-C characters 243 257 text = text.replaceAll("" + (char) 3, ""); 244 xmlRepresentation += text;258 xmlRepresentation.append(text); 245 259 } 246 260 … … 260 274 else if (nodeType == Node.COMMENT_NODE) { 261 275 String text = xmlNode.getNodeValue(); 262 xmlRepresentation += "<!-- "+text+" -->"; 276 xmlRepresentation.append("<!-- "); 277 xmlRepresentation.append(text); 278 xmlRepresentation.append(" -->"); 263 279 } 264 280 … … 270 286 } 271 287 272 return xmlRepresentation;288 return; 273 289 } 274 290
Note:
See TracChangeset
for help on using the changeset viewer.