org.apache.commons.betwixt.io
Class AbstractBeanWriter

java.lang.Object
  extended by org.apache.commons.betwixt.io.AbstractBeanWriter
Direct Known Subclasses:
BeanWriter, SAXBeanWriter

public abstract class AbstractBeanWriter
extends java.lang.Object

Abstract superclass for bean writers. This class encapsulates the processing logic. Subclasses provide implementations for the actual expression of the xml.

SAX Inspired Writing API

This class is intended to be used by subclassing: concrete subclasses perform the actual writing by providing suitable implementations for the following methods inspired by SAX:

Note that this class contains many deprecated versions of the writing API. These will be removed soon so care should be taken to use the latest version.

Note that this class is designed to be used in a single threaded environment. When used in multi-threaded environments, use of a common XMLIntrospector and pooled writer instances should be considered.

Author:
Robert Burrell Donkin

Constructor Summary
AbstractBeanWriter()
           
 
Method Summary
protected  void bodyText(java.lang.String text)
          Deprecated. 0.5 use bodyText(WriteContext, String)
protected  void bodyText(WriteContext context, java.lang.String text)
          Writes body text
 void end()
          Marks the start of the bean writing.
protected  void endElement(java.lang.String uri, java.lang.String localName, java.lang.String qName)
          Deprecated. 0.5 use endElement(WriteContext, String, String, String)
protected  void endElement(WriteContext context, java.lang.String uri, java.lang.String localName, java.lang.String qName)
          Writes the end tag for an element
protected  void expressAttribute(java.lang.String qualifiedName, java.lang.String value)
          Deprecated. 0.5 replaced by new SAX inspired API
protected  void expressAttribute(java.lang.String namespaceUri, java.lang.String localName, java.lang.String qualifiedName, java.lang.String value)
          Deprecated. 0.5 replaced by new SAX inspired API
protected  void expressBodyText(java.lang.String text)
          Deprecated. 0.5 replaced by new SAX inspired API
protected  void expressElementEnd()
          Deprecated. 0.5 replaced by new SAX inspired API
protected  void expressElementEnd(java.lang.String qualifiedName)
          Deprecated. 0.5 replaced by new SAX inspired API
protected  void expressElementEnd(java.lang.String uri, java.lang.String localName, java.lang.String qualifiedName)
          Deprecated. 0.5 replaced by new SAX inspired API
protected  void expressElementStart(java.lang.String qualifiedName)
          Deprecated. 0.5 replaced by new SAX inspired API
protected  void expressElementStart(java.lang.String uri, java.lang.String localName, java.lang.String qualifiedName)
          Deprecated. 0.5 replaced by new SAX inspired API
protected  void expressTagClose()
          Deprecated. 0.5 replaced by new SAX inspired API
 org.apache.commons.logging.Log getAbstractBeanWriterLog()
          Gets the current logging implementation.
 BindingConfiguration getBindingConfiguration()
          Gets the dynamic configuration setting to be used for bean reading.
 IDGenerator getIdGenerator()
          Get IDGenerator implementation used to generate ID attribute values .
protected  int getIndentLevel()
          Deprecated. 0.5 replaced by new SAX inspired API
 boolean getWriteEmptyElements()
          Gets whether empty elements should be written into the output.
 boolean getWriteIDs()
          Deprecated. 0.5 use BindingConfiguration.getMapIDs()
 XMLIntrospector getXMLIntrospector()
          Gets the introspector used.
protected  java.lang.Object popBean()
          Pops the top bean off from the ancestry stack
protected  void pushBean(java.lang.Object bean)
          Pushes the bean onto the ancestry stack.
 void setAbstractBeanWriterLog(org.apache.commons.logging.Log log)
           Set the current logging implementation.
 void setBindingConfiguration(BindingConfiguration bindingConfiguration)
          Sets the dynamic configuration setting to be used for bean reading.
 void setIdGenerator(IDGenerator idGenerator)
          Set IDGenerator implementation used to generate ID attribute values.
 void setWriteEmptyElements(boolean writeEmptyElements)
          Sets whether empty elements should be written into the output.
 void setWriteIDs(boolean writeIDs)
          Deprecated. 0.5 use BindingConfiguration.setMapIDs(boolean)
 void setXMLIntrospector(XMLIntrospector introspector)
          Sets the introspector to be used.
 void start()
          Marks the start of the bean writing.
