jan

11

C# NVelocity

Posted by : admin | On : 11 janvier 2016

 

 

demo NVelocity

 

using NVelocityTemplateEngine.Engines;
using NVelocityTemplateEngine.Interfaces;

namespace NVelocityTemplateEngine
{
	/// <summary>
	/// Summary description for NVelocityEngineFactory.
	/// </summary>
	public class NVelocityEngineFactory
	{
		/// <summary>
		/// Creates a new instance of NVelocityFileEngine class.
		/// </summary>
		/// <param name="templateDirectory">The template directory.</param>
		/// <param name="cacheTemplate">if set to <c>true</c> [cache template].</param>
		/// <returns></returns>
		public static INVelocityEngine CreateNVelocityFileEngine(string templateDirectory, bool cacheTemplate)
		{
			return new NVelocityFileEngine(templateDirectory, cacheTemplate);
		}

		/// <summary>
		/// Creates a new instance of NVelocityAssemblyEngine class.
		/// </summary>
		/// <param name="assemblyName">Name of the assembly.</param>
		/// <param name="cacheTemplate">if set to <c>true</c> [cache template].</param>
		/// <returns></returns>
		public static INVelocityEngine CreateNVelocityAssemblyEngine(string assemblyName, bool cacheTemplate)
		{
			return new NVelocityAssemblyEngine(assemblyName, cacheTemplate);
		}

		/// <summary>
		/// Creates a new instance of NVelocityMemoryEngine class.
		/// </summary>
		/// <param name="cacheTemplate">if set to <c>true</c> [cache template].</param>
		/// <returns></returns>
		public static INVelocityEngine CreateNVelocityMemoryEngine(bool cacheTemplate)
		{
			return new NVelocityMemoryEngine(cacheTemplate);
		}
	}
}
//=============================================================================
// Project : NVelocity wrapper
// Author  : Simone Busoli
// Updated : Thu 09/02/2006 01:35:00
// Compiler: Microsoft Visual C# .NET
//
// This code may be used in compiled form in any way you desire.  This
// file may be redistributed by any means in modified or unmodified form
// PROVIDING that this notice and the author's name and any copyright
// notices remain intact.
//
// This file is provided "as is" with no warranty either express or
// implied.  The author accepts no liability for any damage or loss of
// business that this product may cause.
//
// Version   Date        Who            Comments
// ============================================================================
// 1.0.0     09/02/2006  Simone Busoli  First release
//=============================================================================

using System.Reflection;
//
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
//
[assembly: AssemblyTitle("NVelocityTemplateEngine")]
[assembly: AssemblyDescription("A wrapper for NVelocity template engine.")]
[assembly: AssemblyCompany("Simone Busoli")]
[assembly: AssemblyVersion("1.0.0.0")]
using System.Collections;
using System.IO;
using NVelocity;
using NVelocity.Exception;
using NVelocity.Runtime;
using NVelocityTemplateEngine.BaseClasses;
using NVelocityTemplateEngine.Interfaces;

namespace NVelocityTemplateEngine.Engines
{
	/// <summary>
	/// Summary description for NVelocityAssemblyEngine.
	/// </summary>
	public sealed class NVelocityAssemblyEngine : NVelocityEngineBase, INVelocityEngine
	{
		private string assemblyName;

		/// <summary>
		/// Initializes a new instance of the <see cref="NVelocityAssemblyEngine"/> class.
		/// </summary>
		/// <param name="assemblyName">Name of the assembly.</param>
		/// <param name="cacheTamplate">if set to <c>true</c> [cache tamplate].</param>
		internal NVelocityAssemblyEngine(string assemblyName, bool cacheTamplate) : base(cacheTamplate)
		{
			this.assemblyName = assemblyName;

			this.SetProperty(RuntimeConstants.RESOURCE_LOADER, "assembly");
			this.SetProperty("assembly.resource.loader.class", "NVelocity.Runtime.Resource.Loader.AssemblyResourceLoader;NVelocity");
			this.SetProperty("assembly.resource.loader.assembly", assemblyName);
			this.Init();
		}

		/// <summary>
		/// Processes the specified context.
		/// </summary>
		/// <param name="context">The context.</param>
		/// <param name="templateName">Name of the template.</param>
		/// <returns></returns>
		public string Process(IDictionary context, string templateName)
		{
			StringWriter writer = new StringWriter();

			try
			{
				Template template = this.GetTemplate(this.assemblyName + "." + templateName);
				template.Merge(CreateContext(context), writer);
			}
			catch (ResourceNotFoundException rnf)
			{
				return rnf.Message;
			}
			catch (ParseErrorException pe)
			{
				return pe.Message;
			}

			return writer.ToString();
		}

