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 theRecordReaderimplementation 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.StringaddFieldError(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.StringaddRecordError(java.lang.String rule, java.lang.Object... params)Adds a record level error to this record.protected java.lang.StringaddRecordError(ErrorContext errorContext, java.lang.String rule, java.lang.Object... params)Adds a record level error to this record.intgetLineNumber()Returns the last line number read from the input stream.java.util.LocalegetLocale()Returns the locale to format error messages in.MessageFactorygetMessageFactory()Returns theMessageFactoryfor formatting error messages.chargetMode()Returns the parsing mode.RecordContextgetRecordContext(int index)Returns the record context for a record read for the last unmarshalled bean object.intgetRecordCount()Returns the number of record read for the last unmarshalled bean object.RecordReadergetRecordReader()Returns theRecordReaderto read from.TextLengthCountergetTextLengthCounter()booleanhasFieldErrors()Returnstrueif a field error was reported while parsing this record.booleanhasRecordErrors()Returnstrueif a record level error was reported while parsing this record.booleanisEOF()Returns whether the end of the stream was reached afternextRecord()was called.BeanReaderExceptionnewMalformedRecordException(RecordIOException cause)BeanReaderExceptionnewUnsatisfiedGroupException(java.lang.String groupName)BeanReaderExceptionnewUnsatisfiedRecordException(java.lang.String recordName)voidnextRecord()Reads the next record from the input stream and callssetRecordValue(Object).voidprepare(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).voidrecordCompleted()Either this method (orrecordSkipped()) must be invoked after a record is unmarshalled, even if an error has occurred.protected ErrorContextrecordException(java.lang.String recordName, java.lang.String rule, java.lang.Object... params)Handles a record level exception and returns a newErrorContextfor the exception.voidrecordSkipped()This method should be invoked when a record is skipped.voidrecordStarted(java.lang.String recordName)This method must be invoked before a record is unmarshalled.BeanReaderExceptionrecordUnexpectedException(java.lang.String recordName)BeanReaderExceptionrecordUnidentifiedException()voidsetFieldText(java.lang.String fieldName, java.lang.String text)Sets the raw field text for a named field.voidsetLocale(java.util.Locale locale)Sets the locale to format error messages in.voidsetMessageFactory(MessageFactory messageFactory)Sets theMessageFactoryfor formatting error messages.voidsetRecordReader(RecordReader recordReader)Sets theRecordReaderto read from.abstract voidsetRecordValue(java.lang.Object value)Sets the value of the record returned from theRecordReaderjava.lang.ObjecttoRecordValue(java.lang.String[] array)Converts aString[]to a record value.java.lang.ObjecttoRecordValue(java.util.List<java.lang.String> list)Converts aListto a record value.java.lang.ObjecttoRecordValue(org.w3c.dom.Node node)Converts aNodeto a record value.voidvalidate()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:ParsingContextReturns the parsing mode.- Specified by:
getModein classParsingContext- Returns:
- either
ParsingContext.UNMARSHALLINGorParsingContext.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 aListto a record value.- Parameters:
list- theListto convert- Returns:
- the record value, or null if not supported
-
toRecordValue
public java.lang.Object toRecordValue(org.w3c.dom.Node node)
Converts aNodeto a record value.- Parameters:
node- theNodeto 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 InvalidRecordExceptionValidates 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()
Returnstrueif a field error was reported while parsing this record.- Returns:
trueif a field error was reported
-
hasRecordErrors
public final boolean hasRecordErrors()
Returnstrueif a record level error was reported while parsing this record.- Returns:
trueif 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 newErrorContextfor 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 BeanReaderExceptionReads 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 theRecordReaderto read from.- Returns:
- the
RecordReaderto read from
-
setRecordReader
public final void setRecordReader(RecordReader recordReader)
Sets theRecordReaderto read from.- Parameters:
recordReader- theRecordReaderto read from
-
getMessageFactory
public MessageFactory getMessageFactory()
Returns theMessageFactoryfor formatting error messages.- Returns:
- the
MessageFactory
-
setMessageFactory
public void setMessageFactory(MessageFactory messageFactory)
Sets theMessageFactoryfor 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
-
-