|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectbiz.source_code.miniTemplator.MiniTemplator
public class MiniTemplator
A compact template engine for HTML files.
Template syntax:
Variables:
${VariableName}
Blocks:
<!-- $beginBlock blockName -->
... block contents ...
<!-- $endBlock blockName -->
Conditional blocks:
<!-- $if flag1 flag2 -->
... included if flag1 or flag2 is set ...
<!-- $elseIf !flag3 flag4 -->
... included if flag3 is not set or flag4 is set ...
<!-- $else -->
... included if none of the above conditions is met ...
<!-- $endIf -->
Short form of conditional blocks:
(only recognized if TemplateSpecification.shortFormEnabled is true)
<$? flag1 flag2 >
... included if flag1 or flag2 is set ...
<$: !flag3 flag4 >
... included if flag3 is not set or flag4 is set ...
<$:>
... included if none of the above conditions is met ...
<$/?>
Example:
<$?de> Hallo Welt!
<$:fr> Bonjour tout le monde!
<$: > Hello world!
<$/?>
Include a subtemplate:
<!-- $include relativeFileName -->
General remarks:
TemplateSpecification.
addBlock().
MiniTemplatorCache class may be used to cache MiniTemplator objects with parsed templates.
Project home page: www.source-code.biz/MiniTemplator
Author: Christian d'Heureuse, Inventec Informatik AG, Zurich, Switzerland
| Nested Class Summary | |
|---|---|
static class |
MiniTemplator.BlockNotDefinedException
Thrown when Minitemplator.addBlock
is called with a blockName that is not defined
within the template. |
static class |
MiniTemplator.Builder
Specifies the parameters for constructing a MiniTemplator object. |
static class |
MiniTemplator.TemplateSyntaxException
Thrown when a syntax error is encountered within the template. |
static class |
MiniTemplator.VariableNotDefinedException
Thrown when Minitemplator.setVariable
is called with a variableName that is not defined
within the template and the isOptional parameter is false. |
| Constructor Summary | |
|---|---|
protected |
MiniTemplator()
Dummy constructor, used internally in newInstance(). |
| Method Summary | |
|---|---|
void |
addBlock(String blockName)
Adds an instance of a template block. |
void |
addBlock(String blockName,
boolean isOptional)
Adds an instance of a template block. |
void |
addBlockOpt(String blockName)
Adds an instance of an optional template block. |
boolean |
blockExists(String blockName)
Checks whether a block with the specified name exists within the template. |
MiniTemplator |
cloneReset()
Clones this MiniTemplator object and resets the clone. |
static String |
escapeHtml(String s)
Escapes special HTML characters. |
String |
generateOutput()
Generates the HTML page and returns it as a string. |
void |
generateOutput(String outputFileName)
Generates the HTML page and writes it into a file. |
void |
generateOutput(Writer outputWriter)
Generates the HTML page and writes it to a character stream. |
Map<String,String> |
getVariables()
Returns a map with the names and current values of the template variables. |
protected String |
loadSubtemplate(String subtemplateName)
Loads the template string of a subtemplate (used for the $Include command). |
protected MiniTemplator |
newInstance()
Allocates a new uninitialized MiniTemplator object. |
void |
reset()
Resets the MiniTemplator object to the initial state. |
void |
setVariable(String variableName,
int variableValue)
Sets a template variable to an integer value. |
void |
setVariable(String variableName,
String variableValue)
Sets a template variable. |
void |
setVariable(String variableName,
String variableValue,
boolean isOptional)
Sets a template variable. |
void |
setVariableEsc(String variableName,
String variableValue)
Sets a template variable to an escaped value. |
void |
setVariableEsc(String variableName,
String variableValue,
boolean isOptional)
Sets a template variable to an escaped value. |
void |
setVariableOpt(String variableName,
int variableValue)
Sets an optional template variable to an integer value. |
void |
setVariableOpt(String variableName,
String variableValue)
Sets an optional template variable. |
void |
setVariableOptEsc(String variableName,
String variableValue)
Sets an optional template variable to an escaped value. |
boolean |
variableExists(String variableName)
Checks whether a variable with the specified name exists within the template. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
protected MiniTemplator()
| Method Detail |
|---|
protected MiniTemplator newInstance()
protected String loadSubtemplate(String subtemplateName)
throws IOException
This implementation of the method interprets subtemplateName
as a relative file path name and reads the template string from that file.
MiniTemplator.TemplateSpecification#subtemplateBasePath is used to convert
the relative path of the subtemplate into an absolute path.
subtemplateName - the name of the subtemplate.
Normally a relative file path.
This is the argument string that was specified with the "$Include" command.
If the string has quotes, the quotes are removed before this method is called.
IOExceptionpublic void reset()
public MiniTemplator cloneReset()
This method is used by the MiniTemplatorCache class to
clone the cached MiniTemplator objects.
public void setVariable(String variableName,
String variableValue,
boolean isOptional)
throws MiniTemplator.VariableNotDefinedException
For variables that are used in blocks, the variable value
must be set before addBlock() is called.
variableName - the name of the variable to be set. Case-insensitive.variableValue - the new value of the variable. May be null.isOptional - specifies whether an exception should be thrown when the
variable does not exist in the template. If isOptional is
false and the variable does not exist, an exception is thrown.
MiniTemplator.VariableNotDefinedException - when no variable with the
specified name exists in the template and isOptional is false.
public void setVariable(String variableName,
String variableValue)
throws MiniTemplator.VariableNotDefinedException
Convenience method for: setVariable (variableName, variableValue, false)
variableName - the name of the variable to be set. Case-insensitive.variableValue - the new value of the variable. May be null.
MiniTemplator.VariableNotDefinedException - when no variable with the
specified name exists in the template.setVariable(String, String, boolean)
public void setVariable(String variableName,
int variableValue)
throws MiniTemplator.VariableNotDefinedException
Convenience method for: setVariable (variableName, Integer.toString(variableValue))
variableName - the name of the variable to be set. Case-insensitive.variableValue - the new value of the variable.
MiniTemplator.VariableNotDefinedException - when no variable with the
specified name exists in the template.
public void setVariableOpt(String variableName,
String variableValue)
Convenience method for: setVariable (variableName, variableValue, true)
variableName - the name of the variable to be set. Case-insensitive.variableValue - the new value of the variable. May be null.setVariable(String, String, boolean)
public void setVariableOpt(String variableName,
int variableValue)
Convenience method for: setVariableOpt (variableName, Integer.toString(variableValue))
variableName - the name of the variable to be set. Case-insensitive.variableValue - the new value of the variable.
public void setVariableEsc(String variableName,
String variableValue,
boolean isOptional)
throws MiniTemplator.VariableNotDefinedException
Convenience method for: setVariable (variableName, MiniTemplator.escapeHtml(variableValue), isOptional)
variableName - the name of the variable to be set.variableValue - the new value of the variable. May be null.
Special HTML/XML characters are escaped.isOptional - specifies whether an exception should be thrown when the
variable does not exist in the template. If isOptional is
false and the variable does not exist, an exception is thrown.
MiniTemplator.VariableNotDefinedException - when no variable with the
specified name exists in the template and isOptional is false.setVariable(String, String, boolean),
escapeHtml(String)
public void setVariableEsc(String variableName,
String variableValue)
throws MiniTemplator.VariableNotDefinedException
Convenience method for: setVariable (variableName, MiniTemplator.escapeHtml(variableValue), false)
variableName - the name of the variable to be set. Case-insensitive.variableValue - the new value of the variable. May be null.
Special HTML/XML characters are escaped.
MiniTemplator.VariableNotDefinedException - when no variable with the
specified name exists in the template.setVariable(String, String, boolean),
escapeHtml(String)
public void setVariableOptEsc(String variableName,
String variableValue)
Convenience method for: setVariable (variableName, MiniTemplator.escapeHtml(variableValue), true)
variableName - the name of the variable to be set. Case-insensitive.variableValue - the new value of the variable. May be null.
Special HTML/XML characters are escaped.setVariable(String, String, boolean),
escapeHtml(String)public boolean variableExists(String variableName)
variableName - the name of the variable. Case-insensitive.
true if the variable exists.false if no variable with the specified name exists in the template.public Map<String,String> getVariables()
public void addBlock(String blockName,
boolean isOptional)
throws MiniTemplator.BlockNotDefinedException
If the block contains variables, these variables must be set before the block is added. If the block contains subblocks (nested blocks), the subblocks must be added before this block is added. If multiple blocks exist with the specified name, an instance is added for each block occurrence.
blockName - the name of the block to be added. Case-insensitive.isOptional - specifies whether an exception should be thrown when the
block does not exist in the template. If isOptional is
false and the block does not exist, an exception is thrown.
MiniTemplator.BlockNotDefinedException - when no block with the specified name
exists in the template and isOptional is false.
public void addBlock(String blockName)
throws MiniTemplator.BlockNotDefinedException
Convenience method for: addBlock (blockName, false)
blockName - the name of the block to be added. Case-insensitive.
MiniTemplator.BlockNotDefinedException - when no block with the specified name
exists in the template.addBlock(String, boolean)public void addBlockOpt(String blockName)
Convenience method for: addBlock (blockName, true)
blockName - the name of the block to be added. Case-insensitive.addBlock(String, boolean)public boolean blockExists(String blockName)
blockName - the name of the block.
true if the block exists.false if no block with the specified name exists in the template.
public void generateOutput(String outputFileName)
throws IOException
outputFileName - name of the file to which the generated HTML page will be written.
IOException - when an i/o error occurs while writing to the file.
public void generateOutput(Writer outputWriter)
throws IOException
outputWriter - a character stream (writer) to which
the HTML page will be written.
IOException - when an i/o error occurs while writing to the stream.public String generateOutput()
public static String escapeHtml(String s)
s - the input string.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||