		/// <summary>
		/// Processes the specified context.
		/// </summary>
		/// <param name="context">The context.</param>
		/// <param name="writer">The writer.</param>
		/// <param name="templateName">Name of the template.</param>
		public void Process(IDictionary context, TextWriter writer, string templateName)
		{
			try
			{
				Template template = this.GetTemplate(this.assemblyName + "." + templateName);
				template.Merge(CreateContext(context), writer);
			}
			catch (ResourceNotFoundException rnf)
			{
				writer.Write(rnf.Message);
			}
			catch (ParseErrorException pe)
			{
				writer.Write(pe.Message);
			}
		}
	}
}
using System.Collections;
using System.IO;
using NVelocity;
using NVelocity.Exception;
using NVelocity.Runtime;
using NVelocityTemplateEngine.BaseClasses;
using NVelocityTemplateEngine.Interfaces;

namespace NVelocityTemplateEngine.Engines
{
	/// <summary>
	/// Summary description for NVelocityFileEngine.
	/// </summary>
	public sealed class NVelocityFileEngine : NVelocityEngineBase, INVelocityEngine
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="NVelocityFileEngine"/> class.
		/// </summary>
		/// <param name="templateDirectory">The template directory.</param>
		/// <param name="cacheTemplate">if set to <c>true</c> [cache template].</param>
		internal NVelocityFileEngine(string templateDirectory, bool cacheTemplate) : base(cacheTemplate)
		{
			this.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file");
			this.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, templateDirectory);
			this.Init();
		}

		/// <summary>
		/// Processes the specified context.
		/// </summary>
		/// <param name="context">The context.</param>
		/// <param name="templateName">Name of the template.</param>
		/// <returns></returns>
		public string Process(IDictionary context, string templateName)
		{
			StringWriter writer = new StringWriter();

			try
			{
				Template template = this.GetTemplate(templateName);
				template.Merge(CreateContext(context), writer);
			}
			catch (ResourceNotFoundException rnf)
			{
				return rnf.Message;
			}
			catch (ParseErrorException pe)
			{
				return pe.Message;
			}

			return writer.ToString();
		}

		/// <summary>
		/// Processes the specified context.
		/// </summary>
		/// <param name="context">The context.</param>
		/// <param name="writer">The writer.</param>
		/// <param name="templateName">Name of the template.</param>
		public void Process(IDictionary context, TextWriter writer, string templateName)
		{
			try
			{
				Template template = this.GetTemplate(templateName);
				template.Merge(CreateContext(context), writer);
			}
			catch (ResourceNotFoundException rnf)
			{
				writer.Write(rnf.Message);
			}
			catch (ParseErrorException pe)
			{
				writer.Write(pe.Message);
			}
		}
	}
}
using System;
using System.Collections;
using System.IO;
using NVelocity.Context;
using NVelocity.Exception;
using NVelocityTemplateEngine.BaseClasses;
using NVelocityTemplateEngine.Interfaces;

namespace NVelocityTemplateEngine.Engines
{
	/// <summary>
	/// Summary description for NVelocityMemoryEngine.
	/// </summary>
	public sealed class NVelocityMemoryEngine : NVelocityEngineBase, INVelocityEngine
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="NVelocityMemoryEngine"/> class.
		/// </summary>
		/// <param name="cacheTamplate">if set to <c>true</c> [cache tamplate].</param>
		internal NVelocityMemoryEngine(bool cacheTamplate) : base(cacheTamplate)
		{
			this.Init();
		}

		/// <summary>
		/// Processes the specified context.
		/// </summary>
		/// <param name="context">The context.</param>
		/// <param name="template">The template.</param>
		/// <returns></returns>
		public string Process(IDictionary context, string template)
		{
			StringWriter writer = new StringWriter();

			try
			{
				this.Evaluate(CreateContext(context), writer, "mystring", template);
			}
			catch (ParseErrorException pe)
			{
				return pe.Message;
			}
			catch (MethodInvocationException mi)
			{
				return mi.Message;
			}

			return writer.ToString();
		}

		/// <summary>
		/// Processes the specified context.
		/// </summary>
		/// <param name="context">The context.</param>
		/// <param name="writer">The writer.</param>
		/// <param name="template">The template.</param>
		public void Process(IDictionary context, TextWriter writer, string template)
		{
			try
			{
				this.Evaluate(CreateContext(context), writer, "mystring", template);
			}
			catch (ParseErrorException pe)
			{
				writer.Write(pe.Message);
			}
			catch (MethodInvocationException mi)
			{
				writer.Write(mi.Message);
			}
		}
	}
}