protected  void startElement(java.lang.String uri, java.lang.String localName, java.lang.String qName, org.xml.sax.Attributes attr)
          Deprecated. 0.5 use startElement(WriteContext, String, String, String, Attributes)
protected  void startElement(WriteContext context, java.lang.String uri, java.lang.String localName, java.lang.String qName, org.xml.sax.Attributes attr)
          Writes the start tag for an element.
 void write(java.lang.Object bean)
           Writes the given bean to the current stream using the XML introspector.
 void write(java.lang.Object bean, org.xml.sax.InputSource source)
          Writes the bean using the mapping specified in the InputSource.
protected  void write(java.lang.String qualifiedName, ElementDescriptor elementDescriptor, Context context)
          Deprecated. 0.5 replaced by new SAX inspired API
protected  void write(java.lang.String qualifiedName, ElementDescriptor elementDescriptor, Context context, java.lang.String idAttribute, java.lang.String idValue)
          Deprecated. 0.5 replaced by new SAX inspired API
 void write(java.lang.String qualifiedName, java.lang.Object bean)
          Writes the given bean to the current stream using the given qualifiedName.
protected  void writeAttribute(AttributeDescriptor attributeDescriptor, Context context)
          Deprecated. 0.5 replaced by new SAX inspired API
protected  void writeAttributes(ElementDescriptor elementDescriptor, Context context)
          Deprecated. 0.5 replaced by new SAX inspired API
protected  boolean writeContent(ElementDescriptor elementDescriptor, Context context)
          Deprecated. 0.5 replaced by new SAX inspired API
protected  void writeIDREFElement(java.lang.String qualifiedName, java.lang.String idrefAttributeName, java.lang.String idrefAttributeValue)
          Deprecated. 0.5 replaced by new SAX inspired API
protected  void writeIndent()
          Deprecated. 0.5 replaced by new BeanWriter API
protected  void writePrintln()
          Deprecated. 0.5 replaced by new SAX inspired API
protected  void writeRestOfElement(java.lang.String qualifiedName, ElementDescriptor elementDescriptor, Context context)
          Deprecated. 0.5 replaced by new SAX inspired API
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractBeanWriter

public AbstractBeanWriter()
Method Detail

start

public void start()
           throws java.io.IOException,
                  org.xml.sax.SAXException
Marks the start of the bean writing. By default doesn't do anything, but can be used to do extra start processing

Throws:
java.io.IOException - if an IO problem occurs during writing
org.xml.sax.SAXException - if an SAX problem occurs during writing

end

public void end()
         throws java.io.IOException,
                org.xml.sax.SAXException
Marks the start of the bean writing. By default doesn't do anything, but can be used to do extra end processing

Throws:
java.io.IOException - if an IO problem occurs during writing
org.xml.sax.SAXException - if an SAX problem occurs during writing

write

public void write(java.lang.Object bean)
           throws java.io.IOException,
                  org.xml.sax.SAXException,
                  java.beans.IntrospectionException

Writes the given bean to the current stream using the XML introspector.

This writes an xml fragment representing the bean to the current stream.

This method will throw a CyclicReferenceException when a cycle is encountered in the graph only if the getMapIDs() setting of the BindingConfiguration is false.

Parameters:
bean - write out representation of this bean
Throws:
java.io.IOException - if an IO problem occurs during writing
org.xml.sax.SAXException - if an SAX problem occurs during writing
java.beans.IntrospectionException - if a java beans introspection problem occurs

write

public void write(java.lang.String qualifiedName,
                  java.lang.Object bean)
           throws java.io.IOException,
                  org.xml.sax.SAXException,
                  java.beans.IntrospectionException

Writes the given bean to the current stream using the given qualifiedName.

This method will throw a CyclicReferenceException when a cycle is encountered in the graph only if the getMapIDs() setting of the BindingConfiguration is false.

Parameters:
qualifiedName - the string naming root element
bean - the Object to write out as xml
Throws:
java.io.IOException - if an IO problem occurs during writing
org.xml.sax.SAXException - if an SAX problem occurs during writing
java.beans.IntrospectionException - if a java beans introspection problem occurs

