2.17.2: 2011-06-17

net.sf.basedb.util.basefile
Class BaseFileWriter

java.lang.Object
  extended by java.io.Writer
      extended by java.io.PrintWriter
          extended by net.sf.basedb.util.export.TableWriter
              extended by net.sf.basedb.util.basefile.BaseFileWriter
All Implemented Interfaces:
Closeable, Flushable, Appendable

public class BaseFileWriter
extends TableWriter

Wraps a writer stream and provides methods for easier creation of BASEfiles. All methods that start with base are used for BASEfile specified formatting. It is also possible to use the regular PrintWriter methods for special cases.

A BASEfile is divided in sections. Each section has a header part and a data part. To start a new section use baseBeginSection(String). When a section has been started use basePrintHeader(String, Object) or one of the overloaded methods to print headers for that section. To end the header part and start the data part, use baseBeginDataPart() and then basePrintData(Object...) to print data lines. When all data has been printed either start a new section or end the current section with baseEndSection().

Version:
2.12
Author:
Nicklas
Last modified
$Date: 2010-08-13 10:50:27 +0200 (Fri, 13 Aug 2010) $

Field Summary
private  String currentSection
           
private  boolean dataPart
           
private  boolean hasBom
           
private  boolean headerPart
           
private  String metadataPrefix
           
private  Pattern n
           
private  Pattern r
           
 
Fields inherited from class java.io.PrintWriter
out
 
Fields inherited from class java.io.Writer
lock
 
Constructor Summary
BaseFileWriter(Writer out)
          Create a new BASEfile writer that is writing it's output to the given writer.
 
Method Summary
 void baseBeginDataPart()
          End the header part of the current section (by writing a single % on a line by itself) and start the data part.
 void baseBeginSection(String name)
          Start a new section in the BASEfile.
 void baseEndSection()
          Ends the current section by writing an empty line.
 String baseEscape(String in)
          Escape new line and carrige return characters in the input to the literal string '\n' and '\r' in the output.
 void basePrintData(Object... data)
          Print a data line to the BASEfile.
<T> void
basePrintHeader(String key, Object value)
          Prints a header in the BASEfile.
 void baseWriteBom()
          Write the beginning-of-file marker (BASEfile) to the output.
 String getCurrentSection()
           
 String getMetadataPrefix()
          The metadata prefix is written to the beginning of each line that is not a data line.
 boolean isWritingData()
           
 boolean isWritingHeaders()
           
 void setMetadataPrefix(String metaDataPrefix)
          Change the metadata prefix string.
protected  void setMode(boolean headerPart, boolean dataPart)
           
 
Methods inherited from class net.sf.basedb.util.export.TableWriter
getDataSeparator, getEncoder, getNullValue, setDataSeparator, setEncoder, setNullValue, tableEncode, tablePrintData, tablePrintData
 
Methods inherited from class java.io.PrintWriter
append, append, append, checkError, clearError, close, flush, format, format, print, print, print, print, print, print, print, print, print, printf, printf, println, println, println, println, println, println, println, println, println, println, setError, write, write, write, write, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

metadataPrefix

private String metadataPrefix

currentSection

private String currentSection

hasBom

private boolean hasBom

headerPart

private boolean headerPart

dataPart

private boolean dataPart

r

private Pattern r

n

private Pattern n
Constructor Detail

BaseFileWriter

public BaseFileWriter(Writer out)
Create a new BASEfile writer that is writing it's output to the given writer. The file prefix 'BASEfile' is automatically written.

Parameters:
out - The writer to write the data to
Method Detail

getCurrentSection

public String getCurrentSection()
Returns:
The name of the current section, or null if no section has been started.

isWritingHeaders

public boolean isWritingHeaders()
Returns:
TRUE if this writer is currently in the header part of a section, FALSE if no section is started or if the writer is in the data part

isWritingData

public boolean isWritingData()
Returns:
TRUE if this writer is currently in the data part of a section, FALSE if no section is started or if the writer is in the header part

setMode

protected void setMode(boolean headerPart,
                       boolean dataPart)

getMetadataPrefix

public String getMetadataPrefix()
The metadata prefix is written to the beginning of each line that is not a data line. The default value is an empty string.

Tip! Settting this value to '#' makes it possible to send a BASEfile to gnuplot.

Returns:
The current value of the metadata prefix

setMetadataPrefix

public void setMetadataPrefix(String metaDataPrefix)
Change the metadata prefix string.

Parameters:
metaDataPrefix - The new metadata prefix

baseWriteBom

public void baseWriteBom()
Write the beginning-of-file marker (BASEfile) to the output. This method must be called before it is possible to writer other data to the file.


baseBeginSection

public void baseBeginSection(String name)
Start a new section in the BASEfile. If the current section hasn't been ended, baseEndSection() is automatically called to end it. After this call the BASEfile is ready for writing headers to the new section.

If the beginning-of-file-marker hasn't been written the baseWriteBom() is automatically called.

Parameters:
name - The name of the section

baseEndSection

public void baseEndSection()
Ends the current section by writing an empty line.


basePrintHeader

public <T> void basePrintHeader(String key,
                                Object value)
Prints a header in the BASEfile. This call is ignored if no section has been started or if the current section is currently in data writing mode. Line breaks (\n) and carriage returns (\r) in the value are converted to the literal string values '\n' and '\r'.

Parameters:
key - The header key, 'section' is not allowed
value - The value, if null the configured null value is used, otherwise the toString() method is called
Throws:
InvalidDataException - If the key is 'section'

baseBeginDataPart

public void baseBeginDataPart()
End the header part of the current section (by writing a single % on a line by itself) and start the data part. The call to this method is ignored if no section has been started or if the data part of the current section has alredy been started.


basePrintData

public void basePrintData(Object... data)
Print a data line to the BASEfile. Each value is separated by the data separator string ('tab' by default). Null values are replaced with the null value string (empty string by default). Non-null objects are converted to strings values by calling their toString() method.

This call is ignored if the data part has not been started.

Parameters:
data - The values to write
See Also:
isWritingData(), baseBeginDataPart()

baseEscape

public String baseEscape(String in)
Escape new line and carrige return characters in the input to the literal string '\n' and '\r' in the output.

Parameters:
in - The input string

2.17.2: 2011-06-17