|
|
@@ -1,5 +1,16 @@
|
|
|
package cn.hutool.extra.template.engine.velocity;
|
|
|
|
|
|
+import cn.hutool.core.exceptions.NotInitedException;
|
|
|
+import cn.hutool.core.exceptions.UtilException;
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
+import cn.hutool.core.io.IORuntimeException;
|
|
|
+import cn.hutool.core.io.IoUtil;
|
|
|
+import cn.hutool.core.util.IdUtil;
|
|
|
+import org.apache.velocity.Template;
|
|
|
+import org.apache.velocity.VelocityContext;
|
|
|
+import org.apache.velocity.app.Velocity;
|
|
|
+import org.apache.velocity.app.VelocityEngine;
|
|
|
+
|
|
|
import java.io.PrintWriter;
|
|
|
import java.io.StringWriter;
|
|
|
import java.io.Writer;
|
|
|
@@ -9,37 +20,30 @@ import java.util.Map;
|
|
|
import java.util.Map.Entry;
|
|
|
import java.util.Properties;
|
|
|
|
|
|
-import org.apache.velocity.Template;
|
|
|
-import org.apache.velocity.VelocityContext;
|
|
|
-import org.apache.velocity.app.Velocity;
|
|
|
-import org.apache.velocity.app.VelocityEngine;
|
|
|
-
|
|
|
-import cn.hutool.core.exceptions.NotInitedException;
|
|
|
-import cn.hutool.core.exceptions.UtilException;
|
|
|
-import cn.hutool.core.io.FileUtil;
|
|
|
-import cn.hutool.core.io.IORuntimeException;
|
|
|
-import cn.hutool.core.io.IoUtil;
|
|
|
-import cn.hutool.core.util.IdUtil;
|
|
|
-
|
|
|
/**
|
|
|
* Velocity模板引擎工具类<br>
|
|
|
* 使用前必须初始化工具类
|
|
|
- *
|
|
|
+ *
|
|
|
* @author xiaoleilu
|
|
|
- *
|
|
|
+ * @deprecated 使用TemplateUtil替代
|
|
|
*/
|
|
|
+@Deprecated
|
|
|
public class VelocityUtil {
|
|
|
|
|
|
- /** 是否初始化了默认引擎 */
|
|
|
+ /**
|
|
|
+ * 是否初始化了默认引擎
|
|
|
+ */
|
|
|
private static boolean isInited;
|
|
|
- /** 全局上下文,当设定值时,对于每个模板都有效 */
|
|
|
+ /**
|
|
|
+ * 全局上下文,当设定值时,对于每个模板都有效
|
|
|
+ */
|
|
|
private static Map<String, Object> globalContext = new HashMap<>();
|
|
|
|
|
|
/**
|
|
|
* 设置Velocity全局上下文<br>
|
|
|
* 当设定值时,对于每个模板都有效
|
|
|
- *
|
|
|
- * @param name 名
|
|
|
+ *
|
|
|
+ * @param name 名
|
|
|
* @param value 值
|
|
|
*/
|
|
|
public void putGlobalContext(String name, Object value) {
|
|
|
@@ -48,9 +52,9 @@ public class VelocityUtil {
|
|
|
|
|
|
/**
|
|
|
* 初始化Velocity全局属性
|
|
|
- *
|
|
|
+ *
|
|
|
* @param templateDir 模板所在目录,绝对路径
|
|
|
- * @param charset 编码
|
|
|
+ * @param charset 编码
|
|
|
*/
|
|
|
synchronized public static void init(String templateDir, String charset) {
|
|
|
Velocity.init(_newInitedProp(templateDir, charset));
|
|
|
@@ -61,9 +65,9 @@ public class VelocityUtil {
|
|
|
|
|
|
/**
|
|
|
* 初始化全局属性
|
|
|
- *
|
|
|
- * @param templateDir 模板目录
|
|
|
- * @param charset 字符集编码
|
|
|
+ *
|
|
|
+ * @param templateDir 模板目录
|
|
|
+ * @param charset 字符集编码
|
|
|
* @param initedGlobalContext 初始的全局上下文
|
|
|
*/
|
|
|
public static void init(String templateDir, String charset, Map<String, Object> initedGlobalContext) {
|
|
|
@@ -73,9 +77,9 @@ public class VelocityUtil {
|
|
|
|
|
|
/**
|
|
|
* 新建Velocity模板引擎
|
|
|
- *
|
|
|
+ *
|
|
|
* @param templateDir 模板所在目录,绝对路径
|
|
|
- * @param charset 编码
|
|
|
+ * @param charset 编码
|
|
|
* @return VelocityEngine
|
|
|
*/
|
|
|
public static VelocityEngine newEngine(String templateDir, String charset) {
|
|
|
@@ -89,11 +93,11 @@ public class VelocityUtil {
|
|
|
|
|
|
/**
|
|
|
* 获得指定模板填充后的内容
|
|
|
- *
|
|
|
- * @param templateDir 模板所在目录,绝对路径
|
|
|
+ *
|
|
|
+ * @param templateDir 模板所在目录,绝对路径
|
|
|
* @param templateFileName 模板名称
|
|
|
- * @param context 上下文(变量值的容器)
|
|
|
- * @param charset 字符集
|
|
|
+ * @param context 上下文(变量值的容器)
|
|
|
+ * @param charset 字符集
|
|
|
* @return 模板和内容匹配后的内容
|
|
|
*/
|
|
|
public static String getContent(String templateDir, String templateFileName, VelocityContext context, String charset) {
|
|
|
@@ -105,10 +109,10 @@ public class VelocityUtil {
|
|
|
|
|
|
/**
|
|
|
* 获得指定模板填充后的内容
|
|
|
- *
|
|
|
- * @param ve 模板引擎
|
|
|
+ *
|
|
|
+ * @param ve 模板引擎
|
|
|
* @param templateFileName 模板名称
|
|
|
- * @param context 上下文(变量值的容器)
|
|
|
+ * @param context 上下文(变量值的容器)
|
|
|
* @return 模板和内容匹配后的内容
|
|
|
*/
|
|
|
public static String getContent(VelocityEngine ve, String templateFileName, VelocityContext context) {
|
|
|
@@ -119,9 +123,9 @@ public class VelocityUtil {
|
|
|
|
|
|
/**
|
|
|
* 获得指定模板填充后的内容,使用默认引擎
|
|
|
- *
|
|
|
+ *
|
|
|
* @param templateFileName 模板文件
|
|
|
- * @param context 上下文(变量值的容器)
|
|
|
+ * @param context 上下文(变量值的容器)
|
|
|
* @return 模板和内容匹配后的内容
|
|
|
*/
|
|
|
public static String getContent(String templateFileName, VelocityContext context) {
|
|
|
@@ -132,11 +136,11 @@ public class VelocityUtil {
|
|
|
|
|
|
/**
|
|
|
* 生成文件
|
|
|
- *
|
|
|
- * @param ve 模板引擎
|
|
|
+ *
|
|
|
+ * @param ve 模板引擎
|
|
|
* @param templateFileName 模板文件名
|
|
|
- * @param context 上下文
|
|
|
- * @param destPath 目标路径(绝对)
|
|
|
+ * @param context 上下文
|
|
|
+ * @param destPath 目标路径(绝对)
|
|
|
*/
|
|
|
public static void toFile(VelocityEngine ve, String templateFileName, VelocityContext context, String destPath) {
|
|
|
toFile(ve.getTemplate(templateFileName), context, destPath);
|
|
|
@@ -144,10 +148,10 @@ public class VelocityUtil {
|
|
|
|
|
|
/**
|
|
|
* 生成文件,使用默认引擎
|
|
|
- *
|
|
|
+ *
|
|
|
* @param templateFileName 模板文件名
|
|
|
- * @param context 模板上下文
|
|
|
- * @param destPath 目标路径(绝对)
|
|
|
+ * @param context 模板上下文
|
|
|
+ * @param destPath 目标路径(绝对)
|
|
|
*/
|
|
|
public static void toFile(String templateFileName, VelocityContext context, String destPath) {
|
|
|
assertInit();
|
|
|
@@ -157,9 +161,9 @@ public class VelocityUtil {
|
|
|
|
|
|
/**
|
|
|
* 生成文件
|
|
|
- *
|
|
|
+ *
|
|
|
* @param template 模板
|
|
|
- * @param context 模板上下文
|
|
|
+ * @param context 模板上下文
|
|
|
* @param destPath 目标路径(绝对)
|
|
|
*/
|
|
|
public static void toFile(Template template, VelocityContext context, String destPath) {
|
|
|
@@ -177,11 +181,11 @@ public class VelocityUtil {
|
|
|
/**
|
|
|
* 生成内容写入流<br>
|
|
|
* 会自动关闭Writer
|
|
|
- *
|
|
|
- * @param ve 引擎
|
|
|
+ *
|
|
|
+ * @param ve 引擎
|
|
|
* @param templateFileName 模板文件名
|
|
|
- * @param context 上下文
|
|
|
- * @param writer 流
|
|
|
+ * @param context 上下文
|
|
|
+ * @param writer 流
|
|
|
*/
|
|
|
public static void toWriter(VelocityEngine ve, String templateFileName, VelocityContext context, Writer writer) {
|
|
|
final Template template = ve.getTemplate(templateFileName);
|
|
|
@@ -191,10 +195,10 @@ public class VelocityUtil {
|
|
|
/**
|
|
|
* 生成内容写入流<br>
|
|
|
* 会自动关闭Writer
|
|
|
- *
|
|
|
+ *
|
|
|
* @param templateFileName 模板文件名
|
|
|
- * @param context 上下文
|
|
|
- * @param writer 流
|
|
|
+ * @param context 上下文
|
|
|
+ * @param writer 流
|
|
|
*/
|
|
|
public static void toWriter(String templateFileName, VelocityContext context, Writer writer) {
|
|
|
assertInit();
|
|
|
@@ -206,10 +210,10 @@ public class VelocityUtil {
|
|
|
/**
|
|
|
* 生成内容写到响应内容中<br>
|
|
|
* 模板的变量来自于Request的Attribute对象
|
|
|
- *
|
|
|
+ *
|
|
|
* @param templateFileName 模板文件
|
|
|
- * @param request 请求对象,用于获取模板中的变量值
|
|
|
- * @param response 响应对象
|
|
|
+ * @param request 请求对象,用于获取模板中的变量值
|
|
|
+ * @param response 响应对象
|
|
|
*/
|
|
|
public static void toWriter(String templateFileName, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) {
|
|
|
final VelocityContext context = new VelocityContext();
|
|
|
@@ -229,9 +233,9 @@ public class VelocityUtil {
|
|
|
|
|
|
/**
|
|
|
* 融合模板和内容
|
|
|
- *
|
|
|
+ *
|
|
|
* @param templateContent 模板的内容字符串
|
|
|
- * @param context 上下文
|
|
|
+ * @param context 上下文
|
|
|
* @return 模板和内容匹配后的内容
|
|
|
*/
|
|
|
public static String merge(String templateContent, VelocityContext context) {
|
|
|
@@ -246,10 +250,10 @@ public class VelocityUtil {
|
|
|
|
|
|
/**
|
|
|
* 融合模板和内容并写入到Writer
|
|
|
- *
|
|
|
+ *
|
|
|
* @param template 模板
|
|
|
- * @param context 内容
|
|
|
- * @param writer Writer
|
|
|
+ * @param context 内容
|
|
|
+ * @param writer Writer
|
|
|
*/
|
|
|
public static void merge(Template template, VelocityContext context, Writer writer) {
|
|
|
if (template == null) {
|
|
|
@@ -270,7 +274,7 @@ public class VelocityUtil {
|
|
|
/**
|
|
|
* 将Request中的数据转换为模板引擎<br>
|
|
|
* 取值包括Session和Request
|
|
|
- *
|
|
|
+ *
|
|
|
* @param context 内容
|
|
|
* @param request 请求对象
|
|
|
* @return VelocityContext
|
|
|
@@ -289,7 +293,7 @@ public class VelocityUtil {
|
|
|
|
|
|
/**
|
|
|
* 将Session中的值放入模板上下文
|
|
|
- *
|
|
|
+ *
|
|
|
* @param context 模板上下文
|
|
|
* @param session Session
|
|
|
* @return VelocityContext
|
|
|
@@ -309,9 +313,10 @@ public class VelocityUtil {
|
|
|
}
|
|
|
|
|
|
// -------------------------------------------------------------------------- Private method start
|
|
|
+
|
|
|
/**
|
|
|
* 新建一个初始化后的属性对象
|
|
|
- *
|
|
|
+ *
|
|
|
* @param templateDir 模板所在目录
|
|
|
* @return 初始化后的属性对象
|
|
|
*/
|