sunlabs.brazil.template
Class TemplateRunner

java.lang.Object
  extended by sunlabs.brazil.template.TemplateRunner (view source)

public class TemplateRunner
extends 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.


Constructor Summary
TemplateRunner(Server server, String prefix, String names)
          Process an HTML template with a template processing class.
 
Method Summary
 String getError()
          Return the last error message generated, or null of no errors have occurred since the last call to "process".
 String process(Request request, String content, 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".
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TemplateRunner

public TemplateRunner(Server server,
                      String prefix,
                      String names)
               throws ClassNotFoundException,
                      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:

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:
ClassNotFoundException
ClassCastException
Method Detail

process

public String process(Request request,
                      String content,
                      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:
request - The HTTP request
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.
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 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".


Version Kenai-svn-r24, Generated 08/18/09
Copyright (c) 2001-2009, Sun Microsystems.