BeanIO 2.0.4

org.beanio.internal.parser
Class UnmarshallingContext

java.lang.Object
  extended by org.beanio.internal.parser.ParsingContext
      extended by org.beanio.internal.parser.UnmarshallingContext
Direct Known Subclasses:
DelimitedUnmarshallingContext, FixedLengthUnmarshallingContext, JsonUnmarshallingContext, XmlUnmarshallingContext

public abstract class UnmarshallingContext
extends ParsingContext

Stores context information needed to unmarshal a bean object.

Subclasses must implement setRecordValue(Object) which is called by nextRecord() each time a new record is read from the RecordReader. The Java object used to represent a record is dependent on the RecordReader implementation for the stream format.

Since:
2.0
Author:
Kevin Seim

Field Summary
 
Fields inherited from class org.beanio.internal.parser.ParsingContext
MARSHALLING, UNMARSHALLING
 
Constructor Summary
UnmarshallingContext()
          Constructs a new UnmarshallingContext.
 
Method Summary
 String addFieldError(String fieldName, String fieldText, String rule, Object... params)
          Adds a field error to this record.
protected  String addRecordError(ErrorContext errorContext, String rule, Object... params)
          Adds a record level error to this record.
 String addRecordError(String rule, Object... params)
          Adds a record level error to this record.
 int getLineNumber()
          Returns the last line number read from the input stream.
 Locale getLocale()
          Returns the locale to format error messages in.
 MessageFactory getMessageFactory()
          Returns the MessageFactory for formatting error messages.
 char getMode()
          Returns the parsing mode.
 RecordContext getRecordContext(int index)
          Returns the record context for a record read for the last unmarshalled bean object.
 int getRecordCount()
          Returns the number of record read for the last unmarshalled bean object.
 RecordReader getRecordReader()
          Returns the RecordReader to read from.
 boolean hasFieldErrors()
          Returns true if a field error was reported while parsing this record.
 boolean hasRecordErrors()
          Returns true if a record level error was reported while parsing this record.
 boolean isEOF()
          Returns whether the end of the stream was reached after nextRecord() was called.
 BeanReaderException newMalformedRecordException(RecordIOException cause)
           
 BeanReaderException newUnsatisfiedGroupException(String groupName)
           
 BeanReaderException newUnsatisfiedRecordException(String recordName)
           
 void nextRecord()
          Reads the next record from the input stream and calls setRecordValue(Object).
 void prepare(String componentName, boolean isRecordGroup)
          Prepares this context for unmarshalling a record (or group of records that are combined to form a single bean object).
 void recordCompleted()
          Either this method (or recordSkipped()) must be invoked after a record is unmarshalled, even if an error has occurred.
protected  ErrorContext recordException(String recordName, String rule, Object... params)
          Handles a record level exception and returns a new ErrorContext for the exception.
 void recordSkipped()
          This method should be invoked when a record is skipped.
 void recordStarted(String recordName)
          This method must be invoked before a record is unmarshalled.
 BeanReaderException recordUnexpectedException(String recordName)
           
 BeanReaderException recordUnidentifiedException()
           
 void setFieldText(String fieldName, String text)
          Sets the raw field text for a named field.
 void setLocale(Locale locale)
          Sets the locale to format error messages in.
 void setMessageFactory(MessageFactory messageFactory)
          Sets the MessageFactory for formatting error messages.
 void setRecordReader(RecordReader recordReader)
          Sets the RecordReader to read from.
abstract  void setRecordValue(Object value)
          Sets the value of the record returned from the RecordReader
 Object toRecordValue(List<String> list)
          Converts a List to a record value.
 Object toRecordValue(Node node)
          Converts a Node to a record value.
 Object toRecordValue(String[] array)
          Converts a String[] to a record value.
 void validate()
          Validates all unmarshalled records and throws an exception if any record is invalid.
 
Methods inherited from class org.beanio.internal.parser.ParsingContext
createHeap, getAdjustedFieldPosition, getLocal, getRelativeFieldIndex, isRepeating, popIteration, pushIteration, setLocal
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

UnmarshallingContext

public UnmarshallingContext()
Constructs a new UnmarshallingContext.

Method Detail

getMode

public final char getMode()
Description copied from class: ParsingContext
Returns the parsing mode.

Specified by:
getMode in class ParsingContext
Returns:
either ParsingContext.UNMARSHALLING or ParsingContext.MARSHALLING

setRecordValue

public abstract void setRecordValue(Object value)
Sets the value of the record returned from the RecordReader

Parameters:
value - the record value read by a record reader
See Also:
RecordReader

toRecordValue

public Object toRecordValue(String[] array)
Converts a String[] to a record value.

Parameters:
array - the String[] to convert
Returns:
the record value, or null if not supported

toRecordValue

public Object toRecordValue(List<String> list)
Converts a List to a record value.

Parameters:
list - the List to convert
Returns:
the record value, or null if not supported

toRecordValue

public Object toRecordValue(Node node)
Converts a Node to a record value.

Parameters:
node - the Node to convert
Returns:
the record value, or null if not supported

