Top Description Fields Constructors Methods
com.thoughtworks.xstream.io.xml

public final Class SaxWriter

extends AbstractXmlWriter
implements XMLReader
Class Inheritance
All Implemented Interfaces
org.xml.sax.XMLReader
Imports
com.thoughtworks.xstream.XStream, com.thoughtworks.xstream.io.StreamException, com.thoughtworks.xstream.io.naming.NameCoder, org.xml.sax.ContentHandler, .DTDHandler, .EntityResolver, .ErrorHandler, .InputSource, .SAXException, .SAXNotRecognizedException, .SAXNotSupportedException, .XMLReader, org.xml.sax.helpers.AttributesImpl, java.util.ArrayList, .Collections, .HashMap, .Iterator, .LinkedList, .List, .Map

Deprecated

as a consequence of AbstractXmlWriter being deprecated. See corresponding docs for further information.

A SAX parser that acts as an XStream com.thoughtworks.xstream.io.HierarchicalStreamWriter to enable direct generation of a SAX event flow from the XStream serialization of a list of list of Java objects.

As a custom SAX parser, this class ignores the arguments of the two standard parse methods (parse(java.lang.String) and parse(org.xml.sax.InputSource)) but relies on a proprietary SAX property SOURCE_OBJECT_LIST_PROPERTY to define the list of objects to serialize.

Configuration of this SAX parser is achieved through the standard SAX property mechanism. While specific setter methods require direct access to the parser instance, SAX properties support configuration settings to be propagated through a chain of filters down to the underlying parser object.

This mechanism shall be used to configure the objects to be serialized as well as the XStream facade.

Author
Laurent Bihanic

Field Summary

Modifier and TypeField and Description
private final AttributesImpl
private char[]
public static final String
CONFIGURED_XSTREAM_PROPERTY

The SAX property to configure the XStream facade to be used for object serialization.

private ContentHandler
contentHandler

The SAX ContentHandler associated to this XMLReader.

private int
private DTDHandler
dtdHandler

The SAX DTDHandler associated to this XMLReader.

private List<E>
private EntityResolver
entityResolver

The SAX EntityResolver associated to this XMLReader.

private ErrorHandler
errorHandler

The SAX ErrorHandler associated to this XMLReader.

private Map<K, V>
features

The SAX features defined for this XMLReader.

private final boolean
private final Map<K, V>
properties

The SAX properties defined for this XMLReader.

public static final String
SOURCE_OBJECT_LIST_PROPERTY

The SAX property to configure the list of Java objects to serialize.

private boolean

Constructor Summary

AccessConstructor and Description
public
SaxWriter(NameCoder nameCoder)

public
SaxWriter(boolean includeEnclosingDocument, NameCoder nameCoder)

public
SaxWriter(XmlFriendlyReplacer replacer)

Deprecated As of 1.4 use SaxWriter#SaxWriter(NameCoder) instead.
public
SaxWriter(boolean includeEnclosingDocument, XmlFriendlyReplacer replacer)

Deprecated As of 1.4 use SaxWriter#SaxWriter(boolean, NameCoder) instead.
public
SaxWriter(boolean includeEnclosingDocument)

public

Method Summary

Modifier and TypeMethod and Description
public void
public void
close()

Implements com.thoughtworks.xstream.io.HierarchicalStreamWriter.close.

Close the writer, if necessary.
private void
endDocument(boolean
whether serialization of several object will be merge into a single SAX document.
multiObjectMode
)

Fires the SAX endDocument event towards the configured ContentHandler.

public void
public void
flush()

Implements com.thoughtworks.xstream.io.HierarchicalStreamWriter.flush.

Flush the writer, if necessary.
private void
flushStartTag()

Fires any pending SAX startElement event towards the configured ContentHandler.

public ContentHandler

Returns:

the current content handler, or null if none has been registered.
getContentHandler
()

Implements org.xml.sax.XMLReader.getContentHandler.

Returns the current content handler.
public DTDHandler

Returns:

the current DTD handler, or null if none has been registered.
getDTDHandler
()

