Class TemplateEngine
java.lang.Object
de.powerstat.phplib.templateengine.TemplateEngine
PHPLib compatible template engine.
Unconditionally thread safe.
Not serializable, because serialization is dangerous, use Protocol Buffers or JSON instead!
- Author:
- Kai Hofmann
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final PatternBlock matcher regexp.private final de.powerstat.phplib.templateengine.intern.BlockManagerBlock manager.private final de.powerstat.phplib.templateengine.intern.FileManagerFile manager.private static final intMaximum template size.private static final intMaximum varname size.private static final StringTemplate name constant.private static final StringTemplate is empty message.private final HandleUndefinedHandling of undefined template variables.private final de.powerstat.phplib.templateengine.intern.VariableManagerVariable manager.private static final StringVarname name constant.private static final StringVarname does not match name pattern error message constant.private static final StringVarname is empty error message constant.private static final StringVarname is to long error message constant.private static final PatternVarname regexp pattern. -
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor - unknown variables will be handled as "remove", root is current directory.TemplateEngine(HandleUndefined unknowns) Constructor.TemplateEngine(TemplateEngine engine) Copy constructor. -
Method Summary
Modifier and TypeMethodDescriptionbooleanIs equal with another object.Handle undefined template variables after parsing has happened.Shortcut for finish(getVar(varname)).getUndefined(String varname) Get list with all undefined template variables.Get template variable value.getVars()Get list of all template variables.inthashCode()Calculate hash code.static TemplateEnginenewInstance(TemplateEngine engine) Copy factory.static TemplateEnginenewInstance(File file) Get new instance from a UTF-8 encoded text file.static TemplateEnginenewInstance(InputStream stream) Get new instance from a stream.static TemplateEnginenewInstance(String template) Get new instance from a string.Parse a variable and replace all variables within it by their content.Parse a variable and replace all variables within it by their content.booleanSet template block (cut it from parent template and replace it with a variable).booleanSet template block (cut it from parent template and replace it with a variable).booleanSet template file for variable.voidSet template variable as empty.voidSet template variables value.Substitute variable with its content.toString()Returns the string representation of this TemplatEngine.voidUnset template variable.
-
Field Details
-
TEMPLATE
Template name constant.- See Also:
-
TEMPLATE_IS_EMPTY
Template is empty message.- See Also:
-
MAX_VARNAME_SIZE
private static final int MAX_VARNAME_SIZEMaximum varname size.- See Also:
-
VARNAME
Varname name constant.- See Also:
-
VARNAME_IS_EMPTY
Varname is empty error message constant.- See Also:
-
VARNAME_IS_TO_LONG
Varname is to long error message constant.- See Also:
-
VARNAME_DOES_NOT_MATCH_NAME_PATTERN
Varname does not match name pattern error message constant.- See Also:
-
VARNAME_REGEXP
Varname regexp pattern. -
BLOCK_MATCHER_REGEXP
Block matcher regexp. -
MAX_TEMPLATE_SIZE
private static final int MAX_TEMPLATE_SIZEMaximum template size.- See Also:
-
unknowns
Handling of undefined template variables. "remove"/0 => remove undefined variables "keep"/1 => keep undefined variables "comment"/2 => replace undefined variables with comments -
variableManager
private final de.powerstat.phplib.templateengine.intern.VariableManager variableManagerVariable manager. -
fileManager
private final de.powerstat.phplib.templateengine.intern.FileManager fileManagerFile manager. -
blockManager
private final de.powerstat.phplib.templateengine.intern.BlockManager blockManagerBlock manager.
-
-
Constructor Details
-
TemplateEngine
Copy constructor.- Parameters:
engine- Template engine- Throws:
NullPointerException- If engine is null
-
TemplateEngine
Constructor.- Parameters:
unknowns- Handling of unknown template variables- See Also:
-
TemplateEngine
public TemplateEngine()Default constructor - unknown variables will be handled as "remove", root is current directory.
-
-
Method Details
-
newInstance
Copy factory.- Parameters:
engine- TemplateEngine to copy- Returns:
- A new TemplateEngine instance that is a copy of engine.
- Throws:
NullPointerException- If engine is null
-
newInstance
Get new instance from a UTF-8 encoded text file.- Parameters:
file- Text file (UTF-8 encoded) to load as template- Returns:
- A new TemplateEngine instance where the template variable name is 'template'
- Throws:
FileNotFoundException- When the given file does not existIOException- When the given file is to largeIllegalArgumentException- When the given file is nullNullPointerException- If file is null
-
newInstance
Get new instance from a stream.- Parameters:
stream- UTF-8 stream to read the template from- Returns:
- A new TemplateEngine instance where the template variable name is 'template'.
- Throws:
IOException- If an I/O error occursIllegalArgumentException- When the given stream is nullIllegalStateException- If the stream is emptyNullPointerException- If stream is null
-
newInstance
Get new instance from a string.- Parameters:
template- Template string- Returns:
- A new TemplateEngine instance where the template variable name is 'template'.
- Throws:
IllegalArgumentException- When the given string is null or emptyNullPointerException- If template is null
-
setFile
Set template file for variable.- Parameters:
newVarname- Variable that should hold the templatenewFile- Template file UTF-8 encoded- Returns:
- true when successful (file exists) otherwise false
- Throws:
NullPointerException- If newVarname or newFile is nullIllegalArgumentException- If newVarname is empty
-
getVar
Get template variable value.- Parameters:
varname- Template variable name- Returns:
- Template variables value
- Throws:
NullPointerException- If varname is nullIllegalArgumentException- If varname is empty
-
setVar
Set template variables value.- Parameters:
varname- Template variable namevalue- Template variable value, could be null- Throws:
NullPointerException- If varname is nullIllegalArgumentException- If varname is empty
-
setVar
Set template variable as empty.- Parameters:
varname- Template variable name- Throws:
NullPointerException- If varname is nullIllegalArgumentException- If varname is empty
-
unsetVar
Unset template variable.- Parameters:
varname- Template variable name- Throws:
NullPointerException- If varname is nullIllegalArgumentException- If varname is empty
-
setBlock
Set template block (cut it from parent template and replace it with a variable). Used for repeatable blocks- Parameters:
parent- Name of parent template variablevarname- Name of template blockname- Name of variable in which the block will be placed - if empty will be the same as varname- Returns:
- true on sucess otherwise false
- Throws:
IOException- IO exceptionIllegalStateException- When no block with varname is found.NullPointerException- If parent or varname is nullIllegalArgumentException- If parent or varname is empty
-
setBlock
Set template block (cut it from parent template and replace it with a variable). Used for on/off blocks- Parameters:
parent- Name of parent template variablevarname- Name of template block- Returns:
- true on sucess otherwise false
- Throws:
IOException- IO exceptionIllegalStateException- When no block with varname is found.NullPointerException- If parent or varname is nullIllegalArgumentException- If parent or varname is empty
-
subst
Substitute variable with its content.- Parameters:
varname- Variable name- Returns:
- Replaced variable content or empty string
- Throws:
IOException- File not found or IO exceptionNullPointerException- If varname is nullIllegalArgumentException- If varname is empty
-
parse
Parse a variable and replace all variables within it by their content.- Parameters:
target- Target for parsing operationvarname- Parse the content of this variableappend- true for appending blocks to target, otherwise false for replacing targets content- Returns:
- Variables content after parsing
- Throws:
IOException- File not found or IO exceptionNullPointerException- If target or varname is nullIllegalArgumentException- If target or varname is empty
-
parse
Parse a variable and replace all variables within it by their content. Don't append- Parameters:
target- Target for parsing operationvarname- Parse the content of this variable- Returns:
- Variables content after parsing
- Throws:
IOException- File not found or IO exceptionNullPointerException- If target or varname is nullIllegalArgumentException- If target or varname is empty
-
getVars
Get list of all template variables.- Returns:
- Array with names of template variables
-
getUndefined
Get list with all undefined template variables.- Parameters:
varname- Variable to parse for undefined variables- Returns:
- List with undefined template variables names
- Throws:
IOException- File not found or IO exceptionNullPointerException- If varname is nullIllegalArgumentException- If varname is empty
-
finish
Handle undefined template variables after parsing has happened.- Parameters:
template- Template to parse for unknown variables- Returns:
- Modified template as specified by the "unknowns" setting
- Throws:
NullPointerException- If varname is nullIllegalArgumentException- If varname is empty
-
get
Shortcut for finish(getVar(varname)).- Parameters:
varname- Name of template variable- Returns:
- Value of template variable
- Throws:
NullPointerException- If varname is nullIllegalArgumentException- If varname is empty
-
toString
Returns the string representation of this TemplatEngine. The exact details of this representation are unspecified and subject to change, but the following may be regarded as typical: "TemplateEngine[unknowns=REMOVE, vManager=[name, ...]]" -
hashCode
public int hashCode()Calculate hash code. -
equals
Is equal with another object.
-