net.sf.japaki.self
Class ParserBench

java.lang.Object
  extended by net.sf.japaki.self.ParserBench

public class ParserBench
extends Object

This is the standard entry point to the japaki library. The typical usage is as follows:

  1. Create a new parser bench.
  2. Configure the parser store, if necessary.
  3. Add parser definitions, either by
  4. Create an instance of the topmost bean.
  5. Start parsing or formatting.
  6. Handle exceptions.


Constructor Summary
ParserBench()
           
 
Method Summary
<T> void
add(String name, Class<T> type, String pattern)
          Adds a new parser to the underlying parser store.
<T extends Enum<T>>
void
addEnum(Class<T> type)
          Creates a mold from an enum type: Looks for a property file in the class path at the same place as the class file of the given type.
 ParserStore getParserStore()
          Returns the underlying parser store.
static void handleException(Exception e)
          Performs the default way to handle exceptions that occur during parsing or formatting.
 void loadSyntax(String fileName)
          Loads parsers definitions from a file and stores them in the underlying parser store.
<T> void
loadSyntaxFromResource(String fileName)
          Loads parsers definitions from a file in the classpath and stores them in the underlying parser store.
<T> void
parse(String parserName, Reader reader, T target)
          Performs the following steps: The parser is looked up in the parser store.
<T> void
parse(String parserName, String fileName, T target)
          Performs the following steps: The parser is looked up in the parser store.
 void setInspectorMill(InspectorMill inspectorMill)
          Specifies the inspector mill to be used.
static void useLoggingDefaults()
          Initializes the logging mechanism.
<T> void
write(String parserName, String fileName, T source)
          Performs the following steps: The parser is looked up in the parser store.
<T> void
write(String parserName, Writer writer, T source)
          Performs the following steps: The parser is looked up in the parser store.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ParserBench

public ParserBench()
Method Detail

setInspectorMill

public void setInspectorMill(InspectorMill inspectorMill)
Specifies the inspector mill to be used.

Parameters:
inspectorMill - new inspector mill

getParserStore

public ParserStore getParserStore()
Returns the underlying parser store.

Returns:
the underlying parser store

addEnum

public <T extends Enum<T>> void addEnum(Class<T> type)
Creates a mold from an enum type:
  1. Looks for a property file in the class path at the same place as the class file of the given type.
  2. Creates a new enum format from the property file.
  3. Creates a mold from the format and stores it in the mold repository under the simple name of the type.
  4. The created mold is also default for the specified type.

Parameters:
type - an enum type
Throws:
NullPointerException - if type is null
DuplicateKeyException - if there is already an entry for the name
MissingResourceException - if no resource bundle for the specified base name can be found
See Also:
ResourceBundle

add

public <T> void add(String name,
                    Class<T> type,
                    String pattern)
         throws IOException,
                ParseException
Adds a new parser to the underlying parser store.

Parameters:
name - name of the new parser
type - the class that the parse can handle
pattern - pattern representation of the new parser, the syntax is described in SyntaxParser.
Throws:
NullPointerException - if type is null or if a parameter in the pattern is missing
DuplicateKeyException - if there is already an entry for the name
IllegalArgumentException - if a parameter in the pattern can not be used to create a parser
NoSuchPropertyException - if the pattern contains an invalid property name
ParseException - when an exception occured while parsing the pattern
IOException - If an I/O error occurs

loadSyntax

public void loadSyntax(String fileName)
                throws IOException,
                       ParseException
Loads parsers definitions from a file and stores them in the underlying parser store.
The file must contain entries of the form
name,class := pattern
where

Parameters:
fileName - name of the syntax definition file
Throws:
BeanStoreException - if the parsed value could not be stored
IOException - If an I/O error occurs
ParseException - when an exception occured while parsing the file

loadSyntaxFromResource

public <T> void loadSyntaxFromResource(String fileName)
                            throws IOException,
                                   ParseException
Loads parsers definitions from a file in the classpath and stores them in the underlying parser store.

Parameters:
fileName - name of the syntax definition file
Throws:
BeanStoreException - if the parsed value could not be stored
IOException - If an I/O error occurs
ParseException - when an exception occured while parsing the file
See Also:
loadSyntax(String)

parse

public <T> void parse(String parserName,
                      Reader reader,
                      T target)
           throws IOException,
                  ParseException
Performs the following steps:
  1. The parser is looked up in the parser store.
  2. The content of the specified reader is parsed.
  3. The parsing result is stored in the target object.

Parameters:
parserName - name of the parser to be used
reader - reader with the text to be parsed
target - object where the parsing result is stored
Throws:
NullPointerException - if reader or target or one of its (sub-) properties is null and the parser cannot handle it
IllegalArgumentException - if a parser of the given name is not found in the store
BeanStoreException - if the parsed value could not be stored
IOException - If an I/O error occurs
ParseException - if the reader content could not be parsed

parse

public <T> void parse(String parserName,
                      String fileName,
                      T target)
           throws IOException,
                  ParseException
Performs the following steps:
  1. The parser is looked up in the parser store.
  2. The content of the specified file is parsed.
  3. The parsing result is stored in the target object.

Parameters:
parserName - name of the parser to be used
fileName - name of the file with the text to be parsed
target - object where the parsing result is stored
Throws:
NullPointerException - if target or one of its (sub-) properties is null and the parser cannot handle it
IllegalArgumentException - if a parser of the given name is not found in the store
BeanStoreException - if the parsed value could not be stored
IOException - If an I/O error occurs
ParseException - if the file content could not be parsed

write

public <T> void write(String parserName,
                      Writer writer,
                      T source)
           throws IOException,
                  FormatException
Performs the following steps:
  1. The parser is looked up in the parser store.
  2. The source object is formatted with the found parser.
  3. The result is written to the specified writer.

Parameters:
parserName - name of the parser to be used
writer - writer where the formatted source object is written to
source - object to be formatted
Throws:
NullPointerException - if writer or source or one of its (sub-) properties is null and the parser cannot handle it
IllegalArgumentException - if a parser of the given name is not found in the store or if the parser cannot handle the source object
IOException - If an I/O error occurs
FormatException - if the source object can not be formatted with this parser.

write

public <T> void write(String parserName,
                      String fileName,
                      T source)
           throws IOException,
                  FormatException
Performs the following steps:
  1. The parser is looked up in the parser store.
  2. The source object is formatted with the found parser.
  3. The result is written to the specified file.

Parameters:
parserName - name of the parser to be used
fileName - name of the file where the formatted source object is written to
source - object to be formatted
Throws:
NullPointerException - if source or one of its (sub-) properties is null and the parser cannot handle it
IllegalArgumentException - if a parser of the given name is not found in the store or if the parser cannot handle the source object
IOException - If an I/O error occurs
FormatException - if the source object can not be formatted with this parser.

handleException

public static void handleException(Exception e)
                            throws RuntimeException
Performs the default way to handle exceptions that occur during parsing or formatting.

Parameters:
e - an exception that was thrown
Throws:
RuntimeException - RuntimeExceptions are not handled by this method, but passed on.

useLoggingDefaults

public static void useLoggingDefaults()
Initializes the logging mechanism. Messages are handled by a ConsoleHandler.