PageRenderTime 291ms CodeModel.GetById 283ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/source/app/NOS.Registration/NVelocityEntryFormatter.cs

http://github.com/agross/netopenspace
C# | 43 lines | 35 code | 8 blank | 0 comment | 2 complexity | b47d78b726fee996ea75e22eb6e5f4bc MD5 | raw file
 1using System.IO;
 2
 3using NVelocity;
 4using NVelocity.App;
 5
 6namespace NOS.Registration
 7{
 8	internal class NVelocityEntryFormatter : IEntryFormatter
 9	{
10		readonly VelocityEngine _engine;
11
12		public NVelocityEntryFormatter()
13		{
14			_engine = new VelocityEngine();
15			_engine.Init();
16		}
17
18		public string FormatUserEntry(User user, ISettings settings, string template)
19		{
20			var context = new VelocityContext();
21			context.Put("user", user);
22			context.Put("settings", settings);
23
24			using (StringWriter writer = new StringWriter())
25			{
26				template = PrepareTemplate(template);
27				_engine.Evaluate(context, writer, null, template);
28
29				return writer.ToString();
30			}
31		}
32
33		static string PrepareTemplate(string value)
34		{
35			if (value != null)
36			{
37				return value.Replace("\\n", "\n");
38			}
39
40			return value;
41		}
42	}
43}