예제 #1
0
 /// <summary>
 /// 设置模板引擎(因为模板引擎读写文件目录不同所以在每次生成模板前要设置模板读写的路径)
 /// </summary>
 /// <param name="path">模板引擎将要读取的模板的更目录</param>
 public static void SetProperty(string path)
 {
     engine = new VelocityEngine(); //必须每次都要实例化新的对象,因为模板地址属性没办法重新设置
     property.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, path);
     engine.Init(property);         //初始化模板引擎
     if (engine.GetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH) != null)
     {
         string param = engine.GetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH).ToString();
         if (SetNVelociryProperty != null)
         {
             SetNVelociryProperty(new DateTime(), param);//发送事件参数,至少有一个封送地址存在,否则报错
         }
     }
 }
        public void TestAssemblyBasedConfig()
        {
            VelocityEngine velocityEngine = appContext.GetObject("cnAssemblyVelocityEngine") as VelocityEngine;

            Assert.IsNotNull(velocityEngine, "velocity engine is null");
            Assert.AreEqual(VelocityConstants.Assembly, getSingleProperty(velocityEngine, RuntimeConstants.RESOURCE_LOADER), "incorrect resource loader");
            Assert.AreEqual(TemplateDefinitionConstants.AssemblyResourceLoaderClass, getSingleProperty(velocityEngine,
                                                                                                       TemplateNamespaceParser.getResourceLoaderProperty(VelocityConstants.Assembly, VelocityConstants.Class)), "incorrect resource loader type");
            Assert.AreEqual("Spring.Template.Velocity.Castle.Tests", getSingleProperty(velocityEngine,
                                                                                       TemplateNamespaceParser.getResourceLoaderProperty(VelocityConstants.Assembly, VelocityConstants.Assembly)), "incorrect resource loader path");
            Assert.AreEqual(DEFAULT_CACHE_SIZE, velocityEngine.GetProperty(RuntimeConstants.RESOURCE_MANAGER_DEFAULTCACHE_SIZE), "incorrect default cache size");
            Assert.AreEqual(DEFAULT_CACHE_FLAG, velocityEngine.GetProperty(VelocityConstants.Assembly + VelocityConstants.Separator + PropertyResourceLoaderCachce),
                            "incorrect caching flag");
            AssertMergedValue(velocityEngine, "Spring.Template.Velocity.SimpleTemplate.vm");
        }
        public void TestFileBasedConfig()
        {
            VelocityEngine velocityEngine = appContext.GetObject("cnFileVelocityEngine") as VelocityEngine;

            Assert.IsNotNull(velocityEngine, "velocity engine is null");
            Assert.AreEqual(VelocityConstants.File, getSingleProperty(velocityEngine, RuntimeConstants.RESOURCE_LOADER), "incorrect resource loader");
            Assert.AreEqual(TemplateDefinitionConstants.FileResourceLoaderClass, getSingleProperty(velocityEngine,
                                                                                                   TemplateNamespaceParser.getResourceLoaderProperty(VelocityConstants.File, VelocityConstants.Class)), "incorrect resource loader type");
            Assert.AreEqual(new string[] { "Template/Velocity/", "Template/" }, velocityEngine.GetProperty(
                                TemplateNamespaceParser.getResourceLoaderProperty(VelocityConstants.File, VelocityConstants.Path)), "incorrect resource loader path");
            Assert.AreEqual(DEFAULT_CACHE_SIZE, velocityEngine.GetProperty(RuntimeConstants.RESOURCE_MANAGER_DEFAULTCACHE_SIZE), "incorrect default cache size");
            Assert.AreEqual(DEFAULT_MOD_CHECK, velocityEngine.GetProperty(
                                VelocityConstants.File + VelocityConstants.Separator + PropertyModificationCheck), "incorrect mod check interval");
            Assert.AreEqual(DEFAULT_CACHE_FLAG, velocityEngine.GetProperty(VelocityConstants.File + VelocityConstants.Separator + PropertyResourceLoaderCachce),
                            "incorrect caching flag");

            AssertMergedValue(velocityEngine, "SimpleTemplate.vm");
        }
        public void TestSpringBasedConfig()
        {
            VelocityEngine velocityEngine = appContext.GetObject("cnSpringVelocityEngine") as VelocityEngine;

            Assert.IsNotNull(velocityEngine, "velocity engine is null");
            const string PropertySpring = TemplateDefinitionConstants.Spring;

            Assert.AreEqual(PropertySpring, getSingleProperty(velocityEngine, RuntimeConstants.RESOURCE_LOADER), "incorrect resource loader");
            Assert.AreEqual(TemplateDefinitionConstants.SpringResourceLoaderClass, getSingleProperty(velocityEngine,
                                                                                                     TemplateNamespaceParser.getResourceLoaderProperty(PropertySpring, VelocityConstants.Class)), "incorrect resource loader type");
            // no way to test the path property other than trying to actually perform a merge (it is set in velocity engine as an application attribute which is not exposed)
            Assert.AreEqual(DEFAULT_CACHE_SIZE, velocityEngine.GetProperty(RuntimeConstants.RESOURCE_MANAGER_DEFAULTCACHE_SIZE), "incorrect default cache size");
            Assert.AreEqual(DEFAULT_CACHE_FLAG, velocityEngine.GetProperty(PropertySpring + VelocityConstants.Separator + PropertyResourceLoaderCachce),
                            "incorrect caching flag");

            AssertMergedValue(velocityEngine, "SimpleTemplate.vm");
            AssertMergedValue(velocityEngine, "EmbeddedTemplate.vm");
        }
        /// <summary>
        /// Grab a single property from (if it's an array return the first value) from the velocityEngine
        /// </summary>
        private string getSingleProperty(VelocityEngine velocityEngine, string prop)
        {
            object property = velocityEngine.GetProperty(prop);

            if (property is ArrayList)
            {
                ArrayList list = (ArrayList)property;
                return((string)list[0]);
            }
            return((string)property);
        }
