com.fasterxml.jackson.dataformat.csv
Class CsvParser

java.lang.Object
  extended by com.fasterxml.jackson.core.JsonParser
      extended by com.fasterxml.jackson.core.base.ParserMinimalBase
          extended by com.fasterxml.jackson.dataformat.csv.CsvParser
All Implemented Interfaces:
Versioned, Closeable

public class CsvParser
extends ParserMinimalBase

JsonParser implementation used to expose CSV documents in form that allows other Jackson functionality to deal with it.

Implementation is based on a state-machine that pulls information using CsvReader.


Nested Class Summary
static class CsvParser.Feature
          Enumeration that defines all togglable features for CSV parsers
 
Nested classes/interfaces inherited from class com.fasterxml.jackson.core.JsonParser
JsonParser.NumberType
 
Field Summary
protected  byte[] _binaryValue
          We will hold on to decoded binary data, for duration of current event, so that multiple calls to getBinaryValue(com.fasterxml.jackson.core.Base64Variant) will not need to decode data more than once.
protected  ByteArrayBuilder _byteArrayBuilder
           
protected  int _columnCount
          Number of columns defined by schema.
protected  int _columnIndex
          Index of the column we are exposing
protected  int _csvFeatures
           
protected  String _currentName
          Name of column that we exposed most recently, accessible after JsonToken.FIELD_NAME as well as value tokens immediately following field name.
protected  String _currentValue
          String value for the current column, if accessed.
protected  ObjectCodec _objectCodec
          Codec used for data binding when (if) requested.
protected  JsonReadContext _parsingContext
          Information about parser context, context in which the next token is to be parsed (root, array, object).
protected  CsvReader _reader
          Thing that actually reads the CSV content
protected  CsvSchema _schema
          Definition of columns being read.
protected  int _state
          Current logical state of the parser; one of STATE_ constants.
protected  TextBuffer _textBuffer
          Buffer that contains contents of all values after processing of doubled-quotes, escaped characters.
protected static int STATE_DOC_END
          State in which end marker is returned; either null (if no array wrapping), or JsonToken.END_ARRAY for wrapping.
protected static int STATE_DOC_START
          Initial state before anything is read from document.
protected static int STATE_NAMED_VALUE
          State in which value matching field name will be returned.
protected static int STATE_NEXT_ENTRY
          State in which next entry will be available, returning either JsonToken.FIELD_NAME or value (depending on whether entries are expressed as Objects or just Arrays); or matching close marker.
protected static int STATE_RECORD_START
          State before logical start of a record, in which next token to return will be JsonToken.START_OBJECT (or if no Schema is provided, JsonToken.START_ARRAY).
protected static int STATE_UNNAMED_VALUE
          State in which "unnamed" value (entry in an array) will be returned, if one available; otherwise end-array is returned.
 
Fields inherited from class com.fasterxml.jackson.core.base.ParserMinimalBase
_currToken, _lastClearedToken, INT_APOSTROPHE, INT_ASTERISK, INT_b, INT_BACKSLASH, INT_COLON, INT_COMMA, INT_CR, INT_f, INT_LBRACKET, INT_LCURLY, INT_LF, INT_n, INT_QUOTE, INT_r, INT_RBRACKET, INT_RCURLY, INT_SLASH, INT_SPACE, INT_t, INT_TAB, INT_u
 
Fields inherited from class com.fasterxml.jackson.core.JsonParser
_features
 
Constructor Summary
CsvParser(IOContext ctxt, BufferRecycler br, int parserFeatures, int csvFeatures, ObjectCodec codec, Reader reader)
           
 
Method Summary
 ByteArrayBuilder _getByteArrayBuilder()
           
protected  void _handleEOF()
           
protected  JsonToken _handleNamedValue()
           
protected  JsonToken _handleNextEntry()
           
protected  JsonToken _handleRecordStart()
           
protected  JsonToken _handleStartDoc()
          Method called to handle details of initializing things to return the very first token.
protected  JsonToken _handleUnnamedValue()
           
