BeanIO 2.0.4

org.beanio.internal.parser
Class Field

java.lang.Object
  extended by org.beanio.internal.util.TreeNode<Component>
      extended by org.beanio.internal.parser.Component
          extended by org.beanio.internal.parser.ParserComponent
              extended by org.beanio.internal.parser.Field
All Implemented Interfaces:
Cloneable, 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
Author:
Kevin Seim

Field Summary
 
Fields inherited from interface org.beanio.internal.parser.Property
AGGREGATION_ARRAY, AGGREGATION_COLLECTION, AGGREGATION_MAP, COLLECTION, COMPLEX, MAP, SIMPLE
 
Constructor Summary
Field()
          Constructs a new Field.
 
Method Summary
 void clearValue(ParsingContext context)
          Clears the property value.
 Object createValue(ParsingContext context)
          Creates the property value and returns it.
 boolean defines(Object value)
           
protected  String formatValue(Object value)
          Formats a field/property value.
 PropertyAccessor getAccessor()
           
 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()
           
 String getLiteral()
           
 int getMaxLength()
           
 int getMinLength()
           
 Class<?> getPropertyType()
           
 String getRegex()
          Returns the regular expression pattern the field text parsed by this field definition must match.
protected  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.
 Class<?> getType()
           
 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()
          Returns whether this node must exist during unmarshalling.
protected  boolean isMatch(String text)
          Returns true if the provided field text is a match for this field definition based on the configured literal value or regular expression.
 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  Object parseValue(UnmarshallingContext context, String fieldText)
          Parses and validates a field property value from the given field text.
 void registerLocals(Set<ParserLocal<? extends Object>> locals)
          Called by a stream to register variables stored in the parsing context.
 void setAccessor(PropertyAccessor accessor)
           
 void setBound(boolean property)
           
 void setDefaultValue(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 setLiteral(String literal)
           
 void setMaxLength(int maxLength)
           
 void setMinLength(int minLength)
           
 void setPropertyType(Class<?> type)
           
 void setRegex(Pattern regex)
           
 void setRegex(String pattern)
          Sets the regular expression pattern the field text parsed by this field definition must match.
 void setRequired(boolean required)
           
 void setTrim(boolean trim)
           
 void setType(Class<?> type)
           
 void setValue(ParsingContext context, Object value)
          Sets the property value (before marshalling).
protected  void toParamString(StringBuilder s)
          Called by TreeNode.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, iterator, print, setName, size, sort, toString, updateReferences
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.beanio.internal.parser.Property
getName
 
Methods inherited from interface org.beanio.internal.parser.Parser
getName
 

Constructor Detail

Field

public Field()
Constructs a new Field.

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 interface Parser
Parameters:
context - the ParsingContext
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 interface Property
Returns:
Property.SIMPLE, Property.COMPLEX, Property.AGGREGATION_ARRAY, Property.COLLECTION, Property.AGGREGATION_COLLECTION, or Property.MAP

isLazy

public boolean isLazy()
Description copied from interface: Parser
Returns whether this node must exist during unmarshalling.

Specified by:
isLazy in interface Parser
Returns:
true if this node is optional during unmarshalling, false otherwise

defines

public boolean defines(Object value)
Specified by:
defines in interface Property

matches

public boolean matches(UnmarshallingContext context)
Tests if the field text in the record matches this field.

Specified by:
matches in interface Parser
Parameters:
context - the UnmarshallingContext 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(String text)
Returns true 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, or false 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 interface Parser
Parameters:
context - the MarshallingContext
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 interface Parser
Parameters:
context - the UnmarshallingContext
Returns:
true if this component was present in the unmarshalled record, or false otherwise

parseValue

protected Object parseValue(UnmarshallingContext context,
                            String fieldText)
Parses and validates a field property value from the given field text.

Parameters:
context - the UnmarshallingContext to report field errors to
fieldText - the field text to parse
Returns:
the parsed field value, or Value.INVALID if the field was invalid, or Value.MISSING if the field was not present in the record

formatValue

protected String formatValue(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 to Property.getValue(ParsingContext) should return null, or Value.MISSING for lazy property values.

Specified by:
clearValue in interface Parser
Specified by:
clearValue in interface Property
Parameters:
context - the ParsingContext

createValue

public Object createValue(ParsingContext context)
Description copied from interface: Property
Creates the property value and returns it.

Specified by:
createValue in interface Property
Parameters:
context - the ParsingContext
Returns:
the property value

getValue

public 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 return Value.MISSING.

Specified by:
getValue in interface Parser
Specified by:
getValue in interface Property
Parameters:
context - the ParsingContext
Returns:
the property value, or Value.MISSING if not present in the stream, or Value.INVALID if the field was invalid

setValue

public void setValue(ParsingContext context,
                     Object value)
Description copied from interface: Property
Sets the property value (before marshalling).

Specified by:
setValue in interface Parser
Specified by:
setValue in interface Property
Parameters:
context - the ParsingContext
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 by TreeNode.add(TreeNode).

Overrides:
isSupportedChild in class ParserComponent
Parameters:
child - the node to test
Returns:
true if the child is allowed

getRegex

public 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(String pattern)
              throws PatternSyntaxException
Sets the regular expression pattern the field text parsed by this field definition must match.

Parameters:
pattern - the regular expression pattern
Throws:
PatternSyntaxException - if the pattern is invalid

getRegexPattern

protected Pattern getRegexPattern()
Returns the regular expression the field text parsed by this field definition must match.

Returns:
the regular expression

registerLocals

public void registerLocals(Set<ParserLocal<? extends 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 class Component
Parameters:
locals - set of local variables

setPropertyType

public void setPropertyType(Class<?> type)

getPropertyType

public 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 interface Parser
Specified by:
isIdentifier in interface Property
Returns:
true if this property identifies a bean

setIdentifier

public void setIdentifier(boolean recordIdentifier)
Specified by:
setIdentifier in interface Property

getFormat

public FieldFormat getFormat()

setFormat

public void setFormat(FieldFormat format)

getLiteral

public String getLiteral()

setLiteral

public void setLiteral(String literal)

getType

public Class<?> getType()
Specified by:
getType in interface Property

isTrim

public boolean isTrim()

setTrim

public void setTrim(boolean trim)

isRequired

public boolean isRequired()

setRequired

public void setRequired(boolean required)

getMinLength

public int getMinLength()

setMinLength

public void setMinLength(int minLength)

getMaxLength

public int getMaxLength()

setMaxLength

public void setMaxLength(int maxLength)

setRegex

public void setRegex(Pattern regex)

setType

public void setType(Class<?> type)
Specified by:
setType in interface Property

getAccessor

public PropertyAccessor getAccessor()
Specified by:
getAccessor in interface Property

setAccessor

public void setAccessor(PropertyAccessor accessor)
Specified by:
setAccessor in interface Property

getDefaultValue

public 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(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(StringBuilder s)
Description copied from class: TreeNode
Called by TreeNode.toString() to append node parameters to the output.

Overrides:
toParamString in class TreeNode<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.

Specified by:
getSize in interface Parser
Returns:
the size of this parser element

isBound

public boolean isBound()

setBound

public void setBound(boolean property)

BeanIO 2.0.4

Copyright © 2010-2013 Kevin Seim