Class XmlTemplateEngine
public class XmlTemplateEngine extends TemplateEngine
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
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 SummaryFields Modifier and Type Field Description static java.lang.StringDEFAULT_INDENTATION
- 
Constructor SummaryConstructors Constructor Description XmlTemplateEngine()XmlTemplateEngine(XmlParser xmlParser, GroovyShell groovyShell)XmlTemplateEngine(XmlParser xmlParser, java.lang.ClassLoader parentLoader)XmlTemplateEngine(java.lang.String indentation, boolean validating)
- 
Method SummaryModifier and Type Method Description TemplatecreateTemplate(java.io.Reader reader)Creates a template by reading content from the Reader.java.lang.StringgetIndentation()voidsetConfigurePrinter(Closure configurePrinter)Closure that can be used to configure the printer.voidsetIndentation(java.lang.String indentation)java.lang.StringtoString()Methods inherited from class groovy.text.TemplateEnginecreateTemplate, createTemplate, createTemplate, createTemplate, createTemplateMethods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
- 
Field Details- 
DEFAULT_INDENTATIONpublic static final java.lang.String DEFAULT_INDENTATION- See Also:
- Constant Field Values
 
 
- 
- 
Constructor Details- 
XmlTemplateEnginepublic XmlTemplateEngine() throws org.xml.sax.SAXException, javax.xml.parsers.ParserConfigurationException- Throws:
- org.xml.sax.SAXException
- javax.xml.parsers.ParserConfigurationException
 
- 
XmlTemplateEnginepublic XmlTemplateEngine(java.lang.String indentation, boolean validating) throws org.xml.sax.SAXException, javax.xml.parsers.ParserConfigurationException- Throws:
- org.xml.sax.SAXException
- javax.xml.parsers.ParserConfigurationException
 
- 
XmlTemplateEngine
- 
XmlTemplateEngine
 
- 
- 
Method Details- 
setConfigurePrinterClosure that can be used to configure the printer. The printer is passed as a parameter to the closure.new XmlTemplateEngine(configurePrinter: { it.preserveWhitespace = true })
- 
createTemplatepublic Template createTemplate(java.io.Reader reader) throws CompilationFailedException, java.lang.ClassNotFoundException, java.io.IOExceptionDescription copied from class:TemplateEngineCreates a template by reading content from the Reader.- Specified by:
- createTemplatein class- TemplateEngine
- Throws:
- CompilationFailedException
- java.lang.ClassNotFoundException
- java.io.IOException
 
- 
getIndentationpublic java.lang.String getIndentation()
- 
setIndentationpublic void setIndentation(java.lang.String indentation)
- 
toStringpublic java.lang.String toString()- Overrides:
- toStringin class- java.lang.Object
 
 
-