org.jmonks.batch.io.xml
Class XMLFileWriter

java.lang.Object
  extended byorg.jmonks.batch.io.FileWriter
      extended byorg.jmonks.batch.io.xml.XMLFileWriter

public class XMLFileWriter
extends FileWriter

XMLFileWriter writes the specified xml file according to the given file spec with the the recrods submitted to write into the file. It generates the root tag from the attribute given in file-spec. It uses the last part in record xpath to write the record element. If nested elements or repeated elements needs to be written, writer record provides the methods to write these elements.

XMLFileWriter looks for two attributes "indentation-engine" and "encoding" in the file spec to format the generated xml and use the encoding value in generated xml processing instruction. The value to the "indentation-engine" should be the class name implements XMLIndentationEngine interface. If it doesnt find this attribute or not a valid value in this attribute, it uses the default indentation engine. The value specified in "encoding" attribute will be used in the processing instruction.

Since:
1.0
Version:
1.0
Author:
Suresh Pragada

Nested Class Summary
 class XMLFileWriter.XMLWriterRecord
          XMLWriterRecord implements WriterRecord by maintaing the field names and values as a map and provides the methods with proper access privileges to read into and write from the record.
 
Field Summary
protected  XMLFileSpec fileSpec
          File spec to be adhered with.
 
Constructor Summary
XMLFileWriter(java.io.OutputStream outputStream, FileSpec fileSpec)
          Constructs and initializes the writer with the given values.
XMLFileWriter(java.io.Writer writer, FileSpec fileSpec)
          Constructs and initializes the writer with the given values.
 
Method Summary
 void close()
          Closes the writer.
 WriterRecord createWriterRecord(RecordType recordType)
          Creates the writer record assocites with the given record type.
protected  void writeComplexElement(java.lang.String recordElementName, XMLFileWriter.XMLWriterRecord fieldValueAsRecord)
          Writes the nested/composite element into the file.
 void writeRecord(WriterRecord writerRecord)
          Writes the record into the file.
protected  void writeRepeatElement(java.lang.String multiElementName, java.util.List fieldValueAsList)
          Writes the repeated element into the file.
protected  void writeSimpleElement(java.lang.String fieldName, java.lang.Object fieldValue)
          Writes the simple element into the file.
 
Methods inherited from class org.jmonks.batch.io.FileWriter
getFileWriter, getFileWriter, getFileWriter, getFileWriter
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

fileSpec

protected XMLFileSpec fileSpec
File spec to be adhered with.

Constructor Detail

XMLFileWriter

public XMLFileWriter(java.io.OutputStream outputStream,
                     FileSpec fileSpec)
Constructs and initializes the writer with the given values.

Parameters:
outputStream - Output stream to write the records.
fileSpec - File spec to be used to generate the file.

XMLFileWriter

public XMLFileWriter(java.io.Writer writer,
                     FileSpec fileSpec)
Constructs and initializes the writer with the given values.

Parameters:
writer - Writer to write the records.
fileSpec - File spec to be used to generate the file.
Method Detail

writeRecord

public void writeRecord(WriterRecord writerRecord)
Writes the record into the file. This record should be obtained from this file writer only by using the createWriterRecord method.

Specified by:
writeRecord in class FileWriter
Parameters:
writerRecord - Record to be written into the file.
Throws:
java.lang.IllegalStateException - If writer is closed and trying to write the recrod.
java.lang.IllegalArgumentException - If writer record is null.
FileParseException - Problems while trying to write the record.

writeSimpleElement

protected void writeSimpleElement(java.lang.String fieldName,
                                  java.lang.Object fieldValue)
                           throws javax.xml.stream.XMLStreamException
Writes the simple element into the file.

Parameters:
fieldName - Name of the element.
fieldValue - Value of the element.
Throws:
javax.xml.stream.XMLStreamException

writeComplexElement

protected void writeComplexElement(java.lang.String recordElementName,
                                   XMLFileWriter.XMLWriterRecord fieldValueAsRecord)
                            throws javax.xml.stream.XMLStreamException
Writes the nested/composite element into the file.

Parameters:
recordElementName - Name of the nested element.
fieldValueAsRecord - Value of the nested element.
Throws:
javax.xml.stream.XMLStreamException

writeRepeatElement

protected void writeRepeatElement(java.lang.String multiElementName,
                                  java.util.List fieldValueAsList)
                           throws javax.xml.stream.XMLStreamException
Writes the repeated element into the file.

Parameters:
multiElementName - Name of the repeated element.
fieldValueAsList - Value of the repeated element.
Throws:
javax.xml.stream.XMLStreamException

createWriterRecord

public WriterRecord createWriterRecord(RecordType recordType)
Creates the writer record assocites with the given record type. IllegalArgumentException will be thrown, if there is no record spec is found with this record type.

Specified by:
createWriterRecord in class FileWriter
Parameters:
recordType - Type fo the record to be created.
Returns:
Returns the requested writer record.
Throws:
java.lang.IllegalArgumentException - No record spec is found with this record type.

close

public void close()
Closes the writer.

Specified by:
close in class FileWriter