class ST extends AnyRef
A Scala interface to a StringTemplate ST template. objet Note that this
interface does not directly expose all the underlying ST methods. In
particular, this Scala interface is geared primarily toward reading and
rendering external templates, not toward generating templates in code.
This class cannot be instantiated directly. Instead, use the apply()
methods in the companion object.
Because of the way the ST API instantiates templates, this class cannot
easily subclass the real ST class. So, it wraps the underlying string
template object and stores it internally. You can retrieve the wrapped
template object via the nativeST method. You are free to call methods
directly on template, though they will use Java semantics, rather than
Scala semantics.
Note that this class explicitly handles mapping the following types of values in an attribute map:
- A Scala
Seq(which includes lists and array buffers) is mapped to ajava.util.List, so it's treated as a multivalued attribute by the underlying ST library. - A Scala iterator is also mapped to a
java.util.List. - Numbers and strings are added as is.
- Anything else is treated as a regular object and wrapped in a Java Bean. See below.
Bean Wrapping
Regular objects are, by default, wrapped in a Java Bean, because the
underlying String Template API uses Java Bean semantics to access object
fields. Thus, if a template references "foo.bar", StringTemplate will expect
that the object associated with the name "foo" has a method called
getBar(). To allow Scala objects (and, especially, case class objects)
to be used directly, Scalasti automatically generates wrapper Java Beans
for them.
There are cases where you don't want this behavior, however. For instance,
it doesn't make much sense with numeric values or strings, so Scalasti
deliberately does not wrap those objects. There are other cases where
you might not want the automatic Bean-wrapper behavior; see the
add method for more details.
- Alphabetic
- By Inheritance
- ST
- AnyRef
- Any
- Hide All
- Show All
- Public
- All
Value Members
-
final
def
!=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
##(): Int
- Definition Classes
- AnyRef → Any
-
final
def
==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
def
add(name: String, value: Any, raw: Boolean = false): ST
Add an attribute to the template.
Add an attribute to the template. Sequences, iterators and maps are converted (copied) to their Java equivalents, with their contents mapped to strings. (If you need nested objects of arbitrary depth, used a mapped aggregate.) Other objects are wrapped in Java Beans, unless
rawis set totrue.NOTE: If you add an
AttributeRendererto a parent group, any objects you intend the renderer to render must be added as raw objects. The underlying StringTemplate API matches attribute renders to attributes by type (i.e., by Java class). If you don't specifyraw=true, Scalasti will wrap the object in a generated Java Bean, and StringTemplate won't be able to match it with the corresponding renderer.Adding an attribute twice seems to add it twice, causing it to be rendered concatenated, in some cases. If you want to ensure that it is set "clean", use the
setmethod, which clears any existing attribute first.- name
the name to associate with the attribute. This is the name by which the attribute can be referenced in the template
- value
the value of the attribute
- raw
false(the default) to wrap the value in a Java Bean.trueto add it, as is.- returns
this object, for chaining
-
def
addAggregate(aggrSpec: String, values: Any*): ST
Set an automatic aggregate from the specified arguments.
Set an automatic aggregate from the specified arguments. An automatic aggregate looks like an object from within a template, but it isn't backed by a bean. Instead, you specify the aggregate with a special syntax. For instance, the following code defines an aggregate attribute called
name, with two fields,firstandlast. Those fields can be interpolated within a template via$item.first$and$item.last$.val st = new ST( ... ) st.setAggregate("name.{first,last}", "Moe", "Howard")
Setting the same aggregate multiple times results in a list of aggregates:
val st = new ST( ... ) st.setAggregate("name.{first,last}", "Moe", "Howard") st.setAggregate("name.{first,last}", "Larry", "Fine") st.setAggregate("name.{first,last}", "Curley", "Howard")
Note, however, that this syntax does not support nested aggregates. Use
addMappedAggregate()for that.See http://www.antlr.org/wiki/display/ST/Expressions#Expressions-Automaticaggregatecreation for more information.
- aggrSpec
the spec, as described above
- values
one or more values. The values are treated as discrete; that is, lists are not supported.
- returns
this object, for convenience
-
def
addAttributes(attrs: Map[String, Any], raw: Boolean = true): ST
Add a map of objects (key=value pairs) to the template.
Add a map of objects (key=value pairs) to the template.
- attrs
the attributes to add
- raw
false(the default) to wrap all values in Java Beans.trueto add it, as is.- returns
this object, for chaining
-
def
addMappedAggregate(attrName: String, valueMap: Map[String, Any]): ST
Create a "mapped aggregate".
Create a "mapped aggregate". The supplied map's keys are used as the fields of the aggregate. With a mapped aggregate, Scalasti actually translates the map into a Java Bean, which it then uses to set the attribute. Because Scalasti recursively converts all maps it finds (as long as they are of type
Map[String, Any]), a mapped attribute can handle nested attribute references.The underlying ST library does _not_ support the notion of a mapped aggregate; mapped aggregates are a Scalasti add-on.
For example, given this map:
Map("foo" -> List(1, 2), "bar" -> "barski")
and the name "mystuff", this method will produce the equivalent of the following call:
template.setAggregate("mystuff.{foo, bar}", List(1, 2), "barski")
Nested maps are supported. For instance, this code fragment:
val attrMap = Map("foo" -> "FOO", "alien" -> Map("firstName" -> "John", "lastName" -> "Smallberries")) template.setAggregate("thing", attrMap)
will make the following values available in a template:
$thing.foo$ # expands to "FOO" $things.alien.firstName$ # expands to "John" $things.alien.lastName$ # expands to "Smallberries"
- attrName
the attribute's name (i.e., the outermost name)
- valueMap
the map of attribute fields
- returns
this object, for convenience
-
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
-
def
attribute[T](name: String)(implicit arg0: scala.reflect.api.JavaUniverse.TypeTag[T]): Option[T]
Retrieve an attribute from the map.
-
def
attributes: Map[String, Any]
Get a map of the attributes in the template.
Get a map of the attributes in the template.
- returns
a copy of the internal attributes
-
def
clone(): AnyRef
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
equals(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
def
finalize(): Unit
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( classOf[java.lang.Throwable] )
-
final
def
getClass(): Class[_]
- Definition Classes
- AnyRef → Any
-
def
hashCode(): Int
- Definition Classes
- AnyRef → Any
-
def
isAnonSubtemplate: Boolean
Determine whether this template is an anonymous sub-template.
Determine whether this template is an anonymous sub-template. See the StringTemplate documents for details.
- returns
trueif the template is an anonymous subtemplate,falseotherwise
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
-
def
name: String
Get the template's name.
Get the template's name.
- returns
the name, which will always be non-null
-
def
nativeTemplate: stringtemplate.v4.ST
Get the underlying native StringTemplate API template.
Get the underlying native StringTemplate API template.
- returns
the template
-
final
def
ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
final
def
notify(): Unit
- Definition Classes
- AnyRef
-
final
def
notifyAll(): Unit
- Definition Classes
- AnyRef
-
def
remove(name: String): Unit
Remove an attribute from the template.
Remove an attribute from the template.
- name
the name of the attribute to remove
-
def
render(locale: Locale = Locale.getDefault, lineWidth: Int = 80): String
Render the template to a string.
Render the template to a string.
- locale
The locale to use
- lineWidth
The line width
- returns
The rendered string
-
def
set(name: String, value: Any, raw: Boolean = false): ST
Set an attribute in the template, clearing any existing attribute of the same name first.
Set an attribute in the template, clearing any existing attribute of the same name first. Calling this method is the same as calling
remove, followed byadd.- name
the name to associate with the attribute. This is the name by which the attribute can be referenced in the template
- value
the value of the attribute
- raw
false(the default) to wrap the value in a Java Bean.trueto add it, as is.- returns
this object, for chaining
-
def
setAttributes(attrs: Map[String, Any], raw: Boolean = true): ST
Clear all existing attributes from the template, and add a new map of objects (key=value pairs) to the template.
Clear all existing attributes from the template, and add a new map of objects (key=value pairs) to the template.
- attrs
the attributes to add
- raw
false(the default) to wrap all values in Java Beans.trueto add it, as is.- returns
this object, for chaining
-
final
def
synchronized[T0](arg0: ⇒ T0): T0
- Definition Classes
- AnyRef
-
def
toString(): String
Return a string representation of the template.
-
final
def
wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
def
write(out: STWriter, locale: Locale = Locale.getDefault, listener: Option[STErrorListener] = None): Try[Int]
Write the template to an
STWriter.Write the template to an
STWriter. TheSTWriterinterface is supplied by the underlying StringTemplate API, which also supplies some implementations of it (e.g.,AutoIndentWriterandNoIndentWriter).- out
the
STWriter- locale
the locale
- listener
an implementation of the StringTemplate's
STErrorListenerinterface, to receive an errors that occur during the write- returns
Success(total), with the total number of characters written; orFailure(exception)on error.