Top Description Inners Fields Constructors Methods
groovy.text

public Class XmlTemplateEngine

extends TemplateEngine
Class Inheritance
Imports
groovy.lang.Binding, .Closure, .GroovyRuntimeException, .GroovyShell, .Script, .Writable, groovy.util.IndentPrinter, .Node, groovy.xml.XmlNodePrinter, .XmlParser, groovy.namespace.QName, org.apache.groovy.io.StringBuilderWriter, org.codehaus.groovy.control.CompilationFailedException, org.codehaus.groovy.runtime.InvokerHelper, org.xml.sax.SAXException, javax.xml.parsers.ParserConfigurationException, java.io.IOException, .PrintWriter, .Reader, .Writer, java.lang.ref.WeakReference, java.util.HashMap, .Map, java.util.concurrent.atomic.AtomicInteger

Template engine for use in templating scenarios where both the template source and the expected output are intended to be XML.

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 &quot;blackdrag&quot; 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>

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
private static class
private static class

Field Summary

Modifier and TypeField and Description
private Closure<V>
private static AtomicInteger
public static final String
private final GroovyShell
private String
private final XmlParser

Constructor Summary

AccessConstructor and Description
public
public
XmlTemplateEngine(String indentation, boolean validating)

public
XmlTemplateEngine(XmlParser xmlParser, ClassLoader parentLoader)

public
XmlTemplateEngine(XmlParser xmlParser, GroovyShell groovyShell)

Method Summary

Modifier and TypeMethod and Description
public Template
createTemplate(Reader reader)

Implements abstract groovy.text.TemplateEngine.createTemplate.

Creates a template by reading content from the Reader.

public String
public void
setConfigurePrinter(Closure<V> configurePrinter)

Closure that can be used to configure the printer.

public void
setIndentation(String indentation)

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from groovy.text.TemplateEngine:
createTemplatecreateTemplatecreateTemplatecreateTemplatecreateTemplate

Field Detail

configurePrinterback to summary
private Closure<V> configurePrinter
counterback to summary
private static AtomicInteger counter
DEFAULT_INDENTATIONback to summary
public static final String DEFAULT_INDENTATION
groovyShellback to summary
private final GroovyShell groovyShell
indentationback to summary
private String indentation
xmlParserback to summary
private final XmlParser xmlParser

Constructor Detail

XmlTemplateEngineback to summary
public XmlTemplateEngine() throws SAXException, ParserConfigurationException
XmlTemplateEngineback to summary
public XmlTemplateEngine(String indentation, boolean validating) throws SAXException, ParserConfigurationException
XmlTemplateEngineback to summary
public XmlTemplateEngine(XmlParser xmlParser, ClassLoader parentLoader)
XmlTemplateEngineback to summary
public XmlTemplateEngine(XmlParser xmlParser, GroovyShell groovyShell)

Method Detail

createTemplateback to summary
public Template createTemplate(Reader reader) throws CompilationFailedException, ClassNotFoundException, IOException

Implements abstract groovy.text.TemplateEngine.createTemplate.

Doc from groovy.text.TemplateEngine.createTemplate.

Creates a template by reading content from the Reader.

getIndentationback to summary
public String getIndentation()
setConfigurePrinterback 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 })
setIndentationback to summary
public void setIndentation(String indentation)
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

groovy.text back to summary

private Class XmlTemplateEngine.GspPrinter

extends XmlNodePrinter
Class Inheritance

Field Summary

Inherited from groovy.xml.XmlNodePrinter:
out

Constructor Summary

AccessConstructor and Description
public
public

Method Summary

Modifier and TypeMethod and Description
private void
append(StringBuilder sb, char plainChar, String xmlString, boolean inGString)

private String
protected void
protected void
protected void
protected void
printName(Node node, XmlNodePrinter.NamespaceContext ctx, boolean begin, boolean preserve)

Overrides groovy.xml.XmlNodePrinter.printName.

protected void
protected boolean
Inherited from groovy.xml.XmlNodePrinter:
getNamegetQuoteisExpandEmptyElementsisNamespaceAwareisPreserveWhitespaceprintprintprintLineEndprintListprintNameAttributesprintNamespacesetExpandEmptyElementssetNamespaceAwaresetPreserveWhitespacesetQuote

Constructor Detail

GspPrinterback to summary
public GspPrinter(PrintWriter out, String indent)
GspPrinterback to summary
public GspPrinter(IndentPrinter out)

Method Detail

appendback to summary
private void append(StringBuilder sb, char plainChar, String xmlString, boolean inGString)
escapeSpecialCharsback to summary
private String escapeSpecialChars(String s)
printGroovyTagback to summary
protected void printGroovyTag(String tag, String text)
printLineBeginback to summary
protected void printLineBegin()

Overrides groovy.xml.XmlNodePrinter.printLineBegin.

printLineEndback to summary
protected void printLineEnd(String comment)

Overrides groovy.xml.XmlNodePrinter.printLineEnd.

printNameback to summary
protected void printName(Node node, XmlNodePrinter.NamespaceContext ctx, boolean begin, boolean preserve)

Overrides groovy.xml.XmlNodePrinter.printName.

Annotations
@Override
printSimpleItemback to summary
protected void printSimpleItem(Object value)

Overrides groovy.xml.XmlNodePrinter.printSimpleItem.

Annotations
@Override
printSpecialNodeback to summary
protected boolean printSpecialNode(Node node)

Overrides groovy.xml.XmlNodePrinter.printSpecialNode.

groovy.text back to summary

private Class XmlTemplateEngine.XmlTemplate

extends Object
implements Template
Class Inheritance
All Implemented Interfaces
groovy.text.Template

Field Summary

Modifier and TypeField and Description
private final Script

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public Writable
public Writable
make(Map<K, V> map)

Implements groovy.text.Template.make.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

scriptback to summary
private final Script script

Constructor Detail

XmlTemplateback to summary
public XmlTemplate(Script script)

Method Detail

makeback to summary
public Writable make()

Implements groovy.text.Template.make.

makeback to summary
public Writable make(Map<K, V> map)

Implements groovy.text.Template.make.

groovy.text back to summary

private Class XmlTemplateEngine.XmlWritable

extends Object
implements Writable
Class Inheritance
All Implemented Interfaces
groovy.lang.Writable

Field Summary

Modifier and TypeField and Description
private final Binding
private WeakReference<T>
private final Script

Constructor Summary

AccessConstructor and Description
public
XmlWritable(Script script, Binding binding)

Method Summary

Modifier and TypeMethod and Description
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

public Writer
writeTo(Writer
the Writer to which this Writable should output its data.
out
)

Implements groovy.lang.Writable.writeTo.

Writes this object to the given writer.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

bindingback to summary
private final Binding binding
resultback to summary
private WeakReference<T> result
scriptback to summary
private final Script script

Constructor Detail

XmlWritableback to summary
public XmlWritable(Script script, Binding binding)

Method Detail

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

writeToback to summary
public Writer writeTo(Writer out)

Implements groovy.lang.Writable.writeTo.

Doc from groovy.lang.Writable.writeTo.

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

Parameters
out:Writer

the Writer to which this Writable should output its data.

Returns:Writer

the Writer that was passed