/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

  1. using System.IO;
  2. using NVelocity;
  3. using NVelocity.App;
  4. namespace NOS.Registration
  5. {
  6. internal class NVelocityEntryFormatter : IEntryFormatter
  7. {
  8. readonly VelocityEngine _engine;
  9. public NVelocityEntryFormatter()
  10. {
  11. _engine = new VelocityEngine();
  12. _engine.Init();
  13. }
  14. public string FormatUserEntry(User user, ISettings settings, string template)
  15. {
  16. var context = new VelocityContext();
  17. context.Put("user", user);
  18. context.Put("settings", settings);
  19. using (StringWriter writer = new StringWriter())
  20. {
  21. template = PrepareTemplate(template);
  22. _engine.Evaluate(context, writer, null, template);
  23. return writer.ToString();
  24. }
  25. }
  26. static string PrepareTemplate(string value)
  27. {
  28. if (value != null)
  29. {
  30. return value.Replace("\\n", "\n");
  31. }
  32. return value;
  33. }
  34. }
  35. }