write

public void write(java.lang.Object bean,
                  org.xml.sax.InputSource source)
           throws java.io.IOException,
                  org.xml.sax.SAXException,
                  java.beans.IntrospectionException

Writes the bean using the mapping specified in the InputSource.

Note: that the custom mapping will not be registered for later use. Please use XMLIntrospector.register(org.xml.sax.InputSource) to register the custom mapping for the class and then call write(Object).

Parameters:
bean - Object to be written as xml, not null
source - InputSource/code> containing an xml document specifying the mapping to be used (in the usual way), not null
Throws:
java.io.IOException
org.xml.sax.SAXException
java.beans.IntrospectionException
Since:
0.7
See Also:
since the standard notes also apply

getIdGenerator

public IDGenerator getIdGenerator()
Get IDGenerator implementation used to generate ID attribute values .

Returns:
implementation used for ID attribute generation

setIdGenerator

public void setIdGenerator(IDGenerator idGenerator)
Set IDGenerator implementation used to generate ID attribute values. This property can be used to customize the algorithm used for generation.

Parameters:
idGenerator - use this implementation for ID attribute generation

getBindingConfiguration

public BindingConfiguration getBindingConfiguration()
Gets the dynamic configuration setting to be used for bean reading.

Returns:
the BindingConfiguration settings, not null
Since:
0.5

setBindingConfiguration

public void setBindingConfiguration(BindingConfiguration bindingConfiguration)
Sets the dynamic configuration setting to be used for bean reading.

Parameters:
bindingConfiguration - the BindingConfiguration settings, not null
Since:
0.5

getWriteIDs

public boolean getWriteIDs()
Deprecated. 0.5 use BindingConfiguration.getMapIDs()

Should generated ID attribute values be added to the elements?

If IDs are not being written then if a cycle is encountered in the bean graph, then a CyclicReferenceException will be thrown by the write method.

Returns:
true if ID and IDREF attributes are to be written

setWriteIDs

public void setWriteIDs(boolean writeIDs)
Deprecated. 0.5 use BindingConfiguration.setMapIDs(boolean)

Set whether generated ID attribute values should be added to the elements If this property is set to false, then CyclicReferenceException will be thrown whenever a cyclic occurs in the bean graph.

Parameters:
writeIDs - true if ID's and IDREF's should be written

getWriteEmptyElements

public boolean getWriteEmptyElements()

Gets whether empty elements should be written into the output.

An empty element is one that has no attributes, no child elements and no body text. For example, <element/> is an empty element but <element attr='value'/> is not.

Returns:
true if empty elements will be written into the output
Since:
0.5

setWriteEmptyElements

public void setWriteEmptyElements(boolean writeEmptyElements)

Sets whether empty elements should be written into the output.

An empty element is one that has no attributes, no child elements and no body text. For example, <element/> is an empty element but <element attr='value'/> is not.

Parameters:
writeEmptyElements - true if empty elements should be written into the output
Since:
0.5

getXMLIntrospector

public XMLIntrospector getXMLIntrospector()

Gets the introspector used.

The XMLBeanInfo used to map each bean is created by the XMLIntrospector. One way in which the mapping can be customized is by altering the XMLIntrospector.

Returns:
the XMLIntrospector used for introspection

setXMLIntrospector

public void setXMLIntrospector(XMLIntrospector introspector)

Sets the introspector to be used.

The XMLBeanInfo used to map each bean is created by the XMLIntrospector. One way in which the mapping can be customized is by altering the XMLIntrospector.

Parameters:
introspector - use this introspector

getAbstractBeanWriterLog

public final org.apache.commons.logging.Log getAbstractBeanWriterLog()

Gets the current logging implementation.

Returns:
the Log implementation which this class logs to

setAbstractBeanWriterLog

public final void setAbstractBeanWriterLog(org.apache.commons.logging.Log log)

Set the current logging implementation.

Parameters:
log - Log implementation to use

startElement

protected void startElement(WriteContext context,
                            java.lang.String uri,
                            java.lang.String localName,
                            java.lang.String qName,
                            org.xml.sax.Attributes attr)
                     throws java.io.IOException,
                            org.xml.sax.SAXException
