org.aitools.programd.parser
Class GenericParser<P extends Processor>

java.lang.Object
  extended by org.aitools.programd.parser.GenericParser<P>
Type Parameters:
P - the base class of Processor to be used
Direct Known Subclasses:
BotsConfigurationFileParser, TemplateParser

public abstract class GenericParser<P extends Processor>
extends java.lang.Object

A generic parser that allows us to register processors for any element type. This has been heavily modified (simplified) to use DOM.

Since:
4.1.3
Author:
Noel Bush

Field Summary
protected static java.lang.String COMMA
          A comma, for convenience.
protected  Core core
          The Core in use.
protected  java.util.Stack<java.net.URL> docURL
          The URL of this document.
protected static java.lang.String EMPTY_STRING
          An empty string, for convenience.
protected static java.lang.String INDEX
          The word "index", for convenience.
protected  org.apache.log4j.Logger logger
          The logger to use.
private  ProcessorRegistry<P> processorRegistry
          Set by subclasses.
protected static javax.xml.parsers.DocumentBuilder utilDocBuilder
          A DocumentBuilder for producing new Documents.
 
Constructor Summary
GenericParser(ProcessorRegistry<P> registry, Core coreToUse)
          Creates a new GenericParser with the given Core as its owner.
 
Method Summary
private  void contextualize(java.net.URL url)
          Pushes the given URL onto the docURL stack, contextualizing the given URL with respect to the current docURL (if set).
 java.lang.String evaluate(org.w3c.dom.Document document)
          Evaluates the given document and returns the result.
 java.lang.String evaluate(org.w3c.dom.Node node)
           Recursively evaluates a tree.
 java.lang.String evaluate(org.w3c.dom.NodeList list)
          Evaluates the given node list and returns the result.
 Core getCore()
           
 java.net.URL getCurrentDocURL()
           
static int getValid1dIndex(org.w3c.dom.Element element)
          Corrects a tag to use a valid 1-dimensional index, and returns the index.
static int[] getValid2dIndex(org.w3c.dom.Element element)
          Corrects a tag to use a valid 2-dimensional index, and returns the indices.
protected  java.lang.String handleUnknownElement(org.w3c.dom.Element element, NotARegisteredClassException e)
          Handles an unknown element.
private  org.w3c.dom.Document parseCurrentURL()
          Processes the current document URL.
 void process(java.net.URL url)
          Processes whatever is at the given URL, returning nothing.
 java.lang.String processElement(org.w3c.dom.Element element)
          Processes a given XML node.
 java.lang.String processResponse(java.lang.String input)
          Processes an XML fragment provided in a string.
 java.lang.String processResponse(java.net.URL url)
          Processes whatever is at the given URL and returns a response.
 java.lang.String shortcutTag(org.w3c.dom.Element element, java.lang.String newElementName, java.lang.String childContent, short childType)
           Creates a "mini-template" with a given tag and an optional child tag, then evaluates it recursively.
 void verifyAndProcess(java.lang.String urlString)
          Verifies that the given URL points to something real/accessible, then processes it (not returning anything).
 void verifyAndProcess(java.net.URL url)
          Verifies that the given URL points to something real/accessible, then processes it (not returning anything).
 java.lang.String verifyAndProcessResponse(java.lang.String urlString)
          Verifies that the given URL points to something real/accessible, then processes it, returning a response.
 java.lang.String verifyAndProcessResponse(java.net.URL url)
          Verifies that the given URL points to something real/accessible, then processes it (not returning anything).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

processorRegistry

private ProcessorRegistry<P extends Processor> processorRegistry
Set by subclasses.


docURL

protected java.util.Stack<java.net.URL> docURL
The URL of this document.


core

protected Core core
The Core in use.


logger

protected org.apache.log4j.Logger logger
The logger to use.


EMPTY_STRING

protected static final java.lang.String EMPTY_STRING
An empty string, for convenience.

See Also:
Constant Field Values

INDEX

protected static final java.lang.String INDEX
The word "index", for convenience.

See Also:
Constant Field Values

COMMA

protected static final java.lang.String COMMA
A comma, for convenience.

See Also:
Constant Field Values

utilDocBuilder

protected static javax.xml.parsers.DocumentBuilder utilDocBuilder
A DocumentBuilder for producing new Documents.

Constructor Detail

GenericParser

public GenericParser(ProcessorRegistry<P> registry,
                     Core coreToUse)
Creates a new GenericParser with the given Core as its owner.

Parameters:
registry - the registry of processors
coreToUse - the Core that owns this
Method Detail

parseCurrentURL

private org.w3c.dom.Document parseCurrentURL()
                                      throws ProcessorException
Processes the current document URL. This is an internal method.

Returns:
the DOM produced by parsing
Throws:
ProcessorException - if the content cannot be processed

processResponse

public java.lang.String processResponse(java.net.URL url)
                                 throws ProcessorException
Processes whatever is at the given URL and returns a response.

Parameters:
url - where to find what is to be processed
Returns:
the result of processing whatever is found at the URL
Throws:
ProcessorException - if there is a problem processing what is found at the URL

