java.lang.Object
de.powerstat.phplib.templateengine.TemplateEngine

public final class TemplateEngine extends Object
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 Details

    • TEMPLATE

      private static final String TEMPLATE
      Template name constant.
      See Also:
    • TEMPLATE_IS_EMPTY

      private static final String TEMPLATE_IS_EMPTY
      Template is empty message.
      See Also:
    • MAX_VARNAME_SIZE

      private static final int MAX_VARNAME_SIZE
      Maximum varname size.
      See Also:
    • VARNAME

      private static final String VARNAME
      Varname name constant.
      See Also:
    • VARNAME_IS_EMPTY

      private static final String VARNAME_IS_EMPTY
      Varname is empty error message constant.
      See Also:
    • VARNAME_IS_TO_LONG

      private static final String VARNAME_IS_TO_LONG
      Varname is to long error message constant.
      See Also:
    • VARNAME_DOES_NOT_MATCH_NAME_PATTERN

      private static final String VARNAME_DOES_NOT_MATCH_NAME_PATTERN
      Varname does not match name pattern error message constant.
      See Also:
    • VARNAME_REGEXP

      private static final Pattern VARNAME_REGEXP
      Varname regexp pattern.
    • BLOCK_MATCHER_REGEXP

      private static final Pattern BLOCK_MATCHER_REGEXP
      Block matcher regexp.
    • MAX_TEMPLATE_SIZE

      private static final int MAX_TEMPLATE_SIZE
      Maximum template size.
      See Also:
    • unknowns

      private final HandleUndefined 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 variableManager
      Variable manager.
    • fileManager

      private final de.powerstat.phplib.templateengine.intern.FileManager fileManager
      File manager.
    • blockManager

      private final de.powerstat.phplib.templateengine.intern.BlockManager blockManager
      Block manager.
  • Constructor Details

    • TemplateEngine

      public TemplateEngine(TemplateEngine engine)
      Copy constructor.
      Parameters:
      engine - Template engine
      Throws:
      NullPointerException - If engine is null
    • TemplateEngine

      public TemplateEngine(HandleUndefined unknowns)
      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

      public static TemplateEngine newInstance(TemplateEngine engine)
      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

      public static TemplateEngine newInstance(File file) throws IOException
      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 exist
      IOException - When the given file is to large
      IllegalArgumentException - When the given file is null
      NullPointerException - If file is null
    • newInstance

      public static TemplateEngine newInstance(InputStream stream) throws IOException
      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 occurs
      IllegalArgumentException - When the given stream is null
      IllegalStateException - If the stream is empty
      NullPointerException - If stream is null
    • newInstance

      public static TemplateEngine newInstance(String template)
      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 empty
      NullPointerException - If template is null
    • setFile

      public boolean setFile(String newVarname, File newFile)
      Set template file for variable.
      Parameters:
      newVarname - Variable that should hold the template
      newFile - Template file UTF-8 encoded
      Returns:
      true when successful (file exists) otherwise false
      Throws:
      NullPointerException - If newVarname or newFile is null
      IllegalArgumentException - If newVarname is empty
    • getVar

      public String getVar(String varname)
      Get template variable value.
      Parameters:
      varname - Template variable name
      Returns:
      Template variables value
      Throws:
      NullPointerException - If varname is null
      IllegalArgumentException - If varname is empty
    • setVar

      public void setVar(String varname, String value)
      Set template variables value.
      Parameters:
      varname - Template variable name
      value - Template variable value, could be null
      Throws:
      NullPointerException - If varname is null
      IllegalArgumentException - If varname is empty
    • setVar

      public void setVar(String varname)
      Set template variable as empty.
      Parameters:
      varname - Template variable name
      Throws:
      NullPointerException - If varname is null
      IllegalArgumentException - If varname is empty
    • unsetVar

      public void unsetVar(String varname)
      Unset template variable.
      Parameters:
      varname - Template variable name
      Throws:
      NullPointerException - If varname is null
      IllegalArgumentException - If varname is empty
    • setBlock

      public boolean setBlock(String parent, String varname, String name) throws IOException
      Set template block (cut it from parent template and replace it with a variable). Used for repeatable blocks
      Parameters:
      parent - Name of parent template variable
      varname - Name of template block
      name - 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 exception
      IllegalStateException - When no block with varname is found.
      NullPointerException - If parent or varname is null
      IllegalArgumentException - If parent or varname is empty
    • setBlock

      public boolean setBlock(String parent, String varname) throws IOException
      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 variable
      varname - Name of template block
      Returns:
      true on sucess otherwise false
      Throws:
      IOException - IO exception
      IllegalStateException - When no block with varname is found.
      NullPointerException - If parent or varname is null
      IllegalArgumentException - If parent or varname is empty
    • subst

      public String subst(String varname) throws IOException
      Substitute variable with its content.
      Parameters:
      varname - Variable name
      Returns:
      Replaced variable content or empty string
      Throws:
      IOException - File not found or IO exception
      NullPointerException - If varname is null
      IllegalArgumentException - If varname is empty
    • parse

      public String parse(String target, String varname, boolean append) throws IOException
      Parse a variable and replace all variables within it by their content.
      Parameters:
      target - Target for parsing operation
      varname - Parse the content of this variable
      append - true for appending blocks to target, otherwise false for replacing targets content
      Returns:
      Variables content after parsing
      Throws:
      IOException - File not found or IO exception
      NullPointerException - If target or varname is null
      IllegalArgumentException - If target or varname is empty
    • parse

      public String parse(String target, String varname) throws IOException
      Parse a variable and replace all variables within it by their content. Don't append
      Parameters:
      target - Target for parsing operation
      varname - Parse the content of this variable
      Returns:
      Variables content after parsing
      Throws:
      IOException - File not found or IO exception
      NullPointerException - If target or varname is null
      IllegalArgumentException - If target or varname is empty
    • getVars

      public List<String> getVars()
      Get list of all template variables.
      Returns:
      Array with names of template variables
    • getUndefined

      public List<String> getUndefined(String varname) throws IOException
      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 exception
      NullPointerException - If varname is null
      IllegalArgumentException - If varname is empty
    • finish

      public String finish(String template)
      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 null
      IllegalArgumentException - If varname is empty
    • get

      public String get(String varname)
      Shortcut for finish(getVar(varname)).
      Parameters:
      varname - Name of template variable
      Returns:
      Value of template variable
      Throws:
      NullPointerException - If varname is null
      IllegalArgumentException - If varname is empty
    • toString

      public String 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, ...]]"
      Overrides:
      toString in class Object
      Returns:
      String representation of this TemplatEngine.
      See Also:
    • hashCode

      public int hashCode()
      Calculate hash code.
      Overrides:
      hashCode in class Object
      Returns:
      Hash
      See Also:
    • equals

      public boolean equals(Object obj)
      Is equal with another object.
      Overrides:
      equals in class Object
      Parameters:
      obj - Object
      Returns:
      true when equal, false otherwise
      See Also: