Alternate constructor that takes a Seq
of characters containing
an unnamed, ungrouped template.
Alternate constructor that takes a Seq
of characters containing
an unnamed, ungrouped template.
This constructor basically just reads the contents of the sequence into
a string. While this approach makes using a lazy sequence (e.g., a
Stream
) somewhat pointless, the underlying StringTemplate API will only
accept a string.
This constructor can obviously be used with Stream
objects. For
instance:
val t1 = new StringTemplate(Stream('a', 'b', 'c')) val t2 = new StringTemplate(Stream("abc", "def").flatMap(_.toString))
Don't give this constructor an infinite stream, if you want to get control back.
the template, as a sequence of characters
Alternate constructor that takes an unnamed, ungrouped template, as contained in a string.
Alternate constructor that takes an unnamed, ungrouped template, as contained in a string.
the contents of the template
the StringTemplateGroup in which the template resides
the real, underlying String Template
Get the template's internally assigned ID.
Get the template's internally assigned ID.
the ID
Get a map containing all attribute names and values.
Get a map containing all attribute names and values.
a read-only map of attributes.
Get the current error listener, which is notified when errors occur.
Get the current error listener, which is notified when errors occur.
the error listener
Set the current error listener, which is notified when errors occur.
Set the current error listener, which is notified when errors occur.
the error listener
Given a Scala map, with string keys and any kind of value, retrieve the value for a key (if any) only if it conforms to a specific type.
Given a Scala map, with string keys and any kind of value, retrieve the value for a key (if any) only if it conforms to a specific type. Otherwise, return None.
the type to which the result must conform
the map
the key
the StringTemplateGroup in which the template resides
Set the isRegion
flag.
Set the isRegion
flag.
true
to set the flag, false
to clear it.
Set attribute named attrName
to one or many different values.
Set attribute named attrName
to one or many different values.
Internally, a single value is stored as is, and multiple values are
coalesced into a java.util.List
of type T
. To pass a Scala list
(or sequence) in, use this syntax:
template.makeAttribute("name", List(1, 2, 3): _*)
Unlike the setAttribute()
methods, this method automatically
converts the Scala object values to Java Beans, using the
ClassUtil] library's
ScalaObjectToBean
capability. Thus, using makeBeanAttribute()
allows you to pass Scala objects to StringTemplate, without using
the @BeanProperty' annotation to generate Java Bean getters for
StringTemplate to use. This is especially useful if you want to pass
instances of case classes or instances of final, non-bean classes to
StringTemplate.
This capability requires the presence of the ASM byte code generation library at runtime.
the type of the values to assign to the attribute
the name of the attribute
the values to associate with the attribute
this object, for convenience
Set attribute named attrName
to one or many different values.
Set attribute named attrName
to one or many different values.
Internally, a single value is stored as is, and multiple values are
coalesced into a java.util.List
of type T
. To pass a Scala list
(or sequence) in, use this syntax:
template.makeAttribute("name", List(1, 2, 3): _*)
Unlike the setAttribute()
methods, this method automatically
converts the Scala object values to Java Beans, using the
ClassUtil] library's
ScalaObjectToBean
capability. Thus, using makeBeanAttribute()
allows you to pass Scala objects to StringTemplate, without using
the @BeanProperty' annotation to generate Java Bean getters for
StringTemplate to use. This is especially useful if you want to pass
instances of case classes or instances of final, non-bean classes to
StringTemplate.
This capability requires the presence of the ASM byte code generation library at runtime.
the type of the values to assign to the attribute
the name of the attribute
one or more values to associate with the attribute
this object, for convenience
Maps a Scala map of attributes into a Java map of attributes.
Maps a Scala map of attributes into a Java map of attributes. The
Scala map is converted to a java.util.HashMap
. The keys are
assumed to be strings. The values are mapped as follows:
- A Scala Seq
(which includes lists and array buffers) is mapped to
a java.util.List
, so it's treated as a multivalued attribute by the
underlying StringTemplate library.
- A Scala iterator is also mapped to a java.util.List
.
- Anything else is treated as a single-valued object.
To enhance how these mappings are done, override this method.
The Scala map to convert.
the Java map
Get the template's name, if any.
Get the template's name, if any.
the name, or None
Set or change the template's name.
Set or change the template's name.
the new name. Must not be null.
Returns a copy of the underlying (wrapped) StringTemplate API object.
Returns a copy of the underlying (wrapped) StringTemplate API object.
Unlike StringTemplate.getInstanceOf()
, this method copies the
current set of attributes and the enclosing instance reference (if any)
to the returned copy.
NOTE: If you decide to use the native, underlying template, be aware
that nativeTemplate
returns a copy, so do all your Scala work first:
StringTemplate
(i.e., this class).StringTemplate
object.nativeTemplate
, to get copy of the underlying (real) template.a copy of the underlying StringTemplate object.
Register an attribute renderer for a specific type.
Register an attribute renderer for a specific type. The
attribute renderer object must implement the AttributeRenderer
trait for the specific type.
the type of attribute that the renderer can render
the attribute renderer to use for values of type T
this object, for convenience
Reset the template, clearing all its associated values.
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 StringTemplate 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"
the attribute's name (i.e., the outermost name)
the map of attribute fields
this object, for convenience
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, first
and
last
. Those fields can be interpolated within a template via
$item.first$
and $item.last$
.
val st = new StringTemplate( ... ) st.setAggregate("name.{first,last}", "Moe", "Howard")
Setting the same aggregate multiple times results in a list of aggregates:
val st = new StringTemplate( ... ) 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 the map version of setAggregate()
for that.
See http://www.antlr.org/wiki/display/ST/Expressions#Expressions-Automaticaggregatecreation for more information.
the spec, as described above
one or more values. The values are treated as discrete; that is, lists are not supported.
this object, for convenience
Set attribute named attrName
to many different values.
Set attribute named attrName
to many different values. Internally,
the values are coalesced into a java.util.List
of type T
.
Note that StringTemplate expects the values to be Java Beans, if
they contain fields. This method does not automatically convert the
values, since it has no way of knowing whether or not that's what
the caller wants. If you want to have Scalasti automatically convert
your values from Scala objects to Java Beans (which useful for case
classes or other classes where using @BeanProperty
isn't possible),
use one of the makeBeanAttribute()
methods.
the type of the values to assign to the attribute
the name of the attribute
the values to associate with the attribute
this object, for convenience
Set attribute named attrName
to many different values.
Set attribute named attrName
to many different values. Internally,
the values are coalesced into a java.util.List
of type T
.
Note that StringTemplate expects the values to be Java Beans, if
they contain fields. This method does not automatically convert the
values, since it has no way of knowing whether or not that's what
the caller wants. If you want to have Scalasti automatically convert
your values from Scala objects to Java Beans (which useful for case
classes or other classes where using @BeanProperty
isn't possible),
use one of the makeBeanAttribute()
methods.
the type of the values to assign to the attribute
the name of the attribute
the values to associate with the attribute
this object, for convenience
Set attribute named attrName
to one or many different values.
Set attribute named attrName
to one or many different values.
Internally, a single value is stored as is, and multiple values are
coalesced into a java.util.List
of type T
. To pass a Scala list
(or sequence) in, use this syntax:
template.setAttribute("name", List(1, 2, 3): _*)
Note that StringTemplate expects the values to be Java Beans, if
they contain fields. This method does not automatically convert the
values, since it has no way of knowing whether or not that's what
the caller wants. If you want to have Scalasti automatically convert
your values from Scala objects to Java Beans (which useful for case
classes or other classes where using @BeanProperty
isn't possible),
use one of the makeBeanAttribute()
methods.
the type of the values to assign to the attribute
the name of the attribute
one or more values to associate with the attribute
this object, for convenience
Replace the current set of attributes with the attributes in the specified map.
Replace the current set of attributes with the attributes in the specified map. Multivalued attributes are supported via Scala sequences and iterators, as described in the class documentation.
Note that StringTemplate expects the values to be Java Beans, if
they contain fields. This method does not automatically convert the
values, since it has no way of knowing whether or not that's what
the caller wants. If you want to have Scalasti automatically convert
your values from Scala objects to Java Beans (which useful for case
classes or other classes where using @BeanProperty
isn't possible),
use one of the makeBeanAttribute()
methods.
the map of new attributes
this object, for convenience
Render the template with the current attributes.
Render the template with the current attributes.
the rendered template.
Transform a value for use in a template.
Transform a value for use in a template.
the value
a Java object, suitable for use in a template
Unset the value of the named attribute.
Unset the value of the named attribute. Corresponds to the underlying
StringTemplate API's removeAttribute()
call.
the name of the attribute to unset
this object, for convenience
A Scala interface to a StringTemplate template. Note that this interface does not directly expose all the underlying StringTemplate methods. In particular, this Scala interface is geared primarily toward reading and rendering external templates, not toward generating templates in code.
Because of the way the StringTemplate API instantiates templates, this class cannot easily subclass the real StringTemplate class. So, it wraps the underlying string template object and stores it internally. You can retrieve the wrapped template object via the
nativeTemplate
method You are free to call methods directly ontemplate
, though they will use Java semantics, rather than Scala semantics.NOTE: If you decide to use the native, underlying template, be aware that
nativeTemplate
returns a copy, so do all your Scala work first:StringTemplate
(i.e., this class).StringTemplate
object.nativeTemplate
, to get copy of the underlying (real) template.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 StringTemplate library. A Scala iterator is also mapped to ajava.util.List
. Anything else is treated as a single-valued object.