Top Description Fields Constructors Methods
org.antlr.stringtemplate.language

public Class ASTExpr

extends Expr
Class Inheritance
Known Direct Subclasses
org.antlr.stringtemplate.language.ConditionalExpr
Imports
antlr.RecognitionException, antlr.collections.AST, org.antlr.stringtemplate.AttributeRenderer, .StringTemplate, .StringTemplateGroup, .StringTemplateWriter, java.io.IOException, .StringWriter, java.lang.reflect.Field, .InvocationTargetException, .Method, java.util.*

A single string template expression enclosed in $...; separator=...$ parsed into an AST chunk to be evaluated.

Field Summary

Modifier and TypeField and Description
public static final String
public static final String
public static final String
public static final String
public static final String
public static final String
public static final Map<K, V>
public static final String
EMPTY_OPTION

Using an expr option w/o value, makes options table hold EMPTY_OPTION value for that key.

pack-priv AST
pack-priv String
formatString

A cached value of option format=expr

public static final StringTemplate
MAP_KEY_VALUE

Used to indicate "default:key" in maps within groups

public static final int
pack-priv String
nullValue

For null values in iterated attributes and single attributes that are null, use this value instead of skipping.

pack-priv Map<K, V>
options

store separator etc...

pack-priv String
separatorString

A cached value of separator=expr from the <...> expression.

public static final Set<E>
supportedOptions

John Snyders gave me an example implementation for this checking

pack-priv String
wrapString

A cached value of wrap=expr from the <...> expression.

Inherited from org.antlr.stringtemplate.language.Expr:
enclosingTemplateindentation

Constructor Summary

AccessConstructor and Description
public
ASTExpr(StringTemplate enclosingTemplate, AST exprTree, Map<K, V> options)

Method Summary

Modifier and TypeMethod and Description
protected Object
public Object
add(Object a, Object b)

For now, we can only add two objects as strings; convert objects to Strings then cat.

public Object
applyListOfAlternatingTemplates(StringTemplate self, Object attributeValue, List<E> templatesToApply)

public Object
applyTemplateToListOfAttributes(StringTemplate self, List<E> attributes, StringTemplate templateToApply)

For <names,phones:{n,p | ...}> treat the names, phones as lists to be walked in lock step as n=names[i], p=phones[i].

protected static Object
protected static Iterator<E>
public static Object
convertArrayToList(Object value)

Do a standard conversion of array attributes to a List.

protected void
evaluateArguments(StringTemplate self)

Evaluate an argument list within the context of the enclosing template but store the values in the context of self, the new embedded template.

public String
evaluateExpression(StringTemplate self, Object expr)

A expr is normally just a string literal, but is still an AST that we must evaluate.

public Object
first(Object attribute)

Return the first attribute if multiple valued or the attribute itself if single-valued.

public AST
getAST()

Return the tree interpreted when this template is written out.

protected Method
getMethod(Class<T> c, String methodName)

public Object
getObjectProperty(StringTemplate self, final Object o, final Object propertyName)

Return o.getPropertyName() given o and propertyName.

public Object
public StringTemplate
getTemplateInclude(StringTemplate enclosing, String templateName, StringTemplateAST argumentsAST)

Call a string template with args and return result.

protected void
handleExprOptions(StringTemplate self)

Grab and cache options; verify options are valid

protected Object
public Object
last(Object attribute)

Return the last attribute if multiple valued or the attribute itself if single-valued.

public Object
length(Object attribute)

Return the length of a multiple valued attribute or 1 if it is a single attribute.

protected Object
public Object
rest(Object attribute)

Return the everything but the first attribute if multiple valued or null if single-valued.

protected void
setSoleFormalArgumentToIthValue(StringTemplate embedded, Map<K, V> argumentContext, Object ithValue)

public Object
strip(Object attribute)

Return a new list w/o null values.

public boolean
testAttributeTrue(Object a)

Normally StringTemplate tests presence or absence of attributes for adherence to my principles of separation, but some people disagree and want to change.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
public Object
trunc(Object attribute)

Return all but the last element.

public int
write(StringTemplate self, StringTemplateWriter out)

Implements abstract org.antlr.stringtemplate.language.Expr.write.

To write out the value of an ASTExpr, invoke the evaluator in eval.g to walk the tree writing out the values.
protected int
public int
writeAttribute(StringTemplate self, Object o, StringTemplateWriter out)