process

public void process(java.net.URL url)
             throws ProcessorException
Processes whatever is at the given URL, returning nothing.

Parameters:
url - the URL at which to find whatever is to be processed
Throws:
ProcessorException - if there is a problem processing whatever is at the given URL

contextualize

private void contextualize(java.net.URL url)
Pushes the given URL onto the docURL stack, contextualizing the given URL with respect to the current docURL (if set).

Parameters:
url -

processResponse

public java.lang.String processResponse(java.lang.String input)
                                 throws ProcessorException
Processes an XML fragment provided in a string.

Parameters:
input - the string from which to create the document fragment
Returns:
the result of processing the document fragment created from the given string
Throws:
ProcessorException - if there was a problem processing the document fragment created from the given string

processElement

public java.lang.String processElement(org.w3c.dom.Element element)
                                throws ProcessorException
Processes a given XML node.

Parameters:
element - the element being evaluated
Returns:
the result of processing the element
Throws:
ProcessorException - if the content cannot be processed

handleUnknownElement

protected java.lang.String handleUnknownElement(org.w3c.dom.Element element,
                                                NotARegisteredClassException e)
Handles an unknown element. By default it throws an exception. This can be overridden by subclasses that want to do something else, like ignore it, and/or just print a warning message.

Parameters:
element - the element that could not be handled
e - the exception generated by trying to find a processor for the element
Returns:
the result of handling the unknown element (unless, of course, an exception is throw)

evaluate

public java.lang.String evaluate(org.w3c.dom.Document document)
                          throws ProcessorException
Evaluates the given document and returns the result.

Parameters:
document - the document to evaluate
Returns:
the result of evaluating the document
Throws:
ProcessorException - if there is an error in processing

evaluate

public java.lang.String evaluate(org.w3c.dom.NodeList list)
                          throws ProcessorException
Evaluates the given node list and returns the result.

Parameters:
list - the list of nodes to evaluate
Returns:
the result of evaluating the given list of nodes
Throws:
ProcessorException - if there is an error in processing

evaluate

public java.lang.String evaluate(org.w3c.dom.Node node)
                          throws ProcessorException

Recursively evaluates a tree.

Depending on whether the engine is at load-time or run time, determines whether or not to process the tag. When the tag is processed, returns the result of processing it.

Parameters:
node - the node to parse
Returns:
the result of processing the tag
Throws:
ProcessorException - if there is an error in processing

shortcutTag

public java.lang.String shortcutTag(org.w3c.dom.Element element,
                                    java.lang.String newElementName,
                                    java.lang.String childContent,
                                    short childType)
                             throws ProcessorException

Creates a "mini-template" with a given tag and an optional child tag, then evaluates it recursively.

This method is used to map certain tags as combinations of other tags (as in short-cut elements ).

Parameters:
element - the element to modify
newElementName - the new name to give the element
childContent - the name or content for the child to add
childType - the type of the child
Returns:
the result of processing this structure
Throws:
ProcessorException - if there is an error in processing

verifyAndProcess

public void verifyAndProcess(java.lang.String urlString)
                      throws ProcessorException
Verifies that the given URL points to something real/accessible, then processes it (not returning anything).

Parameters:
urlString - the URL to check
Throws:
ProcessorException - if there is an error in processing

verifyAndProcessResponse

public java.lang.String verifyAndProcessResponse(java.lang.String urlString)
                                          throws ProcessorException
Verifies that the given URL points to something real/accessible, then processes it, returning a response.

Parameters:
urlString - the URL to check
Returns:
the response produced by processing whatever is at the URL
Throws:
ProcessorException - if there is an error in processing

verifyAndProcess

public void verifyAndProcess(java.net.URL url)
                      throws ProcessorException
Verifies that the given URL points to something real/accessible, then processes it (not returning anything).

Parameters:
url - the URL to check
Throws:
ProcessorException - if there is an error in processing

verifyAndProcessResponse

public java.lang.String verifyAndProcessResponse(java.net.URL url)
                                          throws ProcessorException
Verifies that the given URL points to something real/accessible, then processes it (not returning anything).

Parameters:
url - the URL to check
Returns:
the result of processing whatever is at the URL
Throws:
ProcessorException - if there is an error in processing

getValid2dIndex

public static int[] getValid2dIndex(org.w3c.dom.Element element)
Corrects a tag to use a valid 2-dimensional index, and returns the indices. If either index is invalid or missing, it is set to 1.

Parameters:
element - the element for which to get a valid 2-dimensional index
Returns:
a valid 2-dimensional index
Since:
4.1.3

getValid1dIndex

public static int getValid1dIndex(org.w3c.dom.Element element)
Corrects a tag to use a valid 1-dimensional index, and returns the index. If the index is missing or valid, 1 is returned.

Parameters:
element - the element for which to get a valid 1-dimensional index
Returns:
a valid 1-dimensional index
Since:
4.1.3

getCore

public Core getCore()
Returns:
the Core

getCurrentDocURL

public java.net.URL getCurrentDocURL()
Returns:
the current docURL