views:

317

answers:

1

I am generating XML using Apache Velocity. What is the best (most straight-forward) way to XML-escape the output?

(I saw there is an escape tool, but could not figure out it's dev state. I also think that XML escaping is something that is very likely supported by Velocity directly.)

+2  A: 

Take a look at event handlers.

eventhandler.referenceinsertion.class = org.apache.velocity.app.event.implement.EscapeXmlReference

Escape tool is a production ready as well if you need to escape only selective references (final version of tools was released just recently but it was in beta stage before that for 2 years if not longer)

$esc.xml($var)

How to init velocity tools.

Include velocity-tools.xml into your project and enable required tools:

<tools> 
    <data type="number" key="TOOLS_VERSION" value="2.0"/>
    <data type="boolean" key="GENERIC_TOOLS_AVAILABLE" value="true"/>
    <toolbox scope="application">
        <tool class="org.apache.velocity.tools.generic.AlternatorTool"/>
        <tool class="org.apache.velocity.tools.generic.DisplayTool"/>
        <tool class="org.apache.velocity.tools.generic.MathTool"/>
        <tool class="org.apache.velocity.tools.generic.NumberTool"/>
        <tool class="org.apache.velocity.tools.generic.ComparisonDateTool"/>
        <tool class="org.apache.velocity.tools.generic.ClassTool"/>
        <tool class="org.apache.velocity.tools.generic.ConversionTool"/>
        <tool class="org.apache.velocity.tools.generic.EscapeTool"/>
        <tool class="org.apache.velocity.tools.generic.FieldTool"/>
        <tool class="org.apache.velocity.tools.generic.ListTool"/>
        <tool class="org.apache.velocity.tools.generic.ResourceTool"/>
        <tool class="org.apache.velocity.tools.generic.SortTool"/>
    </toolbox>
    <toolbox scope="request">
        <tool class="org.apache.velocity.tools.generic.LoopTool"/>
        <tool class="org.apache.velocity.tools.generic.ContextTool"/>
        <tool class="org.apache.velocity.tools.generic.LinkTool"/>
        <tool class="org.apache.velocity.tools.generic.RenderTool"/>
    </toolbox>
</tools>

Then velocity context creation procedure would look like:

ToolManager velocityToolManager = new ToolManager();
velocityToolManager.configure("velocity-tools.xml");
VelocityContext context = new VelocityContext(velocityToolManager.createContext());
serg
Thanks serg555, that makes my day!
Jan Algermissen
serg555,can you tell me, how I add Escape tool using property config (without an XML file)?
Jan Algermissen
Update: I tried the following:p.setProperty("tools.toolbox","request,session,application"); p.setProperty("tools.session.esc", "org.apache.velocity.tools.generic.EscapeTool"); p.setProperty("tools.request.esc", "org.apache.velocity.tools.generic.EscapeTool"); p.setProperty("tools.application.esc", "org.apache.velocity.tools.generic.EscapeTool");But that does not seem to work. What am I doing wrong?
Jan Algermissen
@Jan Algermissen : I added an example how to init Tools 2.0. And what do you mean by without xml? There should be xml file describing enabled tools (velocity tools already includes default xml file with all tools enabled into its jar, so if you don't want this xml in your project just load default one instead, should be in your classpath already)
serg
@serg555 - Thanks for helping out!
Jan Algermissen