Package org.stringtemplate.v4
Class ST
- java.lang.Object
-
- org.stringtemplate.v4.ST
-
public class ST extends java.lang.ObjectAn instance of the StringTemplate. It consists primarily of a reference to its implementation (shared among all instances) and a hash table of attributes. Because of dynamic scoping, we also need a reference to any enclosing instance. For example, in a deeply nested template for an HTML page body, we could still reference the title attribute defined in the outermost page template.To use templates, you create one (usually via
STGroup) and then inject attributes usingadd(java.lang.String, java.lang.Object). To render its attacks, userender().TODO:
localsis not actually a hash table like the documentation says.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classST.AttributeListJust an alias forArrayList, but this way I can track whether a list is something ST created or it's an incoming list.static classST.DebugStateEvents during template hierarchy construction (not evaluation)static classST.RegionType<@r()>,<@r>...<@end>, and@t.r() ::= "..."defined manually by coder
-
Field Summary
Fields Modifier and Type Field Description ST.DebugStatedebugStateIfSTGroup.trackCreationEvents, track creation and add attribute events for each object.static java.lang.ObjectEMPTY_ATTRSTGroupgroupThatCreatedThisInstanceCreated as instance of which group? We need this to initialize interpreter via render.CompiledSTimplThe implementation for this template among all instances of same template .static java.lang.StringIMPLICIT_ARG_NAMEWhen there are no formal args for template t and you map t across some values, t implicitly gets arg "it".protected java.lang.Object[]localsSafe to simultaneously write viaadd(java.lang.String, java.lang.Object), which is synchronized.static java.lang.StringUNKNOWN_NAMEstatic java.lang.StringVERSION
-
Constructor Summary
Constructors Modifier Constructor Description protectedST()Used by group creation routine, not by usersST(java.lang.String template)Used to make templates inline in code for simple things like SQL or log records.ST(java.lang.String template, char delimiterStartChar, char delimiterStopChar)Create ST using non-default delimiters; each one of these will live in it's own group since you're overriding a default; don't want to alterSTGroup.defaultGroup.ST(ST proto)Clone a prototype template.ST(STGroup group, java.lang.String template)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description STadd(java.lang.String name, java.lang.Object value)Inject an attribute (name/value pair).STaddAggr(java.lang.String aggrSpec, java.lang.Object... values)SplitaggrName.{propName1,propName2}into list[propName1, propName2]and theaggrName.protected static ST.AttributeListconvertToAttributeList(java.lang.Object curvalue)static java.lang.Stringformat(int lineWidth, java.lang.String template, java.lang.Object... attributes)static java.lang.Stringformat(java.lang.String template, java.lang.Object... attributes)ST.format("<%1>:<%2>", n, p);java.lang.ObjectgetAttribute(java.lang.String name)Find an attribute in this template only.java.util.Map<java.lang.String,java.lang.Object>getAttributes()java.util.List<InterpEvent>getEvents()java.util.List<InterpEvent>getEvents(int lineWidth)java.util.List<InterpEvent>getEvents(java.util.Locale locale)java.util.List<InterpEvent>getEvents(java.util.Locale locale, int lineWidth)java.lang.StringgetName()STVizinspect()STVizinspect(int lineWidth)STVizinspect(java.util.Locale locale)STVizinspect(ErrorManager errMgr, java.util.Locale locale, int lineWidth)booleanisAnonSubtemplate()protected voidrawSetAttribute(java.lang.String name, java.lang.Object value)Setlocalsattribute value when you only know the name, not the index.voidremove(java.lang.String name)Remove an attribute value entirely (can't remove attribute definitions).java.lang.Stringrender()java.lang.Stringrender(int lineWidth)java.lang.Stringrender(java.util.Locale locale)java.lang.Stringrender(java.util.Locale locale, int lineWidth)java.lang.StringtoString()intwrite(java.io.File outputFile, STErrorListener listener)intwrite(java.io.File outputFile, STErrorListener listener, java.lang.String encoding)intwrite(java.io.File outputFile, STErrorListener listener, java.lang.String encoding, int lineWidth)intwrite(java.io.File outputFile, STErrorListener listener, java.lang.String encoding, java.util.Locale locale, int lineWidth)intwrite(STWriter out)intwrite(STWriter out, java.util.Locale locale)intwrite(STWriter out, java.util.Locale locale, STErrorListener listener)intwrite(STWriter out, STErrorListener listener)
-
-
-
Field Detail
-
VERSION
public static final java.lang.String VERSION
- See Also:
- Constant Field Values
-
UNKNOWN_NAME
public static final java.lang.String UNKNOWN_NAME
- See Also:
- Constant Field Values
-
EMPTY_ATTR
public static final java.lang.Object EMPTY_ATTR
-
IMPLICIT_ARG_NAME
public static final java.lang.String IMPLICIT_ARG_NAME
When there are no formal args for template t and you map t across some values, t implicitly gets arg "it". E.g., "$it$"- See Also:
- Constant Field Values
-
impl
public CompiledST impl
The implementation for this template among all instances of same template .
-
locals
protected java.lang.Object[] locals
Safe to simultaneously write viaadd(java.lang.String, java.lang.Object), which is synchronized. Reading during exec is, however, NOT synchronized. So, not thread safe to add attributes while it is being evaluated. Initialized toEMPTY_ATTRto distinguishnullfrom empty.
-
groupThatCreatedThisInstance
public STGroup groupThatCreatedThisInstance
Created as instance of which group? We need this to initialize interpreter via render. So, we create st and then it needs to know which group created it for sake of polymorphism:st = skin1.getInstanceOf("searchbox"); result = st.render(); // knows skin1 created itSay we have a groupg1with templatetthat imports templatestandufrom another groupg2.g1.getInstanceOf("u")findsuing2but remembers thatg1created it. Ifuincludest, it should createg1.tnotg2.t.g1 = {t(), u()} | v g2 = {t()}
-
debugState
public ST.DebugState debugState
IfSTGroup.trackCreationEvents, track creation and add attribute events for each object. Create this object on first use.
-
-
Constructor Detail
-
ST
protected ST()
Used by group creation routine, not by users
-
ST
public ST(java.lang.String template)
Used to make templates inline in code for simple things like SQL or log records. No formal arguments are set and there is no enclosing instance.
-
ST
public ST(java.lang.String template, char delimiterStartChar, char delimiterStopChar)Create ST using non-default delimiters; each one of these will live in it's own group since you're overriding a default; don't want to alterSTGroup.defaultGroup.
-
ST
public ST(STGroup group, java.lang.String template)
-
ST
public ST(ST proto)
Clone a prototype template. Copy all fields minusdebugState; don't delegate toST(), which createsConstructionEvent.
-
-
Method Detail
-
add
public ST add(java.lang.String name, java.lang.Object value)
Inject an attribute (name/value pair). If there is already an attribute with that name, this method turns the attribute into anST.AttributeListwith both the previous and the new attribute as elements. This method will never alter aListthat you inject. If you send in aListand then inject a single value element,addcopies original list and adds the new value. The attribute name cannot be null or contain '.'.Return
thisso we can chain:t.add("x", 1).add("y", "hi")
-
addAggr
public ST addAggr(java.lang.String aggrSpec, java.lang.Object... values)
SplitaggrName.{propName1,propName2}into list[propName1, propName2]and theaggrName. Spaces are allowed around','.
-
remove
public void remove(java.lang.String name)
Remove an attribute value entirely (can't remove attribute definitions).
-
rawSetAttribute
protected void rawSetAttribute(java.lang.String name, java.lang.Object value)Setlocalsattribute value when you only know the name, not the index. This is ultimately invoked by callingST#addfrom outside so toss an exception to notify them.
-
getAttribute
public java.lang.Object getAttribute(java.lang.String name)
Find an attribute in this template only.
-
getAttributes
public java.util.Map<java.lang.String,java.lang.Object> getAttributes()
-
convertToAttributeList
protected static ST.AttributeList convertToAttributeList(java.lang.Object curvalue)
-
getName
public java.lang.String getName()
-
isAnonSubtemplate
public boolean isAnonSubtemplate()
-
write
public int write(STWriter out) throws java.io.IOException
- Throws:
java.io.IOException
-
write
public int write(STWriter out, java.util.Locale locale)
-
write
public int write(STWriter out, STErrorListener listener)
-
write
public int write(STWriter out, java.util.Locale locale, STErrorListener listener)
-
write
public int write(java.io.File outputFile, STErrorListener listener) throws java.io.IOException- Throws:
java.io.IOException
-
write
public int write(java.io.File outputFile, STErrorListener listener, java.lang.String encoding) throws java.io.IOException- Throws:
java.io.IOException
-
write
public int write(java.io.File outputFile, STErrorListener listener, java.lang.String encoding, int lineWidth) throws java.io.IOException- Throws:
java.io.IOException
-
write
public int write(java.io.File outputFile, STErrorListener listener, java.lang.String encoding, java.util.Locale locale, int lineWidth) throws java.io.IOException- Throws:
java.io.IOException
-
render
public java.lang.String render()
-
render
public java.lang.String render(int lineWidth)
-
render
public java.lang.String render(java.util.Locale locale)
-
render
public java.lang.String render(java.util.Locale locale, int lineWidth)
-
inspect
public STViz inspect()
-
inspect
public STViz inspect(int lineWidth)
-
inspect
public STViz inspect(java.util.Locale locale)
-
inspect
public STViz inspect(ErrorManager errMgr, java.util.Locale locale, int lineWidth)
-
getEvents
public java.util.List<InterpEvent> getEvents()
-
getEvents
public java.util.List<InterpEvent> getEvents(int lineWidth)
-
getEvents
public java.util.List<InterpEvent> getEvents(java.util.Locale locale)
-
getEvents
public java.util.List<InterpEvent> getEvents(java.util.Locale locale, int lineWidth)
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
format
public static java.lang.String format(java.lang.String template, java.lang.Object... attributes)ST.format("<%1>:<%2>", n, p);
-
format
public static java.lang.String format(int lineWidth, java.lang.String template, java.lang.Object... attributes)
-
-