com.sas.util.transforms
Class CSVStreamToTableModelTransform

com.sas.util.transforms.CSVStreamToTableModelTransform
All Implemented Interfaces:
com.sas.util.transforms.TransformInterface, java.io.Serializable

public class CSVStreamToTableModelTransform
implements com.sas.util.transforms.TransformInterface

The CSVStreamToTableModelTransform allows a CSV stream (from a file or other source) to be read in and converted into a model for JTable (or any other viewer which uses a TableModel as its model).

The current implementation reads the entire contents of the CSV stream and creates a DefaultTableModel with the data.

 CSVStreamToTableModelTransform transform = new CSVStreamToTableModelTransform();
 TableModel model = transform.transform( new FileInputStream( "c:\\file.csv" ) );
 TableView tableView = new TableView();
 tableView.setCellSpacing(0);
 tableView.setCellPadding(2);
 tableView.setBorderWidth(1);
 tableView.setColumnHeaderVisible(true);
 tableView.setModel( model );
 tableView.write( writer );
 

See Also:
Serialized Form

Field Summary
protected  java.lang.String[] columnHeaders
           
protected  java.lang.String delimiters
           
protected  java.lang.String encoding
           
protected static java.lang.String localeDefaultDelimiter
           
 
Constructor Summary
CSVStreamToTableModelTransform()
          Default constructor which uses the default comma delimiter.
CSVStreamToTableModelTransform(java.lang.String delimiters)
          Constructor which allows the delimiters to be specified.
CSVStreamToTableModelTransform(java.lang.String[] columnHeaders, java.lang.String delimiters, java.lang.String encoding)
          Constructor which allows the column headersm, delimiters and encoding to be specified.
CSVStreamToTableModelTransform(java.lang.String delimiters, java.lang.String encoding)
          Constructor which allows the delimiters to be specified.
 
Method Summary
protected  java.lang.String[] parseLineToArray(java.lang.String line)
          Parse a single delimited line into an array of Strings.
 java.lang.Object transform(java.lang.Object input)
          Creates a DefaultTableModel which contains the contents of the CSV stream.
 

Field Detail

localeDefaultDelimiter

protected static final java.lang.String localeDefaultDelimiter

delimiters

protected java.lang.String delimiters

encoding

protected java.lang.String encoding

columnHeaders

protected java.lang.String[] columnHeaders
Constructor Detail

CSVStreamToTableModelTransform

public CSVStreamToTableModelTransform()
Default constructor which uses the default comma delimiter.


CSVStreamToTableModelTransform

public CSVStreamToTableModelTransform(java.lang.String delimiters)
Constructor which allows the delimiters to be specified.

Parameters:
delimiters - A string containing the characters used to delimit values on each line. null indicates that the default delimiters will be used.

CSVStreamToTableModelTransform

public CSVStreamToTableModelTransform(java.lang.String delimiters,
                                      java.lang.String encoding)
Constructor which allows the delimiters to be specified.

Parameters:
delimiters - A string containing the characters used to delimit values on each line. null indicates that the default delimiters will be used.
encoding - The encoding to use with the InputStreamReader when reading the stream. null indicates that the default encoding for the JRE will be used.

CSVStreamToTableModelTransform

public CSVStreamToTableModelTransform(java.lang.String[] columnHeaders,
                                      java.lang.String delimiters,
                                      java.lang.String encoding)
Constructor which allows the column headersm, delimiters and encoding to be specified.

Parameters:
columnHeaders - The text to use for each of the column headings. If null, the first line of the CSV stream will be used as the column headers.
delimiters - A string containing the characters used to delimit values on each line. null indicates that the default delimiters will be used.
encoding - The encoding to use with the InputStreamReader when reading the stream. null indicates that the default encoding for the JRE will be used.
Method Detail

transform

public java.lang.Object transform(java.lang.Object input)
                           throws com.sas.util.transforms.TransformException
Creates a DefaultTableModel which contains the contents of the CSV stream. param input A java.io.InputStream to read the CSV from.

Specified by:
transform in interface com.sas.util.transforms.TransformInterface
Returns:
An object which implements the TableModel interface and contains the data from stream.
Throws:
com.sas.util.transforms.TransformException

parseLineToArray

protected java.lang.String[] parseLineToArray(java.lang.String line)
Parse a single delimited line into an array of Strings.

Parameters:
line - The line of text to parse the row of values from.
Returns:
A artray of Strings containing the values.



Copyright © 2009 SAS Institute Inc. All Rights Reserved.