Implements org.xml.sax.XMLReader.getDTDHandler.

Returns the current DTD handler.
public EntityResolver

Returns:

the current entity resolver, or null if none has been registered.
getEntityResolver
()

Implements org.xml.sax.XMLReader.getEntityResolver.

Returns the current entity resolver.
public ErrorHandler

Returns:

the current error handler, or null if none has been registered.
getErrorHandler
()

Implements org.xml.sax.XMLReader.getErrorHandler.

Returns the current error handler.
public boolean

Returns:

the current state of the feature (true or false).
getFeature
(String
the feature name, which is a fully-qualified URI.
name
)

Implements org.xml.sax.XMLReader.getFeature.

Looks up the value of a feature.
public Object

Returns:

the current value of the property.
getProperty
(String
the property name, which is a fully-qualified URI.
name
)

Implements org.xml.sax.XMLReader.getProperty.

Looks up the value of a property.
public void
parse(String
the system identifier (URI).
systemId
)

Implements org.xml.sax.XMLReader.parse.

Parses an XML document from a system identifier (URI).
public void
parse(InputSource
The input source for the top-level of the XML document.
input
)

Implements org.xml.sax.XMLReader.parse.

Parse an XML document.
private void
parse()

Serializes the Java objects of the configured list into a flow of SAX events.

public void
setContentHandler(ContentHandler
the content handler.
handler
)

Implements org.xml.sax.XMLReader.setContentHandler.

Allows an application to register a content event handler.
public void
setDTDHandler(DTDHandler
the DTD handler.
handler
)

Implements org.xml.sax.XMLReader.setDTDHandler.

Allows an application to register a DTD event handler.
public void
setEntityResolver(EntityResolver
the entity resolver.
resolver
)

Implements org.xml.sax.XMLReader.setEntityResolver.

Allows an application to register an entity resolver.
public void
setErrorHandler(ErrorHandler
the error handler.
handler
)

Implements org.xml.sax.XMLReader.setErrorHandler.

Allows an application to register an error event handler.
public void
setFeature(String
the feature name, which is a fully-qualified URI.
name
,
boolean
the requested state of the feature (true or false).
value
)

Implements org.xml.sax.XMLReader.setFeature.

Sets the state of a feature.
public void
setProperty(String
the property name, which is a fully-qualified URI.
name
,
Object
the requested value for the property.
value
)

Implements org.xml.sax.XMLReader.setProperty.

Sets the value of a property.
public void
setValue(String text)

Implements com.thoughtworks.xstream.io.HierarchicalStreamWriter.setValue.

Write the value (text content) of the current node.
private void
startDocument(boolean
whether serialization of several object will be merge into a single SAX document.
multiObjectMode
)

Fires the SAX startDocument event towards the configured ContentHandler.

public void
Inherited from com.thoughtworks.xstream.io.xml.AbstractXmlWriter:
escapeXmlName

Field Detail

attributeListback to summary
private final AttributesImpl attributeList
bufferback to summary
private char[] buffer
CONFIGURED_XSTREAM_PROPERTYback to summary
public static final String CONFIGURED_XSTREAM_PROPERTY

The SAX property to configure the XStream facade to be used for object serialization. If the property is not set, a new XStream facade will be allocated for each parse.

contentHandlerback to summary
private ContentHandler contentHandler

The SAX ContentHandler associated to this XMLReader.

depthback to summary
private int depth
dtdHandlerback to summary
private DTDHandler dtdHandler

The SAX DTDHandler associated to this XMLReader.

elementStackback to summary
private List<E> elementStack
entityResolverback to summary
private EntityResolver entityResolver

The SAX EntityResolver associated to this XMLReader.

errorHandlerback to summary
private ErrorHandler errorHandler

The SAX ErrorHandler associated to this XMLReader.

featuresback to summary
private Map<K, V> features

The SAX features defined for this XMLReader.

This class does not define any feature (yet) and ignores the SAX mandatory feature. Thus, this member is present only to support the mandatory feature setting and retrieval logic defined by SAX.

includeEnclosingDocumentback to summary
private final boolean includeEnclosingDocument
propertiesback to summary
private final Map<K, V> properties

The SAX properties defined for this XMLReader.

SOURCE_OBJECT_LIST_PROPERTYback to summary
public static final String SOURCE_OBJECT_LIST_PROPERTY

The SAX property to configure the list of Java objects to serialize. Setting this property prior invoking one of the parse() methods is mandatory.

See Also
parse(java.lang.String), parse(org.xml.sax.InputSource)
startTagInProgressback to summary
private boolean startTagInProgress

Constructor Detail

SaxWriterback to summary
public SaxWriter(NameCoder nameCoder)
Since
1.4
SaxWriterback to summary
public SaxWriter(boolean includeEnclosingDocument, NameCoder nameCoder)
Since
1.4
SaxWriterback to summary
public SaxWriter(XmlFriendlyReplacer replacer)

Deprecated

As of 1.4 use SaxWriter#SaxWriter(NameCoder) instead.

SaxWriterback to summary
public SaxWriter(boolean includeEnclosingDocument, XmlFriendlyReplacer replacer)

Deprecated

As of 1.4 use SaxWriter#SaxWriter(boolean, NameCoder) instead.

SaxWriterback to summary
public SaxWriter(boolean includeEnclosingDocument)
SaxWriterback to summary
public SaxWriter()

Method Detail

addAttributeback to summary
public void addAttribute(String name, String value)

Implements com.thoughtworks.xstream.io.HierarchicalStreamWriter.addAttribute.

closeback to summary
public void close()

Implements com.thoughtworks.xstream.io.HierarchicalStreamWriter.close.

Doc from com.thoughtworks.xstream.io.HierarchicalStreamWriter.close.

Close the writer, if necessary.

endDocumentback to summary
private void endDocument(boolean multiObjectMode) throws SAXException

Fires the SAX endDocument event towards the configured ContentHandler.

Parameters
multiObjectMode:boolean

whether serialization of several object will be merge into a single SAX document.

Exceptions
SAXException:
if thrown by the ContentHandler.
endNodeback to summary
public void endNode()

Implements com.thoughtworks.xstream.io.HierarchicalStreamWriter.endNode.

flushback to summary
public void flush()

Implements com.thoughtworks.xstream.io.HierarchicalStreamWriter.flush.

Doc from com.thoughtworks.xstream.io.HierarchicalStreamWriter.flush.

Flush the writer, if necessary.

flushStartTagback to summary
private void flushStartTag() throws SAXException

Fires any pending SAX startElement event towards the configured ContentHandler.

Exceptions
SAXException:
if thrown by the ContentHandler.
getContentHandlerback to summary
public ContentHandler getContentHandler()

Implements org.xml.sax.XMLReader.getContentHandler.

Returns the current content handler.

Returns:ContentHandler

the current content handler, or null if none has been registered.

See Also
setContentHandler
getDTDHandlerback to summary
public DTDHandler getDTDHandler()

Implements org.xml.sax.XMLReader.getDTDHandler.

Returns the current DTD handler.

Returns:DTDHandler

the current DTD handler, or null if none has been registered.

See Also
setDTDHandler
getEntityResolverback to summary
public EntityResolver getEntityResolver()

Implements org.xml.sax.XMLReader.getEntityResolver.

Returns the current entity resolver.

Returns:EntityResolver

the current entity resolver, or null if none has been registered.

See Also
setEntityResolver
getErrorHandlerback to summary
public ErrorHandler getErrorHandler()

Implements org.xml.sax.XMLReader.getErrorHandler.

Returns the current error handler.

Returns:ErrorHandler

the current error handler, or null if none has been registered.

See Also
setErrorHandler
getFeatureback to summary
public boolean getFeature(String name) throws SAXNotRecognizedException

Implements org.xml.sax.XMLReader.getFeature.

Looks up the value of a feature.

