[英]Automatically HTML Encoding NVelocity Output (EventCartridge & ReferenceInsert)
I wanted to try getting NVelocity to automatically HTML encode certain strings in my MonoRail app. 我想尝试让NVelocity自动对我的MonoRail应用程序中的某些字符串进行HTML编码。
I looked through the NVelocity source code and found EventCartridge
, which seems to be a class which you can plugin to change various behaviours. 我查看了NVelocity源代码并找到了
EventCartridge
,它似乎是一个可以插入来改变各种行为的类。
In particular this class has a ReferenceInsert
method which would seem to do exactly what I want. 特别是这个类有一个
ReferenceInsert
方法,它似乎完全符合我的要求。 It basically gets called just before the value of a reference (eg $foobar) gets output, and allows you to modify the results. 它基本上在引用值(例如$ foobar)输出之前被调用,并允许您修改结果。
What I can't work out is how I configure NVelocity/the MonoRail NVelocity view engine to use my implementation? 我无法解决的是如何配置NVelocity / MonoRail NVelocity视图引擎来使用我的实现?
The Velocity docs suggest that velocity.properties can contain an entries for adding specific event handlers like this, but I can't find anywhere in the NVelocity source code that looks for this configuration. Velocity文档建议velocity.properties可以包含用于添加这样的特定事件处理程序的条目,但我找不到查找此配置的NVelocity源代码中的任何位置。
Any help greatly appreciated! 任何帮助非常感谢!
Edit: A simple test that shows this working (proof of concept so not production code!) 编辑:一个简单的测试,显示这个工作(概念证明,所以不是生产代码!)
private VelocityEngine _velocityEngine;
private VelocityContext _velocityContext;
[SetUp]
public void Setup()
{
_velocityEngine = new VelocityEngine();
_velocityEngine.Init();
// creates the context...
_velocityContext = new VelocityContext();
// attach a new event cartridge
_velocityContext.AttachEventCartridge(new EventCartridge());
// add our custom handler to the ReferenceInsertion event
_velocityContext.EventCartridge.ReferenceInsertion += EventCartridge_ReferenceInsertion;
}
[Test]
public void EncodeReference()
{
_velocityContext.Put("thisShouldBeEncoded", "<p>This \"should\" be 'encoded'</p>");
var writer = new StringWriter();
var result = _velocityEngine.Evaluate(_velocityContext, writer, "HtmlEncodingEventCartridgeTestFixture.EncodeReference", @"<p>This ""shouldn't"" be encoded.</p> $thisShouldBeEncoded");
Assert.IsTrue(result, "Evaluation returned failure");
Assert.AreEqual(@"<p>This ""shouldn't"" be encoded.</p> <p>This "should" be 'encoded'</p>", writer.ToString());
}
private static void EventCartridge_ReferenceInsertion(object sender, ReferenceInsertionEventArgs e)
{
var originalString = e.OriginalValue as string;
if (originalString == null) return;
e.NewValue = HtmlEncode(originalString);
}
private static string HtmlEncode(string value)
{
return value
.Replace("&", "&")
.Replace("<", "<")
.Replace(">", ">")
.Replace("\"", """)
.Replace("'", "'"); // ' does not work in IE
}
Try attaching a new EventCartridge to the VelocityContext. 尝试将新的EventCartridge附加到VelocityContext。 See these tests as reference.
请参阅这些测试作为参考。
Now that you've confirmed that this approach works, inherit from Castle.MonoRail.Framework.Views.NVelocity.NVelocityEngine
, override BeforeMerge
and set the EventCartridge
and event there. 现在,你已经证实,这种方法的工作原理,继承
Castle.MonoRail.Framework.Views.NVelocity.NVelocityEngine
,覆盖BeforeMerge
并设置EventCartridge
和事件出现。 Then configure MonoRail to use this custom view engine. 然后配置MonoRail以使用此自定义视图引擎。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.