BeanIO 1.2.3

org.beanio.parser
Class StreamDefinition

java.lang.Object
  extended by org.beanio.parser.StreamDefinition
All Implemented Interfaces:
MessageFactory
Direct Known Subclasses:
DelimitedStreamDefinition, FixedLengthStreamDefinition, XmlStreamDefinition

public abstract class StreamDefinition
extends Object
implements MessageFactory

A StreamDefinition stores all information needed to parse an input stream and format an output stream, and is used to create BeanReader and BeanWriter instances.

Internally, a stream definition is a tree structure with GroupDefinition instances for branches and RecordDefinition instances for leaves.

All classes and subclasses used to define a stream may be shared across multiple threads and must be thread-safe.

Since:
1.0
Author:
Kevin Seim

Field Summary
static int READ_ONLY_MODE
          Stream definition supports reading only
static int READ_WRITE_MODE
          Stream definition supports reading and writing
static int WRITE_ONLY_MODE
          Stream definition supports writing only
 
Constructor Summary
StreamDefinition(String format)
          Creates a new StreamDefinition.
 
Method Summary
 BeanReader createBeanReader(Reader in, Locale locale)
          Creates a new BeanReader for reading from the given input stream.
 BeanWriter createBeanWriter(Writer out)
          Creates a new BeanWriter for writing to the given output stream.
protected abstract  RecordReader createDefaultReader(Reader in)
          Creates a default RecordReader to read from the given input stream.
protected abstract  RecordWriter createDefaultWriter(Writer out)
          Creates a default RecordWriter for writing to the given output stream.
protected  Record createRecord()
          Constructs a new Record for this stream to operate on.
 Record createRecord(Locale locale)
          Constructs a new Record for this stream to operate on.
 RecordReader createRecordReader(Reader in)
          Creates a new RecordReader to read from the given input stream.
 RecordWriter createRecordWriter(Writer out)
          Creates a new RecordWriter for writing to the given output stream.
 String getFieldErrorMessage(String recordName, String fieldName, String rule)
          Returns a field level error message.
 String getFieldLabel(String recordName, String fieldName)
          Returns the localized label for a field.
 String getFormat()
          Returns the stream format, one of 'csv', 'delimited' or 'fixedlength'.
protected  String getLabel(String key)
          Returns a label from the configured resource bundle.
 int getMode()
          Returns the allowed mode of operation for this stream configuration.
 String getName()
          Returns the name of this stream context.
 String getRecordErrorMessage(String recordName, String rule)
          Returns a record level error message.
 String getRecordLabel(String recordName)
          Returns the localized label for a record.
 GroupDefinition getRootGroupDefinition()
          Returns the top level or root group definition for this stream, to which all child records and subgroups were added.
protected  GroupDefinition newGroupDefinition()
          This method is called to instantiate a new group definition so that subclasses may override the implementation.
protected  GroupNode newGroupNode(GroupDefinition definition)
          Constructs a new group node for parsing an input stream.
protected  RecordNode newRecordNode(RecordDefinition definition)
          Constructs a new record node for parsing an input stream.
 void setDefaultResourceBundle(ResourceBundle defaultResourceBundle)
          Sets the default resource bundle to check of messages not found in the primary resource bundle.
 void setMaxOccurs(int maxOccurs)
          Sets the maximum number of times the record layout can be read from the input stream.
 void setMinOccurs(int minOccurs)
          Sets the minimum number of times the record layout must be read from in the input stream.
 void setMode(int mode)
          Sets the allowed mode of operation for this stream configuration.
 void setName(String name)
          Sets the name of this stream context.
 void setReaderFactory(RecordReaderFactory readerFactory)
          Sets the record reader factory to use to create new RecordReader's.
 void setResourceBundle(ResourceBundle resourceBundle)
          Sets the primary resource bundle to check for messages.
 void setWriterFactory(RecordWriterFactory writerFactory)
          Sets the record writer factory to use to create new RecordWriter's.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

READ_WRITE_MODE

public static final int READ_WRITE_MODE
Stream definition supports reading and writing

See Also:
Constant Field Values

READ_ONLY_MODE

public static final int READ_ONLY_MODE
Stream definition supports reading only

See Also:
Constant Field Values

WRITE_ONLY_MODE

public static final int WRITE_ONLY_MODE
Stream definition supports writing only

See Also:
Constant Field Values
Constructor Detail

StreamDefinition

public StreamDefinition(String format)
Creates a new StreamDefinition.

Method Detail

newGroupDefinition

protected GroupDefinition newGroupDefinition()
This method is called to instantiate a new group definition so that subclasses may override the implementation.

Returns:
the new GroupDefinition

getName

public String getName()
Returns the name of this stream context.

Returns:
the name of this stream context

setName

public void setName(String name)
Sets the name of this stream context.

Parameters:
name - the new name of this stream context

getFormat

public String getFormat()
Returns the stream format, one of 'csv', 'delimited' or 'fixedlength'.

Returns:
the stream format

getMode

public int getMode()
Returns the allowed mode of operation for this stream configuration.

Returns:
READ_WRITE_MODE if reading and writing from a stream is allowed,
READ_ONLY_MODE if only reading is allowed,
WRITE_ONLY_MODE if only writing is allowed
Since:
1.2

setMode

public void setMode(int mode)
Sets the allowed mode of operation for this stream configuration.

