de.hpi.fgis.dude.datasource
Class CSVSource

java.lang.Object
  extended by de.hpi.fgis.dude.util.AbstractCleanable
      extended by de.hpi.fgis.dude.datasource.AbstractDataSource<CSVSource>
          extended by de.hpi.fgis.dude.datasource.CSVSource
All Implemented Interfaces:
DataSource, Cleanable, AutoJsonable, Jsonable, JsonableReader<DuDeObject>, Closeable, Iterable<DuDeObject>

public class CSVSource
extends AbstractDataSource<CSVSource>
implements Jsonable

CSVSource represents *.csv files.

Author:
Matthias Pohl

Nested Class Summary
protected  class CSVSource.CSVSourceIterator
          CSVSourceIterator is used for generating DuDeObjects out of CSVSources.
 
Nested classes/interfaces inherited from class de.hpi.fgis.dude.datasource.AbstractDataSource
AbstractDataSource.AbstractDataSourceIterator<T extends AbstractDataSource<?>>
 
Constructor Summary
protected CSVSource()
          Internal constructor for Jsonable deserialization.
  CSVSource(String identifier, File file)
          Initializes a CSVSource.
  CSVSource(String identifier, File file, String... colNames)
          Initializes a CSVSource with column names.
 
Method Summary
 void disableHeader()
          Disables header reading.
 void enableHeader()
          Enables header reading.
 void fromJson(DuDeJsonParser<?> jsonParser)
          Initializes the current instance using the passed DuDeJsonParser.
protected  String[] getColumnNames()
          Returns the column names or null, if no column names were set.
protected  String getFilePath()
          Returns the path of the CSV file.
 char getQuoteCharacter()
          Returns the set quote character.
 char getSeparatorCharacter()
          Returns the set separator character.
 Iterator<DuDeObject> iterator()
           
 boolean readHeader()
          Checks whether interpreting the first line as header is enabled.
 void setQuoteCharacter(char quoteChar)
          Sets the quote character.
 void setSeparatorCharacter(char separatorChar)
          Sets the separator character.
 void toJson(DuDeJsonGenerator jsonGenerator)
          Generates the Json code using the passed DuDeJsonGenerator.
 CSVSource withHeader()
          By calling this method the extractor will interpret the first line as a header.
 CSVSource withQuoteCharacter(char quChar)
          Set the quote character that is used in the file.
 CSVSource withSeparatorCharacter(char sepChar)
          Sets the separator character that is used in the file.
 
Methods inherited from class de.hpi.fgis.dude.datasource.AbstractDataSource
addIdAttributes, autoGeneratedIds, close, equals, getExtractedRecordCount, getIdAttributes, getIdentifier, hashCode, setExtractedRecordCount, toString, withIdAttributes
 
Methods inherited from class de.hpi.fgis.dude.util.AbstractCleanable
cleanUp, registerCleanable, registerCloseable
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface de.hpi.fgis.dude.util.Cleanable
cleanUp, registerCleanable, registerCloseable
 

Constructor Detail

CSVSource

protected CSVSource()
Internal constructor for Jsonable deserialization.


CSVSource

public CSVSource(String identifier,
                 File file)
          throws FileNotFoundException
Initializes a CSVSource.

Parameters:
identifier - The identifier of the DataSource instance.
file - The file of which the data will be extracted.
Throws:
FileNotFoundException - If the passed file does not exist.

CSVSource

public CSVSource(String identifier,
                 File file,
                 String... colNames)
          throws FileNotFoundException
Initializes a CSVSource with column names.

Parameters:
identifier - The identifier of the DataSource instance.
file - The file of which the data will be extracted.
colNames - The column names.
Throws:
FileNotFoundException - If the passed file does not exist.
Method Detail

getQuoteCharacter

public char getQuoteCharacter()
Returns the set quote character.

Returns:
The set quote character.

setQuoteCharacter

public void setQuoteCharacter(char quoteChar)
Sets the quote character.

Parameters:
quoteChar - The quote character to set.

getSeparatorCharacter

public char getSeparatorCharacter()
Returns the set separator character.

Returns:
The set separator character.

setSeparatorCharacter

public void setSeparatorCharacter(char separatorChar)
Sets the separator character.

Parameters:
separatorChar - The separator character to set.

disableHeader

public void disableHeader()
Disables header reading. The flag needs to be disabled, if the first line does not contain the column names.


enableHeader

public void enableHeader()
Enables header reading. The flag needs to be enabled, if the first line contains the column names.


readHeader

public boolean readHeader()
Checks whether interpreting the first line as header is enabled.

Returns:
true, if the header flag is enabled; otherwise false.

withHeader

public CSVSource withHeader()
By calling this method the extractor will interpret the first line as a header.

Returns:
The current instance.
See Also:
enableHeader()

withQuoteCharacter

public CSVSource withQuoteCharacter(char quChar)
Set the quote character that is used in the file.

Parameters:
quChar - The quote character.
Returns:
The current instance.
See Also:
setQuoteCharacter(char)

withSeparatorCharacter

public CSVSource withSeparatorCharacter(char sepChar)
Sets the separator character that is used in the file.

Parameters:
sepChar - The separator character.
Returns:
The current instance.
See Also:
setSeparatorCharacter(char)

iterator

public Iterator<DuDeObject> iterator()
Specified by:
iterator in interface Iterable<DuDeObject>
Specified by:
iterator in class AbstractDataSource<CSVSource>

getFilePath

protected String getFilePath()
Returns the path of the CSV file.

Returns:
The path of the CSV file.

getColumnNames

protected String[] getColumnNames()
Returns the column names or null, if no column names were set.

Returns:
The column names.

toJson

public void toJson(DuDeJsonGenerator jsonGenerator)
            throws org.codehaus.jackson.JsonGenerationException,
                   IOException
Description copied from interface: Jsonable
Generates the Json code using the passed DuDeJsonGenerator.

Specified by:
toJson in interface Jsonable
Parameters:
jsonGenerator - The DuDeJsonGenerator that is used internally.
Throws:
org.codehaus.jackson.JsonGenerationException - If an error occurs while generating the Json syntax.
IOException - If an error occurs while writing to the output.

fromJson

public void fromJson(DuDeJsonParser<?> jsonParser)
              throws org.codehaus.jackson.JsonParseException,
                     IOException
Description copied from interface: Jsonable
Initializes the current instance using the passed DuDeJsonParser.

Specified by:
fromJson in interface Jsonable
Parameters:
jsonParser - The parser that is used for extracting the data out of the Json.
Throws:
org.codehaus.jackson.JsonParseException - If an error occurs while parsing the Json.
IOException - If an error occurs while reading from the stream.


Copyright © 2011 Hasso Plattner Institute - Chair of Information Systems. All Rights Reserved.