com.ricebridge.xmlman.in
Class XmlOutputHandler

java.lang.Object
  extended bycom.ricebridge.xmlman.in.XmlOutputHandler
All Implemented Interfaces:
Runnable

public class XmlOutputHandler
extends Object
implements Runnable

Internal Class; handles XML saving and generation operations.

This is a single use object, a new instance must be created for each operation. Used internally by XmlManager.


Constructor Summary
XmlOutputHandler()
          Create a new XmlOutputHandler with empty state.
 
Method Summary
 XmlOutputHandler copy(XmlSpec pXmlSpec)
          Copy, using specified XmlSpec.
 OutNode createNodeTree(List pOutPathList)
          Create the tree data structure of nodes that represents the XML format to generate.
 void endWrite(boolean pThrowClose)
          End writing of record data by closing all open elements.
 void findChanges(OutNode pNode, OutNode pSubNode, OutNode pIgnoreNode)
          Recursively find all data field change possibilities, starting from subnode and store them in the specified node.
 OutNode findRoot()
          Find the root node starting from the record node.
 OutNode findRoot(OutNode pOutNode)
          Find the root node starting from the given node.
 OutNode findStartNode()
          Find the starting node for the current record, based on data field changes.
 OutNode findSubNode(OutNode pFind, String pName)
          Find named subnode of specified node, at any depth.
 BadRecordListener getBadRecordListener()
          Get the BadRecordListener object currently in use.
 String[] getFieldNames()
          Get the field names of the primary RecordSpec - may be null.
 RecordProvider getRecordProvider()
          Get the RecordProvider object for the current save operation.
 MutableStats getStats()
          Get the current Stats object used when saving XML record data.
 XmlSpec getXmlSpec()
          Get the current XmlSpec object to use when saving XML record data.
 boolean isFinished()
          When true, the saving process has finished.
 void makeChangeLists(OutNode pSourceNode)
          Resolve data field change possibilities for all ancestors of source node
 void prepare(RecordSpec pRecordSpec)
          Prepare internal data structures using the specified RecordSpec object.
 void run()
          Start saving operation in separate Thread.
 void saveFile(File pXmlFile)
          Save XML data to a file.
 void saveOutputStream(OutputStream pXmlOutputStream)
          Save XML data to an OutputStream.
 void setBadRecordListener(BadRecordListener pBadRecordListener)
          Set the BadRecordListener object to receive BadRecord description objects as bad data records are encountered.
 void setRecordProvider(RecordProvider pRecordProvider)
          Set the RecordProvider object for the current save operation.
 void setStats(MutableStats pStats)
          Set the Stats object to use when saving XML record data.
 void setXmlSpec(XmlSpec pXmlSpec)
          Set the XmlSpec object to use when saving XML record data.
 void setXmlWriter(XmlWriter pXmlWriter)
          Set the XmlWriter object to use when saving XML record data.
 void startWrite()
          Start writing of record data.
 void traverse(OutNode pStartNode, boolean pNoRecord)
          Non-recursive traversal of node structure, outputting XML at each node.
 void write()
          Output the specified XML for each data record.
 void writeRecord(String[] pValues)
          Output XML for record data fields in full, creating parent elements as necessary.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XmlOutputHandler

public XmlOutputHandler()
Create a new XmlOutputHandler with empty state.

Method Detail

copy

public XmlOutputHandler copy(XmlSpec pXmlSpec)
Copy, using specified XmlSpec.


run

public void run()
Start saving operation in separate Thread.

Specified by:
run in interface Runnable
See Also:
XmlSpec.setBackground(boolean)

isFinished

public boolean isFinished()
When true, the saving process has finished.

When false, the saving process is still in progress. Use this method when the saving process is running in the background as a separate thread. See XmlSpec.setBackground(boolean) for details.

See Also:
XmlSpec.setBackground(boolean)

setXmlWriter

public void setXmlWriter(XmlWriter pXmlWriter)
Set the XmlWriter object to use when saving XML record data.

The XmlWriter class handles the generation of XML content. For full details, see the XmlWriter documentation.

Parameters:
pXmlWriter - the XmlWriter object to use
See Also:
XmlWriter

setXmlSpec

public void setXmlSpec(XmlSpec pXmlSpec)
Set the XmlSpec object to use when saving XML record data.

The XmlSpec class lets you set the options to use when saving XML. For full details, see the XmlSpec documentation.

Parameters:
pXmlSpec - the XmlSpec object to use
See Also:
XmlSpec

getXmlSpec

public XmlSpec getXmlSpec()
Get the current XmlSpec object to use when saving XML record data.

If no XmlSpec has been specified, a default one will be created and stored.

