bsh.servlet
Class SimpleTemplate

java.lang.Object
  |
  +--bsh.servlet.SimpleTemplate

public class SimpleTemplate
extends java.lang.Object

This is a simple template engine. An instance of SimpleTemplate wraps a StringBuffer and performs replace operations on one or more parameters embedded as HMTL style comments. The value can then be retrieved as a String or written to a stream. Template values in the text are of the form: Substitutions then take the form of: template.replace( "NAME", value ); Two static util methods are provided to help read the text of a template from a stream (perhaps a URL or resource). e.g.

Author:
Pat Niemeyer

Field Summary
(package private)  java.lang.StringBuffer buff
           
(package private) static boolean cacheTemplates
           
(package private) static java.lang.String NO_TEMPLATE
           
(package private) static java.util.Map templateData
           
 
Constructor Summary
SimpleTemplate(java.io.Reader reader)
           
SimpleTemplate(java.lang.String template)
           
SimpleTemplate(java.net.URL url)
           
 
Method Summary
(package private)  int[] findTemplate(java.lang.String name)
          Find the starting (inclusive) and ending (exclusive) index of the named template and return them as a two element int [].
static java.lang.String getStringFromStream(java.io.InputStream ins)
           
static java.lang.String getStringFromStream(java.io.Reader reader)
           
static SimpleTemplate getTemplate(java.lang.String file)
          Get a template by name, with caching.
private  void init(java.lang.String s)
           
static void main(java.lang.String[] args)
          usage: filename param value
 void replace(java.lang.String param, java.lang.String value)
          Substitute the specified text for the parameter
static void setCacheTemplates(boolean b)
           
 java.lang.String toString()
           
 void write(java.io.PrintStream out)
           
 void write(java.io.PrintWriter out)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

buff

java.lang.StringBuffer buff

NO_TEMPLATE

static java.lang.String NO_TEMPLATE

templateData

static java.util.Map templateData

cacheTemplates

static boolean cacheTemplates
Constructor Detail

SimpleTemplate

public SimpleTemplate(java.lang.String template)

SimpleTemplate

public SimpleTemplate(java.io.Reader reader)
               throws java.io.IOException

SimpleTemplate

public SimpleTemplate(java.net.URL url)
               throws java.io.IOException
Method Detail

getTemplate

public static SimpleTemplate getTemplate(java.lang.String file)
Get a template by name, with caching. Note: this should be updated to search the resource path first, etc. Create a new instance of a template from the specified file. The file text is cached so lookup is fast. Failure to find the file is also cached so the read will not happen twice.


getStringFromStream

public static java.lang.String getStringFromStream(java.io.InputStream ins)
                                            throws java.io.IOException
java.io.IOException

getStringFromStream

public static java.lang.String getStringFromStream(java.io.Reader reader)
                                            throws java.io.IOException
java.io.IOException

init

private void init(java.lang.String s)

replace

public void replace(java.lang.String param,
                    java.lang.String value)
Substitute the specified text for the parameter


findTemplate

int[] findTemplate(java.lang.String name)
Find the starting (inclusive) and ending (exclusive) index of the named template and return them as a two element int []. Or return null if the param is not found.


toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

write

public void write(java.io.PrintWriter out)

write

public void write(java.io.PrintStream out)

main

public static void main(java.lang.String[] args)
                 throws java.io.IOException
usage: filename param value

java.io.IOException

setCacheTemplates

public static void setCacheTemplates(boolean b)