org.clapper.scalasti

StringTemplate

class StringTemplate extends AnyRef

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 on template, 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:

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 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.

Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. StringTemplate
  2. AnyRef
  3. Any
  1. Hide All
  2. Show all
Learn more about member selection
Visibility
  1. Public
  2. All

Instance Constructors

  1. new StringTemplate(template: Seq[Char])

    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.

    template

    the template, as a sequence of characters

  2. new StringTemplate(template: String)

    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.

    template

    the contents of the template

  3. new StringTemplate(group: Option[StringTemplateGroup], template: ScalastiStringTemplate)

    group

    the StringTemplateGroup in which the template resides

    template

    the real, underlying String Template

Value Members

  1. final def !=(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  2. final def !=(arg0: Any): Boolean

    Definition Classes
    Any
  3. final def ##(): Int

    Definition Classes
    AnyRef → Any
  4. final def ==(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  5. final def ==(arg0: Any): Boolean

    Definition Classes
    Any
  6. def ID: Int

    Get the template's internally assigned ID.

    Get the template's internally assigned ID.

    returns

    the ID

  7. final def asInstanceOf[T0]: T0

    Definition Classes
    Any
  8. def attributes: Map[String, Any]

    Get a map containing all attribute names and values.

    Get a map containing all attribute names and values.

    returns

    a read-only map of attributes.

  9. def clone(): AnyRef

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws()
  10. final def eq(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  11. def equals(arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  12. def errorListener: antlr.stringtemplate.StringTemplateErrorListener

    Get the current error listener, which is notified when errors occur.

    Get the current error listener, which is notified when errors occur.

    returns

    the error listener

  13. def errorListener_=(listener: StringTemplateErrorListener): Unit

    Set the current error listener, which is notified when errors occur.

    Set the current error listener, which is notified when errors occur.

    listener

    the error listener

  14. def finalize(): Unit

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws()
  15. final def getClass(): Class[_]

    Definition Classes
    AnyRef → Any
  16. def getType[T](map: Map[String, Any], key: String)(implicit arg0: ClassTag[T]): Option[T]

    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.

    T

    the type to which the result must conform

    map

    the map

    key

    the key

    Attributes
    protected
  17. val group: Option[StringTemplateGroup]

    the StringTemplateGroup in which the template resides

  18. def hashCode(): Int

    Definition Classes
    AnyRef → Any
  19. final def isInstanceOf[T0]: Boolean

    Definition Classes
    Any
  20. def isRegion(flag: Boolean): Unit

    Set the isRegion flag.

    Set the isRegion flag.

    flag

    true to set the flag, false to clear it.

  21. def makeBeanAttribute[T](attrName: String, values: Iterator[T]): StringTemplate

    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.

    T

    the type of the values to assign to the attribute

    attrName

    the name of the attribute

    values

    the values to associate with the attribute

    returns

    this object, for convenience

  22. def makeBeanAttribute[T](attrName: String, values: T*): StringTemplate

    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.

    T

    the type of the values to assign to the attribute

    attrName

    the name of the attribute

    values

    one or more values to associate with the attribute

    returns

    this object, for convenience

  23. def mapToJavaMap(map: Map[String, Any]): Map[String, AnyRef]

    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.

    map

    The Scala map to convert.

    returns

    the Java map

    Attributes
    protected
  24. def name: Option[String]

    Get the template's name, if any.

    Get the template's name, if any.

    returns

    the name, or None

  25. def name_=(name: String): Unit

    Set or change the template's name.

    Set or change the template's name.

    name

    the new name. Must not be null.

  26. def nativeTemplate: antlr.stringtemplate.StringTemplate

    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:

    • Instantiate a Scalasti StringTemplate (i.e., this class).
    • Add values to the template via the Scalasti StringTemplate object.
    • Call nativeTemplate, to get copy of the underlying (real) template.
    • Add to the underlying template, using Java semantics.
    • Render the template with native template copy.
    returns

    a copy of the underlying StringTemplate object.

  27. final def ne(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  28. final def notify(): Unit

    Definition Classes
    AnyRef
  29. final def notifyAll(): Unit

    Definition Classes
    AnyRef
  30. def registerRenderer[T](renderer: AttributeRenderer[T])(implicit arg0: ClassTag[T]): StringTemplate

    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.

    T

    the type of attribute that the renderer can render

    renderer

    the attribute renderer to use for values of type T

    returns

    this object, for convenience

  31. def reset(): Unit

    Reset the template, clearing all its associated values.

  32. def setAggregate(attrName: String, valueMap: Map[String, Any]): StringTemplate

    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"
    attrName

    the attribute's name (i.e., the outermost name)

    valueMap

    the map of attribute fields

    returns

    this object, for convenience

  33. def setAggregate(aggrSpec: String, values: Any*): StringTemplate

    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.

    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

  34. def setAttribute[T](attrName: String, values: List[T]): StringTemplate

    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.

    T

    the type of the values to assign to the attribute

    attrName

    the name of the attribute

    values

    the values to associate with the attribute

    returns

    this object, for convenience

  35. def setAttribute[T](attrName: String, values: Iterator[T]): StringTemplate

    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.

    T

    the type of the values to assign to the attribute

    attrName

    the name of the attribute

    values

    the values to associate with the attribute

    returns

    this object, for convenience

  36. def setAttribute[T](attrName: String, values: T*): StringTemplate

    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.

    T

    the type of the values to assign to the attribute

    attrName

    the name of the attribute

    values

    one or more values to associate with the attribute

    returns

    this object, for convenience

  37. def setAttributes(newAttrs: Map[String, Any]): StringTemplate

    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.

    newAttrs

    the map of new attributes

    returns

    this object, for convenience

  38. final def synchronized[T0](arg0: ⇒ T0): T0

    Definition Classes
    AnyRef
  39. def toString(): String

    Render the template with the current attributes.

    Render the template with the current attributes.

    returns

    the rendered template.

    Definition Classes
    StringTemplate → AnyRef → Any
  40. def transform(v: Any): AnyRef

    Transform a value for use in a template.

    Transform a value for use in a template.

    v

    the value

    returns

    a Java object, suitable for use in a template

  41. def unsetAttribute(attrName: String): StringTemplate

    Unset the value of the named attribute.

    Unset the value of the named attribute. Corresponds to the underlying StringTemplate API's removeAttribute() call.

    attrName

    the name of the attribute to unset

    returns

    this object, for convenience

  42. final def wait(): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws()
  43. final def wait(arg0: Long, arg1: Int): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws()
  44. final def wait(arg0: Long): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws()

Inherited from AnyRef

Inherited from Any

Ungrouped