org.xquark.serialize
Class XMLSerializer

java.lang.Object
  |
  +--org.xquark.util.HandlerDecorator
        |
        +--org.xquark.util.NamespaceContextHandler
              |
              +--org.xquark.util.NSPrefixDecorator
                    |
                    +--org.xquark.serialize.XMLSerializer
All Implemented Interfaces:
org.xml.sax.ContentHandler, org.xml.sax.ErrorHandler, org.xml.sax.ext.LexicalHandler, org.xquark.util.NamespaceContext, org.xquark.util.SAXConstants

public class XMLSerializer
extends org.xquark.util.NSPrefixDecorator

A simple SAX2 XML serializer implementing SAX ContentHandler and LexicalHandler. Warning: Default behavior is to perform automatic indentation and to ignore ignorable whitespace generated by the XML processor. IMPORTANT : SAX2 XMLReader http://xml.org/sax/features/namespaces feature is supposed to be set to true which is the default.


Field Summary
 
Fields inherited from class org.xquark.util.NSPrefixDecorator
NS_PREFIX, XML_PREFIX
 
Fields inherited from class org.xquark.util.NamespaceContextHandler
contextStack
 
Fields inherited from class org.xquark.util.HandlerDecorator
contentHandler, errorHandler, lexicalHandler
 
Fields inherited from interface org.xquark.util.SAXConstants
FRAGMENT_FEATURE, SAX_DECLARATION_PROPERTY, SAX_LEXICAL_PROPERTY, SAX_NAMESPACE_FEATURE, SAX_PREFIX_FEATURE, SAX_VALIDATION_FEATURE, TRACE_FEATURE, XMLNS_PREFIX, XMLNS_URI
 
Constructor Summary
XMLSerializer(java.io.OutputStream out)
          Creates a new XMLSerializer
XMLSerializer(java.io.OutputStream out, java.lang.String encoding)
          Creates a new XMLSerializer
XMLSerializer(java.lang.String encoding, boolean indent)
          Creates a new XMLSerializer
XMLSerializer(java.io.Writer out)
          Creates a new XMLSerializer
XMLSerializer(java.io.Writer out, java.lang.String encoding)
          Creates a new XMLSerializer
 
Method Summary
 void close()
          Closes the user stream.
 void completeStartTag()
          Closes the potentially open element start tag waiting to know by the following events if the tag is empty.
 void flush()
          Flushes the user stream.
 boolean getAutoStreamClose()
          Return the value set for stream automatic close feature.
 boolean getCanonicalOutput()
          Accessor to the current ordering mode for attributes.
 java.lang.String getEncoding()
          Accessor to the current character encoding in use.
 boolean getGenerateXMLDeclaration()
          Accessor to the current flag for XML declaration generation.
 boolean getIndent()
          Accessor to the current indenting mode.
 boolean getUseIgnorableWhitespaces()
          Accessor to the current the status of the use of ignorable whitespace by the serializer.
 void reset()
          Reset internal state before reuse.
 void setAutoStreamClose(boolean close)
          Enable automatic close of the output user stream on endDocument() event.
 void setCanonicalOutput(boolean mode)
          Set the ordering mode for attributes.
 void setEncoding(java.lang.String encoding)
          Set the character encoding.
 void setGenerateEncodingDeclaration(boolean enable)
          Enable encoding attribute generation in XML declaration on startDocument() event.
 void setGenerateXMLDeclaration(boolean enable)
          Enable XML declaration generation on startDocument() event.
 void setIndent(boolean indent)
          Set the indenting mode.
 void setOutputStream(java.io.OutputStream out)
          Set the OutputStream where the serializer writes bytes.
 void setUseIgnorableWhitespaces(boolean use)
          turn on or off the use of ignorable whitespace for indentation.
 void setWriter(java.io.Writer out)
          Set the Writer where the serializer writes characters.
 
Methods inherited from class org.xquark.util.NSPrefixDecorator
endElement, isEnabled, setEnabled, startElement
 
Methods inherited from class org.xquark.util.NamespaceContextHandler
endPrefixMapping, getDeclaredPrefixes, getNamespaceContext, getNamespaceURI, getNamespaceURIs, getPrefix, getPrefixes, getPrefixes, startPrefixMapping
 
Methods inherited from class org.xquark.util.HandlerDecorator
characters, comment, endCDATA, endDocument, endDTD, endEntity, error, fatalError, getContentHandler, getLexicalHandler, getPluggedErrorHandler, ignorableWhitespace, processingInstruction, setContentHandler, setDocumentLocator, setErrorHandler, setLexicalHandler, skippedEntity, startCDATA, startDocument, startDTD, startEntity, warning
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XMLSerializer

public XMLSerializer(java.io.OutputStream out)
              throws java.io.UnsupportedEncodingException
Creates a new XMLSerializer

Parameters:
out - the OutputStream where the serializer writes bytes. For instance a FileOutputStream. The Unicode character encoding used will be the platform default.
Throws:
java.io.UnsupportedEncodingException - if the UTF-8 encoding is not supported by the JAVA platform

XMLSerializer

public XMLSerializer(java.io.OutputStream out,
                     java.lang.String encoding)
              throws java.io.UnsupportedEncodingException
Creates a new XMLSerializer

Parameters:
out - the OutputStream where the serializer writes bytes. For instance a FileOutputStream.
encoding - a string specifying the character encoding, which must be supported by the JDK. Example: UTF-8, ISO-8859-1
Throws:
java.io.UnsupportedEncodingException - if the provided encoding is not supported by the JAVA platform
See Also:
OutputStream

XMLSerializer

public XMLSerializer(java.io.Writer out)
Creates a new XMLSerializer

Parameters:
out - the writer where the serializer writes characters. For instance a FileWriter.

XMLSerializer

public XMLSerializer(java.io.Writer out,
                     java.lang.String encoding)
Creates a new XMLSerializer

Parameters:
out - the writer where the serializer writes characters. For instance a FileWriter.
encoding - a string specifying the character encoding (must be compatible with the encoding used by the writer). This value is used to generate the encoding information at the beginning of the XML file
See Also:
OutputStream

XMLSerializer

public XMLSerializer(java.lang.String encoding,
                     boolean indent)
Creates a new XMLSerializer

Parameters:
encoding - a string for the character encoding (must be supported by the JDK used). Example: UTF-8, ISO-8859-1
indent - if true, tabs are used at the beginning of lines to indent the output file.
Method Detail

setOutputStream

public void setOutputStream(java.io.OutputStream out)
                     throws java.io.UnsupportedEncodingException
Set the OutputStream where the serializer writes bytes. When this method is called, the output Writer is created with the currently specified encoding. Further calls to setEncoding will not change the encoding used by the Writer.

Parameters:
out - the OutputStream where the serializer writes bytes. For instance a FileOutputStream.
Throws:
java.io.UnsupportedEncodingException - if the provided encoding is not supported by the JAVA platform

setWriter

public void setWriter(java.io.Writer out)
Set the Writer where the serializer writes characters.

Parameters:
out - the writer where the serializer writes characters.

setIndent

public void setIndent(boolean indent)
Set the indenting mode. Warning: Default behavior is to perform automatic indentation ( ignorable whitespaces generated by the XML processor are ignored).

Parameters:
indent - if true, tabs are used at the beginning of lines to indent the output file.

setCanonicalOutput

public void setCanonicalOutput(boolean mode)
Set the ordering mode for attributes. The default behavior is to perform no ordering.

Parameters:
mode - If true, ordering is performed following the Canonical XML W3C recommendation. If false, attributes are serialized as passed by the SAX2 XMLReader.

setUseIgnorableWhitespaces

public void setUseIgnorableWhitespaces(boolean use)
turn on or off the use of ignorable whitespace for indentation. Warning: This flag is ignored if automatic indentation is on.

Parameters:
use - if true (the default), ignorable whitespace are used for indentation unless automatic indentation is on.

setEncoding

public void setEncoding(java.lang.String encoding)
Set the character encoding.

Parameters:
encoding - a string specifying the character encoding, which must be supported by the JDK. Example: UTF-8, ISO-8859-1.

setGenerateXMLDeclaration

public void setGenerateXMLDeclaration(boolean enable)
Enable XML declaration generation on startDocument() event.

Parameters:
enable - XML Declaration is generated if true. Default is true.

setGenerateEncodingDeclaration

public void setGenerateEncodingDeclaration(boolean enable)
Enable encoding attribute generation in XML declaration on startDocument() event. Considered only if XML decleration enabled.

Parameters:
enable - encoding declaration generation if true. Default is true.

setAutoStreamClose

public void setAutoStreamClose(boolean close)
Enable automatic close of the output user stream on endDocument() event.

Parameters:
close - Output stream will be closed if true. Default is false.

getIndent

public boolean getIndent()
Accessor to the current indenting mode.

Returns:
the current indenting mode.

getCanonicalOutput

public boolean getCanonicalOutput()
Accessor to the current ordering mode for attributes.

Returns:
the current ordering mode for attributes.

getUseIgnorableWhitespaces

public boolean getUseIgnorableWhitespaces()
Accessor to the current the status of the use of ignorable whitespace by the serializer.

Returns:
the status of the use of ignorable whitespace by the serializer.

getEncoding

public java.lang.String getEncoding()
Accessor to the current character encoding in use.

Returns:
the character encoding in use, if previously specified.

getGenerateXMLDeclaration

public boolean getGenerateXMLDeclaration()
Accessor to the current flag for XML declaration generation.

Returns:
if true, the XML declaration will be generated.

getAutoStreamClose

public boolean getAutoStreamClose()
Return the value set for stream automatic close feature.

See Also:
setAutoStreamClose(boolean).

completeStartTag

public void completeStartTag()
Closes the potentially open element start tag waiting to know by the following events if the tag is empty. By this additional event the user indicates to the serializer that the current element must not be processed with an open-close tag.


flush

public void flush()
Flushes the user stream.


reset

public void reset()
Reset internal state before reuse.


close

public void close()
Closes the user stream.



Copyright © 2004 Université de Versailles Saint-Quentin, XQuark Group. All rights reserved.