How to spit out an object.

protected int
protected int
protected int
Inherited from org.antlr.stringtemplate.language.Expr:
getEnclosingTemplategetIndentationsetIndentation

Field Detail

DEFAULT_ATTRIBUTE_NAMEback to summary
public static final String DEFAULT_ATTRIBUTE_NAME
DEFAULT_ATTRIBUTE_NAME_DEPRECATEDback to summary
public static final String DEFAULT_ATTRIBUTE_NAME_DEPRECATED
DEFAULT_INDEX0_VARIABLE_NAMEback to summary
public static final String DEFAULT_INDEX0_VARIABLE_NAME
DEFAULT_INDEX_VARIABLE_NAMEback to summary
public static final String DEFAULT_INDEX_VARIABLE_NAME
DEFAULT_MAP_KEY_NAMEback to summary
public static final String DEFAULT_MAP_KEY_NAME
DEFAULT_MAP_VALUE_NAMEback to summary
public static final String DEFAULT_MAP_VALUE_NAME
defaultOptionValuesback to summary
public static final Map<K, V> defaultOptionValues
EMPTY_OPTIONback to summary
public static final String EMPTY_OPTION

Using an expr option w/o value, makes options table hold EMPTY_OPTION value for that key.

exprTreeback to summary
pack-priv AST exprTree
formatStringback to summary
pack-priv String formatString

A cached value of option format=expr

MAP_KEY_VALUEback to summary
public static final StringTemplate MAP_KEY_VALUE

Used to indicate "default:key" in maps within groups

MISSINGback to summary
public static final int MISSING
nullValueback to summary
pack-priv String nullValue

For null values in iterated attributes and single attributes that are null, use this value instead of skipping. For single valued attributes like it's a shorthand for n/a For iterated values , you get 0 for for null list values. Works for template application like: }; null="0"> also.

optionsback to summary
pack-priv Map<K, V> options

store separator etc...

separatorStringback to summary
pack-priv String separatorString

A cached value of separator=expr from the <...> expression. Computed in write(StringTemplate, StringTemplateWriter) and used in writeAttribute.

supportedOptionsback to summary
public static final Set<E> supportedOptions

John Snyders gave me an example implementation for this checking

wrapStringback to summary
pack-priv String wrapString

A cached value of wrap=expr from the <...> expression. Computed in write(StringTemplate, StringTemplateWriter) and used in writeAttribute.

Constructor Detail

ASTExprback to summary
public ASTExpr(StringTemplate enclosingTemplate, AST exprTree, Map<K, V> options)

Method Detail

accessFieldback to summary
protected Object accessField(Field f, Object o, Object value) throws IllegalAccessException
addback to summary
public Object add(Object a, Object b)

For now, we can only add two objects as strings; convert objects to Strings then cat.

applyListOfAlternatingTemplatesback to summary
public Object applyListOfAlternatingTemplates(StringTemplate self, Object attributeValue, List<E> templatesToApply)
applyTemplateToListOfAttributesback to summary
public Object applyTemplateToListOfAttributes(StringTemplate self, List<E> attributes, StringTemplate templateToApply)

For treat the names, phones as lists to be walked in lock step as n=names[i], p=phones[i].

convertAnythingIteratableToIteratorback to summary
protected static Object convertAnythingIteratableToIterator(Object o)
convertAnythingToIteratorback to summary
protected static Iterator<E> convertAnythingToIterator(Object o)
convertArrayToListback to summary
public static Object convertArrayToList(Object value)

Do a standard conversion of array attributes to a List. Wrap the array instead of copying like old version. Make an ArrayWrappedInList that knows to create an ArrayIterator.

evaluateArgumentsback to summary
protected void evaluateArguments(StringTemplate self)

Evaluate an argument list within the context of the enclosing template but store the values in the context of self, the new embedded template. For example, bold(item=item) means that bold.item should get the value of enclosing.item.

evaluateExpressionback to summary
public String evaluateExpression(StringTemplate self, Object expr)

A expr is normally just a string literal, but is still an AST that we must evaluate. The expr can be any expression such as a template include or string cat expression etc... Evaluate with its own writer so that we can convert to string and then reuse, don't want to compute all the time; must precompute w/o writing to output buffer.

firstback to summary
public Object first(Object attribute)

Return the first attribute if multiple valued or the attribute itself if single-valued. Used in

getASTback to summary
public AST getAST()

