Package org.beanio.internal.parser
Class UnmarshallingContext
- java.lang.Object
-
- org.beanio.internal.parser.ParsingContext
-
- 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 bynextRecord()
each time a new record is read from theRecordReader
. The Java object used to represent a record is dependent on theRecordReader
implementation for the stream format.- Since:
- 2.0
-
-
Field Summary
-
Fields inherited from class org.beanio.internal.parser.ParsingContext
MARSHALLING, UNMARSHALLING
-
-
Constructor Summary
Constructors Constructor Description UnmarshallingContext()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description java.lang.String
addFieldError(java.lang.String fieldName, java.lang.String fieldText, java.lang.String rule, java.lang.Object... params)
Adds a field error to this record.java.lang.String
addRecordError(java.lang.String rule, java.lang.Object... params)
Adds a record level error to this record.protected java.lang.String
addRecordError(ErrorContext errorContext, java.lang.String rule, java.lang.Object... params)
Adds a record level error to this record.int
getLineNumber()
Returns the last line number read from the input stream.java.util.Locale
getLocale()
Returns the locale to format error messages in.MessageFactory
getMessageFactory()
Returns theMessageFactory
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 theRecordReader
to read from.TextLengthCounter
getTextLengthCounter()
boolean
hasFieldErrors()
Returnstrue
if a field error was reported while parsing this record.boolean
hasRecordErrors()
Returnstrue
if a record level error was reported while parsing this record.boolean
isEOF()
Returns whether the end of the stream was reached afternextRecord()
was called.BeanReaderException
newMalformedRecordException(RecordIOException cause)
BeanReaderException
newUnsatisfiedGroupException(java.lang.String groupName)
BeanReaderException
newUnsatisfiedRecordException(java.lang.String recordName)
void
nextRecord()
Reads the next record from the input stream and callssetRecordValue(Object)
.void
prepare(java.lang.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 (orrecordSkipped()
) must be invoked after a record is unmarshalled, even if an error has occurred.protected ErrorContext
recordException(java.lang.String recordName, java.lang.String rule, java.lang.Object... params)
Handles a record level exception and returns a newErrorContext
for the exception.void
recordSkipped()
This method should be invoked when a record is skipped.void
recordStarted(java.lang.String recordName)
This method must be invoked before a record is unmarshalled.BeanReaderException
recordUnexpectedException(java.lang.String recordName)
BeanReaderException
recordUnidentifiedException()
void
setFieldText(java.lang.String fieldName, java.lang.String text)
Sets the raw field text for a named field.void
setLocale(java.util.Locale locale)
Sets the locale to format error messages in.void
setMessageFactory(MessageFactory messageFactory)
Sets theMessageFactory
for formatting error messages.void
setRecordReader(RecordReader recordReader)
Sets theRecordReader
to read from.abstract void
setRecordValue(java.lang.Object value)
Sets the value of the record returned from theRecordReader
java.lang.Object
toRecordValue(java.lang.String[] array)
Converts aString[]
to a record value.java.lang.Object
toRecordValue(java.util.List<java.lang.String> list)
Converts aList
to a record value.java.lang.Object
toRecordValue(org.w3c.dom.Node node)
Converts aNode
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
clear, createHeap, getAdjustedFieldPosition, getLocal, getRelativeFieldIndex, isRepeating, popIteration, pushIteration, setLocal
-
-
-
-
Method Detail
-
getMode
public final char getMode()
Description copied from class:ParsingContext
Returns the parsing mode.- Specified by:
getMode
in classParsingContext
- Returns:
- either
ParsingContext.UNMARSHALLING
orParsingContext.MARSHALLING
-
setRecordValue
public abstract void setRecordValue(java.lang.Object value)
Sets the value of the record returned from theRecordReader
- Parameters:
value
- the record value read by a record reader- See Also:
RecordReader
-
toRecordValue
public java.lang.Object toRecordValue(java.lang.String[] array)
Converts aString[]
to a record value.- Parameters:
array
- theString[]
to convert- Returns:
- the record value, or null if not supported
-
toRecordValue
public java.lang.Object toRecordValue(java.util.List<java.lang.String> list)
Converts aList
to a record value.- Parameters:
list
- theList
to convert- Returns:
- the record value, or null if not supported
-
toRecordValue
public java.lang.Object toRecordValue(org.w3c.dom.Node node)
Converts aNode
to a record value.- Parameters:
node
- theNode
to convert- Returns:
- the record value, or null if not supported
-
prepare
public void prepare(java.lang.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 unmarshalledisRecordGroup
- true if the component is a group, false if it is a record
-
recordStarted
public final void recordStarted(java.lang.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 (orrecordSkipped()
) 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 java.lang.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:
java.lang.IndexOutOfBoundsException
- if there is no record for the given index
-
setFieldText
public final void setFieldText(java.lang.String fieldName, java.lang.String text)
Sets the raw field text for a named field.- Parameters:
fieldName
- the name of the fieldtext
- the raw field text
-
getTextLengthCounter
public TextLengthCounter getTextLengthCounter()
-
hasFieldErrors
public final boolean hasFieldErrors()
Returnstrue
if a field error was reported while parsing this record.- Returns:
true
if a field error was reported
-
hasRecordErrors
public final boolean hasRecordErrors()
Returnstrue
if a record level error was reported while parsing this record.- Returns:
true
if a record level error was reported
-
addFieldError
public java.lang.String addFieldError(java.lang.String fieldName, java.lang.String fieldText, java.lang.String rule, java.lang.Object... params)
Adds a field error to this record.- Parameters:
fieldName
- the name of the field in errorfieldText
- the invalid field textrule
- the name of the failed validation ruleparams
- an optional list of parameters for formatting the error message- Returns:
- the formatted field error message
-
addRecordError
public final java.lang.String addRecordError(java.lang.String rule, java.lang.Object... params)
Adds a record level error to this record.- Parameters:
rule
- the name of the failed validation ruleparams
- an optional list of parameters for formatting the error message- Returns:
- the formatted record error message
-
addRecordError
protected java.lang.String addRecordError(ErrorContext errorContext, java.lang.String rule, java.lang.Object... params)
Adds a record level error to this record.- Parameters:
errorContext
- the error context to updaterule
- the name of the failed validation ruleparams
- 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(java.lang.String groupName)
-
newUnsatisfiedRecordException
public BeanReaderException newUnsatisfiedRecordException(java.lang.String recordName)
-
recordUnexpectedException
public BeanReaderException recordUnexpectedException(java.lang.String recordName)
-
recordUnidentifiedException
public BeanReaderException recordUnidentifiedException()
-
recordException
protected ErrorContext recordException(java.lang.String recordName, java.lang.String rule, java.lang.Object... params)
Handles a record level exception and returns a newErrorContext
for the exception.- Parameters:
recordName
- the name of the record that failedrule
- the record level rule that failed validationparams
- 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 callssetRecordValue(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 afternextRecord()
was called.- Returns:
- true if the end of the stream was reached
-
getRecordReader
public final RecordReader getRecordReader()
Returns theRecordReader
to read from.- Returns:
- the
RecordReader
to read from
-
setRecordReader
public final void setRecordReader(RecordReader recordReader)
Sets theRecordReader
to read from.- Parameters:
recordReader
- theRecordReader
to read from
-
getMessageFactory
public MessageFactory getMessageFactory()
Returns theMessageFactory
for formatting error messages.- Returns:
- the
MessageFactory
-
setMessageFactory
public void setMessageFactory(MessageFactory messageFactory)
Sets theMessageFactory
for formatting error messages.- Parameters:
messageFactory
- theMessageFactory
-
getLocale
public java.util.Locale getLocale()
Returns the locale to format error messages in.- Returns:
- the
Locale
-
setLocale
public void setLocale(java.util.Locale locale)
Sets the locale to format error messages in.- Parameters:
locale
- theLocale
-
-