Package org.beanio.internal.parser
Class Field
- java.lang.Object
-
- org.beanio.internal.util.TreeNode<Component>
-
- org.beanio.internal.parser.Component
-
- org.beanio.internal.parser.ParserComponent
-
- org.beanio.internal.parser.Field
-
- All Implemented Interfaces:
java.lang.Cloneable
,java.lang.Iterable<Component>
,Parser
,Property
,Replicateable
public class Field extends ParserComponent implements Property
A parser for marshalling and unmarshalling a single field in a record. A field is usually, but optionally, bound to a simple property value.A field component does not have any children.
- Since:
- 2.0
-
-
Field Summary
-
Fields inherited from interface org.beanio.internal.parser.Property
AGGREGATION_ARRAY, AGGREGATION_COLLECTION, AGGREGATION_MAP, COLLECTION, COMPLEX, MAP, SIMPLE
-
-
Constructor Summary
Constructors Constructor Description Field()
Constructs a newField
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clearValue(ParsingContext context)
Clears the property value.java.lang.Object
createValue(ParsingContext context)
Creates the property value and returns it.boolean
defines(java.lang.Object value)
protected java.lang.String
formatValue(java.lang.Object value)
Formats a field/property value.PropertyAccessor
getAccessor()
java.lang.Object
getDefaultValue()
Returns the default value for a field parsed by this field definition when the field text is null or the empty string (after trimming).FieldFormat
getFormat()
TypeHandler
getHandler()
java.lang.String
getLiteral()
int
getMaxLength()
int
getMinLength()
java.lang.Class<?>
getPropertyType()
java.lang.String
getRegex()
Returns the regular expression pattern the field text parsed by this field definition must match.protected java.util.regex.Pattern
getRegexPattern()
Returns the regular expression the field text parsed by this field definition must match.int
getSize()
Returns the size of a single occurrence of this element, which is used to offset field positions for repeating segments and fields.java.lang.Class<?>
getType()
java.lang.Object
getValue(ParsingContext context)
Returns the value of this property.boolean
hasContent(ParsingContext context)
Returns whether this parser or any of its descendant have content for marshalling.boolean
hasNext(UnmarshallingContext context)
boolean
isBound()
boolean
isIdentifier()
Returns whether this property or any of its descendants are used to identify a bean object.boolean
isLazy()
protected boolean
isMatch(java.lang.String text)
Returnstrue
if the provided field text is a match for this field definition based on the configured literal value or regular expression.boolean
isOptional()
Returns whether this node must exist during unmarshalling.boolean
isRequired()
protected boolean
isSupportedChild(Component child)
Returns whether a node is a supported child of this node.boolean
isTrim()
boolean
marshal(MarshallingContext context)
Marshals a record.boolean
matches(UnmarshallingContext context)
Tests if the field text in the record matches this field.protected java.lang.Object
parseValue(UnmarshallingContext context, java.lang.String fieldText)
Parses and validates a field property value from the given field text.void
registerLocals(java.util.Set<ParserLocal<? extends java.lang.Object>> locals)
Called by a stream to register variables stored in the parsing context.void
setAccessor(PropertyAccessor accessor)
void
setBound(boolean property)
void
setDefaultValue(java.lang.Object defaultValue)
Sets the default value for a field parsed by this field definition when the field text is null or the empty string (after trimming).void
setFormat(FieldFormat format)
void
setHandler(TypeHandler handler)
void
setIdentifier(boolean recordIdentifier)
void
setLazy(boolean lazy)
void
setLiteral(java.lang.String literal)
void
setMaxLength(int maxLength)
void
setMinLength(int minLength)
void
setPropertyType(java.lang.Class<?> type)
void
setRegex(java.lang.String pattern)
Sets the regular expression pattern the field text parsed by this field definition must match.void
setRegex(java.util.regex.Pattern regex)
void
setRequired(boolean required)
void
setTrim(boolean trim)
void
setType(java.lang.Class<?> type)
void
setValue(ParsingContext context, java.lang.Object value)
Sets the property value (before marshalling).protected void
toParamString(java.lang.StringBuilder s)
Called byTreeNode.toString()
to append node parameters to the output.int
type()
Returns the property type.boolean
unmarshal(UnmarshallingContext context)
Unmarshals a record.-
Methods inherited from class org.beanio.internal.util.TreeNode
add, clone, find, getChildren, getFirst, getName, isDescendant, iterator, print, print, setName, size, sort, toString, updateReferences
-
-
-
-
Method Detail
-
hasContent
public boolean hasContent(ParsingContext context)
Description copied from interface:Parser
Returns whether this parser or any of its descendant have content for marshalling.- Specified by:
hasContent
in interfaceParser
- Parameters:
context
- theParsingContext
- Returns:
- true if there is content for marshalling, false otherwise
-
type
public int type()
Description copied from interface:Property
Returns the property type.- Specified by:
type
in interfaceProperty
- Returns:
Property.SIMPLE
,Property.COMPLEX
,Property.AGGREGATION_ARRAY
,Property.COLLECTION
,Property.AGGREGATION_COLLECTION
, orProperty.MAP
-
isOptional
public boolean isOptional()
Description copied from interface:Parser
Returns whether this node must exist during unmarshalling.- Specified by:
isOptional
in interfaceParser
- Returns:
- true if this node is optional during unmarshalling, false otherwise
-
matches
public boolean matches(UnmarshallingContext context)
Tests if the field text in the record matches this field.- Specified by:
matches
in interfaceParser
- Parameters:
context
- theUnmarshallingContext
containing the record to match- Returns:
true
if the field text is a match or this field is not used to identify the record
-
isMatch
protected boolean isMatch(java.lang.String text)
Returnstrue
if the provided field text is a match for this field definition based on the configured literal value or regular expression.- Parameters:
text
- the field text to test- Returns:
true
if the field text matches this field definitions constraints, orfalse
if the field text is null or does not match
-
marshal
public boolean marshal(MarshallingContext context)
Description copied from interface:Parser
Marshals a record.- Specified by:
marshal
in interfaceParser
- Parameters:
context
- theMarshallingContext
- Returns:
- whether a value was marshalled
-
hasNext
public boolean hasNext(UnmarshallingContext context)
-
unmarshal
public boolean unmarshal(UnmarshallingContext context)
Description copied from interface:Parser
Unmarshals a record.- Specified by:
unmarshal
in interfaceParser
- Parameters:
context
- theUnmarshallingContext
- Returns:
true
if this component was present in the unmarshalled record, orfalse
otherwise
-
parseValue
protected java.lang.Object parseValue(UnmarshallingContext context, java.lang.String fieldText)
Parses and validates a field property value from the given field text.- Parameters:
context
- theUnmarshallingContext
to report field errors tofieldText
- the field text to parse- Returns:
- the parsed field value, or
Value.INVALID
if the field was invalid, orValue.MISSING
if the field was not present in the record
-
formatValue
protected java.lang.String formatValue(java.lang.Object value)
Formats a field/property value.- Parameters:
value
- the property value to format- Returns:
- the formatted field text
-
clearValue
public void clearValue(ParsingContext context)
Description copied from interface:Property
Clears the property value. A subsequent call toProperty.getValue(ParsingContext)
should return null, orValue.MISSING
for lazy property values.- Specified by:
clearValue
in interfaceParser
- Specified by:
clearValue
in interfaceProperty
- Parameters:
context
- theParsingContext
-
createValue
public java.lang.Object createValue(ParsingContext context)
Description copied from interface:Property
Creates the property value and returns it.- Specified by:
createValue
in interfaceProperty
- Parameters:
context
- theParsingContext
- Returns:
- the property value
-
getValue
public java.lang.Object getValue(ParsingContext context)
Description copied from interface:Property
Returns the value of this property.When unmarshalling, this method should return
Value.MISSING
if the field was not present in the stream. Or if present, but has no value, null should be returned.When marshalling, this method should return
Value.MISSING
for any optional segment bound to a bean object, or null if required. Null field properties should always returnValue.MISSING
.- Specified by:
getValue
in interfaceParser
- Specified by:
getValue
in interfaceProperty
- Parameters:
context
- theParsingContext
- Returns:
- the property value,
or
Value.MISSING
if not present in the stream, orValue.INVALID
if the field was invalid
-
setValue
public void setValue(ParsingContext context, java.lang.Object value)
Description copied from interface:Property
Sets the property value (before marshalling).- Specified by:
setValue
in interfaceParser
- Specified by:
setValue
in interfaceProperty
- Parameters:
context
- theParsingContext
value
- the property value
-
isSupportedChild
protected boolean isSupportedChild(Component child)
Description copied from class:TreeNode
Returns whether a node is a supported child of this node. Called byTreeNode.add(TreeNode)
.- Overrides:
isSupportedChild
in classParserComponent
- Parameters:
child
- the node to test- Returns:
- true if the child is allowed
-
getRegex
public java.lang.String getRegex()
Returns the regular expression pattern the field text parsed by this field definition must match.- Returns:
- the regular expression pattern
-
setRegex
public void setRegex(java.lang.String pattern) throws java.util.regex.PatternSyntaxException
Sets the regular expression pattern the field text parsed by this field definition must match.- Parameters:
pattern
- the regular expression pattern- Throws:
java.util.regex.PatternSyntaxException
- if the pattern is invalid
-
getRegexPattern
protected java.util.regex.Pattern getRegexPattern()
Returns the regular expression the field text parsed by this field definition must match.- Returns:
- the regular expression
-
registerLocals
public void registerLocals(java.util.Set<ParserLocal<? extends java.lang.Object>> locals)
Description copied from class:Component
Called by a stream to register variables stored in the parsing context. This method should be overridden by subclasses that need to register one or more parser context variables.- Overrides:
registerLocals
in classComponent
- Parameters:
locals
- set of local variables
-
setPropertyType
public void setPropertyType(java.lang.Class<?> type)
-
getPropertyType
public java.lang.Class<?> getPropertyType()
-
isIdentifier
public boolean isIdentifier()
Description copied from interface:Property
Returns whether this property or any of its descendants are used to identify a bean object.- Specified by:
isIdentifier
in interfaceParser
- Specified by:
isIdentifier
in interfaceProperty
- Returns:
true
if this property identifies a bean
-
setIdentifier
public void setIdentifier(boolean recordIdentifier)
- Specified by:
setIdentifier
in interfaceProperty
-
getFormat
public FieldFormat getFormat()
-
setFormat
public void setFormat(FieldFormat format)
-
getLiteral
public java.lang.String getLiteral()
-
setLiteral
public void setLiteral(java.lang.String literal)
-
isTrim
public boolean isTrim()
-
setTrim
public void setTrim(boolean trim)
-
isRequired
public boolean isRequired()
-
setRequired
public void setRequired(boolean required)
-
isLazy
public boolean isLazy()
-
setLazy
public void setLazy(boolean lazy)
-
getMinLength
public int getMinLength()
-
setMinLength
public void setMinLength(int minLength)
-
getMaxLength
public int getMaxLength()
-
setMaxLength
public void setMaxLength(int maxLength)
-
setRegex
public void setRegex(java.util.regex.Pattern regex)
-
getAccessor
public PropertyAccessor getAccessor()
- Specified by:
getAccessor
in interfaceProperty
-
setAccessor
public void setAccessor(PropertyAccessor accessor)
- Specified by:
setAccessor
in interfaceProperty
-
getDefaultValue
public java.lang.Object getDefaultValue()
Returns the default value for a field parsed by this field definition when the field text is null or the empty string (after trimming).- Returns:
- default value
-
setDefaultValue
public void setDefaultValue(java.lang.Object defaultValue)
Sets the default value for a field parsed by this field definition when the field text is null or the empty string (after trimming).- Parameters:
defaultValue
- the default value
-
getHandler
public TypeHandler getHandler()
-
setHandler
public void setHandler(TypeHandler handler)
-
toParamString
protected void toParamString(java.lang.StringBuilder s)
Description copied from class:TreeNode
Called byTreeNode.toString()
to append node parameters to the output.- Overrides:
toParamString
in classTreeNode<Component>
- Parameters:
s
- the output to append
-
getSize
public int getSize()
Description copied from interface:Parser
Returns the size of a single occurrence of this element, which is used to offset field positions for repeating segments and fields.The concept of size is dependent on the stream format. The size of an element in a fixed length stream format is determined by the length of the element in characters, while other stream formats calculate size based on the number of fields. Some stream formats, such as XML, may ignore size settings.
-
isBound
public boolean isBound()
-
setBound
public void setBound(boolean property)
-
-