Exemplo n.º 1
0
        public void Test()
        {
            var velocityEngine = new VelocityEngine();

            ExtendedProperties extendedProperties = new ExtendedProperties();
            extendedProperties.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, TemplateTest.FILE_RESOURCE_LOADER_PATH);

            velocityEngine.Init(extendedProperties);

            VelocityContext context = new VelocityContext();

            context.Put("yada", "line");

            Template template = velocityEngine.GetTemplate(
                GetFileName(null, "nv37", TemplateTest.TMPL_FILE_EXT));

            StringWriter writer = new StringWriter();

            #pragma warning disable 612,618
            velocityEngine.MergeTemplate("nv37.vm", context, writer);
            #pragma warning restore 612,618

            //template.Merge(context, writer);

            Console.WriteLine(writer);
        }
Exemplo n.º 2
0
        public void Test()
        {
            var velocityEngine = new VelocityEngine();

            ExtendedProperties extendedProperties = new ExtendedProperties();

            extendedProperties.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, TemplateTest.FILE_RESOURCE_LOADER_PATH);

            velocityEngine.Init(extendedProperties);

            VelocityContext context = new VelocityContext();

            context.Put("yada", "line");

            Template template = velocityEngine.GetTemplate(
                GetFileName(null, "nv37", TemplateTest.TMPL_FILE_EXT));

            StringWriter writer = new StringWriter();

#pragma warning disable 612,618
            velocityEngine.MergeTemplate("nv37.vm", context, writer);
#pragma warning restore 612,618

            //template.Merge(context, writer);

            Console.WriteLine(writer);
        }
Exemplo n.º 3
0
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Processes the specified template.
        /// </summary>
        /// <param name="templateName">Name of the template.</param>
        /// ------------------------------------------------------------------------------------
        public void Process(string templateName)
        {
            Stream stream = null;

            try
            {
                if (m_OutputFileName == null || m_OutputFileName == string.Empty)
                {
                    stream = new MemoryStream();                     // we don't care about the output
                }
                else
                {
                    stream = new FileStream(Path.Combine(m_OutputDir, m_OutputFileName),
                                            FileMode.Create, FileAccess.Write);
                }

                using (StreamWriter writer = new StreamWriter(stream))
                {
                    m_Engine.MergeTemplate(templateName, "UTF-8", m_Context, writer);
                }
            }
            finally
            {
                if (stream != null)
                {
                    stream.Dispose();
                }
            }
        }
Exemplo n.º 4
0
        public string Transform(string transformerFileName, Hashtable transformable)
        {
            // Add a translator to all views
            var translations = Translations.RetrieveCurrent();

            transformable.Add("translations", translations);

            string output = string.Empty;

            using (TextWriter writer = new StringWriter())
            {
                try
                {
                    if (DetermineTemplateLocation(transformerFileName) == TemplateLocation.CustomTemplates)
                    {
                        VelocityEngineCustom.MergeTemplate(transformerFileName, RuntimeConstants.ENCODING_DEFAULT, new VelocityContext(transformable), writer);
                    }
                    else
                    {
                        VelocityEngine.MergeTemplate(transformerFileName, RuntimeConstants.ENCODING_DEFAULT, new VelocityContext(transformable), writer);
                    }
                }
                catch (Exception baseException)
                {
                    throw new CruiseControlException(string.Format(@"Exception calling NVelocity for template: {0}
Template path is {1}", transformerFileName, physicalApplicationPathProvider.GetFullPathFor("templates")), baseException);
                }
                output = writer.ToString();
            }
            return(output);
        }
Exemplo n.º 5
0
        public string Transform(string transformerFileName, Hashtable transformable)
        {
            string output = "";

            using (TextWriter writer = new StringWriter())
            {
                VelocityEngine.MergeTemplate(transformerFileName, new VelocityContext(transformable), writer);
                output = writer.ToString();
            }
            return(output);
        }
 /// <summary>
 /// Merge the specified Velocity template with the given model and write
 /// the result to the given Writer.
 /// </summary>
 /// <param name="velocityEngine">VelocityEngine to work with</param>
 /// <param name="templateLocation">the location of template, relative to Velocity's resource loader path</param>
 /// <param name="encoding">encoding the encoding of the template file</param>
 /// <param name="model">the Hashtable that contains model names as keys and model objects</param>
 /// <param name="writer">writer the TextWriter to write the result to</param>
 /// <exception cref="VelocityException">thrown if any exception is thrown by the velocity engine</exception>
 public static void MergeTemplate(
     VelocityEngine velocityEngine, string templateLocation, string encoding, Hashtable model, TextWriter writer)
 {
     try {
         VelocityContext velocityContext = new VelocityContext(model);
         velocityEngine.MergeTemplate(templateLocation, encoding, velocityContext, writer);
     } catch (VelocityException) {
         throw;
     } catch (Exception ex) {
         throw new VelocityException(ex.ToString(), ex);
     }
 }
Exemplo n.º 7
0
        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);
        }
