From ef7b0da917fc8dd904c0a778d617f161b7e7d467 Mon Sep 17 00:00:00 2001 From: James Date: Sun, 1 Apr 2018 16:29:13 +0800 Subject: [PATCH] enjoy 3.4 --- src/main/java/com/jfinal/template/Engine.java | 15 +- .../java/com/jfinal/template/Template.java | 30 +++ .../java/com/jfinal/template/expr/ast/Id.java | 2 +- .../com/jfinal/template/expr/ast/Map.java | 10 +- .../template/ext/directive/NowDirective.java | 2 +- .../ext/directive/RandomDirective.java | 11 +- .../template/ext/extensionmethod/ByteExt.java | 8 + .../ext/extensionmethod/DoubleExt.java | 8 + .../ext/extensionmethod/FloatExt.java | 8 + .../ext/extensionmethod/IntegerExt.java | 8 + .../template/ext/extensionmethod/LongExt.java | 8 + .../ext/extensionmethod/ShortExt.java | 8 + .../ext/extensionmethod/StringExt.java | 8 + .../template/ext/spring/JFinalView.java | 193 +++++++++--------- .../jfinal/template/source/FileSource.java | 3 + .../com/jfinal/template/stat/ast/Text.java | 2 +- 16 files changed, 215 insertions(+), 109 deletions(-) diff --git a/src/main/java/com/jfinal/template/Engine.java b/src/main/java/com/jfinal/template/Engine.java index 9a3f14a..4f975d9 100644 --- a/src/main/java/com/jfinal/template/Engine.java +++ b/src/main/java/com/jfinal/template/Engine.java @@ -162,6 +162,13 @@ public class Engine { /** * Get template by string content and do not cache the template + */ + public Template getTemplateByString(String content) { + return getTemplateByString(content, false); + } + + /** + * Get template by string content * * 重要:StringSource 中的 key = HashKit.md5(content),也即 key * 与 content 有紧密的对应关系,当 content 发生变化时 key 值也相应变化 @@ -170,13 +177,7 @@ public class Engine { * * 当 getTemplateByString(String, boolean) 中的 String 参数的 * 数量可控并且确定时,才可对其使用缓存 - */ - public Template getTemplateByString(String content) { - return getTemplateByString(content, false); - } - - /** - * Get template by string content + * * @param content 模板内容 * @param cache true 则缓存 Template,否则不缓存 */ diff --git a/src/main/java/com/jfinal/template/Template.java b/src/main/java/com/jfinal/template/Template.java index 80ca56a..7676711 100644 --- a/src/main/java/com/jfinal/template/Template.java +++ b/src/main/java/com/jfinal/template/Template.java @@ -16,6 +16,10 @@ package com.jfinal.template; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; import java.io.OutputStream; import java.io.Writer; import java.util.Map; @@ -108,6 +112,32 @@ public class Template { return fsw.getBuffer(); } + /** + * 渲染到 File 中去 + * 适用于代码生成器类似应用场景 + */ + public void render(Map data, File file) { + FileOutputStream fos = null; + try { + fos = new FileOutputStream(file); + render(data, fos); + } catch (FileNotFoundException e) { + throw new RuntimeException(e); + } finally { + if (fos != null) { + try {fos.close();} catch (IOException e) {e.printStackTrace(System.err);} + } + } + } + + /** + * 渲染到 String fileName 参数所指定的文件中去 + * 适用于代码生成器类似应用场景 + */ + public void render(Map data, String fileName) { + render(data, new File(fileName)); + } + public boolean isModified() { return env.isSourceListModified(); } diff --git a/src/main/java/com/jfinal/template/expr/ast/Id.java b/src/main/java/com/jfinal/template/expr/ast/Id.java index a3f7738..9ce0ed8 100644 --- a/src/main/java/com/jfinal/template/expr/ast/Id.java +++ b/src/main/java/com/jfinal/template/expr/ast/Id.java @@ -23,7 +23,7 @@ import com.jfinal.template.stat.Scope; */ public class Id extends Expr { - private String id; + private final String id; public Id(String id) { this.id = id; diff --git a/src/main/java/com/jfinal/template/expr/ast/Map.java b/src/main/java/com/jfinal/template/expr/ast/Map.java index 21dcd4e..00ab527 100644 --- a/src/main/java/com/jfinal/template/expr/ast/Map.java +++ b/src/main/java/com/jfinal/template/expr/ast/Map.java @@ -24,8 +24,10 @@ import com.jfinal.template.stat.Scope; * Map * * 1:定义 map 常量 - * {k1:123, k2:"abc", 'k3':true, "k4":[1,2,3], k5:1+2} - * 如上所示,map定义的 key 可以为 String 或者 id 标识符,而右侧的 value 可以是任意的常量与表达式 + * {k1:123, k2:"abc", 'k3':true, "k4":[1,2,3], k5:1+2, 1:12, true:"Y", null:"abc"} + * 如上所示,map定义的 key 可以为 id 标识符或者 String、Integer、Long、Boolean、null + * 等常量值 (详见 ExprParser.buildMapEntry(...) 方法), + * 右侧的 value 可以是任意的常量与表达式 * * 2:取值 * 先将 Map 常量赋值给某个变量: #set(map = {...}) @@ -34,6 +36,10 @@ import com.jfinal.template.stat.Scope; * map[expr] * map.get("k1") * map.k1 + * + * 3:不通过中间变量取值 + * {1:'自买', 2:'跟买'}.get(1) + * {1:'自买', 2:'跟买'}[2] * * 如上所示,当以下标方式取值时,下标参数可以是 string 与 expr,而 expr 求值以后的值必须也为 string类型 * 当用 map.k1 这类 field 字段取值形式时,则是使用 id 标识符,而不是 string 形参数 diff --git a/src/main/java/com/jfinal/template/ext/directive/NowDirective.java b/src/main/java/com/jfinal/template/ext/directive/NowDirective.java index f3e6474..f6bf1f4 100644 --- a/src/main/java/com/jfinal/template/ext/directive/NowDirective.java +++ b/src/main/java/com/jfinal/template/ext/directive/NowDirective.java @@ -32,7 +32,7 @@ import com.jfinal.template.stat.Scope; */ public class NowDirective extends Directive { - public void setExrpList(ExprList exprList) { + public void setExprList(ExprList exprList) { if (exprList.length() > 1) { throw new ParseException("#now directive support one parameter only", location); } diff --git a/src/main/java/com/jfinal/template/ext/directive/RandomDirective.java b/src/main/java/com/jfinal/template/ext/directive/RandomDirective.java index 35fe43a..21f857f 100644 --- a/src/main/java/com/jfinal/template/ext/directive/RandomDirective.java +++ b/src/main/java/com/jfinal/template/ext/directive/RandomDirective.java @@ -16,22 +16,29 @@ package com.jfinal.template.ext.directive; +import java.io.IOException; import com.jfinal.template.Directive; import com.jfinal.template.Env; +import com.jfinal.template.TemplateException; import com.jfinal.template.io.Writer; import com.jfinal.template.stat.Scope; /** - * 输出随机数 + * 输出 int 型随机数 */ public class RandomDirective extends Directive { private java.util.Random random = new java.util.Random(); public void exec(Env env, Scope scope, Writer writer) { - write(writer, String.valueOf(random.nextInt())); + try { + writer.write(random.nextInt()); + } catch (IOException e) { + throw new TemplateException(e.getMessage(), location, e); + } } } + diff --git a/src/main/java/com/jfinal/template/ext/extensionmethod/ByteExt.java b/src/main/java/com/jfinal/template/ext/extensionmethod/ByteExt.java index 0940ebf..cce6bda 100644 --- a/src/main/java/com/jfinal/template/ext/extensionmethod/ByteExt.java +++ b/src/main/java/com/jfinal/template/ext/extensionmethod/ByteExt.java @@ -43,6 +43,14 @@ public class ByteExt { public Double toDouble(Byte self) { return self.doubleValue(); } + + public Short toShort(Byte self) { + return self.shortValue(); + } + + public Byte toByte(Byte self) { + return self; + } } diff --git a/src/main/java/com/jfinal/template/ext/extensionmethod/DoubleExt.java b/src/main/java/com/jfinal/template/ext/extensionmethod/DoubleExt.java index f15707d..cb085ef 100644 --- a/src/main/java/com/jfinal/template/ext/extensionmethod/DoubleExt.java +++ b/src/main/java/com/jfinal/template/ext/extensionmethod/DoubleExt.java @@ -43,6 +43,14 @@ public class DoubleExt { public Double toDouble(Double self) { return self; } + + public Short toShort(Double self) { + return self.shortValue(); + } + + public Byte toByte(Double self) { + return self.byteValue(); + } } diff --git a/src/main/java/com/jfinal/template/ext/extensionmethod/FloatExt.java b/src/main/java/com/jfinal/template/ext/extensionmethod/FloatExt.java index 1380df8..f844d53 100644 --- a/src/main/java/com/jfinal/template/ext/extensionmethod/FloatExt.java +++ b/src/main/java/com/jfinal/template/ext/extensionmethod/FloatExt.java @@ -43,6 +43,14 @@ public class FloatExt { public Double toDouble(Float self) { return self.doubleValue(); } + + public Short toShort(Float self) { + return self.shortValue(); + } + + public Byte toByte(Float self) { + return self.byteValue(); + } } diff --git a/src/main/java/com/jfinal/template/ext/extensionmethod/IntegerExt.java b/src/main/java/com/jfinal/template/ext/extensionmethod/IntegerExt.java index 086790e..f7a4b35 100644 --- a/src/main/java/com/jfinal/template/ext/extensionmethod/IntegerExt.java +++ b/src/main/java/com/jfinal/template/ext/extensionmethod/IntegerExt.java @@ -63,6 +63,14 @@ public class IntegerExt { public Double toDouble(Integer self) { return self.doubleValue(); } + + public Short toShort(Integer self) { + return self.shortValue(); + } + + public Byte toByte(Integer self) { + return self.byteValue(); + } } diff --git a/src/main/java/com/jfinal/template/ext/extensionmethod/LongExt.java b/src/main/java/com/jfinal/template/ext/extensionmethod/LongExt.java index 3870090..e65bc2f 100644 --- a/src/main/java/com/jfinal/template/ext/extensionmethod/LongExt.java +++ b/src/main/java/com/jfinal/template/ext/extensionmethod/LongExt.java @@ -43,6 +43,14 @@ public class LongExt { public Double toDouble(Long self) { return self.doubleValue(); } + + public Short toShort(Long self) { + return self.shortValue(); + } + + public Byte toByte(Long self) { + return self.byteValue(); + } } diff --git a/src/main/java/com/jfinal/template/ext/extensionmethod/ShortExt.java b/src/main/java/com/jfinal/template/ext/extensionmethod/ShortExt.java index 0111b4c..2251e4a 100644 --- a/src/main/java/com/jfinal/template/ext/extensionmethod/ShortExt.java +++ b/src/main/java/com/jfinal/template/ext/extensionmethod/ShortExt.java @@ -43,6 +43,14 @@ public class ShortExt { public Double toDouble(Short self) { return self.doubleValue(); } + + public Short toShort(Short self) { + return self; + } + + public Byte toByte(Short self) { + return self.byteValue(); + } } diff --git a/src/main/java/com/jfinal/template/ext/extensionmethod/StringExt.java b/src/main/java/com/jfinal/template/ext/extensionmethod/StringExt.java index 23d0a49..1e8fd3f 100644 --- a/src/main/java/com/jfinal/template/ext/extensionmethod/StringExt.java +++ b/src/main/java/com/jfinal/template/ext/extensionmethod/StringExt.java @@ -79,6 +79,14 @@ public class StringExt { public Double toDouble(String self) { return StrKit.isBlank(self) ? null : Double.parseDouble(self); } + + public Short toShort(String self) { + return StrKit.isBlank(self) ? null : Short.parseShort(self); + } + + public Byte toByte(String self) { + return StrKit.isBlank(self) ? null : Byte.parseByte(self); + } } diff --git a/src/main/java/com/jfinal/template/ext/spring/JFinalView.java b/src/main/java/com/jfinal/template/ext/spring/JFinalView.java index ec50add..a128897 100644 --- a/src/main/java/com/jfinal/template/ext/spring/JFinalView.java +++ b/src/main/java/com/jfinal/template/ext/spring/JFinalView.java @@ -59,101 +59,105 @@ public class JFinalView extends AbstractTemplateView { OutputStream os = response.getOutputStream(); JFinalViewResolver.engine.getTemplate(getUrl()).render(model, os); } -} -@SuppressWarnings({"unchecked", "rawtypes", "deprecation"}) -class InnerSession extends HashMap implements HttpSession { - - private static final long serialVersionUID = -8679493647540628009L; - private HttpSession session; - - public InnerSession(HttpSession session) { - this.session = session; - } - - // HashMap 相关方法处理 ---------------------------------------------------- - /** - * 覆盖 HashMap 的 put - */ - public Object put(Object name, Object value) { - session.setAttribute((String)name, value); - return null; - } - - /** - * 覆盖 HashMap 的 get - */ - public Object get(Object name) { - return session.getAttribute((String)name); - } - - // Session 相关方法处理 ---------------------------------------------------- - public Object getAttribute(String key) { - return session.getAttribute(key); - } - - public Enumeration getAttributeNames() { - return session.getAttributeNames(); - } - - public long getCreationTime() { - return session.getCreationTime(); - } - - public String getId() { - return session.getId(); - } - - public long getLastAccessedTime() { - return session.getLastAccessedTime(); - } - - public int getMaxInactiveInterval() { - return session.getMaxInactiveInterval(); - } - - public ServletContext getServletContext() { - return session.getServletContext(); - } - - public javax.servlet.http.HttpSessionContext getSessionContext() { - return session.getSessionContext(); - } - - public Object getValue(String key) { - return session.getValue(key); - } - - public String[] getValueNames() { - return session.getValueNames(); - } - - public void invalidate() { - session.invalidate(); - } - - public boolean isNew() { - return session.isNew(); - } - - public void putValue(String key, Object value) { - session.putValue(key, value); - } - - public void removeAttribute(String key) { - session.removeAttribute(key); - } - - public void removeValue(String key) { - session.removeValue(key); - } - - public void setAttribute(String key, Object value) { - session.setAttribute(key, value); - } - - public void setMaxInactiveInterval(int maxInactiveInterval) { - session.setMaxInactiveInterval(maxInactiveInterval); + @SuppressWarnings({"unchecked", "rawtypes", "deprecation"}) + public static class InnerSession extends HashMap implements HttpSession { + + private static final long serialVersionUID = -8679493647540628009L; + private HttpSession session; + + public InnerSession(HttpSession session) { + this.session = session; + } + + // HashMap 相关方法处理 ---------------------------------------------------- + /** + * 覆盖 HashMap 的 put + */ + public Object put(Object name, Object value) { + session.setAttribute((String)name, value); + return null; + } + + /** + * 覆盖 HashMap 的 get + */ + public Object get(Object name) { + return session.getAttribute((String)name); + } + + // Session 相关方法处理 ---------------------------------------------------- + public Object getAttribute(String key) { + return session.getAttribute(key); + } + + public Enumeration getAttributeNames() { + return session.getAttributeNames(); + } + + public long getCreationTime() { + return session.getCreationTime(); + } + + public String getId() { + return session.getId(); + } + + public long getLastAccessedTime() { + return session.getLastAccessedTime(); + } + + public int getMaxInactiveInterval() { + return session.getMaxInactiveInterval(); + } + + public ServletContext getServletContext() { + return session.getServletContext(); + } + + public javax.servlet.http.HttpSessionContext getSessionContext() { + return session.getSessionContext(); + } + + public Object getValue(String key) { + return session.getValue(key); + } + + public String[] getValueNames() { + return session.getValueNames(); + } + + public void invalidate() { + session.invalidate(); + } + + public boolean isNew() { + return session.isNew(); + } + + public void putValue(String key, Object value) { + session.putValue(key, value); + } + + public void removeAttribute(String key) { + session.removeAttribute(key); + } + + public void removeValue(String key) { + session.removeValue(key); + } + + public void setAttribute(String key, Object value) { + session.setAttribute(key, value); + } + + public void setMaxInactiveInterval(int maxInactiveInterval) { + session.setMaxInactiveInterval(maxInactiveInterval); + } + + public String toString() { + return session != null ? session.toString() : "null"; + } } } @@ -162,4 +166,3 @@ class InnerSession extends HashMap implements HttpSession { - diff --git a/src/main/java/com/jfinal/template/source/FileSource.java b/src/main/java/com/jfinal/template/source/FileSource.java index 8331242..1554536 100644 --- a/src/main/java/com/jfinal/template/source/FileSource.java +++ b/src/main/java/com/jfinal/template/source/FileSource.java @@ -77,6 +77,9 @@ public class FileSource implements ISource { } private String buildFinalFileName(String baseTemplatePath, String fileName) { + if (baseTemplatePath == null) { + return fileName; + } char firstChar = fileName.charAt(0); String finalFileName; if (firstChar == '/' || firstChar == '\\') { diff --git a/src/main/java/com/jfinal/template/stat/ast/Text.java b/src/main/java/com/jfinal/template/stat/ast/Text.java index bc78ecb..4ecf2f6 100644 --- a/src/main/java/com/jfinal/template/stat/ast/Text.java +++ b/src/main/java/com/jfinal/template/stat/ast/Text.java @@ -25,7 +25,7 @@ import com.jfinal.template.io.Writer; import com.jfinal.template.stat.Scope; /** - * Text 输出纯文本块以及使用 "#[[" 与 "]]#" 指定的非解析块 + * Text 输出纯文本块以及使用 "#[[" 与 "]]#" 定义的原样输出块 */ public class Text extends Stat implements IWritable {