93 lines
3.1 KiB
C#
93 lines
3.1 KiB
C#
using System;
|
|
using System.IO;
|
|
using Commons.Collections;
|
|
using NVelocity;
|
|
using NVelocity.App;
|
|
using NVelocity.Runtime.Directive;
|
|
using NVelocity.Runtime.Resource;
|
|
using NVelocity.Runtime.Resource.Loader;
|
|
using Taloyhtio.GeneralApi.Common;
|
|
using Taloyhtio.GeneralApi.Common.Extensions;
|
|
using Taloyhtio.GeneralApi.Core.Common;
|
|
|
|
namespace Taloyhtio.GeneralApi.Core.Services.Impl
|
|
{
|
|
public class TemplateEngine : ITemplateEngine
|
|
{
|
|
public string Merge(string templateText, params Pair<string, object>[] args)
|
|
{
|
|
if (string.IsNullOrEmpty(templateText))
|
|
{
|
|
return templateText;
|
|
}
|
|
|
|
var context = new VelocityContext();
|
|
if (!args.IsNullOrEmpty())
|
|
{
|
|
foreach (var t in args)
|
|
{
|
|
context.Put(t.Item1, t.Item2);
|
|
}
|
|
}
|
|
|
|
var velocity = new VelocityEngine();
|
|
var props = new ExtendedProperties();
|
|
// string templateFolder = "";
|
|
// props.AddProperty("file.resource.loader.path",
|
|
// templateFolder);
|
|
props.AddProperty("resource.manager.class",
|
|
typeof(ResourceManagerImpl).AssemblyQualifiedName);
|
|
props.AddProperty("file.resource.loader.class",
|
|
typeof(FileResourceLoader).AssemblyQualifiedName);
|
|
props.AddProperty("directive.manager",
|
|
typeof(DirectiveManager).AssemblyQualifiedName);
|
|
velocity.Init(props);
|
|
using (var sw = new StringWriter())
|
|
{
|
|
velocity.Evaluate(context, sw, "", templateText);
|
|
sw.Flush();
|
|
return sw.ToString();
|
|
}
|
|
}
|
|
|
|
public string Merge(string templateFileName, string templateFolder, params Pair<string, object>[] args)
|
|
{
|
|
if (string.IsNullOrEmpty(templateFileName) || string.IsNullOrEmpty(templateFolder))
|
|
{
|
|
return string.Empty;
|
|
}
|
|
|
|
var context = new VelocityContext();
|
|
if (!args.IsNullOrEmpty())
|
|
{
|
|
foreach (var t in args)
|
|
{
|
|
context.Put(t.Item1, t.Item2);
|
|
}
|
|
}
|
|
|
|
var velocity = new VelocityEngine();
|
|
var props = new ExtendedProperties();
|
|
props.AddProperty("file.resource.loader.path",
|
|
templateFolder);
|
|
props.AddProperty("resource.manager.class",
|
|
typeof(ResourceManagerImpl).AssemblyQualifiedName);
|
|
props.AddProperty("file.resource.loader.class",
|
|
typeof(FileResourceLoader).AssemblyQualifiedName);
|
|
props.AddProperty("directive.manager",
|
|
typeof(DirectiveManager).AssemblyQualifiedName);
|
|
velocity.Init(props);
|
|
|
|
var template = velocity.GetTemplate(templateFileName);
|
|
|
|
using (var sw = new StringWriter())
|
|
{
|
|
template.Merge(context, sw);
|
|
sw.Flush();
|
|
return sw.ToString();
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|