예제 #6
0
파일: AnakiaTask.cs 프로젝트: minskowl/MY
        /// <summary>
        /// Process an XML file using Velocity
        /// </summary>
        private void Process(String basedir, String xmlFile, FileInfo destdir, AnakiaXmlDocument projectDocument)
        {
            FileInfo     outFile = null;
            FileInfo     inFile  = null;
            StreamWriter writer  = null;

            try {
                // the current input file relative to the baseDir
                inFile = new System.IO.FileInfo(basedir + Path.DirectorySeparatorChar.ToString() + xmlFile);
                // the output file relative to basedir
                outFile = new System.IO.FileInfo(destdir + Path.DirectorySeparatorChar.ToString() + xmlFile.Substring(0, (xmlFile.LastIndexOf((System.Char) '.')) - (0)) + extension);

                // only process files that have changed
                if (lastModifiedCheck == false || (inFile.LastWriteTime.Ticks > outFile.LastWriteTime.Ticks || styleFile.LastWriteTime.Ticks > outFile.LastWriteTime.Ticks || projectFile.LastWriteTime.Ticks > outFile.LastWriteTime.Ticks))
                {
                    EnsureDirectoryFor(outFile);

                    //-- command line status
                    Log.WriteLine(LogPrefix + "Input:  " + inFile);

                    // Build the Anakia Document
                    AnakiaXmlDocument root = new AnakiaXmlDocument();
                    root.Load(inFile.FullName);

                    // Shove things into the Context
                    VelocityContext context = new VelocityContext();

                    /*
                     *  get the property TEMPLATE_ENCODING
                     *  we know it's a string...
                     */
                    String encoding = (String)ve.GetProperty(RuntimeConstants_Fields.OUTPUT_ENCODING);
                    if (encoding == null || encoding.Length == 0 || encoding.Equals("8859-1") || encoding.Equals("8859_1"))
                    {
                        encoding = "ISO-8859-1";
                    }

                    context.Put("root", root.DocumentElement);
                    context.Put("relativePath", getRelativePath(xmlFile));
                    context.Put("escape", new Escape());
                    context.Put("date", System.DateTime.Now);

                    // only put this into the context if it exists.
                    if (projectDocument != null)
                    {
                        context.Put("project", projectDocument.DocumentElement);
                    }

                    // Process the VSL template with the context and write out
                    // the result as the outFile.
                    writer = new System.IO.StreamWriter(new System.IO.FileStream(outFile.FullName, System.IO.FileMode.Create));
                    // get the template to process

                    Template template = ve.GetTemplate(styleFile.Name)
                    ;
                    template.Merge(context, writer);

                    Log.WriteLine(LogPrefix + "Output: " + outFile);
                }
            } catch (System.Exception e) {
                Log.WriteLine(LogPrefix + "Failed to process " + inFile);
                if (outFile != null)
                {
                    bool tmpBool2;
                    if (System.IO.File.Exists(outFile.FullName))
                    {
                        System.IO.File.Delete(outFile.FullName);
                        tmpBool2 = true;
                    }
                    else if (System.IO.Directory.Exists(outFile.FullName))
                    {
                        System.IO.Directory.Delete(outFile.FullName);
                        tmpBool2 = true;
                    }
                    else
                    {
                        tmpBool2 = false;
                    }
                    bool generatedAux3 = tmpBool2;
                }
                SupportClass.WriteStackTrace(e, Console.Error);
            } finally {
                if (writer != null)
                {
                    try {
                        writer.Flush();
                        writer.Close();
                    } catch (System.Exception e) {
                        // closing down, just ignore
                    }
                }
            }
        }