| 1 |
// Copyright 2002-2007, FreeHEP. |
| 2 |
package org.freehep.graphicsbase.xml.util; |
| 3 |
|
| 4 |
import java.awt.Color; |
| 5 |
import java.io.IOException; |
| 6 |
import java.io.Writer; |
| 7 |
import java.util.Enumeration; |
| 8 |
import java.util.Hashtable; |
| 9 |
import java.util.Stack; |
| 10 |
|
| 11 |
import org.freehep.util.io.IndentPrintWriter; |
| 12 |
import org.w3c.dom.NamedNodeMap; |
| 13 |
import org.w3c.dom.Node; |
| 14 |
import org.w3c.dom.NodeList; |
| 15 |
|
| 16 |
/** |
| 17 |
* A class that makes it easy to write XML documents. |
| 18 |
* |
| 19 |
* @author Tony Johnson |
| 20 |
* @author Mark Donszelmann |
| 21 |
* @version $Id: XMLWriter.java 10527 2007-02-13 17:49:08Z duns $ |
| 22 |
*/ |
| 23 |
public class XMLWriter implements XMLTagWriter |
| 24 |
{ |
| 25 |
public XMLWriter(Writer w, String indentString, String defaultNameSpace) |
| 26 |
{ |
| 27 |
writer = new IndentPrintWriter(w); |
| 28 |
writer.setIndentString(indentString); |
| 29 |
this.defaultNameSpace = defaultNameSpace; |
| 30 |
} |
| 31 |
|
| 32 |
public XMLWriter(Writer w, String indentString) { |
| 33 |
this(w, indentString, ""); |
| 34 |
} |
| 35 |
|
| 36 |
public XMLWriter(Writer w) |
| 37 |
{ |
| 38 |
this(w, " "); // By popular demand of Babar |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* closes the writer |
| 43 |
*/ |
| 44 |
public void close() throws IOException |
| 45 |
{ |
| 46 |
closeDoc(); |
| 47 |
writer.close(); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Opens the document with an xml header |
| 52 |
*/ |
| 53 |
public void openDoc() |
| 54 |
{ |
| 55 |
openDoc("1.0", "", false); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Opens the document with an xml header |
| 60 |
*/ |
| 61 |
public void openDoc(String version, String encoding, boolean standalone) |
| 62 |
{ |
| 63 |
String indentString = writer.getIndentString(); |
| 64 |
writer.setIndentString(indentString); |
| 65 |
|
| 66 |
closed = false; |
| 67 |
if (!XMLCharacterProperties.validVersionNum(version)) throw new RuntimeException("Invalid version number: "+version); |
| 68 |
writer.print("<?xml version=\""); |
| 69 |
writer.print(version); |
| 70 |
writer.print("\" "); |
| 71 |
if ((encoding != null) && (!encoding.equals(""))) { |
| 72 |
if (!XMLCharacterProperties.validEncName(encoding)) throw new RuntimeException("Invalid encoding name: "+encoding); |
| 73 |
writer.print("encoding=\""); |
| 74 |
writer.print(encoding); |
| 75 |
writer.print("\" "); |
| 76 |
} |
| 77 |
if (standalone) { |
| 78 |
writer.print("standalone=\"yes\" "); |
| 79 |
} |
| 80 |
writer.println("?>"); |
| 81 |
writer.setIndentString(indentString); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Writes a reference to a DTD |
| 86 |
*/ |
| 87 |
public void referToDTD(String name, String pid, String ref) { |
| 88 |
if (dtdName != null) { |
| 89 |
throw new RuntimeException("ReferToDTD cannot be called twice"); |
| 90 |
} |
| 91 |
dtdName = name; |
| 92 |
writer.println("<!DOCTYPE "+name+" PUBLIC \""+pid+"\" \""+ref+"\">"); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Writes a reference to a DTD |
| 97 |
*/ |
| 98 |
public void referToDTD(String name, String system) |
| 99 |
{ |
| 100 |
if (dtdName != null) { |
| 101 |
throw new RuntimeException("ReferToDTD cannot be called twice"); |
| 102 |
} |
| 103 |
dtdName = name; |
| 104 |
writer.println("<!DOCTYPE "+name+" SYSTEM \""+system+"\">"); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Closes the document, and checks if you closed all the tags |
| 109 |
*/ |
| 110 |
public void closeDoc() |
| 111 |
{ |
| 112 |
if (!closed) { |
| 113 |
if (!openTags.isEmpty()) { |
| 114 |
StringBuffer sb = new StringBuffer("Not all tags were closed before closing XML document:\n"); |
| 115 |
while (!openTags.isEmpty()) { |
| 116 |
sb.append(" </"); |
| 117 |
sb.append(openTags.pop()); |
| 118 |
sb.append(">\n"); |
| 119 |
} |
| 120 |
throw new RuntimeException(sb.toString()); |
| 121 |
} |
| 122 |
closed = true; |
| 123 |
} |
| 124 |
writer.flush(); |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Print a comment |
| 129 |
*/ |
| 130 |
public void printComment(String comment) |
| 131 |
{ |
| 132 |
if (comment.indexOf("--") >= 0) throw new RuntimeException("'--' sequence not allowed in comment"); |
| 133 |
writer.print("<!--"); |
| 134 |
writer.print(normalizeText(comment)); |
| 135 |
writer.println("-->"); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Prints character data, while escaping < and > |
| 140 |
*/ |
| 141 |
public void print(String text) |
| 142 |
{ |
| 143 |
writer.print(normalizeText(text)); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
/** |
| 148 |
* Prints character data, while escaping < and > |
| 149 |
*/ |
| 150 |
public void println(String text) |
| 151 |
{ |
| 152 |
print(text); |
| 153 |
writer.println(); |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Prints a new XML tag and increases the identation level |
| 158 |
*/ |
| 159 |
public void openTag(String namespace, String name) |
| 160 |
{ |
| 161 |
if (namespace.equals(defaultNameSpace)) { |
| 162 |
openTag(name); |
| 163 |
} else { |
| 164 |
openTag(namespace+":"+name); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Prints a new XML tag and increases the identation level |
| 170 |
*/ |
| 171 |
public void openTag(String name) |
| 172 |
{ |
| 173 |
checkNameValid(name); |
| 174 |
if (openTags.isEmpty() && dtdName != null && !dtdName.equals(name)) { |
| 175 |
throw new RuntimeException("First tag: '"+name+"' not equal to DTD id: '"+dtdName+"'"); |
| 176 |
} |
| 177 |
writer.print("<"+name); |
| 178 |
printAttributes(name.length()); |
| 179 |
writer.println(">"); |
| 180 |
writer.indent(); |
| 181 |
openTags.push(name); |
| 182 |
} |
| 183 |
/** |
| 184 |
* Closes the current XML tag and decreases the indentation level |
| 185 |
*/ |
| 186 |
public void closeTag() |
| 187 |
{ |
| 188 |
if (openTags.isEmpty()) { |
| 189 |
writer.close(); |
| 190 |
throw new RuntimeException("No open tags"); |
| 191 |
} |
| 192 |
Object name = openTags.pop(); |
| 193 |
writer.outdent(); |
| 194 |
writer.print("</"); |
| 195 |
writer.print(name); |
| 196 |
writer.println(">"); |
| 197 |
} |
| 198 |
/** |
| 199 |
* Prints an empty XML tag. |
| 200 |
*/ |
| 201 |
public void printTag(String namespace, String name) |
| 202 |
{ |
| 203 |
if (namespace.equals(defaultNameSpace)) { |
| 204 |
printTag(name); |
| 205 |
} else { |
| 206 |
printTag(namespace+":"+name); |
| 207 |
} |
| 208 |
} |
| 209 |
/** |
| 210 |
* Prints an empty XML tag. |
| 211 |
*/ |
| 212 |
public void printTag(String name) |
| 213 |
{ |
| 214 |
checkNameValid(name); |
| 215 |
writer.print("<"+name); |
| 216 |
printAttributes(name.length()); |
| 217 |
writer.println("/>"); |
| 218 |
} |
| 219 |
/** |
| 220 |
* Sets an attribute which will be included in the next tag |
| 221 |
* printed by openTag or printTag |
| 222 |
*/ |
| 223 |
public void setAttribute(String name, String value) |
| 224 |
{ |
| 225 |
if ((name != null) && (value != null)) { |
| 226 |
attributes.put(name,value); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
public void setAttribute(String namespace, String name, String value) |
| 231 |
{ |
| 232 |
if ((namespace != null) && (name != null)) { |
| 233 |
attributes.put(namespace+":"+name, value); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
public void setAttribute(String name, byte value) { setAttribute(name, String.valueOf(value)); } |
| 238 |
public void setAttribute(String name, char value) { setAttribute(name, String.valueOf(value)); } |
| 239 |
public void setAttribute(String name, long value) { setAttribute(name, String.valueOf(value)); } |
| 240 |
public void setAttribute(String name, int value) { setAttribute(name, String.valueOf(value)); } |
| 241 |
public void setAttribute(String name, short value) { setAttribute(name, String.valueOf(value)); } |
| 242 |
public void setAttribute(String name, boolean value) { setAttribute(name, String.valueOf(value)); } |
| 243 |
public void setAttribute(String name, float value) { setAttribute(name, String.valueOf(value)); } |
| 244 |
public void setAttribute(String name, double value) { setAttribute(name, String.valueOf(value)); } |
| 245 |
public void setAttribute(String name, Color value) { setAttribute(name, String.valueOf(value)); } |
| 246 |
public void setAttribute(String ns, String name, byte value) { setAttribute(ns+":"+name, String.valueOf(value)); } |
| 247 |
public void setAttribute(String ns, String name, char value) { setAttribute(ns+":"+name, String.valueOf(value)); } |
| 248 |
public void setAttribute(String ns, String name, long value) { setAttribute(ns+":"+name, String.valueOf(value)); } |
| 249 |
public void setAttribute(String ns, String name, int value) { setAttribute(ns+":"+name, String.valueOf(value)); } |
| 250 |
public void setAttribute(String ns, String name, short value) { setAttribute(ns+":"+name, String.valueOf(value)); } |
| 251 |
public void setAttribute(String ns, String name, boolean value) { setAttribute(ns+":"+name, String.valueOf(value)); } |
| 252 |
public void setAttribute(String ns, String name, float value) { setAttribute(ns+":"+name, String.valueOf(value)); } |
| 253 |
public void setAttribute(String ns, String name, double value) { setAttribute(ns+":"+name, String.valueOf(value)); } |
| 254 |
public void setAttribute(String ns, String name, Color value) { setAttribute(ns+":"+name, String.valueOf(value)); } |
| 255 |
|
| 256 |
protected void printAttributes(int tagLength) |
| 257 |
{ |
| 258 |
int width = tagLength + 1; |
| 259 |
boolean extraIndent = false; |
| 260 |
Enumeration<String> e = attributes.keys(); |
| 261 |
while (e.hasMoreElements()) |
| 262 |
{ |
| 263 |
String key = e.nextElement().toString(); |
| 264 |
checkNameValid(key); |
| 265 |
String value = normalize(attributes.get(key).toString()); |
| 266 |
int length = key.length() + value.length() + 3; |
| 267 |
if (width > 0 && width + length + 2*writer.getIndent() > 60) |
| 268 |
{ |
| 269 |
width = 0; |
| 270 |
writer.println(); |
| 271 |
if (!extraIndent) |
| 272 |
{ |
| 273 |
writer.indent(); |
| 274 |
extraIndent = true; |
| 275 |
} |
| 276 |
} |
| 277 |
else |
| 278 |
{ |
| 279 |
width += length; |
| 280 |
writer.print(' '); |
| 281 |
} |
| 282 |
writer.print(key); |
| 283 |
writer.print("=\""); |
| 284 |
writer.print(value); |
| 285 |
writer.print("\""); |
| 286 |
} |
| 287 |
attributes.clear(); |
| 288 |
if (extraIndent) writer.outdent(); |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* Prints a DOM node, recursively. |
| 293 |
* No support for a document node |
| 294 |
*/ |
| 295 |
public void print(Node node) |
| 296 |
{ |
| 297 |
if ( node == null ) return; |
| 298 |
|
| 299 |
int type = node.getNodeType(); |
| 300 |
switch ( type ) { |
| 301 |
// print document |
| 302 |
case Node.DOCUMENT_NODE: |
| 303 |
throw new RuntimeException("No support for printing nodes of type Document"); |
| 304 |
|
| 305 |
// print element with attributes |
| 306 |
case Node.ELEMENT_NODE: |
| 307 |
NamedNodeMap attributes = node.getAttributes(); |
| 308 |
for ( int i = 0; i < attributes.getLength(); i++ ) { |
| 309 |
Node attr = attributes.item(i); |
| 310 |
setAttribute(attr.getNodeName(), attr.getNodeValue()); |
| 311 |
} |
| 312 |
NodeList children = node.getChildNodes(); |
| 313 |
if ( children == null ) { |
| 314 |
printTag(node.getNodeName()); |
| 315 |
} else { |
| 316 |
openTag(node.getNodeName()); |
| 317 |
int len = children.getLength(); |
| 318 |
for ( int i = 0; i < len; i++ ) { |
| 319 |
print(children.item(i)); |
| 320 |
} |
| 321 |
closeTag(); |
| 322 |
} |
| 323 |
break; |
| 324 |
|
| 325 |
// handle entity reference nodes |
| 326 |
case Node.ENTITY_REFERENCE_NODE: |
| 327 |
writer.print('&'); |
| 328 |
writer.print(node.getNodeName()); |
| 329 |
writer.print(';'); |
| 330 |
break; |
| 331 |
|
| 332 |
// print cdata sections |
| 333 |
case Node.CDATA_SECTION_NODE: |
| 334 |
writer.print("<![CDATA["); |
| 335 |
writer.print(node.getNodeValue()); |
| 336 |
writer.print("]]>"); |
| 337 |
break; |
| 338 |
|
| 339 |
// print text |
| 340 |
case Node.TEXT_NODE: |
| 341 |
print(node.getNodeValue()); |
| 342 |
break; |
| 343 |
|
| 344 |
// print processing instruction |
| 345 |
case Node.PROCESSING_INSTRUCTION_NODE: |
| 346 |
writer.print("<?"); |
| 347 |
writer.print(node.getNodeName()); |
| 348 |
String data = node.getNodeValue(); |
| 349 |
if ( data != null && data.length() > 0 ) { |
| 350 |
writer.print(' '); |
| 351 |
writer.print(data); |
| 352 |
} |
| 353 |
writer.print("?>"); |
| 354 |
break; |
| 355 |
} |
| 356 |
} // print(Node) |
| 357 |
|
| 358 |
/** Normalizes the given string for an Attribute value*/ |
| 359 |
public static String normalize(String s) |
| 360 |
{ |
| 361 |
StringBuffer str = new StringBuffer(); |
| 362 |
|
| 363 |
int len = (s != null) ? s.length() : 0; |
| 364 |
for (int i = 0; i < len; i++) { |
| 365 |
char ch = s.charAt(i); |
| 366 |
switch (ch) { |
| 367 |
case '<': { |
| 368 |
str.append("<"); |
| 369 |
break; |
| 370 |
} |
| 371 |
case '>': { |
| 372 |
str.append(">"); |
| 373 |
break; |
| 374 |
} |
| 375 |
case '&': { |
| 376 |
str.append("&"); |
| 377 |
break; |
| 378 |
} |
| 379 |
case '"': { |
| 380 |
str.append("""); |
| 381 |
break; |
| 382 |
} |
| 383 |
case '\r': |
| 384 |
case '\n': { |
| 385 |
str.append("&#"); |
| 386 |
str.append(Integer.toString(ch)); |
| 387 |
str.append(';'); |
| 388 |
break; |
| 389 |
} |
| 390 |
default: { |
| 391 |
if (ch > 0x00FF) { |
| 392 |
String hex = "0000"+Integer.toHexString(ch); |
| 393 |
str.append("&#x"); |
| 394 |
str.append(hex.substring(hex.length()-4)); |
| 395 |
str.append(';'); |
| 396 |
} else { |
| 397 |
str.append(ch); |
| 398 |
} |
| 399 |
} |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
return str.toString(); |
| 404 |
|
| 405 |
} // normalize(String):String |
| 406 |
|
| 407 |
/** Normalizes the given string for Text */ |
| 408 |
public static String normalizeText(String s) |
| 409 |
{ |
| 410 |
StringBuffer str = new StringBuffer(); |
| 411 |
|
| 412 |
int len = (s != null) ? s.length() : 0; |
| 413 |
for (int i = 0; i < len; i++) { |
| 414 |
char ch = s.charAt(i); |
| 415 |
switch (ch) { |
| 416 |
case '<': { |
| 417 |
str.append("<"); |
| 418 |
break; |
| 419 |
} |
| 420 |
case '>': { |
| 421 |
str.append(">"); |
| 422 |
break; |
| 423 |
} |
| 424 |
case '&': { |
| 425 |
str.append("&"); |
| 426 |
break; |
| 427 |
} |
| 428 |
default: { |
| 429 |
if (ch > 0x007f) { |
| 430 |
String hex = "0000"+Integer.toHexString(ch); |
| 431 |
str.append("&#x"); |
| 432 |
str.append(hex.substring(hex.length()-4)); |
| 433 |
str.append(';'); |
| 434 |
} else { |
| 435 |
str.append(ch); |
| 436 |
} |
| 437 |
} |
| 438 |
} |
| 439 |
} |
| 440 |
return str.toString(); |
| 441 |
} |
| 442 |
|
| 443 |
protected void checkNameValid(String s) |
| 444 |
{ |
| 445 |
if (!XMLCharacterProperties.validName(s)) throw new RuntimeException("Invalid name: "+s); |
| 446 |
} |
| 447 |
|
| 448 |
|
| 449 |
protected boolean closed = true; |
| 450 |
private String dtdName = null; |
| 451 |
private Hashtable<String, String> attributes = new Hashtable<String, String>(); |
| 452 |
private Stack<String> openTags = new Stack<String>(); |
| 453 |
protected IndentPrintWriter writer; |
| 454 |
protected String defaultNameSpace; |
| 455 |
} |