Package csv.impl
Class XmlWriter
java.lang.Object
csv.impl.AbstractTableWriter
csv.impl.AbstractStreamTableWriter
csv.impl.XmlWriter
- All Implemented Interfaces:
TableWriter
public class XmlWriter extends AbstractStreamTableWriter
Writer implementation for XML streams.
- Author:
- ralph
-
Constructor Summary
Constructors Constructor Description XmlWriter()Constructor.XmlWriter(java.io.File file)Constructor.XmlWriter(java.io.File file, boolean writeHeaderRow)Constructor.XmlWriter(java.io.OutputStream out)Constructor.XmlWriter(java.io.OutputStream out, boolean writeHeaderRow)Constructor.XmlWriter(java.lang.String file)Constructor.XmlWriter(java.lang.String file, boolean writeHeaderRow)Constructor. -
Method Summary
Modifier and Type Method Description voidclose()Prints the closing of the XML file before closing the stream.protected java.lang.Stringencode(java.lang.Object s)Encodes the object for usage in XML file.protected java.lang.StringgetColumnEndTag(int column)Returns column's closing tagprotected java.lang.StringgetColumnName(int index)Returns the column name at given index.java.lang.StringgetColumnNameAttribute()Returns the name of the attribute within column tag that contains the column name.protected java.lang.StringgetColumnStartTag(int column, java.lang.Object value)Returns the complete start tag for the column.protected java.lang.StringgetColumnTagAttributes(int column, java.lang.Object value)Returns all attributes that need to be set for a column opening tag.java.lang.StringgetColumnTagName()Returns the name of the column tag.protected java.lang.StringgetColumnTagName(int column)Returns correct column name.protected java.lang.StringgetColumnType(java.lang.Object value)Returns the type of the value to be set as attribute in column opening tag.java.lang.StringgetColumnTypeAttribute()Returns the name of the attribute in the column tag that contains the Java type of the value.protected java.lang.StringgetDefaultColumnName(int index)Creates a default name for the column.java.lang.StringgetEncoding()Returns the encoding string for XML header.java.lang.Object[]getHeaderColumns()Returns the header column names.java.lang.StringgetRowTagName()Returns the name of the row tag.java.lang.StringgetTopLevelTagName()Returns the XML root tag name.protected voidinit()Initializes the writer.booleanisUseColumnNameTags()Returns whether column tag names will be equal to column names.booleanisWriteHeaderRow()Returns true when the first row being written contains the column names.voidprintComment(java.lang.String comment)Prints the comment into the stream.voidprintComment(java.lang.String comment, int row, int column)Prints the comment at the given location.protected voidprintFooter()Prints the footer.protected voidprintHeader()Prints the header.voidprintRow(java.lang.Object[] columns)Prints the row into the stream.voidsetColumnNameAttribute(java.lang.String columnNameAttribute)Sets the name of the attribute within column tag that contains the column name.voidsetColumnTagName(java.lang.String columnTagName)Sets the name of teh column tag.voidsetColumnTypeAttribute(java.lang.String columnTypeAttribute)Sets the name of the attribute in the column tag that contains the Java type of the value.voidsetEncoding(java.lang.String encoding)Sets the encoding string for XML header.voidsetHeaderColumns(java.lang.Object[] headerColumns)Sets the header column names explicitely.voidsetRowTagName(java.lang.String rowTagName)Set the name of the row tag.voidsetTopLevelTagName(java.lang.String topLevelTagName)Sets the XML root tag name.voidsetUseColumnNameTags(boolean useColumnNameTags)Sets whether column tag names will be equal to column names.voidsetWriteHeaderRow(boolean writeHeaderRow)Sets whether the first row being written contains the column names.Methods inherited from class csv.impl.AbstractStreamTableWriter
createOutputStreamWriter, createWriter, getCharsetEncoder, getOutputStream, getWriter, setCharset, setCharset, setCharsetEncoder, setOutputStreamMethods inherited from class csv.impl.AbstractTableWriter
convert, convert, getRowCount, getTypeConversionHandler, incrementRowCount, printRow, printRow, printRow, registerTypeConversionHandler, unregisterTypeConversionHandler
-
Constructor Details
-
XmlWriter
public XmlWriter()Constructor. -
XmlWriter
public XmlWriter(java.io.OutputStream out)Constructor.- Parameters:
out- output stream
-
XmlWriter
public XmlWriter(java.io.OutputStream out, boolean writeHeaderRow)Constructor.- Parameters:
out- output streamwriteHeaderRow- whether a dedicated header row will be received as first row
-
XmlWriter
public XmlWriter(java.io.File file) throws java.io.IOExceptionConstructor.- Parameters:
file- output file- Throws:
java.io.IOException- if exception occurs
-
XmlWriter
public XmlWriter(java.io.File file, boolean writeHeaderRow) throws java.io.IOExceptionConstructor.- Parameters:
file- output filewriteHeaderRow- whether a dedicated header row will be received as first row- Throws:
java.io.IOException- if exception occurs
-
XmlWriter
public XmlWriter(java.lang.String file) throws java.io.IOExceptionConstructor.- Parameters:
file- file to write- Throws:
java.io.IOException- if exception occurs
-
XmlWriter
public XmlWriter(java.lang.String file, boolean writeHeaderRow) throws java.io.IOExceptionConstructor.- Parameters:
file- file to writewriteHeaderRow- whether a dedicated header row will be received as first row- Throws:
java.io.IOException- if exception occurs
-
-
Method Details
-
close
public void close()Prints the closing of the XML file before closing the stream.- Specified by:
closein interfaceTableWriter- Overrides:
closein classAbstractStreamTableWriter- See Also:
AbstractStreamTableWriter.close()
-
init
protected void init()Initializes the writer.- Overrides:
initin classAbstractTableWriter- See Also:
AbstractTableWriter.init()
-
printRow
public void printRow(java.lang.Object[] columns) throws java.io.IOExceptionPrints the row into the stream. The first row must contain the column names ifsetWriteHeaderRow(boolean)was set.- Parameters:
columns- columns to be written in row- Throws:
java.io.IOException- when an exception occurs- See Also:
TableWriter.printRow(java.lang.Object[])
-
getColumnStartTag
protected java.lang.String getColumnStartTag(int column, java.lang.Object value)Returns the complete start tag for the column.- Parameters:
column- index of columnvalue- value in column- Returns:
- start tag, will include combined closing tag if value is null
-
getColumnEndTag
protected java.lang.String getColumnEndTag(int column)Returns column's closing tag- Parameters:
column- column index- Returns:
- closing column tag
-
getColumnTagName
protected java.lang.String getColumnTagName(int column)Returns correct column name.- Parameters:
column- index of column- Returns:
- column name
- See Also:
isUseColumnNameTags()
-
getColumnTagAttributes
protected java.lang.String getColumnTagAttributes(int column, java.lang.Object value)Returns all attributes that need to be set for a column opening tag.- Parameters:
column- index of columnvalue- value of column- Returns:
- all attributes
-
getColumnType
protected java.lang.String getColumnType(java.lang.Object value)Returns the type of the value to be set as attribute in column opening tag.- Parameters:
value- value of column- Returns:
- column type
-
printComment
public void printComment(java.lang.String comment) throws java.io.IOExceptionPrints the comment into the stream.- Specified by:
printCommentin interfaceTableWriter- Overrides:
printCommentin classAbstractTableWriter- Parameters:
comment- the comment to write- Throws:
java.io.IOException- when an exception occurs- See Also:
AbstractTableWriter.printComment(java.lang.String)
-
printComment
public void printComment(java.lang.String comment, int row, int column) throws java.io.IOExceptionPrints the comment at the given location.- Specified by:
printCommentin interfaceTableWriter- Overrides:
printCommentin classAbstractTableWriter- Parameters:
comment- the comment to writerow- index of row for commentcolumn- index of column for comment- Throws:
java.io.IOException- when an exception occurs- See Also:
AbstractTableWriter.printComment(java.lang.String, int, int)
-
printHeader
protected void printHeader()Prints the header. -
printFooter
protected void printFooter()Prints the footer. -
encode
protected java.lang.String encode(java.lang.Object s)Encodes the object for usage in XML file.- Parameters:
s- object to encode- Returns:
- XML encoded string
-
getColumnName
protected java.lang.String getColumnName(int index)Returns the column name at given index. The method will pass on togetDefaultColumnName(int)if no column header can be found.- Parameters:
index- index of column- Returns:
- column name
-
getDefaultColumnName
protected java.lang.String getDefaultColumnName(int index)Creates a default name for the column. The name is "column" plus the index- Parameters:
index- index of column- Returns:
- default column name
-
getTopLevelTagName
public java.lang.String getTopLevelTagName()Returns the XML root tag name.- Returns:
- the topLevelTagName
-
setTopLevelTagName
public void setTopLevelTagName(java.lang.String topLevelTagName)Sets the XML root tag name.- Parameters:
topLevelTagName- the topLevelTagName to set
-
getRowTagName
public java.lang.String getRowTagName()Returns the name of the row tag.- Returns:
- the rowTagName
-
setRowTagName
public void setRowTagName(java.lang.String rowTagName)Set the name of the row tag.- Parameters:
rowTagName- the rowTagName to set
-
getColumnTagName
public java.lang.String getColumnTagName()Returns the name of the column tag. This property will be used only whensetUseColumnNameTags(boolean)was not set.- Returns:
- the columnTagName
-
setColumnTagName
public void setColumnTagName(java.lang.String columnTagName)Sets the name of teh column tag. This property will be used only whensetUseColumnNameTags(boolean)was not set.- Parameters:
columnTagName- the columnTagName to set
-
isUseColumnNameTags
public boolean isUseColumnNameTags()Returns whether column tag names will be equal to column names.- Returns:
- the useColumnNameTags
-
setUseColumnNameTags
public void setUseColumnNameTags(boolean useColumnNameTags)Sets whether column tag names will be equal to column names.- Parameters:
useColumnNameTags- the useColumnNameTags to set
-
getColumnNameAttribute
public java.lang.String getColumnNameAttribute()Returns the name of the attribute within column tag that contains the column name. This property will be used only whensetUseColumnNameTags(boolean)was not set.- Returns:
- the columnNameAttribute
-
setColumnNameAttribute
public void setColumnNameAttribute(java.lang.String columnNameAttribute)Sets the name of the attribute within column tag that contains the column name. This property will be used only whensetUseColumnNameTags(boolean)was not set.- Parameters:
columnNameAttribute- the columnNameAttribute to set
-
getColumnTypeAttribute
public java.lang.String getColumnTypeAttribute()Returns the name of the attribute in the column tag that contains the Java type of the value.- Returns:
- the columnTypeAttribute
-
setColumnTypeAttribute
public void setColumnTypeAttribute(java.lang.String columnTypeAttribute)Sets the name of the attribute in the column tag that contains the Java type of the value.- Parameters:
columnTypeAttribute- the columnTypeAttribute to set
-
getEncoding
public java.lang.String getEncoding()Returns the encoding string for XML header.- Returns:
- the encoding
-
setEncoding
public void setEncoding(java.lang.String encoding)Sets the encoding string for XML header.- Parameters:
encoding- the encoding to set
-
getHeaderColumns
public java.lang.Object[] getHeaderColumns()Returns the header column names.- Returns:
- the headerColumns
-
setHeaderColumns
public void setHeaderColumns(java.lang.Object[] headerColumns)Sets the header column names explicitely.- Parameters:
headerColumns- the headerColumns to set
-
isWriteHeaderRow
public boolean isWriteHeaderRow()Returns true when the first row being written contains the column names.- Returns:
- the writeHeaderRow
-
setWriteHeaderRow
public void setWriteHeaderRow(boolean writeHeaderRow)Sets whether the first row being written contains the column names.- Parameters:
writeHeaderRow- the writeHeaderRow to set
-