public void Init(IDictionary<string, string> properties) { //初始化NVelocity引擎 var nv_properties = new Commons.Collections.ExtendedProperties(); var prefix = this.GetType().FullName + "."; foreach (String key in properties.Keys.Where(t => t.StartsWith(prefix))) nv_properties.SetProperty(key.Substring(prefix.Length), properties[key]); engine = new VelocityEngine(nv_properties); engine.Init(); }
static HtmlGenerator() { velocity = new VelocityEngine(); var p = new Commons.Collections.ExtendedProperties(); #if DEBUG string temp = Application.ExecutablePath; string[] split = Application.ExecutablePath.Split('\\'); string pathRoot = string.Join("\\", split, 0, split.Length - 3); //cssFile = string.Format(@"file:///{0}\html\best.css", pathRoot); //scriptFile = string.Format(@"file:///{0}\html\scripts.js", pathRoot); p.AddProperty("file.resource.loader.path", new System.Collections.ArrayList(new string[] { pathRoot })); #endif velocity.Init(p); baseContext = new VelocityContext(); baseContext.Put("script", "scripts.js"); baseContext.Put("css", "best.css"); }
public void Test_ExtendedProperties() { FileInfo file = new FileInfo("test1.properties"); StreamWriter sw = file.CreateText(); sw.WriteLine("# lines starting with # are comments. Blank lines are ignored"); sw.WriteLine(string.Empty); sw.WriteLine("# This is the simplest property"); sw.WriteLine("prefix.key = value"); sw.WriteLine(string.Empty); sw.WriteLine("# A long property may be separated on multiple lines"); sw.WriteLine("prefix.longvalue = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \\"); sw.WriteLine(" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); sw.WriteLine(string.Empty); sw.WriteLine("# This is a property with many tokens"); sw.WriteLine("prefix.tokens_on_a_line = first token, second token"); sw.WriteLine(string.Empty); sw.WriteLine("# This sequence generates exactly the same result"); sw.WriteLine("prefix.tokens_on_multiple_lines = first token"); sw.WriteLine("prefix.tokens_on_multiple_lines = second token"); sw.WriteLine(string.Empty); sw.WriteLine("# commas may be escaped in tokens"); sw.WriteLine("prefix.commas.excaped = Hi\\, what'up?"); sw.Flush(); sw.Close(); StreamReader sr = file.OpenText(); String s = sr.ReadToEnd(); sr.Close(); ExtendedProperties props = new ExtendedProperties(file.FullName); VerifyProperties(props, "prefix."); StringWriter writer = new StringWriter(); props.Save(writer, "header"); // make sure that combine does not change types ExtendedProperties p = new ExtendedProperties(); p.Combine(props); VerifyProperties(p, "prefix."); // make sure that subset does not change property types ExtendedProperties p2 = p.Subset("prefix"); VerifyProperties(p2, string.Empty); }
public static string FormatText(string templateText, VelocityContext context) { if (!_isVelocityInitialized) { var properties = new Commons.Collections.ExtendedProperties(); properties.AddProperty("resource.loader", "custom"); properties.AddProperty("custom.resource.loader.class", "ASC.Common.Utils.TextLoader; ASC.Common"); properties.AddProperty("input.encoding", Encoding.UTF8.WebName); properties.AddProperty("output.encoding", Encoding.UTF8.WebName); Velocity.Init(properties); _isVelocityInitialized = true; } using (var writer = new StringWriter()) { var template = Patterns.Get(templateText.GetHashCode().ToString(), () => Velocity.GetTemplate(templateText)); template.Merge(context, writer); return writer.GetStringBuilder().ToString(); } }
protected void SetUp() { ve = new VelocityEngine(); ExtendedProperties ep = new ExtendedProperties(); ep.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, TemplateTest.FILE_RESOURCE_LOADER_PATH); ep.SetProperty(RuntimeConstants.RUNTIME_LOG_ERROR_STACKTRACE, "true"); ep.SetProperty(RuntimeConstants.RUNTIME_LOG_WARN_STACKTRACE, "true"); ep.SetProperty(RuntimeConstants.RUNTIME_LOG_INFO_STACKTRACE, "true"); ep.SetProperty("userdirective", "NVelocity.Runtime.Directive.Component;NVelocity,NVelocity.Runtime.Directive.BlockComponent;NVelocity"); ve.Init(ep); testProperties = new ExtendedProperties(); testProperties.Load(new FileStream(TemplateTest.TEST_CASE_PROPERTIES, FileMode.Open, FileAccess.Read)); }
/// <summary> /// Creates a new instance. /// </summary> public TemplateTestCase() { try { velocityEngine = new VelocityEngine(); ExtendedProperties extendedProperties = new ExtendedProperties(); extendedProperties.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, TemplateTest.FILE_RESOURCE_LOADER_PATH); extendedProperties.SetProperty(RuntimeConstants.RUNTIME_LOG_ERROR_STACKTRACE, "true"); extendedProperties.SetProperty(RuntimeConstants.RUNTIME_LOG_WARN_STACKTRACE, "true"); extendedProperties.SetProperty(RuntimeConstants.RUNTIME_LOG_INFO_STACKTRACE, "true"); velocityEngine.Init(extendedProperties); testProperties = new ExtendedProperties(); testProperties.Load(new FileStream(TemplateTest.TEST_CASE_PROPERTIES, FileMode.Open, FileAccess.Read)); } catch(Exception) { throw new Exception("Cannot setup TemplateTestSuite!"); } }
public static void RenderTemplateToWriter(string templateFile, TextWriter writer, NVelocity.VelocityContext vc) { NVelocity.App.VelocityEngine engine = null; IPersonalSettings provider = PersonalSettings.Provider; if (provider == null) { Commons.Collections.ExtendedProperties props = new Commons.Collections.ExtendedProperties(); props.SetProperty(NVelocity.Runtime.RuntimeConstants.RESOURCE_LOADER, "file"); props.SetProperty(NVelocity.Runtime.RuntimeConstants.FILE_RESOURCE_LOADER_PATH, _defaultPath); props.SetProperty(NVelocity.Runtime.RuntimeConstants.FILE_RESOURCE_LOADER_CACHE, "true"); engine = new NVelocity.App.VelocityEngine(props); } else engine = (NVelocity.App.VelocityEngine)PersonalSettings.Provider.ViewEngine; NVelocity.Template template = engine.GetTemplate(templateFile); template.Merge(vc, writer); }
public void Test_Example1() { String templateFile = "example1.vm"; try { /* * setup */ VelocityEngine velocityEngine = new VelocityEngine(); ExtendedProperties extendedProperties = new ExtendedProperties(); extendedProperties.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, TemplateTest.FILE_RESOURCE_LOADER_PATH); velocityEngine.Init(extendedProperties); /* * Make a context object and populate with the data. This * is where the Velocity engine gets the data to resolve the * references (ex. $list) in the template */ VelocityContext context = new VelocityContext(); context.Put("list", GetNames()); ExtendedProperties props = new ExtendedProperties(); props.Add("runtime.log", "nvelocity.log"); context.Put("props", props); /* * get the Template object. This is the parsed version of your * template input file. Note that getTemplate() can throw * ResourceNotFoundException : if it doesn't find the template * ParseErrorException : if there is something wrong with the VTL * Exception : if something else goes wrong (this is generally * indicative of as serious problem...) */ Template template = null; try { template = velocityEngine.GetTemplate(templateFile); } catch(ResourceNotFoundException resourceNotFoundException) { Console.Out.WriteLine("Example : error : cannot find template {0} : \n{1}", templateFile, resourceNotFoundException.Message); Assert.Fail(); } catch(ParseErrorException parseErrorException) { Console.Out.WriteLine("Example : Syntax error in template {0} : \n{1}", templateFile, parseErrorException); Assert.Fail(); } /* * Now have the template engine process your template using the * data placed into the context. Think of it as a 'merge' * of the template and the data to produce the output stream. */ // using Console.Out will send it to the screen TextWriter writer = new StringWriter(); if (template != null) template.Merge(context, writer); /* * flush and cleanup */ writer.Flush(); writer.Close(); } catch(Exception ex) { Assert.Fail(ex.Message); } }
public void TestJuxtaposePage() { SiteInfo site = new SiteInfo(); site.Copyright = "©2014 - 2015"; site.Description = ""; site.Host = "localhost"; site.KeyWords = ""; site.Logo = ""; site.Name = "xxx"; site.SiteDirectory = ""; site.Theme = "Blue"; site.ThemeDirectory = "theme"; site.Title = "jntemplate测试页"; site.Url = string.Concat("http://localhost"); if (!string.IsNullOrEmpty(site.SiteDirectory) && site.SiteDirectory != "/") { site.Url += "/" + site.SiteDirectory; } site.ThemeUrl = string.Concat(site.Url, "/", site.ThemeDirectory, "/", site.Theme); string basePath = new System.IO.DirectoryInfo(System.Environment.CurrentDirectory).Parent.Parent.FullName; string path = basePath + "\\templets\\nv"; NVelocity.Context.IContext ctx = new NVelocity.VelocityContext(); ctx.Put("func", new TemplateMethod()); ctx.Put("Site", site); NVelocity.App.VelocityEngine velocity = new NVelocity.App.VelocityEngine(); Commons.Collections.ExtendedProperties props = new Commons.Collections.ExtendedProperties(); props.AddProperty(NVelocity.Runtime.RuntimeConstants.RESOURCE_LOADER, "file"); props.AddProperty(NVelocity.Runtime.RuntimeConstants.FILE_RESOURCE_LOADER_PATH, path); props.AddProperty(NVelocity.Runtime.RuntimeConstants.INPUT_ENCODING, "utf-8"); props.AddProperty(NVelocity.Runtime.RuntimeConstants.OUTPUT_ENCODING, "utf-8"); velocity.Init(props); NVelocity.Template t = velocity.GetTemplate("questionlist.html"); string result; using (System.IO.StringWriter write = new StringWriter()) { t.Merge(ctx, write); result = write.ToString(); } //可直接查看项目录下的html/nv.html 文件效果 System.IO.File.WriteAllText(basePath + "\\html\\nv.html", result); }
private void VerifyProperties(ExtendedProperties props, String prefix) { Assert.IsTrue(props.Count == 5, "expected to have 5 properties, had " + props.Count.ToString()); Assert.IsTrue(props.GetString(prefix + "key").Equals("value"), "key was not correct: " + props.GetString(prefix + "key")); // make sure the comma escaping is working correctly Assert.IsTrue(props.GetString(prefix + "commas.excaped").Equals("Hi, what'up?"), "commas.excaped was not correct: " + props.GetString(prefix + "commas.excaped")); // make sure that multiple tokens on a single line are parsed correctly Object o = props.GetProperty(prefix + "tokens_on_a_line"); Assert.IsTrue((o is ArrayList), prefix + "tokens_on_a_line was expected to be an ArrayList"); Assert.IsTrue(((ArrayList) o).Count == 2, prefix + "tokens_on_a_line was expected to be an ArrayList with 2 elements"); // make sure that tokens specified on multiple lines get put together correctly o = props.GetProperty(prefix + "tokens_on_multiple_lines"); Assert.IsTrue((o is ArrayList), prefix + "tokens_on_multiple_lines was expected to be an ArrayList"); Assert.IsTrue(((ArrayList) o).Count == 2, prefix + "tokens_on_multiple_lines was expected to be an ArrayList with 2 elements"); }