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(OutputStream out)
      Constructor.
      Parameters:
      out - output stream
    • XmlWriter

      public XmlWriter(OutputStream out, boolean writeHeaderRow)
      Constructor.
      Parameters:
      out - output stream
      writeHeaderRow - whether a dedicated header row will be received as first row
    • XmlWriter

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

      public XmlWriter(File file, boolean writeHeaderRow) throws IOException
      Constructor.
      Parameters:
      file - output file
      writeHeaderRow - whether a dedicated header row will be received as first row
      Throws:
      IOException - if exception occurs
    • XmlWriter

      public XmlWriter(String file) throws IOException
      Constructor.
      Parameters:
      file - file to write
      Throws:
      IOException - if exception occurs
    • XmlWriter

      public XmlWriter(String file, boolean writeHeaderRow) throws IOException
      Constructor.
      Parameters:
      file - file to write
      writeHeaderRow - whether a dedicated header row will be received as first row
      Throws:
      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:
    • init

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

      public void printRow(Object[] columns) throws 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:
      IOException - when an exception occurs
      See Also:
    • getColumnStartTag

      protected String getColumnStartTag(int column, 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 String getColumnEndTag(int column)
      Returns column's closing tag
      Parameters:
      column - column index
      Returns:
      closing column tag
    • getColumnTagName

      protected String getColumnTagName(int column)
      Returns correct column name.
      Parameters:
      column - index of column
      Returns:
      column name
      See Also:
    • getColumnTagAttributes

      protected String getColumnTagAttributes(int column, 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 String getColumnType(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(String comment) throws 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:
      IOException - when an exception occurs
      See Also:
    • printComment

      public void printComment(String comment, int row, int column) throws 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:
      IOException - when an exception occurs
      See Also:
    • printHeader

      protected void printHeader()
      Prints the header.
    • printFooter

      protected void printFooter()
      Prints the footer.
    • encode

      protected String encode(Object s)
      Encodes the object for usage in XML file.
      Parameters:
      s - object to encode
      Returns:
      XML encoded string
    • getColumnName

      protected 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 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 String getTopLevelTagName()
      Returns the XML root tag name.
      Returns:
      the topLevelTagName
    • setTopLevelTagName

      public void setTopLevelTagName(String topLevelTagName)
      Sets the XML root tag name.
      Parameters:
      topLevelTagName - the topLevelTagName to set
    • getRowTagName

      public String getRowTagName()
      Returns the name of the row tag.
      Returns:
      the rowTagName
    • setRowTagName

      public void setRowTagName(String rowTagName)
      Set the name of the row tag.
      Parameters:
      rowTagName - the rowTagName to set
    • getColumnTagName

      public 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(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 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(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 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(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 String getEncoding()
      Returns the encoding string for XML header.
      Returns:
      the encoding
    • setEncoding

      public void setEncoding(String encoding)
      Sets the encoding string for XML header.
      Parameters:
      encoding - the encoding to set
    • getHeaderColumns

      public Object[] getHeaderColumns()
      Returns the header column names.
      Returns:
      the headerColumns
    • setHeaderColumns

      public void setHeaderColumns(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