问题描述
我想尝试让NVelocity自动HTML EN code在我的单轨的应用程序的某些字符串。
我看遍了NVelocity源$ C $ C,发现 EventCartridge
,这似乎是,你可以插件更改各种行为的类。
在特定该类有一个 ReferenceInsert
方法,这似乎做的正是我想要的。它基本上被调用之前的基准(如:$ foobar的)的价值得到输出,并且允许你修改的结果。
我不能工作了我是怎么配置NVelocity /单轨电车NVelocity视图引擎使用我的执行?
的建议velocity.properties可以包含一个条目添加特定的事件处理程序是这样的,但我找不到在NVelocity源$ C $ C,看起来这个配置的任何地方nofollow的>速度文档。
任何帮助非常AP preciated!
编辑:一个简单的测试,显示这方面的工作(!概念证明这样不生产code)
私人VelocityEngine _velocityEngine;
私人VelocityContext _velocityContext;
[建立]
公共无效设置()
{
_velocityEngine =新VelocityEngine();
_velocityEngine.Init();
//创建的背景下...
_velocityContext =新VelocityContext();
//附加一个新的事件盒
_velocityContext.AttachEventCartridge(新EventCartridge());
//添加我们的自定义处理程序的ReferenceInsertion事件
_velocityContext.EventCartridge.ReferenceInsertion + = EventCartridge_ReferenceInsertion;
}
[测试]
公共无效恩codeReference()
{
_velocityContext.Put(thisShouldBeEn codeD,< P>这\\应该是恩codeD'< / P>中);
VAR作家=新的StringWriter();
VAR的结果= _velocityEngine.Evaluate(_velocityContext,作家,HtmlEncodingEventCartridgeTestFixture.En codeReference,@< P>这,不应该,将连接codeD< / P> $ thisShouldBeEn codeD);
Assert.IsTrue(结果是,评估返回失败);
Assert.AreEqual(@< P>这,不应该,将连接codeD< / P>&放大器; LT; P&安培; gt;这与安培; QUOT; QUOT;应与功放是和放大器; #39; EN coded和放大器;#39;&放大器; LT; / P&放大器; GT;,writer.ToString());
}
私有静态无效EventCartridge_ReferenceInsertion(对象发件人,ReferenceInsertionEventArgs E)
{
VAR originalString = e.OriginalValue为字符串;
如果(originalString == NULL)回报;
e.NewValue = HtmlEn code(originalString);
}
私人静态字符串HtmlEn code(字符串值)
{
返回值
.Replace(与&,&放大器;放大器;)
.Replace(&所述;,&安培;其中;)
.Replace(>中,与& gt;中)
.Replace(\,与& QUOT;)
.Replace(',与'); //&功放;者;在IE浏览器无法正常工作
}
尽量将新EventCartridge到VelocityContext。请参阅这些测试作为参考。
现在,你已经证实,这种方法的工作原理,从 Castle.MonoRail.Framework.Views.NVelocity.NVelocityEngine
继承,覆盖 BeforeMerge
,并设置 EventCartridge
和事件出现。然后配置单轨使用此自定义视图引擎。
I wanted to try getting NVelocity to automatically HTML encode certain strings in my MonoRail app.
I looked through the NVelocity source code and found EventCartridge
, which seems to be a class which you can plugin to change various behaviours.
In particular this class has a ReferenceInsert
method which would seem to do exactly what I want. It basically gets called just before the value of a reference (e.g. $foobar) gets output, and allows you to modify the results.
What I can't work out is how I configure NVelocity/the MonoRail NVelocity view engine to use my implementation?
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.
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. 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. Then configure MonoRail to use this custom view engine.
这篇关于自动HTML编码NVelocity输出(EventCartridge&安培; ReferenceInsert)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!