net.sf.jmatchparser.util.csv
Class FixedWidthColumn

java.lang.Object
  extended by net.sf.jmatchparser.util.csv.FixedWidthColumn

public class FixedWidthColumn
extends Object

A column of a fixed-width CSV file.


Nested Class Summary
static class FixedWidthColumn.Alignment
          Enumeration of possible column alignments.
 
Constructor Summary
FixedWidthColumn(int width)
          Create a left-aligned fixed-width column of the given width.
FixedWidthColumn(int width, FixedWidthColumn.Alignment alignment)
          Create a fixed-width column of the given width and alignment.
FixedWidthColumn(int width, FixedWidthColumn.Alignment alignment, char pad)
          Create a fixed-width column of the given width and alignment, which uses the given character to pad the value on both sides if needed.
FixedWidthColumn(int width, FixedWidthColumn.Alignment alignment, char padLeft, char padRight)
          Create a fixed-width column of the given width and alignment, which uses two different characters to pad the value on left or right side if needed.
FixedWidthColumn(int width, FixedWidthColumn.Alignment alignment, char padLeft, char padRight, String... exceptionStrings)
          Create a fixed-width column of the given width and alignment, which uses two different characters to pad the value on left or right side if needed and the given exception strings.
 
Method Summary
 String formatValue(String value, boolean truncate, boolean allowOverflow)
          Format a value to store it into a fixed-width CSV file.
 int getWidth()
          Get the width of this column.
static FixedWidthColumn parse(String colSpec)
          Parse a column from a column specification string.
 String parseValue(String value)
          Parse a value from a fixed-width CSV file, i. e. remove padding and check for exception strings.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

FixedWidthColumn

public FixedWidthColumn(int width)
Create a left-aligned fixed-width column of the given width. Spaces are used to pad the value on both sides. Values that contain tabs or two consecutive spaces are considered invalid.

Parameters:
width - The width

FixedWidthColumn

public FixedWidthColumn(int width,
                        FixedWidthColumn.Alignment alignment)
Create a fixed-width column of the given width and alignment. Spaces are used to pad the value on both sides. Values that contain tabs or two consecutive spaces are considered invalid.

Parameters:
width - The width
alignment - The alignment

FixedWidthColumn

public FixedWidthColumn(int width,
                        FixedWidthColumn.Alignment alignment,
                        char pad)
Create a fixed-width column of the given width and alignment, which uses the given character to pad the value on both sides if needed. Values that contain tabs or two consecutive spaces are considered invalid.

Parameters:
width - The width
alignment - The alignment
pad - The padding character

FixedWidthColumn

public FixedWidthColumn(int width,
                        FixedWidthColumn.Alignment alignment,
                        char padLeft,
                        char padRight)
Create a fixed-width column of the given width and alignment, which uses two different characters to pad the value on left or right side if needed. Values that contain tabs or two consecutive spaces are considered invalid.

Parameters:
width - The width
alignment - The alignment
padLeft - The left padding character
padRight - The right padding character

FixedWidthColumn

public FixedWidthColumn(int width,
                        FixedWidthColumn.Alignment alignment,
                        char padLeft,
                        char padRight,
                        String... exceptionStrings)
Create a fixed-width column of the given width and alignment, which uses two different characters to pad the value on left or right side if needed and the given exception strings. To use an empty exception string array, the empty array has to be given explicitly to not clash with the FixedWidthColumn(int, Alignment, char, char) constructor. Values that contain one of the given exception strings are considered invalid.

Parameters:
width - The width
alignment - The alignment
padLeft - The left padding character
padRight - The right padding character
exceptionStrings - The exception strings (may not contain null or empty strings!)
Method Detail

parse

public static FixedWidthColumn parse(String colSpec)
Parse a column from a column specification string.

The specification starts with the column width as a decimal number, followed by an optional LRTB for the alignment, followed by an optional slash followed by one or two padding characters. If two padding characters are given, another slash delimits exception strings (each one delimited by yet another slash). An empty exception list (but with the leading slash) is treated as no exceptions.

A missing parameter is treated as the default as used by the FixedWidthColumn(int) constructor.

Parameters:
colSpec - column specification
Returns:
the column

parseValue

public String parseValue(String value)
Parse a value from a fixed-width CSV file, i. e. remove padding and check for exception strings.

Parameters:
value - Value to parse
Returns:
Parsed value

formatValue

public String formatValue(String value,
                          boolean truncate,
                          boolean allowOverflow)
                   throws IOException
Format a value to store it into a fixed-width CSV file. The value is padded as needed. Overlong values can be truncated (on the right unless the column is right-aligned), left as is or cause an exception, based on your needs.

Parameters:
value - Value to format
truncate - Whether to silently truncate overlong values
allowOverflow - Whether to allow overflow for long values
Returns:
Formatted value
Throws:
IOException

getWidth

public int getWidth()
Get the width of this column.


toString

public String toString()
Overrides:
toString in class Object


Copyright © 2011. All Rights Reserved.