prepare

public void prepare(String componentName,
                    boolean isRecordGroup)
Prepares this context for unmarshalling a record (or group of records that are combined to form a single bean object).

Parameters:
componentName - the record or group name to be unmarshalled
isRecordGroup - true if the component is a group, false if it is a record

recordStarted

public final void recordStarted(String recordName)
This method must be invoked before a record is unmarshalled.

Parameters:
recordName - the name of the record

recordCompleted

public final void recordCompleted()
Either this method (or recordSkipped()) must be invoked after a record is unmarshalled, even if an error has occurred.


recordSkipped

public final void recordSkipped()
This method should be invoked when a record is skipped.


validate

public final void validate()
                    throws InvalidRecordException
Validates all unmarshalled records and throws an exception if any record is invalid. This method must be invoked after unmarshalling is completed. If unmarshalling fails due to some other fatal exception, there is no need to call this method.

Throws:
InvalidRecordException - if one or more unmarshalled records were invalid

getRecordCount

public final int getRecordCount()
Returns the number of record read for the last unmarshalled bean object.

Returns:
the record count for the last unmarshalled bean object

getRecordContext

public final RecordContext getRecordContext(int index)
                                     throws IndexOutOfBoundsException
Returns the record context for a record read for the last unmarshalled bean object.

Parameters:
index - the index of the record
Returns:
the RecordContext
Throws:
IndexOutOfBoundsException - if there is no record for the given index

setFieldText

public final void setFieldText(String fieldName,
                               String text)
Sets the raw field text for a named field.

Parameters:
fieldName - the name of the field
text - the raw field text

hasFieldErrors

public final boolean hasFieldErrors()
Returns true if a field error was reported while parsing this record.

Returns:
true if a field error was reported

hasRecordErrors

public final boolean hasRecordErrors()
Returns true if a record level error was reported while parsing this record.

Returns:
true if a record level error was reported

addFieldError

public String addFieldError(String fieldName,
                            String fieldText,
                            String rule,
                            Object... params)
Adds a field error to this record.

Parameters:
fieldName - the name of the field in error
fieldText - the invalid field text
rule - the name of the failed validation rule
params - an optional list of parameters for formatting the error message
Returns:
the formatted field error message

addRecordError

public final String addRecordError(String rule,
                                   Object... params)
Adds a record level error to this record.

Parameters:
rule - the name of the failed validation rule
params - an optional list of parameters for formatting the error message
Returns:
the formatted record error message

addRecordError

protected String addRecordError(ErrorContext errorContext,
                                String rule,
                                Object... params)
Adds a record level error to this record.

Parameters:
errorContext - the error context to update
rule - the name of the failed validation rule
params - an optional list of parameters for formatting the error message
Returns:
the formatted record error message

newMalformedRecordException

public BeanReaderException newMalformedRecordException(RecordIOException cause)

newUnsatisfiedGroupException

public BeanReaderException newUnsatisfiedGroupException(String groupName)

newUnsatisfiedRecordException

public BeanReaderException newUnsatisfiedRecordException(String recordName)

recordUnexpectedException

public BeanReaderException recordUnexpectedException(String recordName)

recordUnidentifiedException

public BeanReaderException recordUnidentifiedException()

recordException

protected ErrorContext recordException(String recordName,
                                       String rule,
                                       Object... params)
Handles a record level exception and returns a new ErrorContext for the exception.

Parameters:
recordName - the name of the record that failed
rule - the record level rule that failed validation
params - message parameters for formatting the error message
Returns:
the created ErrorContext

nextRecord

public final void nextRecord()
                      throws BeanReaderException
Reads the next record from the input stream and calls setRecordValue(Object).

Throws:
BeanReaderException - if the next node cannot be determined

getLineNumber

public final int getLineNumber()
Returns the last line number read from the input stream. If the end of stream was reached, the line number is still incremented so that this method returns the expected line number if another record was read. If newlines are not used to terminate records, this method will always return zero.

Returns:
the line number

isEOF

public final boolean isEOF()
Returns whether the end of the stream was reached after nextRecord() was called.

Returns:
true if the end of the stream was reached

getRecordReader

public final RecordReader getRecordReader()
Returns the RecordReader to read from.

Returns:
the RecordReader to read from

setRecordReader

public final void setRecordReader(RecordReader recordReader)
Sets the RecordReader to read from.

Parameters:
recordReader - the RecordReader to read from

getMessageFactory

public MessageFactory getMessageFactory()
Returns the MessageFactory for formatting error messages.

Returns:
the MessageFactory

setMessageFactory

public void setMessageFactory(MessageFactory messageFactory)
Sets the MessageFactory for formatting error messages.

Parameters:
messageFactory - the MessageFactory

getLocale

public Locale getLocale()
Returns the locale to format error messages in.

Returns:
the Locale

setLocale

public void setLocale(Locale locale)
Sets the locale to format error messages in.

Parameters:
locale - the Locale

BeanIO 2.0.4

Copyright © 2010-2013 Kevin Seim