Parameters:
mode - READ_WRITE_MODE if reading and writing from a stream is allowed,
READ_ONLY_MODE if only reading is allowed,
WRITE_ONLY_MODE if only writing is allowed
Since:
1.2

createRecord

public final Record createRecord(Locale locale)
Constructs a new Record for this stream to operate on.

Parameters:
locale - the locale to use for rendering error messages
Returns:
new Record

createRecord

protected Record createRecord()
Constructs a new Record for this stream to operate on.

Returns:
new Record

getRecordLabel

public String getRecordLabel(String recordName)
Description copied from interface: MessageFactory
Returns the localized label for a record.

Specified by:
getRecordLabel in interface MessageFactory
Parameters:
recordName - the name of the record
Returns:
the record label, or null if no label was found

getFieldLabel

public String getFieldLabel(String recordName,
                            String fieldName)
Description copied from interface: MessageFactory
Returns the localized label for a field.

Specified by:
getFieldLabel in interface MessageFactory
Parameters:
recordName - the name of the record the field belongs to
fieldName - the name of the field
Returns:
the field label, or null if no label was found

getLabel

protected String getLabel(String key)
Returns a label from the configured resource bundle.

Parameters:
key - the resource bundle key
Returns:
the label, or null if not found

getFieldErrorMessage

public String getFieldErrorMessage(String recordName,
                                   String fieldName,
                                   String rule)
Description copied from interface: MessageFactory
Returns a field level error message.

Specified by:
getFieldErrorMessage in interface MessageFactory
Parameters:
recordName - the name of the record
fieldName - the name of the field
rule - the name of the validation rule
Returns:
the error message, or null if no message was configured

getRecordErrorMessage

public String getRecordErrorMessage(String recordName,
                                    String rule)
Description copied from interface: MessageFactory
Returns a record level error message.

Specified by:
getRecordErrorMessage in interface MessageFactory
Parameters:
recordName - the name of the record
rule - the name of the validation rule
Returns:
the error message, or null if no message was configured

createRecordReader

public RecordReader createRecordReader(Reader in)
Creates a new RecordReader to read from the given input stream. This method delegates to the configured reader factory, or if null, it calls createDefaultReader(Reader).

Parameters:
in - the input stream to read from
Returns:
a new RecordReader

createDefaultReader

protected abstract RecordReader createDefaultReader(Reader in)
Creates a default RecordReader to read from the given input stream. This method is called by createRecordReader(Reader) if no record reader factory was configured.

Parameters:
in - the input stream to read from
Returns:
a new RecordReader

createRecordWriter

public RecordWriter createRecordWriter(Writer out)
Creates a new RecordWriter for writing to the given output stream. This method delegates to the configured record writer factory, or if null, it calls createDefaultWriter(Writer).

Parameters:
out - the output stream to write to
Returns:
a new RecordWriter

createDefaultWriter

protected abstract RecordWriter createDefaultWriter(Writer out)
Creates a default RecordWriter for writing to the given output stream. This method is called by createRecordWriter(Writer) if no record writer factory was configured.

Parameters:
out - the output stream to write to
Returns:
a new RecordWriter

setReaderFactory

public void setReaderFactory(RecordReaderFactory readerFactory)
Sets the record reader factory to use to create new RecordReader's. If set to null, this stream context will create a default reader.

Parameters:
readerFactory - the new record reader factory

setWriterFactory

public void setWriterFactory(RecordWriterFactory writerFactory)
Sets the record writer factory to use to create new RecordWriter's. If set to null, this stream context will create a default writer.

Parameters:
writerFactory - the new record writer factor

setResourceBundle

public void setResourceBundle(ResourceBundle resourceBundle)
Sets the primary resource bundle to check for messages.

Parameters:
resourceBundle - the resource bundle

setDefaultResourceBundle

public void setDefaultResourceBundle(ResourceBundle defaultResourceBundle)
Sets the default resource bundle to check of messages not found in the primary resource bundle.

Parameters:
defaultResourceBundle - the default resource bundle

getRootGroupDefinition

public final GroupDefinition getRootGroupDefinition()
Returns the top level or root group definition for this stream, to which all child records and subgroups were added.

Returns:
the root group context

setMinOccurs

public void setMinOccurs(int minOccurs)
Sets the minimum number of times the record layout must be read from in the input stream. Defaults to 0.

Parameters:
minOccurs - the minimum number of times the record layout must be read from the input stream

setMaxOccurs

public void setMaxOccurs(int maxOccurs)
Sets the maximum number of times the record layout can be read from the input stream. Defaults to 1.

Parameters:
maxOccurs - the maximum number of times the record layout can be read from the input straem

createBeanReader

public BeanReader createBeanReader(Reader in,
                                   Locale locale)
Creates a new BeanReader for reading from the given input stream.

Parameters:
in - the input stream to read from
locale - the locale to use for rendering error messages
Returns:
a new BeanReader.

createBeanWriter

public BeanWriter createBeanWriter(Writer out)
Creates a new BeanWriter for writing to the given output stream.

Parameters:
out - the output stream to write to
Returns:
a new BeanWriter

newGroupNode

protected GroupNode newGroupNode(GroupDefinition definition)
Constructs a new group node for parsing an input stream.

Parameters:
definition - the group definition
Returns:
the new group node

newRecordNode

protected RecordNode newRecordNode(RecordDefinition definition)
Constructs a new record node for parsing an input stream.

Parameters:
definition - the record definition
Returns:
the new record node

BeanIO 1.2.3

Copyright © 2010-2012 Kevin Seim