Writes the start tag for an element.

Parameters:
uri - the element's namespace uri
localName - the element's local name
qName - the element's qualified name
attr - the element's attributes
Throws:
java.io.IOException - if an IO problem occurs during writing
org.xml.sax.SAXException - if an SAX problem occurs during writing
Since:
0.5

endElement

protected void endElement(WriteContext context,
                          java.lang.String uri,
                          java.lang.String localName,
                          java.lang.String qName)
                   throws java.io.IOException,
                          org.xml.sax.SAXException
Writes the end tag for an element

Parameters:
uri - the element's namespace uri
localName - the element's local name
qName - the element's qualified name
Throws:
java.io.IOException - if an IO problem occurs during writing
org.xml.sax.SAXException - if an SAX problem occurs during writing
Since:
0.5

bodyText

protected void bodyText(WriteContext context,
                        java.lang.String text)
                 throws java.io.IOException,
                        org.xml.sax.SAXException
Writes body text

Parameters:
text - the body text to be written
Throws:
java.io.IOException - if an IO problem occurs during writing
org.xml.sax.SAXException - if an SAX problem occurs during writing
Since:
0.5

startElement

protected void startElement(java.lang.String uri,
                            java.lang.String localName,
                            java.lang.String qName,
                            org.xml.sax.Attributes attr)
                     throws java.io.IOException,
                            org.xml.sax.SAXException
Deprecated. 0.5 use startElement(WriteContext, String, String, String, Attributes)

Writes the start tag for an element.

Parameters:
uri - the element's namespace uri
localName - the element's local name
qName - the element's qualified name
attr - the element's attributes
Throws:
java.io.IOException - if an IO problem occurs during writing
org.xml.sax.SAXException - if an SAX problem occurs during writing

endElement

protected void endElement(java.lang.String uri,
                          java.lang.String localName,
                          java.lang.String qName)
                   throws java.io.IOException,
                          org.xml.sax.SAXException
Deprecated. 0.5 use endElement(WriteContext, String, String, String)

Writes the end tag for an element

Parameters:
uri - the element's namespace uri
localName - the element's local name
qName - the element's qualified name
Throws:
java.io.IOException - if an IO problem occurs during writing
org.xml.sax.SAXException - if an SAX problem occurs during writing

bodyText

protected void bodyText(java.lang.String text)
                 throws java.io.IOException,
                        org.xml.sax.SAXException
Deprecated. 0.5 use bodyText(WriteContext, String)

Writes body text

Parameters:
text - the body text to be written
Throws:
java.io.IOException - if an IO problem occurs during writing
org.xml.sax.SAXException - if an SAX problem occurs during writing

pushBean

protected void pushBean(java.lang.Object bean)
Pushes the bean onto the ancestry stack. If IDs are not being written, then check for cyclic references.

Parameters:
bean - push this bean onto the ancester stack

popBean

protected java.lang.Object popBean()
Pops the top bean off from the ancestry stack

Returns:
the last object pushed onto the ancester stack

getIndentLevel

protected int getIndentLevel()
Deprecated. 0.5 replaced by new SAX inspired API

Get the indentation for the current element. Used for pretty priting.

Returns:
the amount that the current element is indented

expressElementStart

protected void expressElementStart(java.lang.String qualifiedName)
                            throws java.io.IOException,
                                   org.xml.sax.SAXException
Deprecated. 0.5 replaced by new SAX inspired API

Express an element tag start using given qualified name.

Parameters:
qualifiedName - the qualified name of the element to be expressed
Throws:
java.io.IOException - if an IO problem occurs during writing
org.xml.sax.SAXException - if an SAX problem occurs during writing

expressElementStart

protected void expressElementStart(java.lang.String uri,
                                   java.lang.String localName,
                                   java.lang.String qualifiedName)
                            throws java.io.IOException,
                                   org.xml.sax.SAXException
Deprecated. 0.5 replaced by new SAX inspired API

Express an element tag start using given qualified name.

Parameters:
uri - the namespace uri
localName - the local name for this element
qualifiedName - the qualified name of the element to be expressed
Throws:
java.io.IOException - if an IO problem occurs during writing
org.xml.sax.SAXException - if an SAX problem occurs during writing

expressTagClose

