Package edu.uky.ai.io
Class Parser
java.lang.Object
edu.uky.ai.io.Parser
- All Implemented Interfaces:
java.lang.Cloneable
public class Parser
extends java.lang.Object
implements java.lang.Cloneable
Converts text into various kinds of objects based on customizable rules.
- Author:
- Stephen G. Ware
-
Constructor Summary
Constructors Constructor Description Parser()
Constructs a new parser with no rules or defined objects. -
Method Summary
Modifier and Type Method Description Parser
clone()
<E> E
getDefined(java.lang.String name, java.lang.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>
getParser(java.lang.Class<E> type)
Returns the @{link ObjectParser} responsible for parsing objects of the given type.<E> E
parse(Node node, java.lang.Class<? extends E> type, java.lang.Class<? extends E>... types)
Parses a givenNode
according to the rules defined by this parser and returns the object created.<E> E
parse(java.io.File file, java.lang.Class<? extends E> type)
Parses a given file according to the rules defined by this parser and returns the object created.<E> E
parse(java.lang.String string, java.lang.Class<? extends E> type)
Parses a given string according to the rules defined by this parser and returns the object created.<E> E
requireDefined(java.lang.String name, java.lang.Class<E> type)
LikegetDefined(String, Class)
but throws an exception if no object is found.void
setDefined(java.lang.String name, java.lang.Object object)
Defines an object by name.<E> void
setParser(java.lang.Class<E> type, ObjectParser<? extends E> parser)
Sets theObjectParser
for a the given type.
-
Constructor Details
-
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 theObjectParser
for 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
public <E> E getDefined(java.lang.String name, java.lang.Class<E> type)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
public <E> E requireDefined(java.lang.String name, java.lang.Class<E> type)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
public void setDefined(java.lang.String name, java.lang.Object object)Defines an object by name.- Parameters:
name
- the name of the object being definedobject
- the object (must be non-null)
-
parse
public <E> E parse(java.io.File file, java.lang.Class<? extends E> type) throws java.io.IOExceptionParses 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:
java.io.IOException
- if a problem occurs when reading the fileFormatException
- if a problem occurs while parsing the file
-
parse
public <E> E parse(java.lang.String string, java.lang.Class<? extends E> type)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, java.lang.Class<? extends E> type, java.lang.Class<? extends E>... types)Parses a givenNode
according 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
-