de.janicke.hj.tools.servlet
Class HTMLTemplate

java.lang.Object
  |
  +--de.janicke.hj.tools.servlet.HTMLTemplate

public class HTMLTemplate
extends java.lang.Object

HTMLTemplate is a class to replace parts in a HTML file with the value variables. The parts to be replaced are marked up as the following:
<!--[ VARNAME ]-->
"<!--[" and "]-->" are the delimiters of a variables name. These delimiters are specified by the constants BEGIN_VARIABLE and END_VARIABLE and must be used in the HTML template files. The substring in between is stripped of any leading and trailing spaces and then treated as the name of a variable. The value of the variable is retrieved out of a Hashtable (an implementation of hashtable) using the variable name as key. The recommended way to use this class is to generate a instance using the standard constructor and then put variables into the Hashtable using the put(String name, Object value). Values can be retrieved with the get(Object name) method. To build up a HTML template file use the buildTemplate(String htmlfile) method that actually does the replacement.
To spare time loading files it is also possible to load files into a hash table using the loadTemplate methods. Which way to choose mainly depends on the needed resources and memory available for this servlet. (Remember to set the VM parameters if you need more memory for the servlet!)
This Class supports a kind of localization. The loadFile and loadTemplate methods are also provided with an extra Locale object. If you plan to use localization please add for each language to support a subdirectory in your template directory named as the provided Locale: e.g. "en_EN". The "localized" methods then will look up the file in this directory instead of your default template directory. If localization is not supported (the localized template file doesn't exist) the default file will be taken.


Constructor Summary
HTMLTemplate()
          Constructs a new HTMLTemplate with an empty Hashtable for the variables.
HTMLTemplate(java.util.Hashtable variables)
          Constructs a new HTMLTemplate with the specified Hashtable for the variables.
 
Method Summary
 java.lang.String buildTemplate(java.lang.String htmlFile)
          Searches the htmlFile for the START_VARIABLE and ENDVARIABLE statements.
 void clear()
          Removes all variables, defaults and templates from the hash.
 void clearDefaultVariables()
          Removes all defaults from the hash.
 void clearTemplates()
          Removes all templates from the hash.
 void clearVariables()
          Removes all variabels from the hash.
 java.lang.Object get(java.lang.Object name)
          Returns the value of a variable.
 java.lang.Object getDefault(java.lang.Object name)
          Returns the default value of a variable.
 java.util.Hashtable getDefaultVariables()
          Returns the Hashtable used for the defaultVariables.
 java.lang.Object getTemplate(java.lang.Object key)
          Returns the HTML template that is associated with the key.
 java.util.Hashtable getVariables()
          Returns the Hashtable used for the variables.
 java.lang.String loadFile(java.lang.String path)
          Loads a File from the specified path.
 java.lang.String loadFile(java.lang.String path, java.util.Locale locale)
          Loads a File from the specified path using a Locale.
 java.lang.String loadTemplate(java.lang.String template)
          Loads a template file from the specified location and puts it into the Hash.
 java.lang.String loadTemplate(java.lang.String template, java.util.Locale locale)
          Loads a template file from the specified location and puts it into the Hash.
 java.lang.String makeTemplate(java.lang.Object key)
          Builds the template file with the specified key.
 java.lang.Object put(java.lang.String name, java.lang.Object value)
          Puts a variable into the Hashtable.
 java.lang.Object putDefault(java.lang.String name, java.lang.Object value)
          Puts the default for a variable into the Hashtable.
 java.lang.Object putTemplate(java.lang.Object key, java.lang.Object template)
          Puts a HTML template into the template hashtable.
 void setDefaultVariables(java.util.Hashtable defaultVariables)
          Sets the Hashtable for the defaultVariables.
 void setVariables(java.util.Hashtable variables)
          Sets the Hashtable for the variables.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HTMLTemplate

public HTMLTemplate()
Constructs a new HTMLTemplate with an empty Hashtable for the variables.

HTMLTemplate

public HTMLTemplate(java.util.Hashtable variables)
Constructs a new HTMLTemplate with the specified Hashtable for the variables.
Parameters:
variables - The Hashtable used for the variables.
Method Detail

getVariables

public java.util.Hashtable getVariables()
Returns the Hashtable used for the variables.
Returns:
The Hashtable for the variables.

setVariables

public void setVariables(java.util.Hashtable variables)
Sets the Hashtable for the variables.
Parameters:
the - new Hashtable for the variables.

getDefaultVariables

public java.util.Hashtable getDefaultVariables()
Returns the Hashtable used for the defaultVariables.
Returns:
The Hashtable for the defaultVariables.

setDefaultVariables

public void setDefaultVariables(java.util.Hashtable defaultVariables)
Sets the Hashtable for the defaultVariables.
Parameters:
the - new Hashtable for the defaultVariables.

clear

public void clear()
Removes all variables, defaults and templates from the hash.

clearVariables

public void clearVariables()
Removes all variabels from the hash.

clearDefaultVariables

public void clearDefaultVariables()
Removes all defaults from the hash.

clearTemplates

public void clearTemplates()
Removes all templates from the hash.

putDefault

public java.lang.Object putDefault(java.lang.String name,
                                   java.lang.Object value)
Puts the default for a variable into the Hashtable. If the supplied name is not a String the Object.toString() method is used to find the corresponding variable name in the HTML - file.
Parameters:
name - the name of the variable (which is used in the HTML - file.) This String will be stripped of leading and trailing spaces using the String.trim() method.
value - the value of the variable.
Returns:
The Object that was the previous default value of this variable.

getDefault

public java.lang.Object getDefault(java.lang.Object name)
Returns the default value of a variable.
Parameters:
name - The name of the variable.
Returns:
The defualt value of the variable, null if there is no entry for this variable.

put

public java.lang.Object put(java.lang.String name,
                            java.lang.Object value)
Puts a variable into the Hashtable. If the supplied name is not a String the Object.toString() method is used to find the corresponding variable name in the HTML - file.
Parameters:
name - the name of the variable (which is used in the HTML - file.) This String will be stripped of leading and trailing spaces using the String.trim() method.
value - the value of the variable.
Returns:
The Object that was the previous value of this variable.

get

public java.lang.Object get(java.lang.Object name)
Returns the value of a variable.
Parameters:
name - The name of the variable.
Returns:
The value of the variable, null if there is no entry for this variable.

putTemplate

public java.lang.Object putTemplate(java.lang.Object key,
                                    java.lang.Object template)
Puts a HTML template into the template hashtable.
Parameters:
key - The key for the template file.
template - The template file.
Returns:
The template file that was previously associated with that key.

getTemplate

public java.lang.Object getTemplate(java.lang.Object key)
Returns the HTML template that is associated with the key.
Parameters:
key - The key for the template file.
Returns:
The template file.

makeTemplate

public java.lang.String makeTemplate(java.lang.Object key)
                              throws java.lang.NullPointerException,
                                     java.text.ParseException
Builds the template file with the specified key. Get the template file out of the hash and calls buildTemplate(...).
Parameters:
key - The key of the template file.
Returns:
The build template file. null if the key wasn't found.

loadTemplate

public java.lang.String loadTemplate(java.lang.String template)
                              throws java.io.IOException
Loads a template file from the specified location and puts it into the Hash. The key is the template parameter.
Parameters:
template - The path of the template file.
Returns:
The key the file has in the Hash.

loadTemplate

public java.lang.String loadTemplate(java.lang.String template,
                                     java.util.Locale locale)
                              throws java.io.IOException
Loads a template file from the specified location and puts it into the Hash.
Parameters:
template - The path of the template file.
locale - The local to be used to laod the file
Returns:
The key the file has in the Hash.
See Also:
loadFile(String path, Locale locale)

loadFile

public java.lang.String loadFile(java.lang.String path)
                          throws java.io.IOException,
                                 java.io.FileNotFoundException
Loads a File from the specified path.
Parameters:
path - The path of the file to open.
Returns:
The String holding the contents of the file.

loadFile

public java.lang.String loadFile(java.lang.String path,
                                 java.util.Locale locale)
                          throws java.io.IOException,
                                 java.io.FileNotFoundException
Loads a File from the specified path using a Locale. This method tries to find a subdirectory with the locale identity e.g. "de_DE". Actually the Locale.toString() method is used to look up the directory. If the subdirectory exist it loads the file in the specified locale directory. if the subdirectory is not found or the specified file doesn't exits in this directory it tries to open the default file specified by path.
Parameters:
path - The path of the file to open.
locale - The locale identifying the directory to load the template from.
Returns:
The String holding the contents of the file.

buildTemplate

public java.lang.String buildTemplate(java.lang.String htmlFile)
                               throws java.lang.NullPointerException,
                                      java.text.ParseException
Searches the htmlFile for the START_VARIABLE and ENDVARIABLE statements. The token in between is interpreted as the variable name. If such a token is found, it is replaced by the value of the variable. if there is a variable encountered in the HTML file with no corresponding variable in the Hashtable, then a Exception is thrown.
Parameters:
htmlFile - The HTML template file.
Returns:
The html file with replaced values.