Class TemplateRunner


  • public class TemplateRunner
    extends java.lang.Object
    Class for processing html templates. An html template is an ordinary html string, with additional application specific tags added (sort of like XML). Each tag is mapped to a java method of a template class, that rewrites its tag into normal html.

    The mechanism used to map templates into sessions is inadaquate, and should be fixed in a future version. In the current implementation, Each session maintains its own set of template instances. Instance variables in template classes may be used to hold session specific state. Calls to a template are synchronized on the session id; only one request per session is dealt with simultaneously.

    Version:
    %W
    Author:
    Colin Stevens, Stephen Uhler
    • Constructor Summary

      Constructors 
      Constructor Description
      TemplateRunner​(Server server, java.lang.String prefix, java.lang.String names)
      Process an HTML template with a template processing class.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.String getError()
      Return the last error message generated, or null of no errors have occurred since the last call to "process".
      java.lang.String process​(Request request, java.lang.String content, java.lang.String sessionId)
      Process an html template file, using the supplied template processing Return the content of the template just processed, or null if there was no template processed.
      void process​(RewriteContext hr)
      Processes the next token in the HTML document represented by the given RewriteContext.
      int tagsProcessed()
      Return the # of tags replaced in the previous call to "process".
      int tagsSeen()
      Return the # of HTML tags seen in the previous call to "process".
      protected Template templateFromTag​(RewriteContext hr, java.lang.String tag)
      Return the object instance of the template that will process this tag (if any).
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • TemplateRunner

        public TemplateRunner​(Server server,
                              java.lang.String prefix,
                              java.lang.String names)
                       throws java.lang.ClassNotFoundException,
                              java.lang.ClassCastException
        Process an HTML template with a template processing class. We peruse the template class, looking at all of its methods. When when we process a template, we match the html tags against the declared methods in the template class. Each method name of the form tag_xxx or tag_slash_xxx is invoked when the corrosponding <xxx> or </xxx> tag is found.

        Each instance of _xnn in the method name is replaced by the corrosponding hex code for the character. This allows non-printable tags to to be processed with templates.

        The methods init and done are each called once, at the beginning and end of the page respectively. These methods are called for all templates, in the order they are specified in the templates parameter.

        There are three methods that may be defined that don't follow the naming convention described above. They are:

        • comment
          is called for each html/XML comment.
        • string
          is called for all text between any tags.
        • defaultTag
          is called for every tag that does not specifically have a tag method. If more than one template defines one of these methods, only the first template's method will be called.

        If the server property "tagPrefix" associated with each template's properties prefix exists, it is used to prefix each tag name (this feature is for experimental support of XML namespaces, and probably doesn't belong here).

        Parameters:
        server - The HTTP server that created the Handler or Filter that invoked this TemplateRunner
        prefix - The prefix associated with the parent Handler or Filter
        names - The names of the Template classes or property prefixes (i.e. tokens) that, when concatenated with ".class" define a property that names a Template class. This TemplateRunner will dispatch to the methods described by the union of all the tag methods in the given Template classes.

        The init and done methods for each template specified will be called in order. If any of the calls returns false, this handler terminates and no output is generated.

        The names "comment", "string", and "defaultTag" are handled specially.

        Throws:
        java.lang.ClassNotFoundException
        java.lang.ClassCastException
    • Method Detail

      • process

        public java.lang.String process​(Request request,
                                        java.lang.String content,
                                        java.lang.String sessionId)
        Process an html template file, using the supplied template processing Return the content of the template just processed, or null if there was no template processed.
        Parameters:
        content - The template.
        sessionId - An arbitrary identifier used to locate the correct instance of the template class for processing this template. The first time an identifier is used, a new instance is created.
        args - The arguments passed to the templates init method.
        Returns:
        content or null
      • process

        public void process​(RewriteContext hr)
        Processes the next token in the HTML document represented by the given RewriteContext. Processing a token involves either dispatching to a tag-handling method in one of the Template objects, or just advancing to the next token if no Template was interested.
        Parameters:
        hr - The RewriteContext holding the state of the HTML document.
      • getError

        public java.lang.String getError()
        Return the last error message generated, or null of no errors have occurred since the last call to "process". XXX not thread safe between calls to process() and getError().
      • tagsSeen

        public int tagsSeen()
        Return the # of HTML tags seen in the previous call to "process".
      • tagsProcessed

        public int tagsProcessed()
        Return the # of tags replaced in the previous call to "process".
      • templateFromTag

        protected Template templateFromTag​(RewriteContext hr,
                                           java.lang.String tag)
        Return the object instance of the template that will process this tag (if any). This allows templates to cooperate with each other. If you need to use this method, then one of us did something wrong.