groovy.text

## Class XmlTemplateEngine

- java.lang.Object
- - groovy.text.TemplateEngine
  - - groovy.text.XmlTemplateEngine

-     public class XmlTemplateEngine
      extends TemplateEngine

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

  Author:
    Christian Stein, Paul King

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static String` | `DEFAULT_INDENTATION`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `XmlTemplateEngine()`
    `XmlTemplateEngine(String indentation, boolean validating)`
    `XmlTemplateEngine(XmlParser xmlParser, ClassLoader parentLoader)`
    `XmlTemplateEngine(XmlParser xmlParser, GroovyShell groovyShell)`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `Template` | `createTemplate(Reader reader)`
    `String` | `getIndentation()`
    `void` | `setIndentation(String indentation)`
    `String` | `toString()`

    - ### Methods inherited from class groovy.text.TemplateEngine

      `createTemplate, createTemplate, createTemplate`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- - ### Field Detail

    - #### DEFAULT_INDENTATION

          public static final String DEFAULT_INDENTATION

      See Also:
        Constant Field Values

  - ### Constructor Detail

    - #### XmlTemplateEngine

          public XmlTemplateEngine()
                            throws SAXException,
                                   ParserConfigurationException

      Throws:
        `SAXException`
        `ParserConfigurationException`

    - #### XmlTemplateEngine

          public XmlTemplateEngine(String indentation,
                           boolean validating)
                            throws SAXException,
                                   ParserConfigurationException

      Throws:
        `SAXException`
        `ParserConfigurationException`

    - #### XmlTemplateEngine

          public XmlTemplateEngine(XmlParser xmlParser,
                           ClassLoader parentLoader)

    - #### XmlTemplateEngine

          public XmlTemplateEngine(XmlParser xmlParser,
                           GroovyShell groovyShell)

  - ### Method Detail

    - #### createTemplate

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

      Specified by:
        `createTemplate` in class `TemplateEngine`
      Throws:
        `CompilationFailedException`
        `ClassNotFoundException`
        `IOException`

    - #### getIndentation

          public String getIndentation()

    - #### setIndentation

          public void setIndentation(String indentation)

    - #### toString

          public String toString()

      Overrides:
        `toString` in class `Object`