protected  void _readHeaderLine()
          Method called to process the expected header line
 void _reportCsvError(String msg)
           
 void _reportUnexpectedCsvChar(int ch, String msg)
           
 boolean canUseSchema(FormatSchema schema)
           
 void close()
           
 JsonParser configure(CsvParser.Feature f, boolean state)
          Method for enabling or disabling specified CSV feature (check CsvParser.Feature for list of features)
 JsonParser disable(CsvParser.Feature f)
          Method for disabling specified CSV feature (check CsvParser.Feature for list of features)
 JsonParser enable(CsvParser.Feature f)
          Method for enabling specified CSV feature (check CsvParser.Feature for list of features)
 BigInteger getBigIntegerValue()
           
 byte[] getBinaryValue(Base64Variant variant)
           
 ObjectCodec getCodec()
           
 JsonLocation getCurrentLocation()
           
 String getCurrentName()
           
 BigDecimal getDecimalValue()
           
 double getDoubleValue()
           
 Object getEmbeddedObject()
           
 float getFloatValue()
           
 Object getInputSource()
           
 int getIntValue()
           
 long getLongValue()
           
 JsonParser.NumberType getNumberType()
           
 Number getNumberValue()
           
 JsonStreamContext getParsingContext()
           
 CsvSchema getSchema()
          Accessor for getting active schema definition: it may be "empty" (no column definitions), but will never be null since it defaults to an empty schema (and default configuration)
 String getText()
           
 char[] getTextCharacters()
           
 int getTextLength()
           
 int getTextOffset()
           
 JsonLocation getTokenLocation()
           
 boolean hasTextCharacters()
           
 boolean isClosed()
           
 boolean isEnabled(CsvParser.Feature f)
          Method for checking whether specified CSV CsvParser.Feature is enabled.
 JsonToken nextToken()
           
 void overrideCurrentName(String name)
           
 int releaseBuffered(Writer out)
           
 void setCodec(ObjectCodec c)
           
 void setSchema(FormatSchema schema)
           
 Version version()
           
 
Methods inherited from class com.fasterxml.jackson.core.base.ParserMinimalBase
_constructError, _decodeBase64, _getCharDesc, _handleUnrecognizedCharacterEscape, _reportBase64EOF, _reportError, _reportInvalidBase64, _reportInvalidEOF, _reportInvalidEOF, _reportInvalidEOFInValue, _reportUnexpectedChar, _throwInternal, _throwInvalidSpace, _throwUnquotedSpace, _wrapError, clearCurrentToken, getCurrentToken, getLastClearedToken, getValueAsBoolean, getValueAsDouble, getValueAsInt, getValueAsLong, hasCurrentToken, nextValue, skipChildren
 
Methods inherited from class com.fasterxml.jackson.core.JsonParser
_constructError, configure, disable, enable, getBinaryValue, getBooleanValue, getByteValue, getShortValue, getValueAsBoolean, getValueAsDouble, getValueAsInt, getValueAsLong, isEnabled, isExpectedStartArrayToken, nextBooleanValue, nextFieldName, nextIntValue, nextLongValue, nextTextValue, readValueAs, readValueAs, readValueAsTree, readValuesAs, readValuesAs, releaseBuffered
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

STATE_DOC_START

protected static final int STATE_DOC_START
Initial state before anything is read from document.

See Also:
Constant Field Values

STATE_RECORD_START

protected static final int STATE_RECORD_START
State before logical start of a record, in which next token to return will be JsonToken.START_OBJECT (or if no Schema is provided, JsonToken.START_ARRAY).

See Also:
Constant Field Values

STATE_NEXT_ENTRY

protected static final int STATE_NEXT_ENTRY
State in which next entry will be available, returning either JsonToken.FIELD_NAME or value (depending on whether entries are expressed as Objects or just Arrays); or matching close marker.

See Also:
Constant Field Values

STATE_NAMED_VALUE

protected static final int STATE_NAMED_VALUE
State in which value matching field name will be returned.

See Also:
Constant Field Values

STATE_UNNAMED_VALUE

protected static final int STATE_UNNAMED_VALUE
State in which "unnamed" value (entry in an array) will be returned, if one available; otherwise end-array is returned.

See Also:
Constant Field Values

STATE_DOC_END

protected static final int STATE_DOC_END
State in which end marker is returned; either null (if no array wrapping), or JsonToken.END_ARRAY for wrapping. This step will loop, returning series of nulls if nextToken() is called multiple times.

See Also:
Constant Field Values

_objectCodec

protected ObjectCodec _objectCodec
Codec used for data binding when (if) requested.