Exemplo n.º 8
0
        /// <summary>
        /// 生成内容
        /// </summary>
        /// <returns></returns>
        public string GetString()
        {
            VelocityEngine     ve = new VelocityEngine();                                 //模板引擎实例化
            ExtendedProperties ep = new ExtendedProperties();                             //模板引擎参数实例化

            ep.AddProperty("file.resource.loader.modificationCheckInterval", (Int64)300); //缓存时间(秒)
            ve.Init(ep);

            VelocityContext vc = new VelocityContext(); //当前的数据信息载体集合

            foreach (var item in Items)
            {
                vc.Put(item.Key, item.Value);
            }
            TextWriter writer = new StringWriter();

            ve.MergeTemplate(TemplateName, "utf-8", vc, writer);

            return(writer.ToString());
        }
Exemplo n.º 9
0
        /// <inheritdoc />
        /// <summary>
        /// Renderiza um template a partir do nome utilizando masterPage, substituindo as variáveis do template pelas variáveis passadas como parâmetro
        /// </summary>
        /// <param name="masterPage">Nome da master page</param>
        /// <param name="templateName">Nome do Template</param>
        /// <param name="data">Dicionário com as váriáveis (Chave/Valor)</param>
        /// <returns>Template com as variávies substituídas</returns>
        public string RenderTemplate(string masterPage, string templateName, IDictionary <string, object> data)
        {
            if (string.IsNullOrEmpty(templateName))
            {
                throw new ArgumentException("O parâmetro \"templateName\" não foi informado", nameof(templateName));
            }

            var name = !string.IsNullOrEmpty(masterPage)
                ? masterPage : templateName;

            var engine = new VelocityEngine();
            var props  = new ExtendedProperties();

            props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, _templatesPath);
            engine.Init(props);

            engine.GetTemplate(name, Encoding.UTF8.BodyName);

            var context      = new VelocityContext();
            var templateData = data ?? new Dictionary <string, object>();

            foreach (var key in templateData.Keys)
            {
                context.Put(key, templateData[key]);
            }

            if (!string.IsNullOrEmpty(masterPage))
            {
                context.Put("childContent", templateName);
            }
            string retorno;

            using (var writer = new StringWriter())
            {
                engine.MergeTemplate(name, Encoding.UTF8.BodyName, context, writer);
                retorno = writer.GetStringBuilder().ToString();
            }

            return(retorno);
        }
Exemplo n.º 10
0
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Processes the specified template.
        /// </summary>
        /// <param name="templateName">Name of the template.</param>
        /// ------------------------------------------------------------------------------------
        public void Process(string templateName)
        {
            Stream stream = null;

            try
            {
                stream = string.IsNullOrEmpty(m_OutputFileName)
                                                        ? (Stream) new MemoryStream()
                                                        : new FileStream(Path.Combine(m_OutputDir, m_OutputFileName),
                                                                         FileMode.Create, FileAccess.Write);

                using (var writer = new StreamWriter(stream))
                {
                    m_Engine.MergeTemplate(templateName, "UTF-8", m_Context, writer);
                }
            }
            finally
            {
                if (stream != null)
                {
                    stream.Dispose();
                }
            }
        }