Return the tree interpreted when this template is written out.

getMethodback to summary
protected Method getMethod(Class<T> c, String methodName)
getObjectPropertyback to summary
public Object getObjectProperty(StringTemplate self, final Object o, final Object propertyName)

Return o.getPropertyName() given o and propertyName. If o is a stringtemplate then access it's attributes looking for propertyName instead (don't check any of the enclosing scopes; look directly into that object). Also try isXXX() for booleans. Allow Map as special case (grab value for key). Cache repeated requests for obj.prop within same group.

getOptionback to summary
public Object getOption(String name)
getTemplateIncludeback to summary
public StringTemplate getTemplateInclude(StringTemplate enclosing, String templateName, StringTemplateAST argumentsAST)

Call a string template with args and return result. Do not convert to a string yet. It may need attributes that will be available after this is inserted into another template.

handleExprOptionsback to summary
protected void handleExprOptions(StringTemplate self)

Grab and cache options; verify options are valid

invokeMethodback to summary
protected Object invokeMethod(Method m, Object o, Object value) throws IllegalAccessException, InvocationTargetException
lastback to summary
public Object last(Object attribute)

Return the last attribute if multiple valued or the attribute itself if single-valued. Used in . This is pretty slow as it iterates until the last element. Ultimately, I could make a special case for a List or Vector.

lengthback to summary
public Object length(Object attribute)

Return the length of a multiple valued attribute or 1 if it is a single attribute. If attribute is null return 0. Special case several common collections and primitive arrays for speed. This method by Kay Roepke.

rawGetObjectPropertyback to summary
protected Object rawGetObjectProperty(StringTemplate self, Object o, Object property)
restback to summary
public Object rest(Object attribute)

Return the everything but the first attribute if multiple valued or null if single-valued. Used in .

setSoleFormalArgumentToIthValueback to summary
protected void setSoleFormalArgumentToIthValue(StringTemplate embedded, Map<K, V> argumentContext, Object ithValue)
stripback to summary
public Object strip(Object attribute)

Return a new list w/o null values.

testAttributeTrueback to summary
public boolean testAttributeTrue(Object a)

Normally StringTemplate tests presence or absence of attributes for adherence to my principles of separation, but some people disagree and want to change. For 2.0, if the object is a boolean, do something special. $if(boolean)$ will actually test the value. Now, this breaks my rules of entanglement listed in my paper, but it truly surprises programmers to have booleans always true. Further, the key to isolating logic in the model is avoiding operators (for which you need attribute values). But, no operator is needed to use boolean values. Well, actually I guess "!" (not) is an operator. Regardless, for practical reasons, I'm going to technically violate my rules as I currently have them defined. Perhaps for a future version of the paper I will refine the rules. Post 2.1, I added a check for non-null Iterators, Collections, ... with size==0 to return false. TJP 5/1/2005

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.

truncback to summary
public Object trunc(Object attribute)

Return all but the last element. trunc(x)=null if x is single-valued.

writeback to summary
public int write(StringTemplate self, StringTemplateWriter out) throws IOException

Implements abstract org.antlr.stringtemplate.language.Expr.write.

To write out the value of an ASTExpr, invoke the evaluator in eval.g to walk the tree writing out the values. For efficiency, don't compute a bunch of strings and then pack them together. Write out directly. Compute separator and wrap expressions, save as strings so we don't recompute for each value in a multi-valued attribute or expression. If they set anchor option, then inform the writer to push current char position.

writeback to summary
protected int write(StringTemplate self, Object o, StringTemplateWriter out)
writeAttributeback to summary
public int writeAttribute(StringTemplate self, Object o, StringTemplateWriter out)

How to spit out an object. If it's not a StringTemplate nor a List, just do o.toString(). If it's a StringTemplate, do o.write(out). If it's a Vector, do a write(out, o.elementAt(i)) for all elements. Note that if you do something weird like set the values of a multivalued tag to be vectors, it will effectively flatten it. If self is an embedded template, you might have specified a separator arg; used when is a vector.

writeIterableValueback to summary
protected int writeIterableValue(StringTemplate self, Object o, StringTemplateWriter out) throws IOException
writePOJOback to summary
protected int writePOJO(StringTemplate self, Object o, StringTemplateWriter out) throws IOException
writeTemplateback to summary
protected int writeTemplate(StringTemplate self, Object o, StringTemplateWriter out) throws IOException