miniSql

创建
zgc123@gmail.com authored at 11/19/2023 1:40:15 AM
6136600
Tree
0 Parent(s)
Summary: 1 changed files with 57 additions and 0 deletions.
Added +57 -0
Added +57 -0
diff --git a/minisqlquery-master/src/MiniSqlQuery.Core/Template/NVelocityWrapper.cs b/minisqlquery-master/src/MiniSqlQuery.Core/Template/NVelocityWrapper.cs
new file mode 100644
index 0000000..bce56d2
--- /dev/null
+++ b/minisqlquery-master/src/MiniSqlQuery.Core/Template/NVelocityWrapper.cs
@@ -0,0 +1,57 @@
+#region License
+
+// Copyright 2005-2019 Paul Kohler (https://github.com/paulkohler/minisqlquery). All rights reserved.
+// This source code is made available under the terms of the GNU Lesser General Public License v3.0
+// https://github.com/paulkohler/minisqlquery/blob/master/LICENSE
+#endregion
+
+using System.Collections.Generic;
+using System.IO;
+using NVelocity;
+using NVelocity.App;
+using NVelocity.Exception;
+
+namespace MiniSqlQuery.Core.Template
+{
+    /// <summary>The n velocity wrapper.</summary>
+    public class NVelocityWrapper : ITextFormatter
+    {
+        /// <summary>The format.</summary>
+        /// <param name="text">The text.</param>
+        /// <param name="items">The items.</param>
+        /// <returns>The format.</returns>
+        /// <exception cref="TemplateException"></exception>
+        public string Format(string text, Dictionary<string, object> items)
+        {
+            try
+            {
+                VelocityContext velocityContext = new VelocityContext();
+
+                if (items != null)
+                {
+                    foreach (var pair in items)
+                    {
+                        velocityContext.Put(pair.Key, pair.Value);
+                    }
+                }
+
+                StringWriter sw = new StringWriter();
+                VelocityEngine velocityEngine = new VelocityEngine();
+                velocityEngine.Init();
+
+                bool ok = velocityEngine.Evaluate(velocityContext, sw, "ContextTest.CaseInsensitive", text);
+
+                if (!ok)
+                {
+                    throw new TemplateException("Template run error (try adding an extra newline at the end of the file)");
+                }
+
+                return sw.ToString();
+            }
+            catch (ParseErrorException parseErrorException)
+            {
+                throw new TemplateException(parseErrorException.Message, parseErrorException);
+            }
+        }
+    }
+}
\ No newline at end of file