BeanIO 1.2.3

org.beanio.parser
Class FieldDefinition

java.lang.Object
  extended by org.beanio.parser.PropertyDefinition
      extended by org.beanio.parser.FieldDefinition
Direct Known Subclasses:
FlatFieldDefinition, XmlFieldDefinition

public abstract class FieldDefinition
extends PropertyDefinition

A FieldDefinition is used to parse and format field values that make up a record or bean.

Since:
1.0
Author:
Kevin Seim

Field Summary
static char LEFT
          Left justification
static char RIGHT
          Right justification
 
Fields inherited from class org.beanio.parser.PropertyDefinition
INVALID, MISSING
 
Constructor Summary
FieldDefinition()
           
 
Method Summary
 boolean defines(Object value)
          Tests whether this definition is used to map the given bean or property value.
protected  String formatPaddedNull()
          Returns the field text for a padded field when the property value is null.
protected  String formatText(String text)
          Formats field text.
 String formatValue(Object value)
          Formats the field value.
protected  String getDefaultTextFor(Class<?> type, char padding)
          Returns default unpadded text when the entire field is populated by the padding character.
 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).
 char getJustification()
          Returns the text justification for this field.
 String getLiteral()
          Returns the textual literal value the field text must match, or null if no literal validation will be performed.
 int getMaxLength()
          Returns the maximum length in characters of the field text allowed by this field definition after trimming is performed.
 int getMinLength()
          Returns the minimum length in characters of the field text allowed by this field definition after trimming is performed..
 int getPaddedLength()
          Returns the padded length of this field, or 0 if padding is disabled.
 char getPadding()
          Returns the character used to pad this field.
 int getPosition()
          Returns the position of this field within the record.
 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.
 TypeHandler getTypeHandler()
          Returns the type handler for this field.
 boolean isField()
          Returns whether this property describes a field in the mapped stream.
 boolean isMatch(Object value)
          Tests if the given field value matches this field definition.
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.
protected  boolean isNilSupported()
          Returns whether formatValue(Object) may return TypeHandler.NIL.
 boolean isPadded()
          Returns whether this field is padded.
 boolean isProperty()
          Returns true if the value parsed by this field definition is a property of the record bean.
 boolean isRequired()
          Returns true if this field is required.
 boolean isTrim()
          Returns true if the field text will be trimmed before validation and type conversion.
abstract  boolean matches(Record record)
          Tests if the field text in the record matches this field definition.
protected abstract  String parseField(Record record)
          Parses the raw field text from a record prior to any validation and sets the text on the record.
protected  Object parsePropertyValue(Record record)
          Parses and validates a field property value from the record.
protected  Object parsePropertyValue(Record record, String fieldText)
          Parses and validates a field property value from the given field text.
 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 setJustification(char justification)
          Sets the text justification for this field.
 void setLiteral(String literal)
          Sets the literal text this field must match.
 void setMaxLength(int maxLength)
          Sets the maximum length in characters of the field text allowed by this field definition after trimming is performed.
 void setMinLength(int minLength)
          Sets the minimum length in characters of the field text allowed by this field definition after trimming is performed..
 void setPadded(boolean padded)
          Sets whether this field is padded.
 void setPaddedLength(int length)
          Sets the padded length of this field.
 void setPadding(char padding)
          Sets the character used to pad this field.
 void setPosition(int position)
          Sets the position of this field within the record beginning at 0.
 void setProperty(boolean property)
          Set to true if the value parsed by this field definition is a property of the record bean.
 void setPropertyType(Class<?> type)
          Sets the class type of this property, or if this property is a collection, the class type of the collection value.
 void setRegex(String pattern)
          Sets the regular expression pattern the field text parsed by this field definition must match.
 void setRequired(boolean required)
          Sets to true if this field is required.
 void setTrim(boolean trim)
          Set to true if the field text should be trimmed before validation and type conversion.
 void setTypeHandler(TypeHandler handler)
          Sets the type handler for this field.
protected  String unpad(String fieldText)
          Removes padding from the field text.
 
Methods inherited from class org.beanio.parser.PropertyDefinition
getCollectionType, getLength, getMaxOccurs, getMinOccurs, getName, getParent, getPropertyDescriptor, getPropertyType, isArray, isBean, isCollection, isConstant, isLazy, isRecordIdentifier, parseValue, setCollectionType, setLazy, setLength, setMaxOccurs, setMinOccurs, setName, setParent, setPropertyDescriptor, setRecordIdentifier
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LEFT

public static final char LEFT
Left justification

See Also:
Constant Field Values

RIGHT

public static final char RIGHT
Right justification

See Also:
Constant Field Values
Constructor Detail

FieldDefinition

public FieldDefinition()
Method Detail

isField

public boolean isField()
Description copied from class: PropertyDefinition
Returns whether this property describes a field in the mapped stream.

Overrides:
isField in class PropertyDefinition
Returns:
true if this property describes a field in the mapped stream

matches

public abstract boolean matches(Record record)
Tests if the field text in the record matches this field definition.

Specified by:
matches in class PropertyDefinition
Parameters:
record - the record containing the field to test
Returns:
true if the text is a match

defines

public boolean defines(Object value)
Description copied from class: PropertyDefinition
Tests whether this definition is used to map the given bean or property value. If this property is not used to identify records, the return value of this method is undefined.

Specified by:
defines in class PropertyDefinition
Parameters:
value - the bean or property value to test
Returns:
true if this definition is used to map the given value

isMatch

public boolean isMatch(Object value)
Tests if the given field value matches this field definition.

Parameters:
value - the field value to test
Returns:
true if the value matched, false otherwise

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

parseField

protected abstract String parseField(Record record)
Parses the raw field text from a record prior to any validation and sets the text on the record.

Parameters:
record - the record to parse
Returns:
the parsed field text, or PropertyDefinition.INVALID if the field is invalid, or null if the field is not present in the record

parsePropertyValue

protected Object parsePropertyValue(Record record)
Parses and validates a field property value from the record.

Specified by:
parsePropertyValue in class PropertyDefinition
Parameters:
record - the record to parse
Returns:
the parsed field value, or PropertyDefinition.INVALID if the field was invalid, or PropertyDefinition.MISSING if the field was not present in the record

parsePropertyValue

protected Object parsePropertyValue(Record record,
                                    String fieldText)
Parses and validates a field property value from the given field text.

Parameters:
record - the record being parsed
fieldText - the field text to parse
Returns:
the parsed field value, or PropertyDefinition.INVALID if the field was invalid, or PropertyDefinition.MISSING if the field was not present in the record

formatValue

public String formatValue(Object value)
Formats the field value.

Parameters:
value - the field value to format
Returns:
the formatted field text

isNilSupported

protected boolean isNilSupported()
Returns whether formatValue(Object) may return TypeHandler.NIL. Returns false by default, which will convert TypeHandler.NIL to null.

Returns:
true if formatValue(Object) may return TypeHandler.NIL.

formatText

protected String formatText(String text)
Formats field text. If the padded length of this field is greater than 0, text will be truncated if it exceeds the length, or padded with the padding character if it doesn't.

Parameters:
text - the field text to format
Returns:
the formatted field text

formatPaddedNull

protected String formatPaddedNull()
Returns the field text for a padded field when the property value is null.

Returns:
field text for a null property

unpad

protected String unpad(String fieldText)
Removes padding from the field text.

Parameters:
fieldText - the field text to remove padding
Returns:
the unpadded field text

getPosition

public int getPosition()
Returns the position of this field within the record.

Returns:
the field position

setPosition

public void setPosition(int position)
Sets the position of this field within the record beginning at 0.

Parameters:
position - the field position, starting at 0

isTrim

public boolean isTrim()
Returns true if the field text will be trimmed before validation and type conversion.

Returns:
true if the field text will be trimmed

setTrim

public void setTrim(boolean trim)
Set to true if the field text should be trimmed before validation and type conversion.

