Package edu.uky.ai.io

Class Parser

java.lang.Object
edu.uky.ai.io.Parser
All Implemented Interfaces:
Cloneable
Direct Known Subclasses:
LogicParser

public class Parser extends Object implements Cloneable
Converts text into various kinds of objects based on customizable rules.
Author:
Stephen G. Ware
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
     
    Constructs a new parser with no rules or defined objects.
    protected
    Parser(Parser toClone)
    Constructs a new parser that is a clone of the given parser.
  • Method Summary

    Modifier and Type
    Method
    Description
     
    <E> E
    getDefined(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>
    getParser(Class<E> type)
    Returns the @{link ObjectParser} responsible for parsing objects of the given type.
    final <E> E
    parse(Node node, Class<? extends E> type, Class<? extends E>... types)
    Parses a given Node according to the rules defined by this parser and returns the object created.
    <E> E
    parse(File file, Class<? extends E> type)
    Parses a given file according to the rules defined by this parser and returns the object created.
    <E> E
    parse(String string, Class<? extends E> type)
    Parses a given string according to the rules defined by this parser and returns the object created.
    <E> E
    requireDefined(String name, Class<E> type)
    Like getDefined(String, Class) but throws an exception if no object is found.
    void
    setDefined(String name, Object object)
    Defines an object by name.
    <E> void
    setParser(Class<E> type, ObjectParser<? extends E> parser)
    Sets the ObjectParser for a the given type.

    Methods inherited from class java.lang.Object

    equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Parser

      protected Parser(Parser toClone)
      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

      public Parser clone()
      Overrides:
      clone in class Object
    • getParser

      public <E> ObjectParser<? extends E> getParser(Class<E> type)
      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

      public <E> void setParser(Class<E> type, ObjectParser<? extends E> parser)
      Sets the ObjectParser 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 handle
      parser - the object parser
    • getDefined

      public <E> E getDefined(String name, 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 object
      type - the type of the desired object
      Returns:
      the defined object, or null if no such defined object exists
    • requireDefined

      public <E> E requireDefined(String name, Class<E> type)
      Like getDefined(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 object
      type - the type of the desired object
      Returns:
      the defined object
      Throws:
      FormatException - if no defined object is found
    • setDefined

      public void setDefined(String name, Object object)
      Defines an object by name.
      Parameters:
      name - the name of the object being defined
      object - the object (must be non-null)
    • parse

      public <E> E parse(File file, Class<? extends E> type) throws IOException
      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 parse
      type - 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 file
      FormatException - if a problem occurs while parsing the file
    • parse

      public <E> E parse(String string, 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 parse
      type - 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 given Node 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 parse
      type - the type of object this node should be parsed as
      types - 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