Class Template


  • public class Template
    extends java.lang.Object
    A simple templating system.

    The user supplies a template as a text and a set of variables with their values. In the template, a variable is identified by a percent sign followed by its name. The template system replaces the variables by their values. Each variable can be either block or inline.

    A line contains a block variable if it consists only of whitespace characters and the variable itself. During the replacement, each line of the block variable value is prepended with the whitespace string same as that located before the variable name. This allows inserting the source code with the proper indentation.

    All other variables are inline. An inline variable is replaced by its value as-is.

    Unset variables found in the template are left unmodified.

    • Constructor Summary

      Constructors 
      Constructor Description
      Template​(java.io.BufferedReader template, java.io.BufferedWriter output)
      Constructs a template system.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void setVariable​(java.lang.String name, java.lang.String value)
      Sets a value of a variable.
      void write()
      Writes the whole resulting output.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Template

        public Template​(java.io.BufferedReader template,
                        java.io.BufferedWriter output)
        Constructs a template system.
        Parameters:
        template - the input template
        output - the writer to write the result to
    • Method Detail

      • setVariable

        public void setVariable​(java.lang.String name,
                                java.lang.String value)
        Sets a value of a variable.
        Parameters:
        name - the variable name; must start with a letter or an underscore and continue with letters, numbers and underscores
        value - the variable value
        Throws:
        java.lang.IllegalArgumentException - if the variable name is invalid
      • write

        public void write()
                   throws java.io.IOException
        Writes the whole resulting output.
        Throws:
        java.io.IOException - if an exception occurs during writing