protected void expressTagClose()
                        throws java.io.IOException,
                               org.xml.sax.SAXException
Deprecated. 0.5 replaced by new SAX inspired API

Express a closing tag.

Throws:
java.io.IOException - if an IO problem occurs during writing
org.xml.sax.SAXException - if an SAX problem occurs during writing

expressElementEnd

protected void expressElementEnd(java.lang.String qualifiedName)
                          throws java.io.IOException,
                                 org.xml.sax.SAXException
Deprecated. 0.5 replaced by new SAX inspired API

Express an element end tag (with given name)

Parameters:
qualifiedName - the qualified name for the element to be closed
Throws:
java.io.IOException - if an IO problem occurs during writing
org.xml.sax.SAXException - if an SAX problem occurs during writing

expressElementEnd

protected void expressElementEnd(java.lang.String uri,
                                 java.lang.String localName,
                                 java.lang.String qualifiedName)
                          throws java.io.IOException,
                                 org.xml.sax.SAXException
Deprecated. 0.5 replaced by new SAX inspired API

Express an element end tag (with given name)

Parameters:
uri - the namespace uri of the element close tag
localName - the local name of the element close tag
qualifiedName - the qualified name for the element to be closed
Throws:
java.io.IOException - if an IO problem occurs during writing
org.xml.sax.SAXException - if an SAX problem occurs during writing

expressElementEnd

protected void expressElementEnd()
                          throws java.io.IOException,
                                 org.xml.sax.SAXException
Deprecated. 0.5 replaced by new SAX inspired API

Express an empty element end.

Throws:
java.io.IOException - if an IO problem occurs during writing
org.xml.sax.SAXException - if an SAX problem occurs during writing

expressBodyText

protected void expressBodyText(java.lang.String text)
                        throws java.io.IOException,
                               org.xml.sax.SAXException
Deprecated. 0.5 replaced by new SAX inspired API

Express body text

Parameters:
text - the string to write out as the body of the current element
Throws:
java.io.IOException - if an IO problem occurs during writing
org.xml.sax.SAXException - if an SAX problem occurs during writing

expressAttribute

protected void expressAttribute(java.lang.String qualifiedName,
                                java.lang.String value)
                         throws java.io.IOException,
                                org.xml.sax.SAXException
Deprecated. 0.5 replaced by new SAX inspired API

Express an attribute

Parameters:
qualifiedName - the qualified name of the attribute
value - the attribute value
Throws:
java.io.IOException - if an IO problem occurs during writing
org.xml.sax.SAXException - if an SAX problem occurs during writing

expressAttribute

protected void expressAttribute(java.lang.String namespaceUri,
                                java.lang.String localName,
                                java.lang.String qualifiedName,
                                java.lang.String value)
                         throws java.io.IOException,
                                org.xml.sax.SAXException
Deprecated. 0.5 replaced by new SAX inspired API

Express an attribute

Parameters:
namespaceUri - the namespace uri
localName - the local name
qualifiedName - the qualified name of the attribute
value - the attribute value
Throws:
java.io.IOException - if an IO problem occurs during writing
org.xml.sax.SAXException - if an SAX problem occurs during writing

write

protected void write(java.lang.String qualifiedName,
                     ElementDescriptor elementDescriptor,
                     Context context)
              throws java.io.IOException,
                     org.xml.sax.SAXException,
                     java.beans.IntrospectionException
Deprecated. 0.5 replaced by new SAX inspired API

Writes the given element

Parameters:
qualifiedName - qualified name to use for the element
elementDescriptor - the ElementDescriptor describing the element
context - the Context to use to evaluate the bean expressions
Throws:
java.io.IOException - if an IO problem occurs during writing
org.xml.sax.SAXException - if an SAX problem occurs during writing
java.beans.IntrospectionException - if a java beans introspection problem occurs

write

protected void write(java.lang.String qualifiedName,
                     ElementDescriptor elementDescriptor,
                     Context context,
                     java.lang.String idAttribute,
                     java.lang.String idValue)
              throws java.io.IOException,
                     org.xml.sax.SAXException,
                     java.beans.IntrospectionException
Deprecated. 0.5 replaced by new SAX inspired API

Writes the given element adding an ID attribute

