Class DefaultMustacheFactory

java.lang.Object
com.github.mustachejava.DefaultMustacheFactory
All Implemented Interfaces:
MustacheFactory
Direct Known Subclasses:
DeferringMustacheFactory, FallbackMustacheFactory, SafeMustacheFactory, SpecMustacheFactory

public class DefaultMustacheFactory extends Object implements MustacheFactory
Simplest possible code factory
  • Field Details

    • mustacheCache

      protected final ConcurrentHashMap<String,Mustache> mustacheCache
      Create the default cache for mustache compilations. This is basically required by the specification to handle recursive templates.
    • oh

      protected ObjectHandler oh
      This is the default object handler.
    • mc

      protected final MustacheParser mc
      This parser should work with any MustacheFactory
    • templateCache

      protected final ConcurrentHashMap<FragmentKey,Mustache> templateCache
      New templates that are generated at runtime are cached here. The template key includes the text of the template and the context so we get proper error messages and debugging information.
    • recursionLimit

      protected int recursionLimit
    • mustacheResolver

      private final MustacheResolver mustacheResolver
    • es

      protected ExecutorService es
    • partialCache

      private final ThreadLocal<Map<String,Mustache>> partialCache
  • Constructor Details

    • DefaultMustacheFactory

      public DefaultMustacheFactory()
    • DefaultMustacheFactory

      public DefaultMustacheFactory(MustacheResolver mustacheResolver)
    • DefaultMustacheFactory

      public DefaultMustacheFactory(String classpathResourceRoot)
      Use the classpath to resolve mustache templates.
      Parameters:
      classpathResourceRoot - the location in the resources where templates are stored
    • DefaultMustacheFactory

      public DefaultMustacheFactory(File fileRoot)
      Use the file system to resolve mustache templates.
      Parameters:
      fileRoot - the root of the file system where templates are stored
  • Method Details

    • resolvePartialPath

      public String resolvePartialPath(String dir, String name, String extension)
      Using the directory, namd and extension, resolve a partial to a name.
      Parameters:
      dir -
      name -
      extension -
      Returns:
    • ensureForwardSlash

      private static String ensureForwardSlash(String path)
    • createMustacheVisitor

      public MustacheVisitor createMustacheVisitor()
      Description copied from interface: MustacheFactory
      Creates the visitor for compilation.
      Specified by:
      createMustacheVisitor in interface MustacheFactory
      Returns:
      visitor
    • getReader

      public Reader getReader(String resourceName)
      Description copied from interface: MustacheFactory
      Given a resource name, construct a reader.
      Specified by:
      getReader in interface MustacheFactory
      Parameters:
      resourceName - used to find the resource
      Returns:
      a reader
    • encode

      public void encode(String value, Writer writer)
      Description copied from interface: MustacheFactory
      This defines how "encoded" values are encoded. It defaults to something appropriate for HTML output.
      Specified by:
      encode in interface MustacheFactory
      Parameters:
      value - the unencoded value
      writer - where to encode the value
    • getObjectHandler

      public ObjectHandler getObjectHandler()
      Description copied from interface: MustacheFactory
      The object handler knows how to transform names into fields and methods.
      Specified by:
      getObjectHandler in interface MustacheFactory
      Returns:
      the handler
    • setObjectHandler

      public void setObjectHandler(ObjectHandler oh)
      You can override the default object handler post construction.
      Parameters:
      oh - The object handler to use.
    • getExecutorService

      public ExecutorService getExecutorService()
      There is an ExecutorService that is used when executing parallel operations when a Callable is returned from a mustache value or iterable.
      Returns:
      the executor service
    • setExecutorService

      public void setExecutorService(ExecutorService es)
      If you need to specify your own executor service you can.
      Parameters:
      es - The executor service to use for Future evaluation
    • getFragment

      public Mustache getFragment(FragmentKey templateKey)
    • getFragmentCacheFunction

      protected Function<FragmentKey,Mustache> getFragmentCacheFunction()
    • compile

      public Mustache compile(String name)
      Description copied from interface: MustacheFactory
      Create a mustache given a resource name.
      Specified by:
      compile in interface MustacheFactory
      Parameters:
      name - the name of the resource
      Returns:
      the compiled mustache
    • compile

      public Mustache compile(Reader reader, String name)
      Description copied from interface: MustacheFactory
      Create a mustache given a reader and a name.
      Specified by:
      compile in interface MustacheFactory
      Parameters:
      reader - the reader
      name - the name of the resource
      Returns:
      the compiled mustache
    • compile

      public Mustache compile(Reader reader, String file, String sm, String em)
    • translate

      public String translate(String from)
      Description copied from interface: MustacheFactory
      Converts your arbitrary name to another name.
      Specified by:
      translate in interface MustacheFactory
      Parameters:
      from - the tag to replace
      Returns:
      the new tag
    • filterText

      public String filterText(String appended, boolean startOfLine)
      Override this method to apply any filtering to text that will appear verbatim in the output template.
      Parameters:
      appended - The text to be appended to the output
      startOfLine - Are we at the start of the line?
      Returns:
      the filtered string
    • setRecursionLimit

      public void setRecursionLimit(int recursionLimit)
      Maximum recursion limit for partials.
      Parameters:
      recursionLimit - the number of recursions we will attempt before failing
    • getRecursionLimit

      public int getRecursionLimit()
    • compilePartial

      public Mustache compilePartial(String s)
      In order to handle recursion, we need a temporary thread local cache during compilation that is ultimately thrown away after the top level partial is complete.
      Parameters:
      s - the name of the partial to compile
      Returns:
      the compiled partial
    • createParser

      protected MustacheParser createParser()
    • getMustacheCacheFunction

      protected Function<String,Mustache> getMustacheCacheFunction()
    • createMustacheCache

      protected ConcurrentHashMap<String,Mustache> createMustacheCache()
    • createLambdaCache

      protected ConcurrentHashMap<FragmentKey,Mustache> createLambdaCache()