See Also:
setXmlSpec(com.ricebridge.xmlman.XmlSpec)

setStats

public void setStats(MutableStats pStats)
Set the Stats object to use when saving XML record data.

Statistics (such as number of records) are saved to this object.

Parameters:
pStats - Stats object to use
See Also:
Stats

getStats

public MutableStats getStats()
Get the current Stats object used when saving XML record data.

If no Stats object has been specified, a default one will be created and stored.

See Also:
setStats(com.ricebridge.xmlman.in.MutableStats)

setRecordProvider

public void setRecordProvider(RecordProvider pRecordProvider)
Set the RecordProvider object for the current save operation.

This RecordProvider is used by the save operation.

Parameters:
pRecordProvider - automatic RecordProvider

getRecordProvider

public RecordProvider getRecordProvider()
Get the RecordProvider object for the current save operation.

See Also:
setRecordProvider(com.ricebridge.xmlman.RecordProvider)

setBadRecordListener

public void setBadRecordListener(BadRecordListener pBadRecordListener)
Set the BadRecordListener object to receive BadRecord description objects as bad data records are encountered.

If the ignore bad records option is true then each BadRecord is reported to the BadRecordListener. If it is false, then only the first BadRecord is reported, before processing halts.

Parameters:
pBadRecordListener - BadRecordListener object

getBadRecordListener

public BadRecordListener getBadRecordListener()
Get the BadRecordListener object currently in use.

If a BadRecordListener object has not been specified then a default one will be created and stored.


prepare

public void prepare(RecordSpec pRecordSpec)
Prepare internal data structures using the specified RecordSpec object.

The output XML is defined by the RecordSpec according to a set of simple rules. For more information, see the RecordProvider documentation.

Parameters:
pRecordSpec - RecordSpec object
See Also:
RecordSpec, RecordProvider

getFieldNames

public String[] getFieldNames()
Get the field names of the primary RecordSpec - may be null.


saveFile

public void saveFile(File pXmlFile)
Save XML data to a file.

Parameters:
pXmlFile - XML file to save

saveOutputStream

public void saveOutputStream(OutputStream pXmlOutputStream)
Save XML data to an OutputStream.

Parameters:
pXmlOutputStream - OutputStream to receive XML

write

public void write()
Output the specified XML for each data record.


startWrite

public void startWrite()
Start writing of record data.


endWrite

public void endWrite(boolean pThrowClose)
End writing of record data by closing all open elements.


writeRecord

public void writeRecord(String[] pValues)
Output XML for record data fields in full, creating parent elements as necessary.

Parameters:
pValues - record data field values

findStartNode

public OutNode findStartNode()
Find the starting node for the current record, based on data field changes.

If a node that is an ancestor of the record node has a change, then start from that node, otherwise start from the record node.


createNodeTree

public OutNode createNodeTree(List pOutPathList)
Create the tree data structure of nodes that represents the XML format to generate.

This method expects the OutPaths in the list to be ordered as follows:

  1. absolute fields paths
  2. record path
  3. relative field paths
This order creates a left-to-right node tree where absolute paths appear before relative paths, thus outputting absolute path elements before record and relative field path elements in document order.

Parameters:
pOutPathList - list of output paths in correct order

makeChangeLists

public void makeChangeLists(OutNode pSourceNode)
Resolve data field change possibilities for all ancestors of source node

Parameters:
pSourceNode - resolve against ancestors of source node

findChanges

public void findChanges(OutNode pNode,
                        OutNode pSubNode,
                        OutNode pIgnoreNode)
Recursively find all data field change possibilities, starting from subnode and store them in the specified node.

When a record field changes, then that node has to be restarted, by reversing back up to the last node that is affected by the change, and outputting new start elements for all the child nodes.

Parameters:
pNode - node to store changes in
pSubNode - node to start child node recursion from
pIgnoreNode - child node to ignore

findRoot

public OutNode findRoot()
Find the root node starting from the record node.


findRoot

public OutNode findRoot(OutNode pOutNode)
Find the root node starting from the given node.

Parameters:
pOutNode - node to start root search from

findSubNode

public OutNode findSubNode(OutNode pFind,
                           String pName)
Find named subnode of specified node, at any depth.

Parameters:
pFind - node to search from
pName - name of subnode to find

traverse

public void traverse(OutNode pStartNode,
                     boolean pNoRecord)
Non-recursive traversal of node structure, outputting XML at each node.

Follows all node children, down and up tree, until record, start or root node end element is reached.

Because we normally have to end on record end tag, we output the record end tag, but not the record start tag - as the record is the start node.

Parameters:
pStartNode - node to start traversal at
pNoRecord - do not output record node or below


Copyright © 2004-2005 Ricebridge