Package csv.impl

Class XmlWriter

All Implemented Interfaces:
TableWriter

public class XmlWriter
extends AbstractStreamTableWriter
Writer implementation for XML streams.
Author:
ralph
  • 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 stream
      writeHeaderRow - whether a dedicated header row will be received as first row
    • XmlWriter

      public XmlWriter​(java.io.File file) throws java.io.IOException
      Constructor.
      Parameters:
      file - output file
      Throws:
      java.io.IOException - if exception occurs
    • XmlWriter

      public XmlWriter​(java.io.File file, boolean writeHeaderRow) throws java.io.IOException
      Constructor.
      Parameters:
      file - output file
      writeHeaderRow - 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.IOException
      Constructor.
      Parameters:
      file - file to write
      Throws:
      java.io.IOException - if exception occurs
    • XmlWriter

      public XmlWriter​(java.lang.String file, boolean writeHeaderRow) throws java.io.IOException
      Constructor.
      Parameters:
      file - file to write
      writeHeaderRow - 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:
      close in interface TableWriter
      Overrides:
      close in class AbstractStreamTableWriter
      See Also:
      AbstractStreamTableWriter.close()
    • init

      protected void init()
      Initializes the writer.
      Overrides:
      init in class AbstractTableWriter
      See Also:
      AbstractTableWriter.init()
    • printRow

      public void printRow​(java.lang.Object[] columns) throws java.io.IOException
      Prints the row into the stream. The first row must contain the column names if setWriteHeaderRow(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 column
      value - 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 column
      value - 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.IOException
      Prints the comment into the stream.
      Specified by:
      printComment in interface TableWriter
      Overrides:
      printComment in class AbstractTableWriter
      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.IOException
      Prints the comment at the given location.
      Specified by:
      printComment in interface TableWriter
      Overrides:
      printComment in class AbstractTableWriter
      Parameters:
      comment - the comment to write
      row - index of row for comment
      column - 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 to getDefaultColumnName(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 when setUseColumnNameTags(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 when setUseColumnNameTags(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 when setUseColumnNameTags(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 when setUseColumnNameTags(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