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.

Version:
$Revision: 1.10 $
Author:
Robert Burrell Donkin

Field Summary
private  ArrayStack beanStack
          Stack containing beans - used to detect cycles
private  IDGenerator idGenerator
          Used to generate ID attribute values
private  java.util.HashMap idMap
          Map containing ID attribute values for beans
private  int indentLevel
          indentation level
private  XMLIntrospector introspector
          Introspector used
private  org.apache.commons.logging.Log log
          Log used for logging (Doh!)
private  boolean writeIDs
          Should generated ID attribute values be added to the elements?
 
Constructor Summary
AbstractBeanWriter()
           
 
Method Summary
 void end()
          Marks the start of the bean writing.
protected abstract  void expressAttribute(java.lang.String qualifiedName, java.lang.String value)
          Express an attribute
protected abstract  void expressBodyText(java.lang.String text)
          Express body text
protected abstract  void expressElementEnd()
          Express an empty element end.
protected abstract  void expressElementEnd(java.lang.String qualifiedName)
          Express an element end tag (with given name)
protected abstract  void expressElementStart(java.lang.String qualifiedName)
          Express an element tag start using given qualified name.
protected abstract  void expressTagClose()
          Express a closing tag.
 org.apache.commons.logging.Log getAbstractBeanWriterLog()
          Gets the current logging implementation.
 IDGenerator getIdGenerator()
          Get IDGenerator implementation used to generate ID attribute values .
protected  int getIndentLevel()
          Get the indentation for the current element.
 boolean getWriteIDs()
          Should generated ID attribute values be added to the elements?
 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 setIdGenerator(IDGenerator idGenerator)
          Set IDGenerator implementation used to generate ID attribute values.
 void setWriteIDs(boolean writeIDs)
          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.
 void setXMLIntrospector(XMLIntrospector introspector)
          Sets the introspector to be used.
 void start()
          Marks the start of the bean writing.
 void write(java.lang.Object bean)
           Writes the given bean to the current stream using the XML introspector.
protected  void write(java.lang.String qualifiedName, ElementDescriptor elementDescriptor, Context context)
          Writes the given element
protected  void write(java.lang.String qualifiedName, ElementDescriptor elementDescriptor, Context context, java.lang.String idAttribute, java.lang.String idValue)
          Writes the given element adding an ID attribute
 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)
          Writes an attribute declaration
protected  void writeAttributes(ElementDescriptor elementDescriptor, Context context)
          Writes the attribute declarations
protected  boolean writeContent(ElementDescriptor elementDescriptor, Context context)
          Writes the element content.
protected  void writeIDREFElement(java.lang.String qualifiedName, java.lang.String idrefAttributeName, java.lang.String idrefAttributeValue)
          Writes an element with a IDREF attribute
protected  void writeIndent()
          Writes an indentation.
protected  void writePrintln()
          Writes a empty line.
protected  void writeRestOfElement(java.lang.String qualifiedName, ElementDescriptor elementDescriptor, Context context)
          Write attributes, child elements and element end
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

introspector

private XMLIntrospector introspector
Introspector used


log

private org.apache.commons.logging.Log log
Log used for logging (Doh!)


idMap

private java.util.HashMap idMap
Map containing ID attribute values for beans


beanStack

private ArrayStack beanStack
Stack containing beans - used to detect cycles


idGenerator

private IDGenerator idGenerator
Used to generate ID attribute values


writeIDs

private boolean writeIDs
Should generated ID attribute values be added to the elements?


indentLevel

private int indentLevel
indentation level

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 WriteIDs property is false.

Parameters:
bean - write out representation of this bean
Throws:
CyclicReferenceException - when a cyclic reference is encountered
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 WriteIDs property is false.

Parameters:
qualifiedName - the string naming root element
bean - the Object to write out as xml
Throws:
CyclicReferenceException - when a cyclic reference is encountered
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

getIdGenerator

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

Returns:
implementation used for ID attribute generation

getIndentLevel

protected int getIndentLevel()
Get the indentation for the current element. Used for pretty priting.

Returns:
the amount that the current element is indented

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

getWriteIDs

public boolean getWriteIDs()

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)
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

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

expressElementStart

protected abstract void expressElementStart(java.lang.String qualifiedName)
                                     throws java.io.IOException,
                                            org.xml.sax.SAXException
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

expressTagClose

protected abstract void expressTagClose()
                                 throws java.io.IOException,
                                        org.xml.sax.SAXException
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 abstract void expressElementEnd(java.lang.String qualifiedName)
                                   throws java.io.IOException,
                                          org.xml.sax.SAXException
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 abstract void expressElementEnd()
                                   throws java.io.IOException,
                                          org.xml.sax.SAXException
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 abstract void expressBodyText(java.lang.String text)
                                 throws java.io.IOException,
                                        org.xml.sax.SAXException
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 abstract void expressAttribute(java.lang.String qualifiedName,
                                         java.lang.String value)
                                  throws java.io.IOException,
                                         org.xml.sax.SAXException
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

write

protected void write(java.lang.String qualifiedName,
                     ElementDescriptor elementDescriptor,
                     Context context)
              throws java.io.IOException,
                     org.xml.sax.SAXException,
                     java.beans.IntrospectionException
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
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
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
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
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
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
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
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
Writes an indentation. This implementation does nothing but can be overridden by subclasses.

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

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
Throws:
CyclicReferenceException - if there is an identical bean already on the 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