Templates may use the normal '${expression}' and '$variable' notations to insert an arbitrary expression into the template. In addition, support is also provided for special tags: <gsp:scriptlet> (for inserting code fragments) and <gsp:expression> (for code fragments which produce output).
Comments and processing instructions will be removed as part of processing and special XML characters such as <, >, " and ' will be escaped using the respective XML notation. The output will also be indented using standard XML pretty printing.
The xmlns namespace definition for gsp: tags will be removed
but other namespace definitions will be preserved (but may change to an
equivalent position within the XML tree).
Normally, the template source will be in a file but here is a simple example providing the XML template as a string:
def binding = [firstname:"Jochen", lastname:"Theodorou",
nickname:"blackdrag", salutation:"Dear"]
def engine = new groovy.text.XmlTemplateEngine()
def text = '''\
<?xml version="1.0" encoding="UTF-8"?>
<document xmlns:gsp='http://groovy.codehaus.org/2005/gsp' xmlns:foo='baz' type='letter'>
<gsp:scriptlet>def greeting = "${salutation}est"</gsp:scriptlet>
<gsp:expression>greeting</gsp:expression>
<foo:to>$firstname "$nickname" $lastname</foo:to>
How are you today?
</document>
'''
def template = engine.createTemplate(text).make(binding)
println template.toString()
This example will produce this output:
<document type='letter'> Dearest <foo:to xmlns:foo='baz'> Jochen "blackdrag" Theodorou </foo:to> How are you today? </document>The XML template engine can also be used as the engine for
groovy.servlet.TemplateServlet by placing the
following in your web.xml file (plus a corresponding servlet-mapping element):
<servlet>
<servlet-name>XmlTemplate</servlet-name>
<servlet-class>groovy.servlet.TemplateServlet</servlet-class>
<init-param>
<param-name>template.engine</param-name>
<param-value>groovy.text.XmlTemplateEngine</param-value>
</init-param>
</servlet>
| Modifier and Type | Class and Description |
|---|---|
| private static class | |
| private static class | |
| private static class |
| Modifier and Type | Field and Description |
|---|---|
| private Closure | |
| private static AtomicInteger | |
| public static final String | |
| private final GroovyShell | |
| private String | |
| private final XmlParser |
| Access | Constructor and Description |
|---|---|
| public | |
| public | |
| public | |
| public |
| Modifier and Type | Method and Description |
|---|---|
| public Template | createTemplate(Reader reader)
Implements abstract groovy. Creates a template by reading content from the Reader. |
| public String | |
| public void | |
| public void | |
| public String |
| configurePrinter | back to summary |
|---|---|
| private Closure<V> configurePrinter | |
| counter | back to summary |
|---|---|
| private static AtomicInteger counter | |
| DEFAULT_INDENTATION | back to summary |
|---|---|
| public static final String DEFAULT_INDENTATION | |
| groovyShell | back to summary |
|---|---|
| private final GroovyShell groovyShell | |
| indentation | back to summary |
|---|---|
| private String indentation | |
| xmlParser | back to summary |
|---|---|
| private final XmlParser xmlParser | |
| XmlTemplateEngine | back to summary |
|---|---|
| public XmlTemplateEngine() throws SAXException, ParserConfigurationException | |
| XmlTemplateEngine | back to summary |
|---|---|
| public XmlTemplateEngine(String indentation, boolean validating) throws SAXException, ParserConfigurationException | |
| XmlTemplateEngine | back to summary |
|---|---|
| public XmlTemplateEngine(XmlParser xmlParser, ClassLoader parentLoader) | |
| XmlTemplateEngine | back to summary |
|---|---|
| public XmlTemplateEngine(XmlParser xmlParser, GroovyShell groovyShell) | |
| createTemplate | back to summary |
|---|---|
| public Template createTemplate(Reader reader) throws CompilationFailedException, ClassNotFoundException, IOException Implements abstract groovy. Doc from groovy. Creates a template by reading content from the Reader. | |
| getIndentation | back to summary |
|---|---|
| public String getIndentation() | |
| setConfigurePrinter | back to summary |
|---|---|
| public void setConfigurePrinter(Closure<V> configurePrinter) Closure that can be used to configure the printer. The printer is passed as a parameter to the closure.
new XmlTemplateEngine(configurePrinter: { it.preserveWhitespace = true })
| |
| setIndentation | back to summary |
|---|---|
| public void setIndentation(String indentation) | |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object.
Satisfying this method's contract implies a non-
| |
| Access | Constructor and Description |
|---|---|
| public | |
| public |
| Modifier and Type | Method and Description |
|---|---|
| private void | |
| private String | |
| protected void | |
| protected void | |
| protected void | |
| protected void | printName(Node node, XmlNodePrinter.
Overrides groovy.
|
| protected void | |
| protected boolean |
| GspPrinter | back to summary |
|---|---|
| public GspPrinter(PrintWriter out, String indent) | |
| GspPrinter | back to summary |
|---|---|
| public GspPrinter(IndentPrinter out) | |
| append | back to summary |
|---|---|
| private void append(StringBuilder sb, char plainChar, String xmlString, boolean inGString) | |
| escapeSpecialChars | back to summary |
|---|---|
| private String escapeSpecialChars(String s) | |
| printGroovyTag | back to summary |
|---|---|
| protected void printGroovyTag(String tag, String text) | |
| printLineBegin | back to summary |
|---|---|
| protected void printLineBegin() Overrides groovy. | |
| printLineEnd | back to summary |
|---|---|
| protected void printLineEnd(String comment) Overrides groovy. | |
| printName | back to summary |
|---|---|
| protected void printName(Node node, XmlNodePrinter. Overrides groovy.
| |
| printSimpleItem | back to summary |
|---|---|
| protected void printSimpleItem(Object value) Overrides groovy.
| |
| printSpecialNode | back to summary |
|---|---|
| protected boolean printSpecialNode(Node node) Overrides groovy. | |
| Access | Constructor and Description |
|---|---|
| public |
| script | back to summary |
|---|---|
| private final Script script | |
| XmlTemplate | back to summary |
|---|---|
| public XmlTemplate(Script script) | |
| make | back to summary |
|---|---|
| public Writable make() Implements groovy. | |
| make | back to summary |
|---|---|
| public Writable make(Map<K, V> map) Implements groovy. | |
| Modifier and Type | Field and Description |
|---|---|
| private final Binding | |
| private WeakReference | |
| private final Script |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public String | |
| public Writer | writeTo(Writer
the Writer to which this Writable should output its data. out)Implements groovy. Writes this object to the given writer. |
| binding | back to summary |
|---|---|
| private final Binding binding | |
| result | back to summary |
|---|---|
| private WeakReference<T> result | |
| script | back to summary |
|---|---|
| private final Script script | |
| XmlWritable | back to summary |
|---|---|
| public XmlWritable(Script script, Binding binding) | |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object.
Satisfying this method's contract implies a non-
| |
| writeTo | back to summary |
|---|---|
| public Writer writeTo(Writer out) Implements groovy. Doc from groovy. Writes this object to the given writer. This is used to defer content creation until the point when it is streamed to the output destination. Oftentimes, content will be defined but not necessarily created (as is may be the case with a Closure definition.) In that case, the output is then 'deferred' to the point when it is serialized to the writer. This class may be used whenever an object should be responsible for creating its own textual representation, but creating the entire output as a single String would be inefficient (such as outputting a multi-gigabyte XML document.) | |