Top Description Fields Constructors Methods
org.stringtemplate.v4

public Class STGroup

extends Object
Class Inheritance
Known Direct Subclasses
org.stringtemplate.v4.STGroupDir, org.stringtemplate.v4.STGroupFile, org.stringtemplate.v4.STGroupString
Imports
org.antlr.runtime.ANTLRFileStream, .ANTLRInputStream, .CharStream, .CommonToken, .CommonTokenStream, .RecognitionException, .Token, org.stringtemplate.v4.compiler.CompiledST, .Compiler, .FormalArgument, .GroupLexer, .GroupParser, .STException, org.stringtemplate.v4.gui.STViz, org.stringtemplate.v4.misc.Aggregate, .AggregateModelAdaptor, .ErrorManager, .ErrorType, .MapModelAdaptor, .Misc, .ObjectModelAdaptor, .STModelAdaptor, .TypeRegistry, java.io.IOException, .InputStream, java.net.MalformedURLException, .URL, java.util.ArrayList, .Arrays, .Collections, .Date, .HashMap, .HashSet, .LinkedHashMap, .List, .Map, .Set

A directory or directory tree of .st template files and/or group files. Individual template files contain formal template definitions. In a sense, it's like a single group file broken into multiple files, one for each template. ST v3 had just the pure template inside, not the template name and header. Name inside must match filename (minus suffix).

Field Summary

Modifier and TypeField and Description
protected final Map<Class<?>, ModelAdaptor<?>>
adaptors

A dictionary that allows people to register a model adaptor for a particular kind of object (subclass or implementation).

public static final ErrorManager
public static final String
public static STGroup
public char
public char
public static final String
DICT_KEY

When we use key as a value in a dictionary, this is how we signify.

protected Map<String, Map<String, Object>>
dictionaries

Maps dictionary names to Map objects representing the dictionaries defined by the user like typeInitMap ::= ["int":"0"].

public String
encoding

The encoding to use for loading files.

public ErrorManager
errMgr

The ErrorManager for entire group; all compilations and executions.

public static final String
protected final List<STGroup>
imports

Every group can import templates/dictionaries from other groups.

protected final List<STGroup>
public boolean
iterateAcrossValues

v3 compatibility; used to iterate across Map#values() instead of v4's default Map#keySet().

protected static final CompiledST
NOT_FOUND_ST

Used to indicate that the template doesn't exist.

protected Map<Class<?>, AttributeRenderer<?>>
renderers

A dictionary that allows people to register a renderer for a particular kind of object for any template evaluated relative to this group.

private static final boolean[]
public static final String
protected Map<String, CompiledST>
templates

Maps template name to CompiledST object.

public static boolean
trackCreationEvents

For debugging with STViz.

public static boolean
verbose

Watch loading of groups and templates.

Constructor Summary

AccessConstructor and Description
public
public
STGroup(char delimiterStartChar, char delimiterStopChar)

Method Summary

Modifier and TypeMethod and Description
public CompiledST
compile(String srcName, String name, List<FormalArgument> args, String template, Token templateToken)

Compile a template.

public ST
createSingleton(Token templateToken)

Create singleton template for use with dictionary values.

public ST
public ST
createStringTemplateInternally(CompiledST impl)

Differentiate so we can avoid having creation events for regions, map operations, and other implicit "new ST" events during rendering.

public ST
public void
defineDictionary(String name, Map<String, Object> mapping)

Define a map for this group.

public CompiledST
defineRegion(String enclosingTemplateName, Token regionT, String template, Token templateToken)

public CompiledST
defineTemplate(String templateName, String template)

for testing

public CompiledST
defineTemplate(String name, String argsS, String template)

for testing

public CompiledST
defineTemplate(String fullyQualifiedTemplateName, Token nameT, List<FormalArgument> args, String template, Token templateToken)

public CompiledST
defineTemplateAlias(Token aliasT, Token targetT)

Make name and alias for target.

public void
defineTemplateOrRegion(String fullyQualifiedTemplateName, String regionSurroundingTemplateName, Token templateToken, String template, Token nameToken, List<FormalArgument> args)

public <T> AttributeRenderer<? super T>
getAttributeRenderer(Class<T> attributeType)

Get renderer for class T associated with this group.

protected ST
public String
public List<STGroup>
public ST
getInstanceOf(String name)

The primary means of getting an instance of a template from this group.

public STErrorListener
public static String
getMangledRegionName(String enclosingTemplateName, String name)

