Package com.github.mustachejava
Class DefaultMustacheFactory
- java.lang.Object
-
- com.github.mustachejava.DefaultMustacheFactory
-
- All Implemented Interfaces:
MustacheFactory
- Direct Known Subclasses:
DeferringMustacheFactory,FallbackMustacheFactory,SafeMustacheFactory,SpecMustacheFactory
public class DefaultMustacheFactory extends java.lang.Object implements MustacheFactory
Simplest possible code factory
-
-
Field Summary
Fields Modifier and Type Field Description protected java.util.concurrent.ExecutorServiceesprotected MustacheParsermcThis parser should work with any MustacheFactoryprotected java.util.concurrent.ConcurrentHashMap<java.lang.String,Mustache>mustacheCacheCreate the default cache for mustache compilations.private MustacheResolvermustacheResolverprotected ObjectHandlerohThis is the default object handler.private java.lang.ThreadLocal<java.util.Map<java.lang.String,Mustache>>partialCacheprotected intrecursionLimitprotected java.util.concurrent.ConcurrentHashMap<FragmentKey,Mustache>templateCacheNew templates that are generated at runtime are cached here.
-
Constructor Summary
Constructors Constructor Description DefaultMustacheFactory()DefaultMustacheFactory(MustacheResolver mustacheResolver)DefaultMustacheFactory(java.io.File fileRoot)Use the file system to resolve mustache templates.DefaultMustacheFactory(java.lang.String classpathResourceRoot)Use the classpath to resolve mustache templates.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Mustachecompile(java.io.Reader reader, java.lang.String name)Create a mustache given a reader and a name.Mustachecompile(java.io.Reader reader, java.lang.String file, java.lang.String sm, java.lang.String em)Mustachecompile(java.lang.String name)Create a mustache given a resource name.MustachecompilePartial(java.lang.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.protected java.util.concurrent.ConcurrentHashMap<FragmentKey,Mustache>createLambdaCache()protected java.util.concurrent.ConcurrentHashMap<java.lang.String,Mustache>createMustacheCache()MustacheVisitorcreateMustacheVisitor()Creates the visitor for compilation.protected MustacheParsercreateParser()voidencode(java.lang.String value, java.io.Writer writer)This defines how "encoded" values are encoded.private static java.lang.StringensureForwardSlash(java.lang.String path)java.lang.StringfilterText(java.lang.String appended, boolean startOfLine)Override this method to apply any filtering to text that will appear verbatim in the output template.java.util.concurrent.ExecutorServicegetExecutorService()There is an ExecutorService that is used when executing parallel operations when a Callable is returned from a mustache value or iterable.MustachegetFragment(FragmentKey templateKey)protected java.util.function.Function<FragmentKey,Mustache>getFragmentCacheFunction()protected java.util.function.Function<java.lang.String,Mustache>getMustacheCacheFunction()ObjectHandlergetObjectHandler()The object handler knows how to transform names into fields and methods.java.io.ReadergetReader(java.lang.String resourceName)Given a resource name, construct a reader.intgetRecursionLimit()java.lang.StringresolvePartialPath(java.lang.String dir, java.lang.String name, java.lang.String extension)Using the directory, namd and extension, resolve a partial to a name.voidsetExecutorService(java.util.concurrent.ExecutorService es)If you need to specify your own executor service you can.voidsetObjectHandler(ObjectHandler oh)You can override the default object handler post construction.voidsetRecursionLimit(int recursionLimit)Maximum recursion limit for partials.java.lang.Stringtranslate(java.lang.String from)Converts your arbitrary name to another name.
-
-
-
Field Detail
-
mustacheCache
protected final java.util.concurrent.ConcurrentHashMap<java.lang.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 java.util.concurrent.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 java.util.concurrent.ExecutorService es
-
partialCache
private final java.lang.ThreadLocal<java.util.Map<java.lang.String,Mustache>> partialCache
-
-
Constructor Detail
-
DefaultMustacheFactory
public DefaultMustacheFactory()
-
DefaultMustacheFactory
public DefaultMustacheFactory(MustacheResolver mustacheResolver)
-
DefaultMustacheFactory
public DefaultMustacheFactory(java.lang.String classpathResourceRoot)
Use the classpath to resolve mustache templates.- Parameters:
classpathResourceRoot- the location in the resources where templates are stored
-
DefaultMustacheFactory
public DefaultMustacheFactory(java.io.File fileRoot)
Use the file system to resolve mustache templates.- Parameters:
fileRoot- the root of the file system where templates are stored
-
-
Method Detail
-
resolvePartialPath
public java.lang.String resolvePartialPath(java.lang.String dir, java.lang.String name, java.lang.String extension)Using the directory, namd and extension, resolve a partial to a name.- Parameters:
dir-name-extension-- Returns:
-
ensureForwardSlash
private static java.lang.String ensureForwardSlash(java.lang.String path)
-
createMustacheVisitor
public MustacheVisitor createMustacheVisitor()
Description copied from interface:MustacheFactoryCreates the visitor for compilation.- Specified by:
createMustacheVisitorin interfaceMustacheFactory- Returns:
- visitor
-
getReader
public java.io.Reader getReader(java.lang.String resourceName)
Description copied from interface:MustacheFactoryGiven a resource name, construct a reader.- Specified by:
getReaderin interfaceMustacheFactory- Parameters:
resourceName- used to find the resource- Returns:
- a reader
-
encode
public void encode(java.lang.String value, java.io.Writer writer)Description copied from interface:MustacheFactoryThis defines how "encoded" values are encoded. It defaults to something appropriate for HTML output.- Specified by:
encodein interfaceMustacheFactory- Parameters:
value- the unencoded valuewriter- where to encode the value
-
getObjectHandler
public ObjectHandler getObjectHandler()
Description copied from interface:MustacheFactoryThe object handler knows how to transform names into fields and methods.- Specified by:
getObjectHandlerin interfaceMustacheFactory- 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 java.util.concurrent.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(java.util.concurrent.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 java.util.function.Function<FragmentKey,Mustache> getFragmentCacheFunction()
-
compile
public Mustache compile(java.lang.String name)
Description copied from interface:MustacheFactoryCreate a mustache given a resource name.- Specified by:
compilein interfaceMustacheFactory- Parameters:
name- the name of the resource- Returns:
- the compiled mustache
-
compile
public Mustache compile(java.io.Reader reader, java.lang.String name)
Description copied from interface:MustacheFactoryCreate a mustache given a reader and a name.- Specified by:
compilein interfaceMustacheFactory- Parameters:
reader- the readername- the name of the resource- Returns:
- the compiled mustache
-
compile
public Mustache compile(java.io.Reader reader, java.lang.String file, java.lang.String sm, java.lang.String em)
-
translate
public java.lang.String translate(java.lang.String from)
Description copied from interface:MustacheFactoryConverts your arbitrary name to another name.- Specified by:
translatein interfaceMustacheFactory- Parameters:
from- the tag to replace- Returns:
- the new tag
-
filterText
public java.lang.String filterText(java.lang.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 outputstartOfLine- 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(java.lang.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 java.util.function.Function<java.lang.String,Mustache> getMustacheCacheFunction()
-
createMustacheCache
protected java.util.concurrent.ConcurrentHashMap<java.lang.String,Mustache> createMustacheCache()
-
createLambdaCache
protected java.util.concurrent.ConcurrentHashMap<FragmentKey,Mustache> createLambdaCache()
-
-