* If an output stream is used, the encoding is taken from the * output format (defaults to UTF-8). If a writer is * used, make sure the writer uses the same encoding (if applies) * as specified in the output format. *
* The serializer supports both DOM and SAX. SAX serializing is done by firing * SAX events and using the serializer as a document handler. DOM serializing is done * by calling {@link #serialize(Document)} or by using DOM Level 3 * {@link org.w3c.dom.ls.DOMSerializer} and * serializing with {@link org.w3c.dom.ls.DOMSerializer#write}, * {@link org.w3c.dom.ls.DOMSerializer#writeToString}. *
* If an I/O exception occurs while serializing, the serializer * will not throw an exception directly, but only throw it * at the end of serializing (either DOM or SAX's {@link * org.xml.sax.DocumentHandler#endDocument}. *
* For elements that are not specified as whitespace preserving,
* the serializer will potentially break long text lines at space
* boundaries, indent lines, and serialize elements on separate
* lines. Line terminators will be regarded as spaces, and
* spaces at beginning of line will be stripped.
* @author Assaf Arkin
* @author Rahul Srivastava
* @author Elena Litani IBM
* @version $Revision: 1.8 $ $Date: 2004/01/29 21:11:30 $
* @see Serializer
*/
public class XML11Serializer
extends XMLSerializer {
//
// constants
//
protected static final boolean DEBUG = false;
//
// data
//
//
// DOM Level 3 implementation: variables intialized in DOMSerializerImpl
//
/** stores namespaces in scope */
protected NamespaceSupport fNSBinder;
/** stores all namespace bindings on the current element */
protected NamespaceSupport fLocalNSBinder;
/** symbol table for serialization */
protected SymbolTable fSymbolTable;
// is node dom level 1 node?
protected boolean fDOML1 = false;
// counter for new prefix names
protected int fNamespaceCounter = 1;
protected final static String PREFIX = "NS";
/**
* Controls whether namespace fixup should be performed during
* the serialization.
* NOTE: if this field is set to true the following
* fields need to be initialized: fNSBinder, fLocalNSBinder, fSymbolTable,
* XMLSymbols.EMPTY_STRING, fXmlSymbol, fXmlnsSymbol, fNamespaceCounter.
*/
protected boolean fNamespaces = false;
private boolean fPreserveSpace;
/**
* Constructs a new serializer. The serializer cannot be used without
* calling {@link #setOutputCharStream} or {@link #setOutputByteStream}
* first.
*/
public XML11Serializer() {
super( );
_format.setVersion("1.1");
}
/**
* Constructs a new serializer. The serializer cannot be used without
* calling {@link #setOutputCharStream} or {@link #setOutputByteStream}
* first.
*/
public XML11Serializer( OutputFormat format ) {
super( format );
_format.setVersion("1.1");
}
/**
* Constructs a new serializer that writes to the specified writer
* using the specified output format. If format is null,
* will use a default output format.
*
* @param writer The writer to use
* @param format The output format to use, null for the default
*/
public XML11Serializer( Writer writer, OutputFormat format ) {
super( writer, format );
_format.setVersion("1.1");
}
/**
* Constructs a new serializer that writes to the specified output
* stream using the specified output format. If format
* is null, will use a default output format.
*
* @param output The output stream to use
* @param format The output format to use, null for the default
*/
public XML11Serializer( OutputStream output, OutputFormat format ) {
super( output, format != null ? format : new OutputFormat( Method.XML, null, false ) );
_format.setVersion("1.1");
}
//-----------------------------------------//
// SAX content handler serializing methods //
//-----------------------------------------//
public void characters( char[] chars, int start, int length )
throws SAXException
{
ElementState state;
try {
state = content();
// Check if text should be print as CDATA section or unescaped
// based on elements listed in the output format (the element
// state) or whether we are inside a CDATA section or entity.
if ( state.inCData || state.doCData ) {
int saveIndent;
// Print a CDATA section. The text is not escaped, but ']]>'
// appearing in the code must be identified and dealt with.
// The contents of a text node is considered space preserving.
if ( ! state.inCData ) {
_printer.printText( "' ) {
_printer.printText("]]]]>");
index +=2;
continue;
}
if (!XML11Char.isXML11Valid(ch)) {
// check if it is surrogate
if (++index