Class TemplateEngine


  • public final class TemplateEngine
    extends JxltEngine
    A JxltEngine implementation.
    Since:
    3.0
    • Field Detail

      • jexl

        final Engine jexl
        The JEXL engine instance.
      • logger

        final org.apache.commons.logging.Log logger
        The logger.
      • immediateChar

        final char immediateChar
        The first character for immediate expressions.
      • deferredChar

        final char deferredChar
        The first character for deferred expressions.
      • noscript

        final boolean noscript
        Whether expressions can use JEXL script or only expressions (ie, no for, var, etc).
    • Constructor Detail

      • TemplateEngine

        public TemplateEngine​(Engine jexl,
                              boolean noScript,
                              int cacheSize,
                              char immediate,
                              char deferred)
        Creates a new instance of JxltEngine creating a local cache.
        Parameters:
        jexl - the JexlEngine to use.
        noScript - whether this engine only allows JEXL expressions or scripts
        cacheSize - the number of expressions in this cache, default is 256
        immediate - the immediate template expression character, default is '$'
        deferred - the deferred template expression character, default is '#'
    • Method Detail

      • append

        private static int append​(java.lang.StringBuilder strb,
                                  java.lang.CharSequence expr,
                                  int position,
                                  char c)
        Helper for expression dealing with embedded strings.
        Parameters:
        strb - the expression buffer to copy characters into
        expr - the source
        position - the offset into the source
        c - the separator character
        Returns:
        the new position to read the source from
      • createException

        static JxltEngine.Exception createException​(JexlInfo info,
                                                    java.lang.String action,
                                                    TemplateEngine.TemplateExpression expr,
                                                    java.lang.Exception xany)
        Creates a JxltEngine.Exception from a JexlException.
        Parameters:
        info - the source info
        action - createExpression, prepare, evaluate
        expr - the template expression
        xany - the exception
        Returns:
        an exception containing an explicit error message
      • readLines

        protected static java.util.Iterator<java.lang.CharSequence> readLines​(java.io.Reader reader)
        Read lines from a (buffered / mark-able) reader keeping all new-lines and line-feeds.
        Parameters:
        reader - the reader
        Returns:
        the line iterator
      • clearCache

        public void clearCache()
        Clears the cache.
        Specified by:
        clearCache in class JxltEngine
      • createTemplate

        public TemplateScript createTemplate​(JexlInfo info,
                                             java.lang.String prefix,
                                             java.io.Reader source,
                                             java.lang.String... parms)
        Description copied from class: JxltEngine
        Creates a new template.
        Specified by:
        createTemplate in class JxltEngine
        Parameters:
        info - the jexl info (file, line, column)
        prefix - the directive prefix
        source - the source
        parms - the parameter names
        Returns:
        the template
      • getDeferredChar

        char getDeferredChar()
        Returns:
        the deferred character
      • getEngine

        public Engine getEngine()
        Gets the JexlEngine underlying this JxltEngine.
        Specified by:
        getEngine in class JxltEngine
        Returns:
        the JexlEngine
      • getImmediateChar

        char getImmediateChar()
        Returns:
        the immediate character
      • parseExpression

        TemplateEngine.TemplateExpression parseExpression​(JexlInfo info,
                                                          java.lang.String expr,
                                                          Scope scope)
        Parses a unified expression.
        Parameters:
        info - the source info
        expr - the string expression
        scope - the template scope
        Returns:
        the unified expression instance
        Throws:
        JexlException - if an error occur during parsing
      • readTemplate

        protected java.util.List<TemplateEngine.Block> readTemplate​(java.lang.String prefix,
                                                                    java.io.Reader source)
        Reads lines of a template grouping them by typed blocks.
        Parameters:
        prefix - the directive prefix
        source - the source reader
        Returns:
        the list of blocks
      • startsWith

        protected int startsWith​(java.lang.CharSequence sequence,
                                 java.lang.CharSequence pattern)
        Whether a sequence starts with a given set of characters (following spaces).

        Space characters at beginning of line before the pattern are discarded.

        Parameters:
        sequence - the sequence
        pattern - the pattern to match at start of sequence
        Returns:
        the first position after end of pattern if it matches, -1 otherwise