net.sf.japaki.text
Class OptionalParser<T>

java.lang.Object
  extended by net.sf.japaki.text.AbstractParser<T>
      extended by net.sf.japaki.text.OptionalParser<T>
All Implemented Interfaces:
Parser<T>

public class OptionalParser<T>
extends AbstractParser<T>

Wrapper around a parser to make it optional.


Constructor Summary
protected OptionalParser(Parser<T> parser)
          Creates a new optional parser based on the specified parser.
 
Method Summary
static
<T> Parser<T>
newParser(Parser<T> parser)
          Creates a new optional parser based on the specified parser.
static
<T> Parser<T>
newParser(Parser<T> parser, Factory<? extends T> factory)
          Creates a new optional parser based on a peek parser with the specified parser.
protected  void parseIt(ParseReader in, T target)
          Tries to parse the input text with the base parser.
 String treeView()
          Returns a tree view of this parser.
protected  void writeIt(ParseWriter out, T source)
          Tries to write the source object with the base parser.
 
Methods inherited from class net.sf.japaki.text.AbstractParser
getLogger, handleException, parse, toString, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

OptionalParser

protected OptionalParser(Parser<T> parser)
Creates a new optional parser based on the specified parser.

Parameters:
parser - base parser
Throws:
NullPointerException - if parser is null
Method Detail

newParser

public static <T> Parser<T> newParser(Parser<T> parser)
Creates a new optional parser based on the specified parser.

Parameters:
parser - base parser
Throws:
NullPointerException - if parser is null

newParser

public static <T> Parser<T> newParser(Parser<T> parser,
                                      Factory<? extends T> factory)
Creates a new optional parser based on a peek parser with the specified parser. The peek avoids changes to the bean, when the pattern is not competely matched.

Parameters:
parser - base parser
Throws:
NullPointerException - if parser or factory is null

parseIt

protected void parseIt(ParseReader in,
                       T target)
                throws IOException
Tries to parse the input text with the base parser. If successful the result object is stored in the target. If not successful, that is if a ParseException is thrown, nothing happens. All other exceptions are passed to the caller.

Specified by:
parseIt in class AbstractParser<T>
Parameters:
in - the position where to start parsing
target - place where the parsed object is stored.
Throws:
NullPointerException - if in or target or one of its (sub-) properties is null and the parser cannot handle it
BeanStoreException - if the parsed value could not be stored
IOException - if an I/O error occurs

writeIt

protected void writeIt(ParseWriter out,
                       T source)
                throws IOException
Tries to write the source object with the base parser. If successful the formatted text is written to the output. If not successful, that is if a FormatException is thrown, nothing happens. All other exceptions are passed to the caller.

Specified by:
writeIt in class AbstractParser<T>
Parameters:
out - the stream and position where to put the formatted text.
source - source object
Throws:
NullPointerException - if out is null
IOException - if an I/O error occurs

treeView

public String treeView()
Description copied from interface: Parser
Returns a tree view of this parser.

Returns:
a tree view of this parser