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 given Node 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)
    Like getDefined(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 the ObjectParser for a the given type.

    Methods inherited from class java.lang.Object

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

    • Parser

      public Parser()
      Constructs a new parser with no rules or defined objects.
  • Method Details

    • clone

      public Parser clone()
    • getParser

      public <E> ObjectParser<? extends E> getParser​(java.lang.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​(java.lang.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​(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 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​(java.lang.String name, java.lang.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​(java.lang.String name, java.lang.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​(java.io.File file, java.lang.Class<? extends E> type) throws java.io.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:
      java.io.IOException - if a problem occurs when reading the file
      FormatException - 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 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, java.lang.Class<? extends E> type, java.lang.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