1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/txw2/output/XMLWriter.java Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,1075 @@ 1.4 +/* 1.5 + * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +// @@3RD PARTY CODE@@ 1.30 + 1.31 +// XMLWriter.java - serialize an XML document. 1.32 +// Written by David Megginson, david@megginson.com 1.33 +// NO WARRANTY! This class is in the public domain. 1.34 + 1.35 +// Id: XMLWriter.java,v 1.5 2000/09/17 01:08:16 david Exp 1.36 + 1.37 +package com.sun.xml.internal.txw2.output; 1.38 + 1.39 +import org.xml.sax.Attributes; 1.40 +import org.xml.sax.SAXException; 1.41 +import org.xml.sax.ext.LexicalHandler; 1.42 +import org.xml.sax.helpers.AttributesImpl; 1.43 +import org.xml.sax.helpers.XMLFilterImpl; 1.44 + 1.45 +import java.io.IOException; 1.46 +import java.io.OutputStreamWriter; 1.47 +import java.io.Writer; 1.48 +import java.util.HashMap; 1.49 +import java.util.Iterator; 1.50 +import java.util.Map; 1.51 + 1.52 + 1.53 +/** 1.54 + * Filter to write an XML document from a SAX event stream. 1.55 + * 1.56 + * <p>This class can be used by itself or as part of a SAX event 1.57 + * stream: it takes as input a series of SAX2 ContentHandler 1.58 + * events and uses the information in those events to write 1.59 + * an XML document. Since this class is a filter, it can also 1.60 + * pass the events on down a filter chain for further processing 1.61 + * (you can use the XMLWriter to take a snapshot of the current 1.62 + * state at any point in a filter chain), and it can be 1.63 + * used directly as a ContentHandler for a SAX2 XMLReader.</p> 1.64 + * 1.65 + * <p>The client creates a document by invoking the methods for 1.66 + * standard SAX2 events, always beginning with the 1.67 + * {@link #startDocument startDocument} method and ending with 1.68 + * the {@link #endDocument endDocument} method. There are convenience 1.69 + * methods provided so that clients to not have to create empty 1.70 + * attribute lists or provide empty strings as parameters; for 1.71 + * example, the method invocation</p> 1.72 + * 1.73 + * <pre> 1.74 + * w.startElement("foo"); 1.75 + * </pre> 1.76 + * 1.77 + * <p>is equivalent to the regular SAX2 ContentHandler method</p> 1.78 + * 1.79 + * <pre> 1.80 + * w.startElement("", "foo", "", new AttributesImpl()); 1.81 + * </pre> 1.82 + * 1.83 + * <p>Except that it is more efficient because it does not allocate 1.84 + * a new empty attribute list each time. The following code will send 1.85 + * a simple XML document to standard output:</p> 1.86 + * 1.87 + * <pre> 1.88 + * XMLWriter w = new XMLWriter(); 1.89 + * 1.90 + * w.startDocument(); 1.91 + * w.startElement("greeting"); 1.92 + * w.characters("Hello, world!"); 1.93 + * w.endElement("greeting"); 1.94 + * w.endDocument(); 1.95 + * </pre> 1.96 + * 1.97 + * <p>The resulting document will look like this:</p> 1.98 + * 1.99 + * <pre> 1.100 + * <?xml version="1.0" standalone="yes"?> 1.101 + * 1.102 + * <greeting>Hello, world!</greeting> 1.103 + * </pre> 1.104 + * 1.105 + * <p>In fact, there is an even simpler convenience method, 1.106 + * <var>dataElement</var>, designed for writing elements that 1.107 + * contain only character data, so the code to generate the 1.108 + * document could be shortened to</p> 1.109 + * 1.110 + * <pre> 1.111 + * XMLWriter w = new XMLWriter(); 1.112 + * 1.113 + * w.startDocument(); 1.114 + * w.dataElement("greeting", "Hello, world!"); 1.115 + * w.endDocument(); 1.116 + * </pre> 1.117 + * 1.118 + * <h2>Whitespace</h2> 1.119 + * 1.120 + * <p>According to the XML Recommendation, <em>all</em> whitespace 1.121 + * in an XML document is potentially significant to an application, 1.122 + * so this class never adds newlines or indentation. If you 1.123 + * insert three elements in a row, as in</p> 1.124 + * 1.125 + * <pre> 1.126 + * w.dataElement("item", "1"); 1.127 + * w.dataElement("item", "2"); 1.128 + * w.dataElement("item", "3"); 1.129 + * </pre> 1.130 + * 1.131 + * <p>you will end up with</p> 1.132 + * 1.133 + * <pre> 1.134 + * <item>1</item><item>3</item><item>3</item> 1.135 + * </pre> 1.136 + * 1.137 + * <p>You need to invoke one of the <var>characters</var> methods 1.138 + * explicitly to add newlines or indentation. Alternatively, you 1.139 + * can use {@link DataWriter}, which 1.140 + * is derived from this class -- it is optimized for writing 1.141 + * purely data-oriented (or field-oriented) XML, and does automatic 1.142 + * linebreaks and indentation (but does not support mixed content 1.143 + * properly).</p> 1.144 + * 1.145 + * 1.146 + * <h2>Namespace Support</h2> 1.147 + * 1.148 + * <p>The writer contains extensive support for XML Namespaces, so that 1.149 + * a client application does not have to keep track of prefixes and 1.150 + * supply <var>xmlns</var> attributes. By default, the XML writer will 1.151 + * generate Namespace declarations in the form _NS1, _NS2, etc., wherever 1.152 + * they are needed, as in the following example:</p> 1.153 + * 1.154 + * <pre> 1.155 + * w.startDocument(); 1.156 + * w.emptyElement("http://www.foo.com/ns/", "foo"); 1.157 + * w.endDocument(); 1.158 + * </pre> 1.159 + * 1.160 + * <p>The resulting document will look like this:</p> 1.161 + * 1.162 + * <pre> 1.163 + * <?xml version="1.0" standalone="yes"?> 1.164 + * 1.165 + * <_NS1:foo xmlns:_NS1="http://www.foo.com/ns/"/> 1.166 + * </pre> 1.167 + * 1.168 + * <p>In many cases, document authors will prefer to choose their 1.169 + * own prefixes rather than using the (ugly) default names. The 1.170 + * XML writer allows two methods for selecting prefixes:</p> 1.171 + * 1.172 + * <ol> 1.173 + * <li>the qualified name</li> 1.174 + * <li>the {@link #setPrefix setPrefix} method.</li> 1.175 + * </ol> 1.176 + * 1.177 + * <p>Whenever the XML writer finds a new Namespace URI, it checks 1.178 + * to see if a qualified (prefixed) name is also available; if so 1.179 + * it attempts to use the name's prefix (as long as the prefix is 1.180 + * not already in use for another Namespace URI).</p> 1.181 + * 1.182 + * <p>Before writing a document, the client can also pre-map a prefix 1.183 + * to a Namespace URI with the setPrefix method:</p> 1.184 + * 1.185 + * <pre> 1.186 + * w.setPrefix("http://www.foo.com/ns/", "foo"); 1.187 + * w.startDocument(); 1.188 + * w.emptyElement("http://www.foo.com/ns/", "foo"); 1.189 + * w.endDocument(); 1.190 + * </pre> 1.191 + * 1.192 + * <p>The resulting document will look like this:</p> 1.193 + * 1.194 + * <pre> 1.195 + * <?xml version="1.0" standalone="yes"?> 1.196 + * 1.197 + * <foo:foo xmlns:foo="http://www.foo.com/ns/"/> 1.198 + * </pre> 1.199 + * 1.200 + * <p>The default Namespace simply uses an empty string as the prefix:</p> 1.201 + * 1.202 + * <pre> 1.203 + * w.setPrefix("http://www.foo.com/ns/", ""); 1.204 + * w.startDocument(); 1.205 + * w.emptyElement("http://www.foo.com/ns/", "foo"); 1.206 + * w.endDocument(); 1.207 + * </pre> 1.208 + * 1.209 + * <p>The resulting document will look like this:</p> 1.210 + * 1.211 + * <pre> 1.212 + * <?xml version="1.0" standalone="yes"?> 1.213 + * 1.214 + * <foo xmlns="http://www.foo.com/ns/"/> 1.215 + * </pre> 1.216 + * 1.217 + * <p>By default, the XML writer will not declare a Namespace until 1.218 + * it is actually used. Sometimes, this approach will create 1.219 + * a large number of Namespace declarations, as in the following 1.220 + * example:</p> 1.221 + * 1.222 + * <pre> 1.223 + * <xml version="1.0" standalone="yes"?> 1.224 + * 1.225 + * <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> 1.226 + * <rdf:Description about="http://www.foo.com/ids/books/12345"> 1.227 + * <dc:title xmlns:dc="http://www.purl.org/dc/">A Dark Night</dc:title> 1.228 + * <dc:creator xmlns:dc="http://www.purl.org/dc/">Jane Smith</dc:title> 1.229 + * <dc:date xmlns:dc="http://www.purl.org/dc/">2000-09-09</dc:title> 1.230 + * </rdf:Description> 1.231 + * </rdf:RDF> 1.232 + * </pre> 1.233 + * 1.234 + * <p>The "rdf" prefix is declared only once, because the RDF Namespace 1.235 + * is used by the root element and can be inherited by all of its 1.236 + * descendants; the "dc" prefix, on the other hand, is declared three 1.237 + * times, because no higher element uses the Namespace. To solve this 1.238 + * problem, you can instruct the XML writer to predeclare Namespaces 1.239 + * on the root element even if they are not used there:</p> 1.240 + * 1.241 + * <pre> 1.242 + * w.forceNSDecl("http://www.purl.org/dc/"); 1.243 + * </pre> 1.244 + * 1.245 + * <p>Now, the "dc" prefix will be declared on the root element even 1.246 + * though it's not needed there, and can be inherited by its 1.247 + * descendants:</p> 1.248 + * 1.249 + * <pre> 1.250 + * <xml version="1.0" standalone="yes"?> 1.251 + * 1.252 + * <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 1.253 + * xmlns:dc="http://www.purl.org/dc/"> 1.254 + * <rdf:Description about="http://www.foo.com/ids/books/12345"> 1.255 + * <dc:title>A Dark Night</dc:title> 1.256 + * <dc:creator>Jane Smith</dc:title> 1.257 + * <dc:date>2000-09-09</dc:title> 1.258 + * </rdf:Description> 1.259 + * </rdf:RDF> 1.260 + * </pre> 1.261 + * 1.262 + * <p>This approach is also useful for declaring Namespace prefixes 1.263 + * that be used by qualified names appearing in attribute values or 1.264 + * character data.</p> 1.265 + * 1.266 + * @author David Megginson, david@megginson.com 1.267 + * @version 0.2 1.268 + * @since JAXB1.0 1.269 + * @see org.xml.sax.XMLFilter 1.270 + * @see org.xml.sax.ContentHandler 1.271 + */ 1.272 +public class XMLWriter extends XMLFilterImpl implements LexicalHandler 1.273 +{ 1.274 + //////////////////////////////////////////////////////////////////// 1.275 + // Constructors. 1.276 + //////////////////////////////////////////////////////////////////// 1.277 + 1.278 + 1.279 + 1.280 + 1.281 + /** 1.282 + * Create a new XML writer. 1.283 + * 1.284 + * <p>Write to the writer provided.</p> 1.285 + * 1.286 + * @param writer 1.287 + * The output destination, or null to use standard output. 1.288 + * @param encoding 1.289 + * If non-null string is specified, it is written as a part 1.290 + * of the XML declaration. 1.291 + */ 1.292 + public XMLWriter (Writer writer, String encoding, CharacterEscapeHandler _escapeHandler ) 1.293 + { 1.294 + init(writer,encoding); 1.295 + this.escapeHandler = _escapeHandler; 1.296 + } 1.297 + 1.298 + public XMLWriter (Writer writer, String encoding ) { 1.299 + this( writer, encoding, DumbEscapeHandler.theInstance ); 1.300 + } 1.301 + 1.302 + 1.303 + 1.304 + /** 1.305 + * Internal initialization method. 1.306 + * 1.307 + * <p>All of the public constructors invoke this method. 1.308 + * 1.309 + * @param writer The output destination, or null to use 1.310 + * standard output. 1.311 + */ 1.312 + private void init (Writer writer,String encoding) 1.313 + { 1.314 + setOutput(writer,encoding); 1.315 + } 1.316 + 1.317 + 1.318 + 1.319 + //////////////////////////////////////////////////////////////////// 1.320 + // Public methods. 1.321 + //////////////////////////////////////////////////////////////////// 1.322 + 1.323 + 1.324 + /** 1.325 + * Reset the writer. 1.326 + * 1.327 + * <p>This method is especially useful if the writer throws an 1.328 + * exception before it is finished, and you want to reuse the 1.329 + * writer for a new document. It is usually a good idea to 1.330 + * invoke {@link #flush flush} before resetting the writer, 1.331 + * to make sure that no output is lost.</p> 1.332 + * 1.333 + * <p>This method is invoked automatically by the 1.334 + * {@link #startDocument startDocument} method before writing 1.335 + * a new document.</p> 1.336 + * 1.337 + * <p><strong>Note:</strong> this method will <em>not</em> 1.338 + * clear the prefix or URI information in the writer or 1.339 + * the selected output writer.</p> 1.340 + * 1.341 + * @see #flush() 1.342 + */ 1.343 + public void reset () 1.344 + { 1.345 + elementLevel = 0; 1.346 + startTagIsClosed = true; 1.347 + } 1.348 + 1.349 + 1.350 + /** 1.351 + * Flush the output. 1.352 + * 1.353 + * <p>This method flushes the output stream. It is especially useful 1.354 + * when you need to make certain that the entire document has 1.355 + * been written to output but do not want to _commit the output 1.356 + * stream.</p> 1.357 + * 1.358 + * <p>This method is invoked automatically by the 1.359 + * {@link #endDocument endDocument} method after writing a 1.360 + * document.</p> 1.361 + * 1.362 + * @see #reset() 1.363 + */ 1.364 + public void flush () 1.365 + throws IOException 1.366 + { 1.367 + output.flush(); 1.368 + } 1.369 + 1.370 + 1.371 + /** 1.372 + * Set a new output destination for the document. 1.373 + * 1.374 + * @param writer The output destination, or null to use 1.375 + * standard output. 1.376 + * @see #flush() 1.377 + */ 1.378 + public void setOutput (Writer writer,String _encoding) 1.379 + { 1.380 + if (writer == null) { 1.381 + output = new OutputStreamWriter(System.out); 1.382 + } else { 1.383 + output = writer; 1.384 + } 1.385 + encoding = _encoding; 1.386 + } 1.387 + 1.388 + public void setEncoding(String encoding) { 1.389 + this.encoding = encoding; 1.390 + } 1.391 + 1.392 + /** 1.393 + * Set whether the writer should print out the XML declaration 1.394 + * (<?xml version='1.0' ... ?>). 1.395 + * <p> 1.396 + * This option is set to true by default. 1.397 + */ 1.398 + public void setXmlDecl( boolean _writeXmlDecl ) { 1.399 + this.writeXmlDecl = _writeXmlDecl; 1.400 + } 1.401 + 1.402 + /** 1.403 + * Sets the header string. 1.404 + * 1.405 + * This string will be written right after the xml declaration 1.406 + * without any escaping. Useful for generating a boiler-plate 1.407 + * DOCTYPE decl, PIs, and comments. 1.408 + * 1.409 + * @param _header 1.410 + * passing null will work as if the empty string is passed. 1.411 + */ 1.412 + public void setHeader( String _header ) { 1.413 + this.header = _header; 1.414 + } 1.415 + 1.416 + 1.417 + private final HashMap locallyDeclaredPrefix = new HashMap(); 1.418 + public void startPrefixMapping( String prefix, String uri ) throws SAXException { 1.419 + locallyDeclaredPrefix.put(prefix,uri); 1.420 + } 1.421 + 1.422 + 1.423 + //////////////////////////////////////////////////////////////////// 1.424 + // Methods from org.xml.sax.ContentHandler. 1.425 + //////////////////////////////////////////////////////////////////// 1.426 + 1.427 + /** 1.428 + * Write the XML declaration at the beginning of the document. 1.429 + * 1.430 + * Pass the event on down the filter chain for further processing. 1.431 + * 1.432 + * @exception org.xml.sax.SAXException If there is an error 1.433 + * writing the XML declaration, or if a handler further down 1.434 + * the filter chain raises an exception. 1.435 + * @see org.xml.sax.ContentHandler#startDocument() 1.436 + */ 1.437 + public void startDocument () 1.438 + throws SAXException 1.439 + { 1.440 + try { 1.441 + reset(); 1.442 + 1.443 + if(writeXmlDecl) { 1.444 + String e=""; 1.445 + if(encoding!=null) 1.446 + e = " encoding=\""+encoding+"\""; 1.447 + 1.448 + write("<?xml version=\"1.0\""+e+" standalone=\"yes\"?>\n"); 1.449 + } 1.450 + 1.451 + if(header!=null) 1.452 + write(header); 1.453 + 1.454 + super.startDocument(); 1.455 + } catch( IOException e ) { 1.456 + throw new SAXException(e); 1.457 + } 1.458 + } 1.459 + 1.460 + 1.461 + /** 1.462 + * Write a newline at the end of the document. 1.463 + * 1.464 + * Pass the event on down the filter chain for further processing. 1.465 + * 1.466 + * @exception org.xml.sax.SAXException If there is an error 1.467 + * writing the newline, or if a handler further down 1.468 + * the filter chain raises an exception. 1.469 + * @see org.xml.sax.ContentHandler#endDocument() 1.470 + */ 1.471 + public void endDocument () 1.472 + throws SAXException 1.473 + { 1.474 + try { 1.475 + if (!startTagIsClosed) { 1.476 + write("/>"); 1.477 + startTagIsClosed = true; 1.478 + } 1.479 + write('\n'); 1.480 + super.endDocument(); 1.481 + try { 1.482 + flush(); 1.483 + } catch (IOException e) { 1.484 + throw new SAXException(e); 1.485 + } 1.486 + } catch( IOException e ) { 1.487 + throw new SAXException(e); 1.488 + } 1.489 + } 1.490 + 1.491 + 1.492 + /** 1.493 + * Write a start tag. 1.494 + * 1.495 + * Pass the event on down the filter chain for further processing. 1.496 + * 1.497 + * @param uri The Namespace URI, or the empty string if none 1.498 + * is available. 1.499 + * @param localName The element's local (unprefixed) name (required). 1.500 + * @param qName The element's qualified (prefixed) name, or the 1.501 + * empty string is none is available. This method will 1.502 + * use the qName as a template for generating a prefix 1.503 + * if necessary, but it is not guaranteed to use the 1.504 + * same qName. 1.505 + * @param atts The element's attribute list (must not be null). 1.506 + * @exception org.xml.sax.SAXException If there is an error 1.507 + * writing the start tag, or if a handler further down 1.508 + * the filter chain raises an exception. 1.509 + * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes) 1.510 + */ 1.511 + public void startElement (String uri, String localName, 1.512 + String qName, Attributes atts) 1.513 + throws SAXException 1.514 + { 1.515 + try { 1.516 + if (!startTagIsClosed) { 1.517 + write(">"); 1.518 + } 1.519 + elementLevel++; 1.520 +// nsSupport.pushContext(); 1.521 + 1.522 + write('<'); 1.523 + writeName(uri, localName, qName, true); 1.524 + writeAttributes(atts); 1.525 + 1.526 + // declare namespaces specified by the startPrefixMapping methods 1.527 + if(!locallyDeclaredPrefix.isEmpty()) { 1.528 + Iterator itr = locallyDeclaredPrefix.entrySet().iterator(); 1.529 + while(itr.hasNext()) { 1.530 + Map.Entry e = (Map.Entry)itr.next(); 1.531 + String p = (String)e.getKey(); 1.532 + String u = (String)e.getValue(); 1.533 + if (u == null) { 1.534 + u = ""; 1.535 + } 1.536 + write(' '); 1.537 + if ("".equals(p)) { 1.538 + write("xmlns=\""); 1.539 + } else { 1.540 + write("xmlns:"); 1.541 + write(p); 1.542 + write("=\""); 1.543 + } 1.544 + char ch[] = u.toCharArray(); 1.545 + writeEsc(ch, 0, ch.length, true); 1.546 + write('\"'); 1.547 + } 1.548 + locallyDeclaredPrefix.clear(); // clear the contents 1.549 + } 1.550 + 1.551 +// if (elementLevel == 1) { 1.552 +// forceNSDecls(); 1.553 +// } 1.554 +// writeNSDecls(); 1.555 + super.startElement(uri, localName, qName, atts); 1.556 + startTagIsClosed = false; 1.557 + } catch( IOException e ) { 1.558 + throw new SAXException(e); 1.559 + } 1.560 + } 1.561 + 1.562 + 1.563 + /** 1.564 + * Write an end tag. 1.565 + * 1.566 + * Pass the event on down the filter chain for further processing. 1.567 + * 1.568 + * @param uri The Namespace URI, or the empty string if none 1.569 + * is available. 1.570 + * @param localName The element's local (unprefixed) name (required). 1.571 + * @param qName The element's qualified (prefixed) name, or the 1.572 + * empty string is none is available. This method will 1.573 + * use the qName as a template for generating a prefix 1.574 + * if necessary, but it is not guaranteed to use the 1.575 + * same qName. 1.576 + * @exception org.xml.sax.SAXException If there is an error 1.577 + * writing the end tag, or if a handler further down 1.578 + * the filter chain raises an exception. 1.579 + * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String) 1.580 + */ 1.581 + public void endElement (String uri, String localName, String qName) 1.582 + throws SAXException 1.583 + { 1.584 + try { 1.585 + if (startTagIsClosed) { 1.586 + write("</"); 1.587 + writeName(uri, localName, qName, true); 1.588 + write('>'); 1.589 + } else { 1.590 + write("/>"); 1.591 + startTagIsClosed = true; 1.592 + } 1.593 + if (elementLevel == 1) { 1.594 + write('\n'); 1.595 + } 1.596 + super.endElement(uri, localName, qName); 1.597 +// nsSupport.popContext(); 1.598 + elementLevel--; 1.599 + } catch( IOException e ) { 1.600 + throw new SAXException(e); 1.601 + } 1.602 + } 1.603 + 1.604 + 1.605 + /** 1.606 + * Write character data. 1.607 + * 1.608 + * Pass the event on down the filter chain for further processing. 1.609 + * 1.610 + * @param ch The array of characters to write. 1.611 + * @param start The starting position in the array. 1.612 + * @param len The number of characters to write. 1.613 + * @exception org.xml.sax.SAXException If there is an error 1.614 + * writing the characters, or if a handler further down 1.615 + * the filter chain raises an exception. 1.616 + * @see org.xml.sax.ContentHandler#characters(char[], int, int) 1.617 + */ 1.618 + public void characters (char ch[], int start, int len) 1.619 + throws SAXException 1.620 + { 1.621 + try { 1.622 + if (!startTagIsClosed) { 1.623 + write('>'); 1.624 + startTagIsClosed = true; 1.625 + } 1.626 + if(inCDATA) 1.627 + output.write(ch,start,len); 1.628 + else 1.629 + writeEsc(ch, start, len, false); 1.630 + super.characters(ch, start, len); 1.631 + } catch( IOException e ) { 1.632 + throw new SAXException(e); 1.633 + } 1.634 + } 1.635 + 1.636 + 1.637 + /** 1.638 + * Write ignorable whitespace. 1.639 + * 1.640 + * Pass the event on down the filter chain for further processing. 1.641 + * 1.642 + * @param ch The array of characters to write. 1.643 + * @param start The starting position in the array. 1.644 + * @param length The number of characters to write. 1.645 + * @exception org.xml.sax.SAXException If there is an error 1.646 + * writing the whitespace, or if a handler further down 1.647 + * the filter chain raises an exception. 1.648 + * @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int, int) 1.649 + */ 1.650 + public void ignorableWhitespace (char ch[], int start, int length) 1.651 + throws SAXException 1.652 + { 1.653 + try { 1.654 + writeEsc(ch, start, length, false); 1.655 + super.ignorableWhitespace(ch, start, length); 1.656 + } catch( IOException e ) { 1.657 + throw new SAXException(e); 1.658 + } 1.659 + } 1.660 + 1.661 + 1.662 + 1.663 + /** 1.664 + * Write a processing instruction. 1.665 + * 1.666 + * Pass the event on down the filter chain for further processing. 1.667 + * 1.668 + * @param target The PI target. 1.669 + * @param data The PI data. 1.670 + * @exception org.xml.sax.SAXException If there is an error 1.671 + * writing the PI, or if a handler further down 1.672 + * the filter chain raises an exception. 1.673 + * @see org.xml.sax.ContentHandler#processingInstruction(java.lang.String, java.lang.String) 1.674 + */ 1.675 + public void processingInstruction (String target, String data) 1.676 + throws SAXException 1.677 + { 1.678 + try { 1.679 + if (!startTagIsClosed) { 1.680 + write('>'); 1.681 + startTagIsClosed = true; 1.682 + } 1.683 + write("<?"); 1.684 + write(target); 1.685 + write(' '); 1.686 + write(data); 1.687 + write("?>"); 1.688 + if (elementLevel < 1) { 1.689 + write('\n'); 1.690 + } 1.691 + super.processingInstruction(target, data); 1.692 + } catch( IOException e ) { 1.693 + throw new SAXException(e); 1.694 + } 1.695 + } 1.696 + 1.697 + 1.698 + 1.699 + //////////////////////////////////////////////////////////////////// 1.700 + // Convenience methods. 1.701 + //////////////////////////////////////////////////////////////////// 1.702 + 1.703 + 1.704 + 1.705 + /** 1.706 + * Start a new element without a qname or attributes. 1.707 + * 1.708 + * <p>This method will provide a default empty attribute 1.709 + * list and an empty string for the qualified name. 1.710 + * It invokes {@link 1.711 + * #startElement(String, String, String, Attributes)} 1.712 + * directly.</p> 1.713 + * 1.714 + * @param uri The element's Namespace URI. 1.715 + * @param localName The element's local name. 1.716 + * @exception org.xml.sax.SAXException If there is an error 1.717 + * writing the start tag, or if a handler further down 1.718 + * the filter chain raises an exception. 1.719 + * @see #startElement(String, String, String, Attributes) 1.720 + */ 1.721 + public void startElement (String uri, String localName) 1.722 + throws SAXException 1.723 + { 1.724 + startElement(uri, localName, "", EMPTY_ATTS); 1.725 + } 1.726 + 1.727 + 1.728 + /** 1.729 + * Start a new element without a qname, attributes or a Namespace URI. 1.730 + * 1.731 + * <p>This method will provide an empty string for the 1.732 + * Namespace URI, and empty string for the qualified name, 1.733 + * and a default empty attribute list. It invokes 1.734 + * #startElement(String, String, String, Attributes)} 1.735 + * directly.</p> 1.736 + * 1.737 + * @param localName The element's local name. 1.738 + * @exception org.xml.sax.SAXException If there is an error 1.739 + * writing the start tag, or if a handler further down 1.740 + * the filter chain raises an exception. 1.741 + * @see #startElement(String, String, String, Attributes) 1.742 + */ 1.743 + public void startElement (String localName) 1.744 + throws SAXException 1.745 + { 1.746 + startElement("", localName, "", EMPTY_ATTS); 1.747 + } 1.748 + 1.749 + 1.750 + /** 1.751 + * End an element without a qname. 1.752 + * 1.753 + * <p>This method will supply an empty string for the qName. 1.754 + * It invokes {@link #endElement(String, String, String)} 1.755 + * directly.</p> 1.756 + * 1.757 + * @param uri The element's Namespace URI. 1.758 + * @param localName The element's local name. 1.759 + * @exception org.xml.sax.SAXException If there is an error 1.760 + * writing the end tag, or if a handler further down 1.761 + * the filter chain raises an exception. 1.762 + * @see #endElement(String, String, String) 1.763 + */ 1.764 + public void endElement (String uri, String localName) 1.765 + throws SAXException 1.766 + { 1.767 + endElement(uri, localName, ""); 1.768 + } 1.769 + 1.770 + 1.771 + /** 1.772 + * End an element without a Namespace URI or qname. 1.773 + * 1.774 + * <p>This method will supply an empty string for the qName 1.775 + * and an empty string for the Namespace URI. 1.776 + * It invokes {@link #endElement(String, String, String)} 1.777 + * directly.</p> 1.778 + * 1.779 + * @param localName The element's local name. 1.780 + * @exception org.xml.sax.SAXException If there is an error 1.781 + * writing the end tag, or if a handler further down 1.782 + * the filter chain raises an exception. 1.783 + * @see #endElement(String, String, String) 1.784 + */ 1.785 + public void endElement (String localName) 1.786 + throws SAXException 1.787 + { 1.788 + endElement("", localName, ""); 1.789 + } 1.790 + 1.791 + 1.792 + /** 1.793 + * Write an element with character data content. 1.794 + * 1.795 + * <p>This is a convenience method to write a complete element 1.796 + * with character data content, including the start tag 1.797 + * and end tag.</p> 1.798 + * 1.799 + * <p>This method invokes 1.800 + * {@link #startElement(String, String, String, Attributes)}, 1.801 + * followed by 1.802 + * {@link #characters(String)}, followed by 1.803 + * {@link #endElement(String, String, String)}.</p> 1.804 + * 1.805 + * @param uri The element's Namespace URI. 1.806 + * @param localName The element's local name. 1.807 + * @param qName The element's default qualified name. 1.808 + * @param atts The element's attributes. 1.809 + * @param content The character data content. 1.810 + * @exception org.xml.sax.SAXException If there is an error 1.811 + * writing the empty tag, or if a handler further down 1.812 + * the filter chain raises an exception. 1.813 + * @see #startElement(String, String, String, Attributes) 1.814 + * @see #characters(String) 1.815 + * @see #endElement(String, String, String) 1.816 + */ 1.817 + public void dataElement (String uri, String localName, 1.818 + String qName, Attributes atts, 1.819 + String content) 1.820 + throws SAXException 1.821 + { 1.822 + startElement(uri, localName, qName, atts); 1.823 + characters(content); 1.824 + endElement(uri, localName, qName); 1.825 + } 1.826 + 1.827 + 1.828 + /** 1.829 + * Write an element with character data content but no attributes. 1.830 + * 1.831 + * <p>This is a convenience method to write a complete element 1.832 + * with character data content, including the start tag 1.833 + * and end tag. This method provides an empty string 1.834 + * for the qname and an empty attribute list.</p> 1.835 + * 1.836 + * <p>This method invokes 1.837 + * {@link #startElement(String, String, String, Attributes)}, 1.838 + * followed by 1.839 + * {@link #characters(String)}, followed by 1.840 + * {@link #endElement(String, String, String)}.</p> 1.841 + * 1.842 + * @param uri The element's Namespace URI. 1.843 + * @param localName The element's local name. 1.844 + * @param content The character data content. 1.845 + * @exception org.xml.sax.SAXException If there is an error 1.846 + * writing the empty tag, or if a handler further down 1.847 + * the filter chain raises an exception. 1.848 + * @see #startElement(String, String, String, Attributes) 1.849 + * @see #characters(String) 1.850 + * @see #endElement(String, String, String) 1.851 + */ 1.852 + public void dataElement (String uri, String localName, String content) 1.853 + throws SAXException 1.854 + { 1.855 + dataElement(uri, localName, "", EMPTY_ATTS, content); 1.856 + } 1.857 + 1.858 + 1.859 + /** 1.860 + * Write an element with character data content but no attributes or Namespace URI. 1.861 + * 1.862 + * <p>This is a convenience method to write a complete element 1.863 + * with character data content, including the start tag 1.864 + * and end tag. The method provides an empty string for the 1.865 + * Namespace URI, and empty string for the qualified name, 1.866 + * and an empty attribute list.</p> 1.867 + * 1.868 + * <p>This method invokes 1.869 + * {@link #startElement(String, String, String, Attributes)}, 1.870 + * followed by 1.871 + * {@link #characters(String)}, followed by 1.872 + * {@link #endElement(String, String, String)}.</p> 1.873 + * 1.874 + * @param localName The element's local name. 1.875 + * @param content The character data content. 1.876 + * @exception org.xml.sax.SAXException If there is an error 1.877 + * writing the empty tag, or if a handler further down 1.878 + * the filter chain raises an exception. 1.879 + * @see #startElement(String, String, String, Attributes) 1.880 + * @see #characters(String) 1.881 + * @see #endElement(String, String, String) 1.882 + */ 1.883 + public void dataElement (String localName, String content) 1.884 + throws SAXException 1.885 + { 1.886 + dataElement("", localName, "", EMPTY_ATTS, content); 1.887 + } 1.888 + 1.889 + 1.890 + /** 1.891 + * Write a string of character data, with XML escaping. 1.892 + * 1.893 + * <p>This is a convenience method that takes an XML 1.894 + * String, converts it to a character array, then invokes 1.895 + * {@link #characters(char[], int, int)}.</p> 1.896 + * 1.897 + * @param data The character data. 1.898 + * @exception org.xml.sax.SAXException If there is an error 1.899 + * writing the string, or if a handler further down 1.900 + * the filter chain raises an exception. 1.901 + * @see #characters(char[], int, int) 1.902 + */ 1.903 + public void characters (String data) throws SAXException { 1.904 + try { 1.905 + if (!startTagIsClosed) { 1.906 + write('>'); 1.907 + startTagIsClosed = true; 1.908 + } 1.909 + char ch[] = data.toCharArray(); 1.910 + characters(ch, 0, ch.length); 1.911 + } catch( IOException e ) { 1.912 + throw new SAXException(e); 1.913 + } 1.914 + } 1.915 + 1.916 + 1.917 + public void startDTD(String name, String publicId, String systemId) throws SAXException { 1.918 + } 1.919 + 1.920 + public void endDTD() throws SAXException { 1.921 + } 1.922 + 1.923 + public void startEntity(String name) throws SAXException { 1.924 + } 1.925 + 1.926 + public void endEntity(String name) throws SAXException { 1.927 + } 1.928 + 1.929 + public void startCDATA() throws SAXException { 1.930 + try { 1.931 + if (!startTagIsClosed) { 1.932 + write('>'); 1.933 + startTagIsClosed = true; 1.934 + } 1.935 + write("<![CDATA["); 1.936 + inCDATA = true; 1.937 + } catch (IOException e) { 1.938 + new SAXException(e); 1.939 + } 1.940 + } 1.941 + 1.942 + public void endCDATA() throws SAXException { 1.943 + try { 1.944 + inCDATA = false; 1.945 + write("]]>"); 1.946 + } catch (IOException e) { 1.947 + throw new SAXException(e); 1.948 + } 1.949 + } 1.950 + 1.951 + public void comment(char ch[], int start, int length) throws SAXException { 1.952 + try { 1.953 + output.write("<!--"); 1.954 + output.write(ch,start,length); 1.955 + output.write("-->"); 1.956 + } catch (IOException e) { 1.957 + throw new SAXException(e); 1.958 + } 1.959 + } 1.960 + 1.961 + 1.962 + 1.963 + //////////////////////////////////////////////////////////////////// 1.964 + // Internal methods. 1.965 + //////////////////////////////////////////////////////////////////// 1.966 + 1.967 + 1.968 + 1.969 + 1.970 + /** 1.971 + * Write a raw character. 1.972 + * 1.973 + * @param c The character to write. 1.974 + */ 1.975 + private void write (char c) throws IOException { 1.976 + output.write(c); 1.977 + } 1.978 + 1.979 + 1.980 + /** 1.981 + * Write a raw string. 1.982 + */ 1.983 + private void write (String s) throws IOException { 1.984 + output.write(s); 1.985 + } 1.986 + 1.987 + 1.988 + /** 1.989 + * Write out an attribute list, escaping values. 1.990 + * 1.991 + * The names will have prefixes added to them. 1.992 + * 1.993 + * @param atts The attribute list to write. 1.994 + * @exception SAXException If there is an error writing 1.995 + * the attribute list, this method will throw an 1.996 + * IOException wrapped in a SAXException. 1.997 + */ 1.998 + private void writeAttributes (Attributes atts) throws IOException, SAXException { 1.999 + int len = atts.getLength(); 1.1000 + for (int i = 0; i < len; i++) { 1.1001 + char ch[] = atts.getValue(i).toCharArray(); 1.1002 + write(' '); 1.1003 + writeName(atts.getURI(i), atts.getLocalName(i), 1.1004 + atts.getQName(i), false); 1.1005 + write("=\""); 1.1006 + writeEsc(ch, 0, ch.length, true); 1.1007 + write('"'); 1.1008 + } 1.1009 + } 1.1010 + 1.1011 + 1.1012 + /** 1.1013 + * Write an array of data characters with escaping. 1.1014 + * 1.1015 + * @param ch The array of characters. 1.1016 + * @param start The starting position. 1.1017 + * @param length The number of characters to use. 1.1018 + * @param isAttVal true if this is an attribute value literal. 1.1019 + * @exception SAXException If there is an error writing 1.1020 + * the characters, this method will throw an 1.1021 + * IOException wrapped in a SAXException. 1.1022 + */ 1.1023 + private void writeEsc (char ch[], int start, 1.1024 + int length, boolean isAttVal) 1.1025 + throws SAXException, IOException 1.1026 + { 1.1027 + escapeHandler.escape(ch, start, length, isAttVal, output); 1.1028 + } 1.1029 + 1.1030 + 1.1031 + /** 1.1032 + * Write an element or attribute name. 1.1033 + * 1.1034 + * @param uri The Namespace URI. 1.1035 + * @param localName The local name. 1.1036 + * @param qName The prefixed name, if available, or the empty string. 1.1037 + * @param isElement true if this is an element name, false if it 1.1038 + * is an attribute name. 1.1039 + */ 1.1040 + private void writeName (String uri, String localName, 1.1041 + String qName, boolean isElement) 1.1042 + throws IOException 1.1043 + { 1.1044 + write(qName); 1.1045 + } 1.1046 + 1.1047 + 1.1048 + 1.1049 + //////////////////////////////////////////////////////////////////// 1.1050 + // Constants. 1.1051 + //////////////////////////////////////////////////////////////////// 1.1052 + 1.1053 + private final Attributes EMPTY_ATTS = new AttributesImpl(); 1.1054 + 1.1055 + 1.1056 + 1.1057 + //////////////////////////////////////////////////////////////////// 1.1058 + // Internal state. 1.1059 + //////////////////////////////////////////////////////////////////// 1.1060 + 1.1061 + private boolean inCDATA = false; 1.1062 + private int elementLevel = 0; 1.1063 + private Writer output; 1.1064 + private String encoding; 1.1065 + private boolean writeXmlDecl = true; 1.1066 + /** 1.1067 + * This string will be written right after the xml declaration 1.1068 + * without any escaping. Useful for generating a boiler-plate DOCTYPE decl 1.1069 + * , PIs, and comments. 1.1070 + */ 1.1071 + private String header=null; 1.1072 + 1.1073 + private final CharacterEscapeHandler escapeHandler; 1.1074 + 1.1075 + private boolean startTagIsClosed = true; 1.1076 +} 1.1077 + 1.1078 +// end of XMLWriter.java