Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Velocity template escape XML from Velocity Context

Tags:

java

xml

velocity

I have a velocity template, it represents an XML file. I am populating the text between tags using data passed to a VelocityContext object. This is then accessed inside the template.

Here is an example lets call it myTemplate.vm:

<text>$myDocument.text</text>

and this is how I am passing that data to the velocity file and building it to output as a String:

private String buildXml(Document pIncomingXml)
  {
    // setup environment
    Properties lProperties = new Properties();
    lProperties.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");

    VelocityContext lVelocityContext = new VelocityContext();
    lVelocityContext.put("myDocument" , pIncomingXml.getRootElement());

    StringWriter lOutput = new StringWriter();

    try
    {
      Velocity.init(lProperties);
      Velocity.mergeTemplate("myTemplate.vm", "ISO-8859-1", lVelocityContext, lOutput);
    }
    catch (Exception lEx)
    {
      throw new RuntimeException("Problems running velocity template, underlying error is " + lEx.getMessage(), lEx);
    }
    return lOutput.toString();
}

The problem is that when I access myDocument.text inside the template file it outputs text which is not escaped for XML.

I found a work around for this by also adding a VelocityContext for an escape tool like so:

lVelocityContext.put("esc", new EscapeTool());

then wrapping my tag in the template using it:

<text>$esc.xml($myDocument.text)</text>

The reality is I have a very large template and for me to manually wrap each element in an $esc.xml context will be time consuming. Is there a way that I can tell velocity to escape for XML on access to myDocument without editing the template file at all?

like image 340
Suipaste Avatar asked Sep 16 '16 15:09

Suipaste


People also ask

How do you escape velocity template?

Velocity allows for explicit escaping of References and Directives using the \ (backslash) character. If the character following the \ would start a new directive or reference, then this character is output verbatim. This can lead to some unexpected behaviour, especially with directives.

Is Velocity template deprecated?

Velocity templates were deprecated in Liferay Portal 7.0 and are now removed in favor of FreeMarker templates in Liferay DXP 7.2.

What is Vlocity template?

Velocity is a server-side template language used by Confluence to render page content. Velocity allows Java objects to be called alongside standard HTML. If you are are writing a user macro or developing a plugin you may need to modify Velocity content.

What is velocity .VM file?

Velocity is a Java-based templating engine. It's an open source web framework designed to be used as a view component in the MVC architecture, and it provides an alternative to some existing technologies such as JSP. Velocity can be used to generate XML files, SQL, PostScript and most other text-based formats.


1 Answers

Yes, it's possible.

What you need to do is to use the EscapeXMLReference, which implements the reference insertion handler interface:

lProperties.put("eventhandler.referenceinsertion.class",
                 "org.apache.velocity.app.event.implement.EscapeXmlReference");
like image 186
Claude Brisson Avatar answered Sep 18 '22 19:09

Claude Brisson