_csvFeatures

protected int _csvFeatures

_schema

protected CsvSchema _schema
Definition of columns being read. Initialized to "empty" instance, which has default configuration settings.


_columnCount

protected int _columnCount
Number of columns defined by schema.


_parsingContext

protected JsonReadContext _parsingContext
Information about parser context, context in which the next token is to be parsed (root, array, object).


_currentName

protected String _currentName
Name of column that we exposed most recently, accessible after JsonToken.FIELD_NAME as well as value tokens immediately following field name.


_currentValue

protected String _currentValue
String value for the current column, if accessed.


_columnIndex

protected int _columnIndex
Index of the column we are exposing


_state

protected int _state
Current logical state of the parser; one of STATE_ constants.


_binaryValue

protected byte[] _binaryValue
We will hold on to decoded binary data, for duration of current event, so that multiple calls to getBinaryValue(com.fasterxml.jackson.core.Base64Variant) will not need to decode data more than once.


_reader

protected final CsvReader _reader
Thing that actually reads the CSV content


_textBuffer

protected final TextBuffer _textBuffer
Buffer that contains contents of all values after processing of doubled-quotes, escaped characters.


_byteArrayBuilder

protected ByteArrayBuilder _byteArrayBuilder
Constructor Detail

CsvParser

public CsvParser(IOContext ctxt,
                 BufferRecycler br,
                 int parserFeatures,
                 int csvFeatures,
                 ObjectCodec codec,
                 Reader reader)
Method Detail

version

public Version version()
Specified by:
version in interface Versioned
Overrides:
version in class ParserMinimalBase

getCodec

public ObjectCodec getCodec()
Specified by:
getCodec in class JsonParser

setCodec

public void setCodec(ObjectCodec c)
Specified by:
setCodec in class JsonParser

canUseSchema

public boolean canUseSchema(FormatSchema schema)
Overrides:
canUseSchema in class JsonParser

setSchema

public void setSchema(FormatSchema schema)
Overrides:
setSchema in class JsonParser

releaseBuffered

public int releaseBuffered(Writer out)
                    throws IOException
Overrides:
releaseBuffered in class JsonParser
Throws:
IOException

isClosed

public boolean isClosed()
Specified by:
isClosed in class ParserMinimalBase

close

public void close()
           throws IOException
Specified by:
close in interface Closeable
Specified by:
close in class ParserMinimalBase
Throws:
IOException

enable

public JsonParser enable(CsvParser.Feature f)
Method for enabling specified CSV feature (check CsvParser.Feature for list of features)


disable

public JsonParser disable(CsvParser.Feature f)
Method for disabling specified CSV feature (check CsvParser.Feature for list of features)


configure

public JsonParser configure(CsvParser.Feature f,
                            boolean state)
Method for enabling or disabling specified CSV feature (check CsvParser.Feature for list of features)


isEnabled

public boolean isEnabled(CsvParser.Feature f)
Method for checking whether specified CSV CsvParser.Feature is enabled.


getSchema

public CsvSchema getSchema()
Accessor for getting active schema definition: it may be "empty" (no column definitions), but will never be null since it defaults to an empty schema (and default configuration)

Overrides:
getSchema in class JsonParser

getParsingContext

public JsonStreamContext getParsingContext()
Specified by:
getParsingContext in class ParserMinimalBase

getTokenLocation

public JsonLocation getTokenLocation()
Specified by:
getTokenLocation in class JsonParser

getCurrentLocation

public JsonLocation getCurrentLocation()
Specified by:
getCurrentLocation in class JsonParser

getInputSource

public Object getInputSource()
Overrides:
getInputSource in class JsonParser

getCurrentName

public String getCurrentName()
                      throws IOException,
                             JsonParseException
Specified by:
getCurrentName in class ParserMinimalBase
Throws:
IOException
JsonParseException

overrideCurrentName

public void overrideCurrentName(String name)
Specified by:
overrideCurrentName in class ParserMinimalBase

nextToken

public JsonToken nextToken()
                    throws IOException,
                           JsonParseException
Specified by:
nextToken in class ParserMinimalBase
Throws:
IOException
JsonParseException

_handleStartDoc

protected JsonToken _handleStartDoc()
                             throws IOException,
                                    JsonParseException