The "foo" of t() ::= "<@foo()>" is mangled to "/region__/t__foo"

public <T> ModelAdaptor<? super T>
getModelAdaptor(Class<T> attributeType)

public String
public URL
getRootDirURL()

Return root dir if this is group dir; return dir containing group file if this is group file.

public Set<String>
public static String
getUnMangledTemplateName(String mangledName)

Return "t.foo" from "/region__/t__foo"

public URL
getURL(String fileName)

public void
importTemplates(STGroup g)

Make this group import templates/dictionaries from g.

public void
importTemplates(Token fileNameToken)

Import template files, directories, and group files.

protected void
importTemplates(STGroup g, boolean clearOnUnload)

public boolean
isDefined(String name)

Is this template defined in this group or from this group below?

public boolean
public static boolean

Returns:

true if the character is reserved by the StringTemplate language; otherwise, false if the character may be used as a delimiter.
isReservedCharacter
(char
The character
c
)

Determines if a specified character may be used as a user-specified delimiter.

protected CompiledST
load(String name)

Load st from disk if directory or load whole group file if .stg file (then return just one template).

public void
load()

Force a load if it makes sense for the group.

public CompiledST
loadAbsoluteTemplateFile(String fileName)

Load template file into this group using absolute fileName.

public void
loadGroupFile(String prefix, String fileName)

Load a group file with full path fileName; it's relative to root by prefix.

public CompiledST
loadTemplateFile(String prefix, String unqualifiedFileName, CharStream templateStream)

Load template stream into this group.

protected CompiledST
public CompiledST
lookupTemplate(String name)

Look up a fully-qualified name.

public void
public Map<String, Object>
public CompiledST
public <T> void
registerModelAdaptor(Class<T> attributeType, ModelAdaptor<? super T> adaptor)

Add an adaptor for a kind of object so ST knows how to pull properties from them.

public <T> void
registerRenderer(Class<T> attributeType, AttributeRenderer<? super T> r)

Register a renderer for all objects of a particular "kind" for all templates evaluated relative to this group.

public <T> void
registerRenderer(Class<T> attributeType, AttributeRenderer<? super T> r, boolean recursive)

public void
public String
show()

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
public void
public synchronized void
unload()

Unload all templates, dictionaries and import relationships, but leave renderers and adaptors.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

adaptorsback to summary
protected final Map<Class<?>, ModelAdaptor<?>> adaptors

A dictionary that allows people to register a model adaptor for a particular kind of object (subclass or implementation). Applies for any template evaluated relative to this group.

ST initializes with model adaptors that know how to pull properties out of Objects, Maps, and STs.

The last one you register gets priority; do least to most specific.

DEFAULT_ERR_MGRback to summary
public static final ErrorManager DEFAULT_ERR_MGR
DEFAULT_KEYback to summary
public static final String DEFAULT_KEY
defaultGroupback to summary
public static STGroup defaultGroup
delimiterStartCharback to summary
public char delimiterStartChar
delimiterStopCharback to summary
public char delimiterStopChar
DICT_KEYback to summary
public static final String DICT_KEY

When we use key as a value in a dictionary, this is how we signify.

dictionariesback to summary
protected Map<String, Map<String, Object>> dictionaries

Maps dictionary names to Map objects representing the dictionaries defined by the user like typeInitMap ::= ["int":"0"].

encodingback to summary
public String encoding

The encoding to use for loading files. Defaults to UTF-8.

errMgrback to summary
public ErrorManager errMgr

The ErrorManager for entire group; all compilations and executions. This gets copied to parsers, walkers, and interpreters.

GROUP_FILE_EXTENSIONback to summary
public static final String GROUP_FILE_EXTENSION
importsback to summary
protected final List<STGroup> imports

