public NVelocityHelper(string key, object value) { //1.创建Velocity 引擎(VelocityEngine)并设置属性 velocityEngine = new VelocityEngine(); velocityEngine.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file"); // Velocity加载类型 velocityEngine.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, // Velocity加载文件路径 HttpContext.Current.Server.MapPath("~/Templates/")); velocityEngine.AddProperty(RuntimeConstants.INPUT_ENCODING, "utf-8"); // 输入编码格式设置 velocityEngine.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "utf-8"); // 输出编码格式设置 velocityEngine.Init(); //2.Velocity 上下文对象设置 vc = new VelocityContext(); vc.Put(key, value); }
// public virtual ExtendedProperties GetContextProperties() { // return contextProperties; // } // // public virtual bool UseClasspath { // set { // this.useClasspath = value; // } // } /// <summary> /// Execute the input script with Velocity /// @throws BuildException /// BuildExceptions are thrown when required attributes are missing. /// Exceptions thrown by Velocity are rethrown as BuildExceptions. /// </summary> protected override void ExecuteTask() { // Make sure the template path is set. if (templatePath == null && useClasspath == false) { throw new BuildException("The template path needs to be defined if you are not using " + "the classpath for locating templates!"); } // Make sure the control template is set. if (controlTemplate == null) { throw new BuildException("The control template needs to be defined!"); } // Make sure the output directory is set. if (outputDirectory == null) { throw new BuildException("The output directory needs to be defined!"); } // Make sure there is an output file. if (outputFile == null) { throw new BuildException("The output file needs to be defined!"); } VelocityEngine ve = new VelocityEngine(); try { // Setup the Velocity Runtime. if (templatePath != null) { //log("Using templatePath: " + templatePath, project.MSG_VERBOSE); Log.WriteLine(LogPrefix + "Using templatePath: " + templatePath); ve.SetProperty(RuntimeConstants_Fields.FILE_RESOURCE_LOADER_PATH, templatePath); } if (useClasspath) { Log.WriteLine(LogPrefix + "Using classpath"); ve.AddProperty(RuntimeConstants_Fields.RESOURCE_LOADER, "classpath"); ve.SetProperty("classpath." + RuntimeConstants_Fields.RESOURCE_LOADER + ".class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); ve.SetProperty("classpath." + RuntimeConstants_Fields.RESOURCE_LOADER + ".cache", "false"); ve.SetProperty("classpath." + RuntimeConstants_Fields.RESOURCE_LOADER + ".modificationCheckInterval", "2"); } ve.Init(); // Create the text generator. Generator generator = Generator.Instance; generator.LogPrefix = LogPrefix; generator.VelocityEngine = ve; generator.OutputPath = outputDirectory; generator.InputEncoding = inputEncoding; generator.OutputEncoding = outputEncoding; if (templatePath != null) { generator.TemplatePath = templatePath; } // Make sure the output directory exists, if it doesn't // then create it. System.IO.FileInfo file = new System.IO.FileInfo(outputDirectory); bool tmpBool; if (System.IO.File.Exists(file.FullName)) tmpBool = true; else tmpBool = System.IO.Directory.Exists(file.FullName); if (!tmpBool) { System.IO.Directory.CreateDirectory(file.FullName); } System.String path = outputDirectory + System.IO.Path.DirectorySeparatorChar.ToString() + outputFile; Log.WriteLine(LogPrefix + "Generating to file " + path); System.IO.StreamWriter writer = generator.getWriter(path, outputEncoding); // The generator and the output path should // be placed in the init context here and // not in the generator class itself. IContext c = initControlContext(); // Everything in the generator class should be // pulled out and placed in here. What the generator // class does can probably be added to the Velocity // class and the generator class can probably // be removed all together. populateInitialContext(c); // Feed all the options into the initial // control context so they are available // in the control/worker templates. if (contextProperties != null) { IEnumerator i = contextProperties.Keys; while (i.MoveNext()) { System.String property = (System.String) i.Current; System.String value_Renamed = contextProperties.GetString(property); // Now lets quickly check to see if what // we have is numeric and try to put it // into the context as an Integer. try { c.Put(property, System.Int32.Parse(value_Renamed)); } catch (System.FormatException nfe) { // Now we will try to place the value into // the context as a boolean value if it // maps to a valid boolean value. System.String booleanString = contextProperties.TestBoolean(value_Renamed); if (booleanString != null) { c.Put(property, booleanString.ToUpper().Equals("TRUE")); } else { // We are going to do something special // for properties that have a "file.contents" // suffix: for these properties will pull // in the contents of the file and make // them available in the context. So for // a line like the following in a properties file: // // license.file.contents = license.txt // // We will pull in the contents of license.txt // and make it available in the context as // $license. This should make texen a little // more flexible. if (property.EndsWith("file.contents")) { // We need to turn the license file from relative to // absolute, and let Ant help :) //value_Renamed = StringUtils.fileContentsToString(project.resolveFile(value_Renamed).CanonicalPath); value_Renamed = StringUtils.fileContentsToString(new FileInfo(value_Renamed).FullName); property = property.Substring(0, (property.IndexOf("file.contents") - 1) - (0)); } c.Put(property, value_Renamed); } } } } writer.Write(generator.parse(controlTemplate, c)); writer.Flush(); writer.Close(); generator.shutdown(); cleanup(); } catch (BuildException e) { throw e; } catch (MethodInvocationException e) { throw new BuildException("Exception thrown by '" + e.ReferenceName + "." + e.MethodName + "'" + ERR_MSG_FRAGMENT, e.WrappedThrowable); } catch (ParseErrorException e) { throw new BuildException("Velocity syntax error" + ERR_MSG_FRAGMENT, e); } catch (ResourceNotFoundException e) { throw new BuildException("Resource not found" + ERR_MSG_FRAGMENT, e); } catch (System.Exception e) { throw new BuildException("Generation failed" + ERR_MSG_FRAGMENT, e); } }
static NVelocityConfig() { velocity = new VelocityEngine(); velocity.AddProperty("assembly.resource.loader.assembly", new[] {"bdUnit.Core"}); velocity.Init(); }