public StringWriter CreateControlFile() { VelocityEngine velocity = new VelocityEngine(); ExtendedProperties props = new ExtendedProperties(); props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "assembly"); props.AddProperty("assembly.resource.loader.class", "NVelocity.Runtime.Resource.Loader.AssemblyResourceLoader, NVelocity"); props.AddProperty("assembly.resource.loader.assembly", GetType().Assembly.GetName().Name); velocity.Init(props); // todo how do i get template as stream. check hwb code. // Set up the required velocity data types and parameters Template controlTemplate = velocity.GetTemplate("CSIRO.Metaheuristics.UseCases.PEST.Resources.ControlFile.vm"); StringWriter writer = new StringWriter(); VelocityContext controlContext = new VelocityContext(); string val = this.RestartFile.ToString(); controlContext.Put("restartFile", this.RestartFile.ToString()); controlContext.Put("ControlData", this); controlTemplate.Merge(controlContext, writer); return(writer); }
private void InitVelocityEngine() { if (string.IsNullOrEmpty(Tpl.RESOURCE_PATH)) { DefaultInit(); } ExtendedProperties props = new ExtendedProperties(); //props.AddProperty(RuntimeConstAnis.RESOURCE_LOADER, "file"); props.AddProperty(RuntimeConstants.ENCODING_DEFAULT, ENCODING_DEFAULT); if (!string.IsNullOrEmpty(Tpl.RESOURCE_PATH)) { props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, Tpl.RESOURCE_PATH); } if (Tpl.CACHE) { props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE, Tpl.CACHE); props.AddProperty("file.resource.loader.modificationCheckInterval", Tpl.INTERVAL); } Velocity.Init(props); }
/// <summary> /// Initializes basic velocity properties. The main purpose of this method is to /// allow this logic to be overrided. /// </summary> /// <param name="props">The <see cref="ExtendedProperties"/> collection to populate.</param> protected virtual void InitializeVelocityProperties(ExtendedProperties props) { props.SetProperty(RuntimeConstants_Fields.RESOURCE_MANAGER_CLASS, "NVelocity.Runtime.Resource.ResourceManagerImpl\\,NVelocity"); props.SetProperty(RuntimeConstants_Fields.FILE_RESOURCE_LOADER_PATH, ViewRootDir); // add support for global macros. they must be defined in "Views/macros". ArrayList macros = new ArrayList(); DirectoryInfo macrosPath = new DirectoryInfo(Path.Combine(ViewRootDir, "macros")); if (macrosPath.Exists) { foreach (FileInfo file in macrosPath.GetFiles("*" + TemplateExtension)) { macros.Add("macros/" + file.Name); } } if (macros.Count > 0) { object m = props.GetProperty(RuntimeConstants_Fields.VM_LIBRARY); if (m is ICollection) { macros.AddRange((ICollection)m); } else if (m is string) { macros.Add(m); } props.AddProperty(RuntimeConstants_Fields.VM_LIBRARY, macros); props.AddProperty(RuntimeConstants_Fields.VM_LIBRARY_AUTORELOAD, true); } }
public NVelocityView1(ControllerContext controllerContext, string viewPath, string viewName, string layoutPath = "", string layoutName = "") { this.controllerContext = controllerContext; this.viewPath = viewPath; this.viewName = viewName; this.layoutPath = layoutPath; this.layoutName = layoutName; this.velocity = new VelocityEngine(); // 使用设置初始化VelocityEngine var props = new ExtendedProperties(); props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, MachineHelper.MapPath(viewPath)); // props.AddProperty(RuntimeConstants, viewPath); props.AddProperty(RuntimeConstants.INPUT_ENCODING, "utf-8"); // props.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "gb2312"); // props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file"); // props.SetProperty(RuntimeConstants.RESOURCE_MANAGER_CLASS, "NVelocity.Runtime.Resource.ResourceManagerImpl\\,NVelocity"); this.velocity.Init(props); // RuntimeConstants.RESOURCE_MANAGER_CLASS // 为模板变量赋值 this.context = new VelocityContext(); }
/// <summary> /// 根据模板解析内容. /// </summary> public static string GenByTemplate(string templatePath, string templateFile, Hashtable ht, string charset) { ExtendedProperties props = new ExtendedProperties(); //使用设置初始化VelocityEngine props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file"); props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, templatePath); Velocity.Init(props); IContext context = new VelocityContext(); if (ht != null && ht.Count > 0) { IDictionaryEnumerator dictenum = ht.GetEnumerator(); while (dictenum.MoveNext()) { String key = dictenum.Key.ToString(); context.Put(key, dictenum.Value); } } Template template = Velocity.GetTemplate(templateFile, charset); StringWriter writer = new StringWriter(); template.Merge(context, writer); Velocity.ClearProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH); // 清除掉该地址 return(writer.ToString()); }
public static Template GetTemplate(string tempPath) { //第一步:Creating a VelocityEngine也就是创建一个VelocityEngine的实例 VelocityEngine vltEngine = new VelocityEngine(); ExtendedProperties vltProps = new ExtendedProperties(); vltProps.AddProperty(RuntimeConstants.INPUT_ENCODING, "utf-8"); vltProps.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "utf-8"); vltEngine.Init(vltProps); //第二步:Creating the Template加载模板文件 Template vltTemplate = new Template(); try { vltTemplate = vltEngine.GetTemplate(tempPath); } catch (Exception e) { Debug.Log(e.Message); return(null); } return(vltTemplate); }
public void Apply(IEnumerable <ChangeScript> changeScripts) { string filename = string.Format(CultureInfo.InvariantCulture, "{0}_{1}.vm", this.syntax, this.GetTemplateQualifier()); var model = new Hashtable(); model.Add("scripts", changeScripts); model.Add("changeLogTableName", this.changeLogTableName); model.Add("delimiter", this.delimiter); model.Add("separator", this.delimiterType is RowDelimiter ? Environment.NewLine : string.Empty); try { ExtendedProperties props = new ExtendedProperties(); var assemblyName = this.GetType().Assembly.GetName().Name; ReplaceManagersWithDbDeployVersions(props, assemblyName); if (this.templateDirectory == null) { props.AddProperty("resource.loader", "assembly"); props.AddProperty("assembly.resource.loader.class", // See the ; there? It will be replaced by , in the resource loader factory // this is because if we add a property with a comma in the value, it will add *two* values to the property. // oh joy. typeof(DbDeployAssemblyResourceLoader).FullName + "; " + assemblyName); props.AddProperty("assembly.resource.loader.assembly", assemblyName); filename = "Net.Sf.Dbdeploy.Resources." + filename; } else { props.SetProperty("file.resource.loader.path", this.templateDirectory.FullName); } var templateEngine = new VelocityEngine(props); var context = new VelocityContext(model); Template template = templateEngine.GetTemplate(filename); template.Merge(context, this.writer); } catch (ResourceNotFoundException ex) { string locationMessage; if (templateDirectory == null) { locationMessage = ""; } else { locationMessage = " at " + templateDirectory.FullName; } throw new UsageException( "Could not find template named " + filename + locationMessage + Environment.NewLine + "Check that you have got the name of the database syntax correct.", ex); } }
/// <summary> /// 静态构造函数,初始化静态模板属性对象 /// </summary> static NVelocityManager() { property.AddProperty(RuntimeConstants.INPUT_ENCODING, "UTF-8"); //模板读入编码格式 property.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "UTF-8"); //模板输入编码格式 property.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file"); //模板加载方式 property.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, ""); //模板加载的目录 }
/// <summary> /// 初始话NVelocity模块 /// </summary> /// <param name="templatePath">模板文件夹路径</param> public void Init(string templatePath) { //创建VelocityEngine实例对象 velocity = new VelocityEngine(); //使用设置初始化VelocityEngine ExtendedProperties props = new ExtendedProperties(); props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file"); string path = "".GetMapPath() + templatePath; props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, path); props.AddProperty(RuntimeConstants.INPUT_ENCODING, "utf-8"); props.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "utf-8"); //props.SetProperty(RuntimeConstants.RESOURCE_LOADER, "assembly"); //props.SetProperty("assembly.resource.loader.assembly", new List<string>() { "Pub.Class" }); //是否缓存 props.AddProperty("file.resource.loader.modificationCheckInterval", (Int64)30); //缓存时间(秒) velocity.Init(props); //为模板变量赋值 context = new VelocityContext(); }
/// <summary> /// 缓存模板对象 /// </summary> /// <param name="folder"></param> /// <param name="fileName"></param> /// <returns></returns> public static Template GetTemplate(string folder, string fileName) { string cacheKey = String.Format("NVelocityHelper_{0}_{1}", folder, fileName); Template template = AppCtx.Cache.RetrieveObject <Template>(cacheKey); if (template == null) { VelocityEngine velocity = new VelocityEngine(); ExtendedProperties props = new ExtendedProperties(); props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file"); props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, folder); props.AddProperty(RuntimeConstants.INPUT_ENCODING, "utf-8"); props.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "utf-8"); velocity.Init(props); template = velocity.GetTemplate(fileName); if (template == null) { throw new NullReferenceException("模板目录不存在。"); } AppCtx.Cache.AddObjectWithFileChange(cacheKey, template, folder.TrimEnd('/').TrimStart('\\') + '\\' + fileName.TrimStart('/').TrimStart('\\')); } return(template); }
private static ExtendedProperties GetBasicProperties() { ExtendedProperties properties = new ExtendedProperties(); properties.AddProperty("resource.loader", "assembly"); properties.AddProperty("assembly.resource.loader.class", "NVelocity.Runtime.Resource.Loader.AssemblyResourceLoader, NVelocity"); return(properties); }
private void Instance(string templatePath) { this._velocity = new VelocityEngine(); var p = new ExtendedProperties(); p.AddProperty("file.resource.loader.path", templatePath); p.AddProperty("input.encoding", "utf-8"); this._velocity.Init(p); this._context = new VelocityContext(); }
static NVelocity() { filePath = Utils.GetMapPath(BaseConfigs.GetForumPath); engine = new VelocityEngine(); props = new ExtendedProperties(); props.AddProperty(RuntimeConstants.INPUT_ENCODING, "utf-8"); props.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "utf-8"); props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, filePath); engine.Init(props); }
protected VelocityEngine createNewVelocityEngine() { VelocityEngine velocity = new VelocityEngine(); var props = new ExtendedProperties(); props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "assembly"); props.AddProperty("assembly.resource.loader.class", "NVelocity.Runtime.Resource.Loader.AssemblyResourceLoader, NVelocity"); props.AddProperty("assembly.resource.loader.assembly", GetType().Assembly.GetName().Name); velocity.Init(props); return(velocity); }
public NVelocityHelper(string path) { ExtendedProperties extendedProperties = new ExtendedProperties(); extendedProperties.AddProperty(RuntimeConstants.INPUT_ENCODING, "utf-8"); extendedProperties.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "utf-8"); extendedProperties.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file"); extendedProperties.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE, false); extendedProperties.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, path); Velocity.Init(extendedProperties); }
public TemplateResolver(string templatePath) { velocity = new VelocityEngine(); //使用设置初始化VelocityEngine ExtendedProperties props = new ExtendedProperties(); props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, templatePath); props.AddProperty(RuntimeConstants.INPUT_ENCODING, "utf-8"); velocity.Init(props); //为模板变量赋值 context = new VelocityContext(); }
private List <GenerateResult> GenerateInternal(TemplateOption option, List <Table> tables, CodeGenerateHandler handler) { var result = new List <GenerateResult>(); var references = new List <Reference>(); foreach (var table in tables) { references.AddRange(table.ForeignKeys); } var tparts = option.Partitions.Where(s => s.Loop == PartitionLoop.Tables).ToList(); var nparts = option.Partitions.Where(s => s.Loop == PartitionLoop.None).ToList(); var engine = new VelocityEngine(); foreach (var table in tables) { var context = new VelocityContext(); context.Put("Tables", tables); context.Put("References", references); context.Put("Current", table); context.Put("Profile", option.Profile); foreach (var part in option.Partitions) { var props = new ExtendedProperties(); var info = new FileInfo(part.FilePath); props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file"); props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, info.DirectoryName); props.AddProperty(RuntimeConstants.COUNTER_INITIAL_VALUE, "0"); engine.Init(props); using (var writer = new StringWriter()) { engine.MergeTemplate(info.Name, "gb2312", context, writer); var r = new GenerateResult(part, writer.ToString()); if (option.WriteToDisk && r.WriteToDisk) { PartitionWriter.Write(r, table, option.Profile, option.OutputDirectory); } result.Add(r); } } } return(result); }
public NVelocityTemplateEngine(IServiceLocator serviceLocator) { _serviceLocator = serviceLocator; _velocity = new VelocityEngine(); var configuration = new ExtendedProperties(); configuration.AddProperty("resource.loader", "custom"); var type = _serviceLocator.Get <ResourceLoader>().GetType(); var typeName = type.FullName; var assemblyName = type.Assembly.GetName().Name; configuration.AddProperty("custom.resource.loader.class", string.Format("{0};{1}", typeName, assemblyName)); configuration.AddProperty("input.encoding", "UTF-8"); configuration.AddProperty("output.encoding", "UTF-8"); _velocity.Init(configuration); }
public void Init(string virtualDir) { //创建VelocityEngine实例对象 velocity = new VelocityEngine(); //使用设置初始化VelocityEngine ExtendedProperties props = new ExtendedProperties(); props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file"); props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, string.Format("{0}\\{1}", Root.TrimEnd('\\'), virtualDir)); props.AddProperty(RuntimeConstants.INPUT_ENCODING, "utf-8"); // props.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "utf-8"); velocity.Init(props); //为模板变量赋值 context = new VelocityContext(); }
public NVelocityViewEngine(IDictionary properties) { base.MasterLocationFormats = new string[] { "~/Views/{1}/{0}.vm", "~/Views/Shared/{0}.vm" }; base.AreaMasterLocationFormats = new string[] { "~/Areas/{2}/Views/{1}/{0}.vm", "~/Areas/{2}/Views/Shared/{0}.vm" }; base.ViewLocationFormats = new string[] { "~/Views/{1}/{0}.vm", "~/Views/Shared/{0}.vm" }; base.AreaViewLocationFormats = new string[] { "~/Areas/{2}/Views/{1}/{0}.vm", "~/Areas/{2}/Views/Shared/{0}.vm" }; base.PartialViewLocationFormats = base.ViewLocationFormats; base.AreaPartialViewLocationFormats = base.AreaViewLocationFormats; base.FileExtensions = new string[] { "vm" }; if (properties == null) { properties = DEFAULT_PROPERTIES; } ExtendedProperties props = new ExtendedProperties(); foreach (string key in properties.Keys) { props.AddProperty(key, properties[key]); } _engine = new VelocityEngine(); _engine.Init(props); }
public static string Format(HttpContext context, string pattern, VelocityContext velocitycontext) { using (var writer = new StringWriter()) { try { if (!_isInitialized) { var props = new ExtendedProperties(); props.AddProperty("file.resource.loader.path", new ArrayList(new[] { ".", Path.Combine( context.Server.MapPath(feed.HandlerBasePath), "Patterns") })); Velocity.Init(props); _isInitialized = true; } //Load patterns var template = Patterns.Get(pattern, () => LoadTemplate(pattern)); template.Merge(velocitycontext, writer); return(writer.GetStringBuilder().ToString()); } catch (Exception) { //Format failed some way return(writer.GetStringBuilder().ToString()); } } }
public override void Init(ExtendedProperties configuration) { if (configuration.GetString("input.encoding") != "UTF-8") { configuration.AddProperty("templateString.encoding", "UTF-8"); } }
/// <summary> /// NVelocity /// </summary> /// <param name="path">path</param> public NVelocityHelper(string path) { //创建VelocityEngine实例对象 ve = new VelocityEngine(); //初始化VelocityEngine ExtendedProperties eps = new ExtendedProperties(); //编码 eps.AddProperty(RuntimeConstants.INPUT_ENCODING, "utf-8"); eps.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "utf-8"); eps.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file"); eps.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, HttpContext.Current.Server.MapPath(path)); ve.Init(eps); //整合模板 vc = new VelocityContext(); }
public void AllowsSingleLoaderAssembly() { ExtendedProperties properties = GetBasicProperties(); properties.AddProperty("assembly.resource.loader.assembly", "MyTestAssembly"); new VelocityEngine().Init(properties); }
/// <summary> /// 初始话NVelocity模块 /// </summary> /// <param name="templatDir">模板文件夹路径</param> public void Init(string templatDir) { //创建VelocityEngine实例对象 velocity = new VelocityEngine(); //使用设置初始化VelocityEngine ExtendedProperties props = new ExtendedProperties(); props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file"); props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, HttpContext.Current.Server.MapPath(templatDir)); props.AddProperty(RuntimeConstants.INPUT_ENCODING, "utf-8"); props.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "utf-8"); velocity.Init(props); //为模板变量赋值 context = new VelocityContext(); }
/// <summary> /// 初始化NVelocity模块 /// </summary> private void Init() { //创建VelocityEngine实例对象 m_velocity = new VelocityEngine(); //使用设置初始化VelocityEngine var props = new ExtendedProperties(); props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file"); props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, Application.StartupPath + "\\Template"); props.AddProperty(RuntimeConstants.INPUT_ENCODING, "gb2312"); props.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "gb2312"); m_velocity.Init(props); //为模板变量赋值 m_context = new VelocityContext(); }
/// <summary> /// 初始话NVelocity模块 /// </summary> /// <param name="templatDir">模板文件夹路径</param> public void Init(string templatDir) { //创建VelocityEngine实例对象 _velocity = new VelocityEngine(); //使用设置初始化VelocityEngine var props = new ExtendedProperties(); props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file"); props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, templatDir); props.AddProperty(RuntimeConstants.INPUT_ENCODING, "utf-8"); props.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "utf-8"); _velocity.Init(props); //为模板变量赋值 _context = new VelocityContext(); }
/// <summary> /// 初始话CNVelocity模块 /// </summary> public void Init(basePortalModule _bpm, String _path = "Effect") { //创建VelocityEngine实例对象 velocity = new VelocityEngine(); //使用设置初始化VelocityEngine ExtendedProperties props = new ExtendedProperties(); props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file"); if (_path != "Effect") { props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, HttpContext.Current.Server.MapPath(String.Format("{0}{1}s/{2}/", _bpm.ModulePath, _path, _bpm.Settings_ResultName))); } else { props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, HttpContext.Current.Server.MapPath(String.Format("{0}{1}s/{2}/", _bpm.ModulePath, _path, _bpm.Settings_EffectName))); } //props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, Path.GetDirectoryName(HttpContext.Current.Request.PhysicalPath)); props.AddProperty(RuntimeConstants.INPUT_ENCODING, "utf-8"); props.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "utf-8"); //模板的缓存设置 props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE, false); //是否缓存 props.AddProperty("file.resource.loader.modificationCheckInterval", (Int64)600); //缓存时间(秒) velocity.Init(props); //为模板变量赋值 context = new VelocityContext(); }
public NVelocityTemplateRepository(string templateDirectory) { engine = new VelocityEngine(); ExtendedProperties props = new ExtendedProperties(); props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, templateDirectory); engine.Init(props); }
/// <summary> /// Add a property to the configuration. If it already /// exists then the value stated here will be added /// to the configuration entry. /// <remarks> /// For example, if /// <c>resource.loader = file</c> /// is already present in the configuration and you /// <c>addProperty("resource.loader", "classpath")</c> /// /// Then you will end up with a <see cref="IList"/> like the /// following: /// /// <c>["file", "classpath"]</c> /// </remarks> /// </summary> /// <param name="key">key</param> /// <param name="value">value</param> public void AddProperty(String key, Object value) { if (overridingProperties == null) { overridingProperties = new ExtendedProperties(); } overridingProperties.AddProperty(key, value); }