Class SimpleTemplater


  • public class SimpleTemplater
    extends java.lang.Object
    To process simple HTML templates. -- Usage -- SimpleTemplater.getInstance().buildOutputUsingStringTemplate(template, parameters); Replaces ${parameter_name} in the template by its value found in the (Map)parameters. -- Syntax -- Simple blocks can be processed. Blocks are surrounded by and $$for(parameter_name) will loop over the items of parameter_name of type Collection> $$if(parameter_name) will execute only if parameter_name is defined and not empty $$ifnot(parameter_name) will execute only if parameter_name is not defined or is empty -- Example -- String template = " Dear ${name}, Here is the list of your things: ${name} Regards, Sincerely, Me"; Map parameters = new HashMap(); parameters.put("name", "You"); parameters.put("regards", "1"); Collection> things = new ArrayList>(); for (int i=0; i<3; i++) { Map p = new HashMap(); p.put("name", "Thing + i); things.addElement(p); } parameters.put("things", things); String output = SimpleTemplater.getInstance().buildOutputUsingStringTemplate(template, parameters); System.out.println(output);
    Author:
    Laurent Wibaux
    • Constructor Summary

      Constructors 
      Constructor Description
      SimpleTemplater​(boolean templateIsHTML)
      Constructor
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.String buildOutputUsingInputStreamTemplate​(java.io.InputStream templateIS, java.util.Map<java.lang.String,​java.lang.Object> parameters)
      Returns the template with all fields replaced by their values
      java.lang.String buildOutputUsingResourceTemplate​(java.lang.String templateName, java.util.Map<java.lang.String,​java.lang.Object> parameters)
      Returns the template with all fields replaced by their values
      java.lang.String buildOutputUsingStringTemplate​(java.lang.String template, java.util.Map<java.lang.String,​java.lang.Object> parameters)
      Returns the template with all fields replaced by their values
      static SimpleTemplater getInstance()
      Returns an instance of SimpleTemplater which will not deal with HTML
      static SimpleTemplater getInstance​(boolean templateIsHTML)
      Returns an instance of SimpleTemplater which will or will not deal with HTML
      static java.util.Collection<java.util.Map<java.lang.String,​java.lang.Object>> getSequence​(java.lang.String indexName, int from, int to)
      Returns a sequence of numbers going from 'from' to 'to', using indexName as key in the map this sequence can then be added to a Map
      boolean templateNameIsHTML​(java.lang.String templateName)  
      • Methods inherited from class java.lang.Object

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

      • SimpleTemplater

        public SimpleTemplater​(boolean templateIsHTML)
        Constructor
        Parameters:
        templateIsHTML - - set to true if the template that will be fed is HTML
    • Method Detail

      • getInstance

        public static SimpleTemplater getInstance()
        Returns an instance of SimpleTemplater which will not deal with HTML
        Returns:
        SimpleTemplater
      • getInstance

        public static SimpleTemplater getInstance​(boolean templateIsHTML)
        Returns an instance of SimpleTemplater which will or will not deal with HTML
        Parameters:
        templateIsHTML - - set to true if the template that will be fed is HTML
        Returns:
        SimpleTemplater
      • getSequence

        public static java.util.Collection<java.util.Map<java.lang.String,​java.lang.Object>> getSequence​(java.lang.String indexName,
                                                                                                               int from,
                                                                                                               int to)
        Returns a sequence of numbers going from 'from' to 'to', using indexName as key in the map this sequence can then be added to a Map
        Parameters:
        indexName - - the name of the parameter
        from - - start of the sequence
        to - - end of the sequence
        Returns:
        a list of Map
      • buildOutputUsingResourceTemplate

        public java.lang.String buildOutputUsingResourceTemplate​(java.lang.String templateName,
                                                                 java.util.Map<java.lang.String,​java.lang.Object> parameters)
        Returns the template with all fields replaced by their values
        Parameters:
        templateName - - the name of the template to be found in the code archive
        parameters - - Map of the parameters
        Returns:
        the result of the processing
        Throws:
        SimpleTemplaterException - - in case of an error in the template or in the passed parameters
      • buildOutputUsingInputStreamTemplate

        public java.lang.String buildOutputUsingInputStreamTemplate​(java.io.InputStream templateIS,
                                                                    java.util.Map<java.lang.String,​java.lang.Object> parameters)
        Returns the template with all fields replaced by their values
        Parameters:
        templateIS - - an InputStream
        parameters - - Map of the parameters
        Returns:
        the result of the processing
        Throws:
        SimpleTemplaterException - - in case of an error in the template or in the passed parameters
      • buildOutputUsingStringTemplate

        public java.lang.String buildOutputUsingStringTemplate​(java.lang.String template,
                                                               java.util.Map<java.lang.String,​java.lang.Object> parameters)
        Returns the template with all fields replaced by their values
        Parameters:
        template - - a String containing the template
        parameters - - Map of the parameters
        Returns:
        the result of the processing
        Throws:
        SimpleTemplaterException - - in case of an error in the template or in the passed parameters
      • templateNameIsHTML

        public boolean templateNameIsHTML​(java.lang.String templateName)