Every group can import templates/dictionaries from other groups. The list must be synchronized (see STGroup#importTemplates).

importsToClearOnUnloadback to summary
protected final List<STGroup> importsToClearOnUnload
iterateAcrossValuesback to summary
public boolean iterateAcrossValues

v3 compatibility; used to iterate across Map#values() instead of v4's default Map#keySet(). But to convert ANTLR templates, it's too hard to find without static typing in templates.

NOT_FOUND_STback to summary
protected static final CompiledST NOT_FOUND_ST

Used to indicate that the template doesn't exist. Prevents duplicate group file loads and unnecessary file checks.

renderersback to summary
protected Map<Class<?>, AttributeRenderer<?>> renderers

A dictionary that allows people to register a renderer for a particular kind of object for any template evaluated relative to this group. For example, a date should be formatted differently depending on the locale. You can set Date.class to an object whose toString(Object) method properly formats a Date attribute according to locale. Or you can have a different renderer object for each locale.

Order of addition is recorded and matters. If more than one renderer works for an object, the first registered has priority.

Renderer associated with type t works for object o if

t.isAssignableFrom(o.getClass()) // would assignment t = o work?
So it works if o is subclass or implements t.

This structure is synchronized.

RESERVED_CHARACTERSback to summary
private static final boolean[] RESERVED_CHARACTERS
TEMPLATE_FILE_EXTENSIONback to summary
public static final String TEMPLATE_FILE_EXTENSION
templatesback to summary
protected Map<String, CompiledST> templates

Maps template name to CompiledST object. This map is synchronized.

trackCreationEventsback to summary
public static boolean trackCreationEvents

For debugging with STViz. Records where in code an ST was created and where code added attributes.

verboseback to summary
public static boolean verbose

Watch loading of groups and templates.

Constructor Detail

STGroupback to summary
public STGroup()
STGroupback to summary
public STGroup(char delimiterStartChar, char delimiterStopChar)

Method Detail

compileback to summary
public CompiledST compile(String srcName, String name, List<FormalArgument> args, String template, Token templateToken)

Compile a template.

createSingletonback to summary
public ST createSingleton(Token templateToken)

Create singleton template for use with dictionary values.

createStringTemplateback to summary
public ST createStringTemplate(CompiledST impl)
createStringTemplateInternallyback to summary
public ST createStringTemplateInternally(CompiledST impl)

Differentiate so we can avoid having creation events for regions, map operations, and other implicit "new ST" events during rendering.

createStringTemplateInternallyback to summary
public ST createStringTemplateInternally(ST proto)
defineDictionaryback to summary
public void defineDictionary(String name, Map<String, Object> mapping)

Define a map for this group.

Not thread safe...do not keep adding these while you reference them.

defineRegionback to summary
public CompiledST defineRegion(String enclosingTemplateName, Token regionT, String template, Token templateToken)
defineTemplateback to summary
public CompiledST defineTemplate(String templateName, String template)

for testing

defineTemplateback to summary
public CompiledST defineTemplate(String name, String argsS, String template)

for testing

defineTemplateback to summary
public CompiledST defineTemplate(String fullyQualifiedTemplateName, Token nameT, List<FormalArgument> args, String template, Token templateToken)
defineTemplateAliasback to summary
public CompiledST defineTemplateAlias(Token aliasT, Token targetT)

Make name and alias for target. Replace any previous definition of name.

defineTemplateOrRegionback to summary
public void defineTemplateOrRegion(String fullyQualifiedTemplateName, String regionSurroundingTemplateName, Token templateToken, String template, Token nameToken, List<FormalArgument> args)
getAttributeRendererback to summary
public <T> AttributeRenderer<? super T> getAttributeRenderer(Class<T> attributeType)

Get renderer for class T associated with this group.

For non-imported groups and object-to-render of class T, use renderer (if any) registered for T. For imports, any renderer set on import group is ignored even when using an imported template. You should set the renderer on the main group you use (or all to be sure). I look at import groups as "helpers" that should give me templates and nothing else. If you have multiple renderers for String, say, then just make uber combined renderer with more specific format names.

getEmbeddedInstanceOfback to summary
protected ST getEmbeddedInstanceOf(Interpreter interp, InstanceScope scope, String name)
getFileNameback to summary
public String getFileName()
getImportedGroupsback to summary
public List<STGroup> getImportedGroups()
getInstanceOfback to summary
public ST getInstanceOf(String name)

The primary means of getting an instance of a template from this group. Names must be absolute, fully-qualified names like /a/b.

getListenerback to summary
public STErrorListener getListener()
getMangledRegionNameback to summary
public static String getMangledRegionName(String enclosingTemplateName, String name)

The "foo" of t() ::= "<@foo()>" is mangled to "/region__/t__foo"

getModelAdaptorback to summary
public <T> ModelAdaptor<? super T> getModelAdaptor(Class<T> attributeType)
getNameback to summary
public String getName()
getRootDirURLback to summary
public URL getRootDirURL()

Return root dir if this is group dir; return dir containing group file if this is group file. This is derived from original incoming dir or filename. If it was absolute, this should come back as full absolute path. If only a URL is available, return URL of one dir up.

getTemplateNamesback to summary
public Set<String> getTemplateNames()
getUnMangledTemplateNameback to summary
public static String getUnMangledTemplateName(String mangledName)

Return "t.foo" from "/region__/t__foo"

getURLback to summary
public URL getURL(String fileName)
importTemplatesback to summary
public void importTemplates(STGroup g)

Make this group import templates/dictionaries from g.

On unload imported templates are unloaded but stay in the imports list.

importTemplatesback to summary
public void importTemplates(Token fileNameToken)

Import template files, directories, and group files. Priority is given to templates defined in the current group; this, in effect, provides inheritance. Polymorphism is in effect so that if an inherited template references template t() then we search for t() in the subgroup first.

Templates are loaded on-demand from import dirs. Imported groups are loaded on-demand when searching for a template.

The listener of this group is passed to the import group so errors found while loading imported element are sent to listener of this group.

On unload imported templates are unloaded and removed from the imports list.

This method is called when processing import statements specified in group files. Use importTemplates(STGroup) to import templates 'programmatically'.

importTemplatesback to summary
protected void importTemplates(STGroup g, boolean clearOnUnload)
isDefinedback to summary
public boolean isDefined(String name)

Is this template defined in this group or from this group below? Names must be absolute, fully-qualified names like /a/b.

isDictionaryback to summary
public boolean isDictionary(String name)
isReservedCharacterback to summary
public static boolean isReservedCharacter(char c)

Determines if a specified character may be used as a user-specified delimiter.

Parameters
c:char

The character

Returns:boolean

true if the character is reserved by the StringTemplate language; otherwise, false if the character may be used as a delimiter.

Since
4.0.9
loadback to summary
protected CompiledST load(String name)

Load st from disk if directory or load whole group file if .stg file (then return just one template). name is fully-qualified.

loadback to summary
public void load()

Force a load if it makes sense for the group.

loadAbsoluteTemplateFileback to summary
public CompiledST loadAbsoluteTemplateFile(String fileName)

Load template file into this group using absolute fileName.

loadGroupFileback to summary
public void loadGroupFile(String prefix, String fileName)

Load a group file with full path fileName; it's relative to root by prefix.

loadTemplateFileback to summary
public CompiledST loadTemplateFile(String prefix, String unqualifiedFileName, CharStream templateStream)

Load template stream into this group. unqualifiedFileName is "a.st". The prefix is path from group root to unqualifiedFileName like "/subdir" if file is in /subdir/a.st.

lookupImportedTemplateback to summary
protected CompiledST lookupImportedTemplate(String name)
lookupTemplateback to summary
public CompiledST lookupTemplate(String name)

Look up a fully-qualified name.

rawDefineTemplateback to summary
public void rawDefineTemplate(String name, CompiledST code, Token defT)
rawGetDictionaryback to summary
public Map<String, Object> rawGetDictionary(String name)
rawGetTemplateback to summary
public CompiledST rawGetTemplate(String name)
registerModelAdaptorback to summary
public <T> void registerModelAdaptor(Class<T> attributeType, ModelAdaptor<? super T> adaptor)

Add an adaptor for a kind of object so ST knows how to pull properties from them. Add adaptors in increasing order of specificity. ST adds Object, Map, ST, and Aggregate model adaptors for you first. Adaptors you add have priority over default adaptors.

If an adaptor for type T already exists, it is replaced by the adaptor argument.

This must invalidate cache entries, so set your adaptors up before calling ST#render for efficiency.

registerRendererback to summary
public <T> void registerRenderer(Class<T> attributeType, AttributeRenderer<? super T> r)

Register a renderer for all objects of a particular "kind" for all templates evaluated relative to this group. Use r to render if object in question is an instance of attributeType. Recursively set renderer into all import groups.

registerRendererback to summary
public <T> void registerRenderer(Class<T> attributeType, AttributeRenderer<? super T> r, boolean recursive)
setListenerback to summary
public void setListener(STErrorListener listener)
showback to summary
public String show()
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object

Annotations
@Override
undefineTemplateback to summary
public void undefineTemplate(String name)
unloadback to summary
public synchronized void unload()

Unload all templates, dictionaries and import relationships, but leave renderers and adaptors. This essentially forces the next call to getInstanceOf to reload templates. Call unload() on each group in the imports list, and remove all elements in importsToClearOnUnload from imports.