Package edu.uky.ai.io
Class Parser
java.lang.Object
edu.uky.ai.io.Parser
- All Implemented Interfaces:
Cloneable
- Direct Known Subclasses:
LogicParser
Converts text into various kinds of objects based on customizable rules.
- Author:
- Stephen G. Ware
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionclone()<E> EgetDefined(String name, Class<E> type) Returns the most recently defined object whose names matches the given name and which is of a given type.<E> ObjectParser<? extends E>Returns the @{link ObjectParser} responsible for parsing objects of the given type.final <E> EParses a givenNodeaccording to the rules defined by this parser and returns the object created.<E> EParses a given file according to the rules defined by this parser and returns the object created.<E> EParses a given string according to the rules defined by this parser and returns the object created.<E> ErequireDefined(String name, Class<E> type) LikegetDefined(String, Class)but throws an exception if no object is found.voidsetDefined(String name, Object object) Defines an object by name.<E> voidsetParser(Class<E> type, ObjectParser<? extends E> parser) Sets theObjectParserfor a the given type.
-
Constructor Details
-
Parser
Constructs a new parser that is a clone of the given parser.- Parameters:
toClone- the parser to clone
-
Parser
public Parser()Constructs a new parser with no rules or defined objects.
-
-
Method Details
-
clone
-
getParser
Returns the @{link ObjectParser} responsible for parsing objects of the given type.- Type Parameters:
E- the type of object to be returned- Parameters:
type- the class for which a parser is needed- Returns:
- the object parser for that class
-
setParser
Sets theObjectParserfor a the given type.- Type Parameters:
E- the type of object the object parser will handle- Parameters:
type- the class this object parser will handleparser- the object parser
-
getDefined
Returns the most recently defined object whose names matches the given name and which is of a given type.- Type Parameters:
E- the type of the desired object- Parameters:
name- the name of the desired objecttype- the type of the desired object- Returns:
- the defined object, or null if no such defined object exists
-
requireDefined
LikegetDefined(String, Class)but throws an exception if no object is found.- Type Parameters:
E- the type of the desired object- Parameters:
name- the name of the desired objecttype- the type of the desired object- Returns:
- the defined object
- Throws:
FormatException- if no defined object is found
-
setDefined
Defines an object by name.- Parameters:
name- the name of the object being definedobject- the object (must be non-null)
-
parse
Parses a given file according to the rules defined by this parser and returns the object created.- Type Parameters:
E- the type of object to be returned- Parameters:
file- the file to parsetype- the type of object this file should be parsed as- Returns:
- the object that was created
- Throws:
IOException- if a problem occurs when reading the fileFormatException- if a problem occurs while parsing the file
-
parse
Parses a given string according to the rules defined by this parser and returns the object created.- Type Parameters:
E- the type of object to be returned- Parameters:
string- the string to parsetype- the type of object this string should be parsed as- Returns:
- the object that was created
- Throws:
FormatException- if a problem occurs while parsing the string
-
parse
@SafeVarargs public final <E> E parse(Node node, Class<? extends E> type, Class<? extends E>... types) Parses a givenNodeaccording to the rules defined by this parser and returns the object created.- Type Parameters:
E- the type of object to be returned- Parameters:
node- the node to parsetype- the type of object this node should be parsed astypes- optional additional types to try to parse the node as if it cannot be parsed as the first type- Returns:
- the object that was created
- Throws:
FormatException- if a problem occurs while parsing the node
-