|
|||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--de.janicke.hj.tools.servlet.HTMLTemplate
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 |
public HTMLTemplate()
public HTMLTemplate(java.util.Hashtable variables)
variables
- The Hashtable used for the variables.Method Detail |
public java.util.Hashtable getVariables()
public void setVariables(java.util.Hashtable variables)
the
- new Hashtable for the variables.public java.util.Hashtable getDefaultVariables()
public void setDefaultVariables(java.util.Hashtable defaultVariables)
the
- new Hashtable for the defaultVariables.public void clear()
public void clearVariables()
public void clearDefaultVariables()
public void clearTemplates()
public java.lang.Object putDefault(java.lang.String name, java.lang.Object value)
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.public java.lang.Object getDefault(java.lang.Object name)
name
- The name of the variable.public java.lang.Object put(java.lang.String name, java.lang.Object value)
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.public java.lang.Object get(java.lang.Object name)
name
- The name of the variable.public java.lang.Object putTemplate(java.lang.Object key, java.lang.Object template)
key
- The key for the template file.template
- The template file.public java.lang.Object getTemplate(java.lang.Object key)
key
- The key for the template file.public java.lang.String makeTemplate(java.lang.Object key) throws java.lang.NullPointerException, java.text.ParseException
key
- The key of the template file.public java.lang.String loadTemplate(java.lang.String template) throws java.io.IOException
template
- The path of the template file.public java.lang.String loadTemplate(java.lang.String template, java.util.Locale locale) throws java.io.IOException
template
- The path of the template file.locale
- The local to be used to laod the fileloadFile(String path, Locale locale)
public java.lang.String loadFile(java.lang.String path) throws java.io.IOException, java.io.FileNotFoundException
path
- The path of the file to open.public java.lang.String loadFile(java.lang.String path, java.util.Locale locale) throws java.io.IOException, java.io.FileNotFoundException
path
- The path of the file to open.locale
- The locale identifying the directory to load the template from.public java.lang.String buildTemplate(java.lang.String htmlFile) throws java.lang.NullPointerException, java.text.ParseException
htmlFile
- The HTML template file.
|
|||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |