sdsu.io
Class StringizableReader

java.lang.Object
  |
  +--sdsu.io.StringizableReader

public class StringizableReader
extends java.lang.Object
implements java.util.Enumeration

Input (String, file, or reader) consists of: optional whitespace followed by optional file metadata line followed by one or more data blocks. A data block consists of optional whitespace followed by optional comment block, optional whitespace, followed by optional metadata block, optional whitespace, followed by optional comment block, followed by data.
The file metadata line must be started with the two characters ##. It can contain any of the following three name-value pairs:
DataSeparator=AString;CommentCharacter=A;MetaDataCharacter=B
The DataSeparator value is the string that separates data blocks. It must be selected not to clash with other strings in the file. The CommentCharacter value, which must be a single character, is used to indicate the start of a line in a comment block. The MetaDataCharacter value, which must be a single character, is used to indicate a meta data block (see below).
A comment block connsists of consecutive lines each starting with a comment character, which defaults to '#'. Comments are ignored. The comment block before the metadata block is called the metadata comment. The comment block before the data is called the data comment.

A metadata block consists of consecutive lines each starting with a comment character then the metadata character, which defaults to '$'. The metadata block contains name-value pairs used to read the data. The format of the name-value pair is the default format for the class sdsu.io.LabeledData, which is used to interpret the name-value pairs. The format is name=value;. That is the name and value are separated by the character '=', pairs are separated by the character ';'. White space can be used. The last pair does not need to be followed by ';'. If the value contains either '=', ';' or whitespace it must be quoted by the single quote character. For example:
message='hi mom'
There are two special name-value pairs. The first is "class=AFullyQuallifiedClassName". If this name-value pair does not exist the data is read as a string. If this name-value pair does exist, the class must implement Stringizable. The indicated class, called the data class., is used to interpret the data. The other special name-value pair is "label=ALabelForTheData". This name-value pair is used to label the data block. Other name-values are used as required to help set the state for the data class. The name-value pairs can be on multiple lines, all pairs in the block must be followed by the ';' character, except the last one in the block.
If a data class is given in the meta data block, the meta data and the data are used to recreate an object that is an instance of the data class. The meta data is used as the argument to the objects setMetaData method. The data is used as the arument to the objects fromString method.
The nextElement method of this class returns the data for the next data block. The object returned is either a string (if there was no data class) or is of type data class.

Version:
0.8 7 January 1998
Author:
Roger Whitney (whitney@cs.sdsu.edu)

Field Summary
static char DEFAULT_COMMENT_CHAR
          Default character used at begining of a line to indicate a comment, which ends at the end of line.
static java.lang.String DEFAULT_DATA_BLOCK_SEPARATOR
          Default string that separates data block.
static char DEFAULT_METADATA_CHAR
          Default character that indicates a line of metaData when it is the second character of a comment line.
static java.lang.String META_COMMENT_CHAR_KEY
          Name used in file meta data line to indicate the comment character.
static java.lang.String META_DATA_CHAR_KEY
          Name used in file meta data line to indicate the meta data character.
static java.lang.String META_SEPARATOR_KEY
          Name used in file meta data line to indicate the data separator string.
 
Constructor Summary
StringizableReader(java.io.Reader in)
          Create a StringizableReader object with a Reader as an input and the given separator.
StringizableReader(java.lang.String input)
          Create a StringizableReader object with a String as an input and the given separator.
 
Method Summary
static StringizableReader fromFile(java.lang.String fileName)
          Create a StringizableReader object with a file as an input and the given separator.
 java.lang.String getDataComment()
          Return the comment for the current data object
 java.lang.String getDataLabel()
          Returns the label for the current data object.
 boolean hasMoreElements()
          Returns true if there are more blocks of data in the input.
 java.lang.Object nextElement()
          Returns the next block of data.
 void setSeparator(java.lang.String newSeparator)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

META_SEPARATOR_KEY

public static final java.lang.String META_SEPARATOR_KEY
Name used in file meta data line to indicate the data separator string.

META_COMMENT_CHAR_KEY

public static final java.lang.String META_COMMENT_CHAR_KEY
Name used in file meta data line to indicate the comment character.

META_DATA_CHAR_KEY

public static final java.lang.String META_DATA_CHAR_KEY
Name used in file meta data line to indicate the meta data character.

DEFAULT_COMMENT_CHAR

public static final char DEFAULT_COMMENT_CHAR
Default character used at begining of a line to indicate a comment, which ends at the end of line.

DEFAULT_METADATA_CHAR

public static final char DEFAULT_METADATA_CHAR
Default character that indicates a line of metaData when it is the second character of a comment line.

DEFAULT_DATA_BLOCK_SEPARATOR

public static final java.lang.String DEFAULT_DATA_BLOCK_SEPARATOR
Default string that separates data block.
Constructor Detail

StringizableReader

public StringizableReader(java.io.Reader in)
                   throws java.io.IOException
Create a StringizableReader object with a Reader as an input and the given separator.

StringizableReader

public StringizableReader(java.lang.String input)
                   throws java.io.IOException
Create a StringizableReader object with a String as an input and the given separator.
Method Detail

fromFile

public static StringizableReader fromFile(java.lang.String fileName)
                                   throws java.io.FileNotFoundException,
                                          java.io.IOException
Create a StringizableReader object with a file as an input and the given separator. File IO will be buffered for you.
Throws:
java.io.FileNotFoundException - thrown if fileName is not valid file name.

setSeparator

public void setSeparator(java.lang.String newSeparator)

getDataComment

public java.lang.String getDataComment()
Return the comment for the current data object

getDataLabel

public java.lang.String getDataLabel()
Returns the label for the current data object. Returns empty string, not null, if no label exists

hasMoreElements

public boolean hasMoreElements()
Returns true if there are more blocks of data in the input.
Specified by:
hasMoreElements in interface java.util.Enumeration

nextElement

public java.lang.Object nextElement()
Returns the next block of data.
Specified by:
nextElement in interface java.util.Enumeration
Throws:
java.util.NoSuchElementException - if there are no blocks of data left.