Parameters:
qualifiedName - qualified name to use for the element
elementDescriptor - the ElementDescriptor describing the element
context - the Context to use to evaluate the bean expressions
idAttribute - the qualified name of the ID attribute
idValue - the value for the ID attribute
Throws:
java.io.IOException - if an IO problem occurs during writing
org.xml.sax.SAXException - if an SAX problem occurs during writing
java.beans.IntrospectionException - if a java beans introspection problem occurs

writeRestOfElement

protected void writeRestOfElement(java.lang.String qualifiedName,
                                  ElementDescriptor elementDescriptor,
                                  Context context)
                           throws java.io.IOException,
                                  org.xml.sax.SAXException,
                                  java.beans.IntrospectionException
Deprecated. 0.5 replaced by new SAX inspired API

Write attributes, child elements and element end

Parameters:
qualifiedName - qualified name to use for the element
elementDescriptor - the ElementDescriptor describing the element
context - the Context to use to evaluate the bean expressions
Throws:
java.io.IOException - if an IO problem occurs during writing
org.xml.sax.SAXException - if an SAX problem occurs during writing
java.beans.IntrospectionException - if a java beans introspection problem occurs

writeIDREFElement

protected void writeIDREFElement(java.lang.String qualifiedName,
                                 java.lang.String idrefAttributeName,
                                 java.lang.String idrefAttributeValue)
                          throws java.io.IOException,
                                 org.xml.sax.SAXException,
                                 java.beans.IntrospectionException
Deprecated. 0.5 replaced by new SAX inspired API

Writes an element with a IDREF attribute

Parameters:
qualifiedName - of the element with IDREF attribute
idrefAttributeName - the qualified name of the IDREF attribute
idrefAttributeValue - the value for the IDREF attribute
Throws:
java.io.IOException - if an IO problem occurs during writing
org.xml.sax.SAXException - if an SAX problem occurs during writing
java.beans.IntrospectionException - if a java beans introspection problem occurs

writeContent

protected boolean writeContent(ElementDescriptor elementDescriptor,
                               Context context)
                        throws java.io.IOException,
                               org.xml.sax.SAXException,
                               java.beans.IntrospectionException
Deprecated. 0.5 replaced by new SAX inspired API

Writes the element content.

Parameters:
elementDescriptor - the ElementDescriptor to write as xml
context - the Context to use to evaluate the bean expressions
Returns:
true if some content was written
Throws:
java.io.IOException - if an IO problem occurs during writing
org.xml.sax.SAXException - if an SAX problem occurs during writing
java.beans.IntrospectionException - if a java beans introspection problem occurs

writeAttributes

protected void writeAttributes(ElementDescriptor elementDescriptor,
                               Context context)
                        throws java.io.IOException,
                               org.xml.sax.SAXException
Deprecated. 0.5 replaced by new SAX inspired API

Writes the attribute declarations

Parameters:
elementDescriptor - the ElementDescriptor to be written out as xml
context - the Context to use to evaluation bean expressions
Throws:
java.io.IOException - if an IO problem occurs during writing
org.xml.sax.SAXException - if an SAX problem occurs during writing

writeAttribute

protected void writeAttribute(AttributeDescriptor attributeDescriptor,
                              Context context)
                       throws java.io.IOException,
                              org.xml.sax.SAXException
Deprecated. 0.5 replaced by new SAX inspired API

Writes an attribute declaration

Parameters:
attributeDescriptor - the AttributeDescriptor to be written as xml
context - the Context to use to evaluation bean expressions
Throws:
java.io.IOException - if an IO problem occurs during writing
org.xml.sax.SAXException - if an SAX problem occurs during writing

writePrintln

protected void writePrintln()
                     throws java.io.IOException
Deprecated. 0.5 replaced by new SAX inspired API

Writes a empty line. This implementation does nothing but can be overridden by subclasses.

Throws:
java.io.IOException - if the line cannot be written

writeIndent

protected void writeIndent()
                    throws java.io.IOException
Deprecated. 0.5 replaced by new BeanWriter API

Writes an indentation. This implementation does nothing but can be overridden by subclasses.

Throws:
java.io.IOException - if the indent cannot be written


Copyright 2002-2008 The Apache Software Foundation. All Rights Reserved.