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 voidclearValue(ParsingContext context)Clears the property value.java.lang.ObjectcreateValue(ParsingContext context)Creates the property value and returns it.booleandefines(java.lang.Object value)protected java.lang.StringformatValue(java.lang.Object value)Formats a field/property value.PropertyAccessorgetAccessor()java.lang.ObjectgetDefaultValue()Returns the default value for a field parsed by this field definition when the field text is null or the empty string (after trimming).FieldFormatgetFormat()TypeHandlergetHandler()java.lang.StringgetLiteral()intgetMaxLength()intgetMinLength()java.lang.Class<?>getPropertyType()java.lang.StringgetRegex()Returns the regular expression pattern the field text parsed by this field definition must match.protected java.util.regex.PatterngetRegexPattern()Returns the regular expression the field text parsed by this field definition must match.intgetSize()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.ObjectgetValue(ParsingContext context)Returns the value of this property.booleanhasContent(ParsingContext context)Returns whether this parser or any of its descendant have content for marshalling.booleanhasNext(UnmarshallingContext context)booleanisBound()booleanisIdentifier()Returns whether this property or any of its descendants are used to identify a bean object.booleanisLazy()protected booleanisMatch(java.lang.String text)Returnstrueif the provided field text is a match for this field definition based on the configured literal value or regular expression.booleanisOptional()Returns whether this node must exist during unmarshalling.booleanisRequired()protected booleanisSupportedChild(Component child)Returns whether a node is a supported child of this node.booleanisTrim()booleanmarshal(MarshallingContext context)Marshals a record.booleanmatches(UnmarshallingContext context)Tests if the field text in the record matches this field.protected java.lang.ObjectparseValue(UnmarshallingContext context, java.lang.String fieldText)Parses and validates a field property value from the given field text.voidregisterLocals(java.util.Set<ParserLocal<? extends java.lang.Object>> locals)Called by a stream to register variables stored in the parsing context.voidsetAccessor(PropertyAccessor accessor)voidsetBound(boolean property)voidsetDefaultValue(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).voidsetFormat(FieldFormat format)voidsetHandler(TypeHandler handler)voidsetIdentifier(boolean recordIdentifier)voidsetLazy(boolean lazy)voidsetLiteral(java.lang.String literal)voidsetMaxLength(int maxLength)voidsetMinLength(int minLength)voidsetPropertyType(java.lang.Class<?> type)voidsetRegex(java.lang.String pattern)Sets the regular expression pattern the field text parsed by this field definition must match.voidsetRegex(java.util.regex.Pattern regex)voidsetRequired(boolean required)voidsetTrim(boolean trim)voidsetType(java.lang.Class<?> type)voidsetValue(ParsingContext context, java.lang.Object value)Sets the property value (before marshalling).protected voidtoParamString(java.lang.StringBuilder s)Called byTreeNode.toString()to append node parameters to the output.inttype()Returns the property type.booleanunmarshal(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:ParserReturns whether this parser or any of its descendant have content for marshalling.- Specified by:
hasContentin interfaceParser- Parameters:
context- theParsingContext- Returns:
- true if there is content for marshalling, false otherwise
-
type
public int type()
Description copied from interface:PropertyReturns the property type.- Specified by:
typein interfaceProperty- Returns:
Property.SIMPLE,Property.COMPLEX,Property.AGGREGATION_ARRAY,Property.COLLECTION,Property.AGGREGATION_COLLECTION, orProperty.MAP
-
isOptional
public boolean isOptional()
Description copied from interface:ParserReturns whether this node must exist during unmarshalling.- Specified by:
isOptionalin 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:
matchesin interfaceParser- Parameters:
context- theUnmarshallingContextcontaining the record to match- Returns:
trueif the field text is a match or this field is not used to identify the record
-
isMatch
protected boolean isMatch(java.lang.String text)
Returnstrueif 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:
trueif the field text matches this field definitions constraints, orfalseif the field text is null or does not match
-
marshal
public boolean marshal(MarshallingContext context)
Description copied from interface:ParserMarshals a record.- Specified by:
marshalin 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:ParserUnmarshals a record.- Specified by:
unmarshalin interfaceParser- Parameters:
context- theUnmarshallingContext- Returns:
trueif this component was present in the unmarshalled record, orfalseotherwise
-
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- theUnmarshallingContextto report field errors tofieldText- the field text to parse- Returns:
- the parsed field value, or
Value.INVALIDif the field was invalid, orValue.MISSINGif 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:PropertyClears the property value. A subsequent call toProperty.getValue(ParsingContext)should return null, orValue.MISSINGfor lazy property values.- Specified by:
clearValuein interfaceParser- Specified by:
clearValuein interfaceProperty- Parameters:
context- theParsingContext
-
createValue
public java.lang.Object createValue(ParsingContext context)
Description copied from interface:PropertyCreates the property value and returns it.- Specified by:
createValuein interfaceProperty- Parameters:
context- theParsingContext- Returns:
- the property value
-
getValue
public java.lang.Object getValue(ParsingContext context)
Description copied from interface:PropertyReturns the value of this property.When unmarshalling, this method should return
Value.MISSINGif 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.MISSINGfor any optional segment bound to a bean object, or null if required. Null field properties should always returnValue.MISSING.- Specified by:
getValuein interfaceParser- Specified by:
getValuein interfaceProperty- Parameters:
context- theParsingContext- Returns:
- the property value,
or
Value.MISSINGif not present in the stream, orValue.INVALIDif the field was invalid
-
setValue
public void setValue(ParsingContext context, java.lang.Object value)
Description copied from interface:PropertySets the property value (before marshalling).- Specified by:
setValuein interfaceParser- Specified by:
setValuein interfaceProperty- Parameters:
context- theParsingContextvalue- the property value
-
isSupportedChild
protected boolean isSupportedChild(Component child)
Description copied from class:TreeNodeReturns whether a node is a supported child of this node. Called byTreeNode.add(TreeNode).- Overrides:
isSupportedChildin 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.PatternSyntaxExceptionSets 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:ComponentCalled 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:
registerLocalsin 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:PropertyReturns whether this property or any of its descendants are used to identify a bean object.- Specified by:
isIdentifierin interfaceParser- Specified by:
isIdentifierin interfaceProperty- Returns:
trueif this property identifies a bean
-
setIdentifier
public void setIdentifier(boolean recordIdentifier)
- Specified by:
setIdentifierin 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:
getAccessorin interfaceProperty
-
setAccessor
public void setAccessor(PropertyAccessor accessor)
- Specified by:
setAccessorin 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:TreeNodeCalled byTreeNode.toString()to append node parameters to the output.- Overrides:
toParamStringin classTreeNode<Component>- Parameters:
s- the output to append
-
getSize
public int getSize()
Description copied from interface:ParserReturns 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)
-
-