Class XmlTemplateEngine
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>
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringDefault indentation sequence used for pretty-printed XML output. -
Constructor Summary
ConstructorsConstructorDescriptionCreates an XML template engine using the default indentation and with XML validation disabled.XmlTemplateEngine(XmlParser xmlParser, GroovyShell groovyShell) Creates an XML template engine backed by the supplied parser and shell.XmlTemplateEngine(XmlParser xmlParser, ClassLoader parentLoader) Creates an XML template engine backed by the supplied parser and aGroovyShellbuilt from the parent loader.XmlTemplateEngine(String indentation, boolean validating) Creates an XML template engine with custom indentation and validation settings. -
Method Summary
Modifier and TypeMethodDescriptioncreateTemplate(Reader reader) Parses XML template source from the supplied reader and compiles it into a template.Returns the indentation string used for pretty-printed XML output.voidsetConfigurePrinter(Closure configurePrinter) Closure that can be used to configure the printer.voidsetIndentation(String indentation) Sets the indentation string used for pretty-printed XML output.toString()Returns a concise engine name for diagnostics.Methods inherited from class groovy.text.TemplateEngine
createTemplate, createTemplate, createTemplate, createTemplate, createTemplate
-
Field Details
-
DEFAULT_INDENTATION
Default indentation sequence used for pretty-printed XML output.- See Also:
-
-
Constructor Details
-
XmlTemplateEngine
Creates an XML template engine using the default indentation and with XML validation disabled.- Throws:
SAXException- if the underlying parser cannot be configuredParserConfigurationException- if the XML parser cannot be created
-
XmlTemplateEngine
public XmlTemplateEngine(String indentation, boolean validating) throws SAXException, ParserConfigurationException Creates an XML template engine with custom indentation and validation settings.- Parameters:
indentation- indentation string used when rendering nested XML nodesvalidating-trueto enable XML validation while parsing templates- Throws:
SAXException- if the underlying parser cannot be configuredParserConfigurationException- if the XML parser cannot be created
-
XmlTemplateEngine
Creates an XML template engine backed by the supplied parser and aGroovyShellbuilt from the parent loader.- Parameters:
xmlParser- parser used to read template XMLparentLoader- class loader used to compile generated template scripts
-
XmlTemplateEngine
Creates an XML template engine backed by the supplied parser and shell.- Parameters:
xmlParser- parser used to read template XMLgroovyShell- shell used to compile generated template scripts
-
-
Method Details
-
setConfigurePrinter
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 }) -
createTemplate
public Template createTemplate(Reader reader) throws CompilationFailedException, ClassNotFoundException, IOException Parses XML template source from the supplied reader and compiles it into a template.- Specified by:
createTemplatein classTemplateEngine- Parameters:
reader- XML template source- Returns:
- a compiled XML template
- Throws:
CompilationFailedException- if Groovy fails to compile the generated template scriptClassNotFoundException- if the generated template class cannot be instantiatedIOException- if reading the XML template fails
-
getIndentation
Returns the indentation string used for pretty-printed XML output.- Returns:
- the indentation sequence, never
null
-
setIndentation
Sets the indentation string used for pretty-printed XML output.- Parameters:
indentation- indentation sequence to use;nullrestoresDEFAULT_INDENTATION
-
toString
Returns a concise engine name for diagnostics.
-