Method called to handle details of initializing things to return the very first token.

Throws:
IOException
JsonParseException

_handleRecordStart

protected JsonToken _handleRecordStart()
                                throws IOException,
                                       JsonParseException
Throws:
IOException
JsonParseException

_handleNextEntry

protected JsonToken _handleNextEntry()
                              throws IOException,
                                     JsonParseException
Throws:
IOException
JsonParseException

_handleNamedValue

protected JsonToken _handleNamedValue()
                               throws IOException,
                                      JsonParseException
Throws:
IOException
JsonParseException

_handleUnnamedValue

protected JsonToken _handleUnnamedValue()
                                 throws IOException,
                                        JsonParseException
Throws:
IOException
JsonParseException

_readHeaderLine

protected void _readHeaderLine()
                        throws IOException,
                               JsonParseException
Method called to process the expected header line

Throws:
IOException
JsonParseException

hasTextCharacters

public boolean hasTextCharacters()
Specified by:
hasTextCharacters in class ParserMinimalBase

getText

public String getText()
               throws IOException,
                      JsonParseException
Specified by:
getText in class ParserMinimalBase
Throws:
IOException
JsonParseException

getTextCharacters

public char[] getTextCharacters()
                         throws IOException,
                                JsonParseException
Specified by:
getTextCharacters in class ParserMinimalBase
Throws:
IOException
JsonParseException

getTextLength

public int getTextLength()
                  throws IOException,
                         JsonParseException
Specified by:
getTextLength in class ParserMinimalBase
Throws:
IOException
JsonParseException

getTextOffset

public int getTextOffset()
                  throws IOException,
                         JsonParseException
Specified by:
getTextOffset in class ParserMinimalBase
Throws:
IOException
JsonParseException

getEmbeddedObject

public Object getEmbeddedObject()
                         throws IOException,
                                JsonParseException
Specified by:
getEmbeddedObject in class JsonParser
Throws:
IOException
JsonParseException

getBinaryValue

public byte[] getBinaryValue(Base64Variant variant)
                      throws IOException,
                             JsonParseException
Specified by:
getBinaryValue in class ParserMinimalBase
Throws:
IOException
JsonParseException

getNumberType

public JsonParser.NumberType getNumberType()
                                    throws IOException,
                                           JsonParseException
Specified by:
getNumberType in class JsonParser
Throws:
IOException
JsonParseException

getNumberValue

public Number getNumberValue()
                      throws IOException,
                             JsonParseException
Specified by:
getNumberValue in class JsonParser
Throws:
IOException
JsonParseException

getIntValue

public int getIntValue()
                throws IOException,
                       JsonParseException
Specified by:
getIntValue in class JsonParser
Throws:
IOException
JsonParseException

getLongValue

public long getLongValue()
                  throws IOException,
                         JsonParseException
Specified by:
getLongValue in class JsonParser
Throws:
IOException
JsonParseException

getBigIntegerValue

public BigInteger getBigIntegerValue()
                              throws IOException,
                                     JsonParseException
Specified by:
getBigIntegerValue in class JsonParser
Throws:
IOException
JsonParseException

getFloatValue

public float getFloatValue()
                    throws IOException,
                           JsonParseException
Specified by:
getFloatValue in class JsonParser
Throws:
IOException
JsonParseException

getDoubleValue

public double getDoubleValue()
                      throws IOException,
                             JsonParseException
Specified by:
getDoubleValue in class JsonParser
Throws:
IOException
JsonParseException

getDecimalValue

public BigDecimal getDecimalValue()
                           throws IOException,
                                  JsonParseException
Specified by:
getDecimalValue in class JsonParser
Throws:
IOException
JsonParseException

_handleEOF

protected void _handleEOF()
                   throws JsonParseException
Specified by:
_handleEOF in class ParserMinimalBase
Throws:
JsonParseException

_reportCsvError

public void _reportCsvError(String msg)
                     throws JsonParseException
Throws:
JsonParseException

_reportUnexpectedCsvChar

public void _reportUnexpectedCsvChar(int ch,
                                     String msg)
                              throws JsonParseException
Throws:
JsonParseException

_getByteArrayBuilder

public ByteArrayBuilder _getByteArrayBuilder()


Copyright © 2012 fasterxml.com. All Rights Reserved.