Parameters:
trim - set to true to trim the field text

getLiteral

public String getLiteral()
Returns the textual literal value the field text must match, or null if no literal validation will be performed.

Returns:
literal field text

setLiteral

public void setLiteral(String literal)
Sets the literal text this field must match. If set to null, no literal validation is performed.

Parameters:
literal - the literal field text

getTypeHandler

public TypeHandler getTypeHandler()
Returns the type handler for this field. May be null if the field value is of type String.

Returns:
the field type handler

setTypeHandler

public void setTypeHandler(TypeHandler handler)
Sets the type handler for this field. May be set to null if the field value is of type String.

Parameters:
handler - the new type handler

isRequired

public boolean isRequired()
Returns true if this field is required. Required fields cannot match the empty String. Note that trimming is performed before the required validation is performed.

Returns:
true if this field is required

setRequired

public void setRequired(boolean required)
Sets to true if this field is required. Required fields cannot match the empty String. Note that trimming is performed before the required validation is performed.

Parameters:
required - true if this field is required

getMinLength

public int getMinLength()
Returns the minimum length in characters of the field text allowed by this field definition after trimming is performed..

Returns:
the minimum field length in characters

setMinLength

public void setMinLength(int minLength)
Sets the minimum length in characters of the field text allowed by this field definition after trimming is performed..

Parameters:
minLength - the minimum length in characters

getMaxLength

public int getMaxLength()
Returns the maximum length in characters of the field text allowed by this field definition after trimming is performed.

Returns:
the maximum field length in characters

setMaxLength

public void setMaxLength(int maxLength)
Sets the maximum length in characters of the field text allowed by this field definition after trimming is performed.

Parameters:
maxLength -

isProperty

public boolean isProperty()
Returns true if the value parsed by this field definition is a property of the record bean.

Overrides:
isProperty in class PropertyDefinition
Returns:
true if the value parsed by this field definition is a property of the record bean

setProperty

public void setProperty(boolean property)
Set to true if the value parsed by this field definition is a property of the record bean.

Overrides:
setProperty in class PropertyDefinition
Parameters:
property - true if the value parsed by this field definition is a property of the record bean

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

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

setPropertyType

public void setPropertyType(Class<?> type)
Description copied from class: PropertyDefinition
Sets the class type of this property, or if this property is a collection, the class type of the collection value. If this property is not a property of its enclosing bean, the class type may be set to null.

Overrides:
setPropertyType in class PropertyDefinition
Parameters:
type - the class type of this property

isPadded

public boolean isPadded()
Returns whether this field is padded.

Returns:
true if this field is padded
Since:
1.1.1

setPadded

public void setPadded(boolean padded)
Sets whether this field is padded.

Parameters:
padded - true if this field is padded
Since:
1.1.1

getPadding

public char getPadding()
Returns the character used to pad this field.

Returns:
the padding character

setPadding

public void setPadding(char padding)
Sets the character used to pad this field.

Parameters:
padding - the padding character

getPaddedLength

public int getPaddedLength()
Returns the padded length of this field, or 0 if padding is disabled.

Returns:
the padded length of this field

setPaddedLength

public void setPaddedLength(int length)
Sets the padded length of this field. If set to 0, padding is disabled.

Parameters:
length - the new padded length of this field

getJustification

public char getJustification()
Returns the text justification for this field.

Returns:
the text justification, LEFT or RIGHT

setJustification

public void setJustification(char justification)
Sets the text justification for this field.

Parameters:
justification - the text justification, LEFT or RIGHT

getDefaultTextFor

protected String getDefaultTextFor(Class<?> type,
                                   char padding)
Returns default unpadded text when the entire field is populated by the padding character. Returns the empty string by default, or the padding character if one of the following conditions apply:

Parameters:
type - the field property type
padding - the character used to pad the fixed length field
Returns:
the default text for a fully padded field

BeanIO 1.2.3

Copyright © 2010-2012 Kevin Seim