Class TextTemplate


  • public class TextTemplate
    extends Object
    Text document creation with templates.

    Different to Freemarker/Velocity, this class generates text documents from templates without the need of a model. The application actively writes into the document to fill variables.

    This class has been designed for cases where a very simple more restricted template syntax is required.

    Example template file:

     Hello {username}, how are you?
    
     {if locked}
         Your account is locked.
     {else locked}
         Welcome on our system.
     {end locked}
    
     The following users are on-line:
         Username       Time
     {loop user}
         {user.name}     {user.time}
     {end user}
     

    Example code to fill this template:

     File file=new File("/temp/test.tpl");
     TextTemplate t=new TextTemplate(file,"UTF-8");
     t.variable("user", "Stefan");
     t.condition("locked",false);
     t.loop("user",2);
     t.variable("user0.name,"Markus");
     t.variable("user0.time,"8:30");
     t.variable("user1.name,"Roland");
     t.variable("user1.time,"8:45");
     

    The code example above shows how variable within loops are numbered. Counting starts with 0. Loops can be nested, for example:

     <table>
     {loop row}
         <tr>
         {loop row.column}
             <td>{row.column.value}</td>
         {end row.column}
         </tr>
     {end row}
     </table>
     

    Example code to fill this nested loop:

     t.loop("row",10);
     for (int i=0; i<10; i++) {
         t.loop("row"+i+".column",10);
         for (int j=0; j<10; j++) {
             t.variable("row"+i+".column+"j"+".value","Hello");
         }
     }
     
    This creates a HTML table with 10 rows and each with 10 columns. The fields are filled with the string "Hello".
    Author:
    Stefan Frings, http://stefanfrings.de/javautils
    • Constructor Detail

      • TextTemplate

        public TextTemplate​(CharSequence template,
                            String sourceName)
        Constructor that copies a template from CharSequence.
        Parameters:
        template - The template as CharSequence
        sourceName - Symbolic name of the template, used in error messages loops cause an exception. If false, only syntax errors cause an exception.
      • TextTemplate

        public TextTemplate​(CombinedURL url,
                            int connTimeout,
                            int readTimeout,
                            String encoding)
                     throws IOException
        Constructor that reads a template from an URL.
        Parameters:
        url - The URL of the template file
        connTimeout - Timeout connectng to the server
        readTimeout - Timeout reading the from the server
        encoding - Default encoding, used if the server does not provide this information
        Throws:
        IOException - In case of an I/O error.
      • TextTemplate

        public TextTemplate​(File file,
                            String encoding)
                     throws IOException
        Constructor that reads a template from a file.
        Parameters:
        file - The template file.
        encoding - Encoding of the file, null is allowed to use the default.
        Throws:
        IOException - In case of an I/O error.
    • Method Detail

      • toString

        public String toString()
        Return the filled template as String.
        Overrides:
        toString in class Object
        Returns:
        new Document
      • check

        public boolean check()
        Check if the template contains unused tags. If there are such tags, then the function logs a warning.
        Returns:
        true if there are one or more unused tags
      • variable

        public int variable​(String name,
                            String value)
                     throws TemplateException
        Replace a variable by a value. This affects tags with the syntax {name}.
        Parameters:
        name - Name of the variable
        value - New value, null produces in an empty string.
        Returns:
        The number of variables that have been replaced
        Throws:
        TemplateException - If the template contains a syntx error
      • condition

        public int condition​(String name,
                             boolean value)
                      throws TemplateException
        Set a condition. This affects tags with the syntax
        • {if name}...{end name}
        • {if name}...{else name}...{end name}
        • {ifnot name}...{end name}
        • {ifnot name}...{else name}...{end name}
        Parameters:
        name - Name of the condition
        value - Value of the condition
        Returns:
        The number of conditions that have been processed
        Throws:
        TemplateException - If the template contains a syntx error
      • loop

        public int loop​(String name,
                        int repetitions)
                 throws TemplateException
        Set repetitions of a loop. This affects tags with the syntax
        • {loop name}...{end name}
        • {loop name}...{else name}...{end name}
        Parameters:
        name - Name of the loop
        repetitions - The number of repetitions
        Returns:
        The number of conditions that have been processed
        Throws:
        TemplateException - If the template contains a syntx error