The feature name is any fully-qualified URI. It is possible for an XMLReader to recognize a feature name but to be unable to return its value; this is especially true in the case of an adapter for a SAX1 Parser, which has no way of knowing whether the underlying parser is performing validation or expanding external entities.

All XMLReaders are required to recognize the http://xml.org/sax/features/namespaces and the http://xml.org/sax/features/namespace-prefixes feature names.

Some feature values may be available only in specific contexts, such as before, during, or after a parse.

Implementors are free (and encouraged) to invent their own features, using names built on their own URIs.

Parameters
name:String

the feature name, which is a fully-qualified URI.

Returns:boolean

the current state of the feature (true or false).

Exceptions
SAXNotRecognizedException:
when the XMLReader does not recognize the feature name.
See Also
setFeature
getPropertyback to summary
public Object getProperty(String name) throws SAXNotRecognizedException

Implements org.xml.sax.XMLReader.getProperty.

Looks up the value of a property.

The property name is any fully-qualified URI. It is possible for an XMLReader to recognize a property name but to be unable to return its state.

XMLReaders are not required to recognize any specific property names, though an initial core set is documented for SAX2.

Some property values may be available only in specific contexts, such as before, during, or after a parse.

Implementors are free (and encouraged) to invent their own properties, using names built on their own URIs.

Parameters
name:String

the property name, which is a fully-qualified URI.

Returns:Object

the current value of the property.

Exceptions
SAXNotRecognizedException:
when the XMLReader does not recognize the property name.
See Also
getProperty
parseback to summary
public void parse(String systemId) throws SAXException

Implements org.xml.sax.XMLReader.parse.

Parses an XML document from a system identifier (URI).

This method is a shortcut for the common case of reading a document from a system identifier. It is the exact equivalent of the following:

   parse(new InputSource(systemId));
 

If the system identifier is a URL, it must be fully resolved by the application before it is passed to the parser.

Note

As a custom SAX parser, this class ignores the systemId argument of this method and relies on the proprietary SAX property SOURCE_OBJECT_LIST_PROPERTY) to define the list of objects to serialize.

Parameters
systemId:String

the system identifier (URI).

Exceptions
SAXException:
Any SAX exception, possibly wrapping another exception.
See Also
parse(org.xml.sax.InputSource)
parseback to summary
public void parse(InputSource input) throws SAXException

Implements org.xml.sax.XMLReader.parse.

Parse an XML document.

The application can use this method to instruct the XML reader to begin parsing an XML document from any valid input source (a character stream, a byte stream, or a URI).

Applications may not invoke this method while a parse is in progress (they should create a new XMLReader instead for each nested XML document). Once a parse is complete, an application may reuse the same XMLReader object, possibly with a different input source.

During the parse, the XMLReader will provide information about the XML document through the registered event handlers.

This method is synchronous: it will not return until parsing has ended. If a client application wants to terminate parsing early, it should throw an exception.

Note

As a custom SAX parser, this class ignores the source argument of this method and relies on the proprietary SAX property SOURCE_OBJECT_LIST_PROPERTY) to define the list of objects to serialize.

Parameters
input:InputSource

The input source for the top-level of the XML document.

Exceptions
SAXException:
Any SAX exception, possibly wrapping another exception.
See Also
org.xml.sax.InputSource, parse(java.lang.String), setEntityResolver, setDTDHandler, setContentHandler, setErrorHandler
parseback to summary
private void parse() throws SAXException

Serializes the Java objects of the configured list into a flow of SAX events.

Exceptions
SAXException:
if the configured object list is invalid or object serialization failed.
setContentHandlerback to summary
public void setContentHandler(ContentHandler handler)

Implements org.xml.sax.XMLReader.setContentHandler.

Allows an application to register a content event handler.

If the application does not register a content handler, all content events reported by the SAX parser will be silently ignored.

Applications may register a new or different handler in the middle of a parse, and the SAX parser must begin using the new handler immediately.

Parameters
handler:ContentHandler

the content handler.

Exceptions
NullPointerException:
if the handler argument is null.
See Also
getContentHandler
setDTDHandlerback to summary
public void setDTDHandler(DTDHandler handler)

Implements org.xml.sax.XMLReader.setDTDHandler.

Allows an application to register a DTD event handler.

If the application does not register a DTD handler, all DTD events reported by the SAX parser will be silently ignored.

Applications may register a new or different handler in the middle of a parse, and the SAX parser must begin using the new handler immediately.

Parameters
handler:DTDHandler

the DTD handler.

Exceptions
NullPointerException:
if the handler argument is null.
See Also
getDTDHandler
setEntityResolverback to summary
public void setEntityResolver(EntityResolver resolver)

Implements org.xml.sax.XMLReader.setEntityResolver.

Allows an application to register an entity resolver.

If the application does not register an entity resolver, the XMLReader will perform its own default resolution.

Applications may register a new or different resolver in the middle of a parse, and the SAX parser must begin using the new resolver immediately.

Parameters
resolver:EntityResolver

the entity resolver.

Exceptions
NullPointerException:
if the resolver argument is null.
See Also
getEntityResolver
setErrorHandlerback to summary
public void setErrorHandler(ErrorHandler handler)

Implements org.xml.sax.XMLReader.setErrorHandler.

Allows an application to register an error event handler.

If the application does not register an error handler, all error events reported by the SAX parser will be silently ignored; however, normal processing may not continue. It is highly recommended that all SAX applications implement an error handler to avoid unexpected bugs.

Applications may register a new or different handler in the middle of a parse, and the SAX parser must begin using the new handler immediately.

Parameters
handler:ErrorHandler

the error handler.

Exceptions
NullPointerException:
if the handler argument is null.
See Also
getErrorHandler
setFeatureback to summary
public void setFeature(String name, boolean value) throws SAXNotRecognizedException

Implements org.xml.sax.XMLReader.setFeature.

Sets the state of a feature.

The feature name is any fully-qualified URI.

All XMLReaders are required to support setting http://xml.org/sax/features/namespaces to true and http://xml.org/sax/features/namespace-prefixes to false.

Some feature values may be immutable or mutable only in specific contexts, such as before, during, or after a parse.

Note

This implementation only supports the two mandatory SAX features.

Parameters
name:String

the feature name, which is a fully-qualified URI.

value:boolean

the requested state of the feature (true or false).

Exceptions
SAXNotRecognizedException:
when the XMLReader does not recognize the feature name.
See Also
getFeature
setPropertyback to summary
public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException

Implements org.xml.sax.XMLReader.setProperty.

Sets the value of a property.

The property name is any fully-qualified URI. It is possible for an XMLReader to recognize a property name but to be unable to set its value.

XMLReaders are not required to recognize setting any specific property names, though a core set is provided with SAX2.

Some property values may be immutable or mutable only in specific contexts, such as before, during, or after a parse.

This method is also the standard mechanism for setting extended handlers.

Note

This implementation only supports two (proprietary) properties: CONFIGURED_XSTREAM_PROPERTY and SOURCE_OBJECT_LIST_PROPERTY.

Parameters
name:String

the property name, which is a fully-qualified URI.

value:Object

the requested value for the property.

Exceptions
SAXNotRecognizedException:
when the XMLReader does not recognize the property name.
SAXNotSupportedException:
when the XMLReader recognizes the property name but cannot set the requested value.
See Also
getProperty
setValueback to summary
public void setValue(String text)

Implements com.thoughtworks.xstream.io.HierarchicalStreamWriter.setValue.

Doc from com.thoughtworks.xstream.io.HierarchicalStreamWriter.setValue.

Write the value (text content) of the current node.

startDocumentback to summary
private void startDocument(boolean multiObjectMode) throws SAXException

Fires the SAX startDocument event towards the configured ContentHandler.

Parameters
multiObjectMode:boolean

whether serialization of several object will be merge into a single SAX document.

Exceptions
SAXException:
if thrown by the ContentHandler.
startNodeback to summary
public void startNode(String name)

Implements com.thoughtworks.xstream.io.HierarchicalStreamWriter.startNode.