enjoy 3.4

This commit is contained in:
James 2018-04-01 16:29:13 +08:00
parent 2ed806c296
commit ef7b0da917
16 changed files with 215 additions and 109 deletions

View File

@ -162,6 +162,13 @@ public class Engine {
/** /**
* Get template by string content and do not cache the template * 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 * 重要StringSource 中的 key = HashKit.md5(content)也即 key
* content 有紧密的对应关系 content 发生变化时 key 值也相应变化 * content 有紧密的对应关系 content 发生变化时 key 值也相应变化
@ -170,13 +177,7 @@ public class Engine {
* *
* getTemplateByString(String, boolean) 中的 String 参数的 * getTemplateByString(String, boolean) 中的 String 参数的
* 数量可控并且确定时才可对其使用缓存 * 数量可控并且确定时才可对其使用缓存
*/ *
public Template getTemplateByString(String content) {
return getTemplateByString(content, false);
}
/**
* Get template by string content
* @param content 模板内容 * @param content 模板内容
* @param cache true 则缓存 Template否则不缓存 * @param cache true 则缓存 Template否则不缓存
*/ */

View File

@ -16,6 +16,10 @@
package com.jfinal.template; 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.OutputStream;
import java.io.Writer; import java.io.Writer;
import java.util.Map; import java.util.Map;
@ -108,6 +112,32 @@ public class Template {
return fsw.getBuffer(); 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() { public boolean isModified() {
return env.isSourceListModified(); return env.isSourceListModified();
} }

View File

@ -23,7 +23,7 @@ import com.jfinal.template.stat.Scope;
*/ */
public class Id extends Expr { public class Id extends Expr {
private String id; private final String id;
public Id(String id) { public Id(String id) {
this.id = id; this.id = id;

View File

@ -24,8 +24,10 @@ import com.jfinal.template.stat.Scope;
* Map * Map
* *
* 1定义 map 常量 * 1定义 map 常量
* {k1:123, k2:"abc", 'k3':true, "k4":[1,2,3], k5:1+2} * {k1:123, k2:"abc", 'k3':true, "k4":[1,2,3], k5:1+2, 1:12, true:"Y", null:"abc"}
* 如上所示map定义的 key 可以为 String 或者 id 标识符而右侧的 value 可以是任意的常量与表达式 * 如上所示map定义的 key 可以为 id 标识符或者 StringIntegerLongBooleannull
* 等常量值 (详见 ExprParser.buildMapEntry(...) 方法)
* 右侧的 value 可以是任意的常量与表达式
* *
* 2取值 * 2取值
* 先将 Map 常量赋值给某个变量 #set(map = {...}) * 先将 Map 常量赋值给某个变量 #set(map = {...})
@ -34,6 +36,10 @@ import com.jfinal.template.stat.Scope;
* map[expr] * map[expr]
* map.get("k1") * map.get("k1")
* map.k1 * map.k1
*
* 3不通过中间变量取值
* {1:'自买', 2:'跟买'}.get(1)
* {1:'自买', 2:'跟买'}[2]
* *
* 如上所示当以下标方式取值时下标参数可以是 string expr expr 求值以后的值必须也为 string类型 * 如上所示当以下标方式取值时下标参数可以是 string expr expr 求值以后的值必须也为 string类型
* 当用 map.k1 这类 field 字段取值形式时则是使用 id 标识符而不是 string 形参数 * 当用 map.k1 这类 field 字段取值形式时则是使用 id 标识符而不是 string 形参数

View File

@ -32,7 +32,7 @@ import com.jfinal.template.stat.Scope;
*/ */
public class NowDirective extends Directive { public class NowDirective extends Directive {
public void setExrpList(ExprList exprList) { public void setExprList(ExprList exprList) {
if (exprList.length() > 1) { if (exprList.length() > 1) {
throw new ParseException("#now directive support one parameter only", location); throw new ParseException("#now directive support one parameter only", location);
} }

View File

@ -16,22 +16,29 @@
package com.jfinal.template.ext.directive; package com.jfinal.template.ext.directive;
import java.io.IOException;
import com.jfinal.template.Directive; import com.jfinal.template.Directive;
import com.jfinal.template.Env; import com.jfinal.template.Env;
import com.jfinal.template.TemplateException;
import com.jfinal.template.io.Writer; import com.jfinal.template.io.Writer;
import com.jfinal.template.stat.Scope; import com.jfinal.template.stat.Scope;
/** /**
* 输出随机数 * 输出 int 随机数
*/ */
public class RandomDirective extends Directive { public class RandomDirective extends Directive {
private java.util.Random random = new java.util.Random(); private java.util.Random random = new java.util.Random();
public void exec(Env env, Scope scope, Writer writer) { 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);
}
} }
} }

View File

@ -43,6 +43,14 @@ public class ByteExt {
public Double toDouble(Byte self) { public Double toDouble(Byte self) {
return self.doubleValue(); return self.doubleValue();
} }
public Short toShort(Byte self) {
return self.shortValue();
}
public Byte toByte(Byte self) {
return self;
}
} }

View File

@ -43,6 +43,14 @@ public class DoubleExt {
public Double toDouble(Double self) { public Double toDouble(Double self) {
return self; return self;
} }
public Short toShort(Double self) {
return self.shortValue();
}
public Byte toByte(Double self) {
return self.byteValue();
}
} }

View File

@ -43,6 +43,14 @@ public class FloatExt {
public Double toDouble(Float self) { public Double toDouble(Float self) {
return self.doubleValue(); return self.doubleValue();
} }
public Short toShort(Float self) {
return self.shortValue();
}
public Byte toByte(Float self) {
return self.byteValue();
}
} }

View File

@ -63,6 +63,14 @@ public class IntegerExt {
public Double toDouble(Integer self) { public Double toDouble(Integer self) {
return self.doubleValue(); return self.doubleValue();
} }
public Short toShort(Integer self) {
return self.shortValue();
}
public Byte toByte(Integer self) {
return self.byteValue();
}
} }

View File

@ -43,6 +43,14 @@ public class LongExt {
public Double toDouble(Long self) { public Double toDouble(Long self) {
return self.doubleValue(); return self.doubleValue();
} }
public Short toShort(Long self) {
return self.shortValue();
}
public Byte toByte(Long self) {
return self.byteValue();
}
} }

View File

@ -43,6 +43,14 @@ public class ShortExt {
public Double toDouble(Short self) { public Double toDouble(Short self) {
return self.doubleValue(); return self.doubleValue();
} }
public Short toShort(Short self) {
return self;
}
public Byte toByte(Short self) {
return self.byteValue();
}
} }

View File

@ -79,6 +79,14 @@ public class StringExt {
public Double toDouble(String self) { public Double toDouble(String self) {
return StrKit.isBlank(self) ? null : Double.parseDouble(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);
}
} }

View File

@ -59,101 +59,105 @@ public class JFinalView extends AbstractTemplateView {
OutputStream os = response.getOutputStream(); OutputStream os = response.getOutputStream();
JFinalViewResolver.engine.getTemplate(getUrl()).render(model, os); JFinalViewResolver.engine.getTemplate(getUrl()).render(model, os);
} }
}
@SuppressWarnings({"unchecked", "rawtypes", "deprecation"}) @SuppressWarnings({"unchecked", "rawtypes", "deprecation"})
class InnerSession extends HashMap<Object, Object> implements HttpSession { public static class InnerSession extends HashMap<Object, Object> implements HttpSession {
private static final long serialVersionUID = -8679493647540628009L; private static final long serialVersionUID = -8679493647540628009L;
private HttpSession session; private HttpSession session;
public InnerSession(HttpSession session) { public InnerSession(HttpSession session) {
this.session = session; this.session = session;
} }
// HashMap 相关方法处理 ---------------------------------------------------- // HashMap 相关方法处理 ----------------------------------------------------
/** /**
* 覆盖 HashMap put * 覆盖 HashMap put
*/ */
public Object put(Object name, Object value) { public Object put(Object name, Object value) {
session.setAttribute((String)name, value); session.setAttribute((String)name, value);
return null; return null;
} }
/** /**
* 覆盖 HashMap get * 覆盖 HashMap get
*/ */
public Object get(Object name) { public Object get(Object name) {
return session.getAttribute((String)name); return session.getAttribute((String)name);
} }
// Session 相关方法处理 ---------------------------------------------------- // Session 相关方法处理 ----------------------------------------------------
public Object getAttribute(String key) { public Object getAttribute(String key) {
return session.getAttribute(key); return session.getAttribute(key);
} }
public Enumeration getAttributeNames() { public Enumeration getAttributeNames() {
return session.getAttributeNames(); return session.getAttributeNames();
} }
public long getCreationTime() { public long getCreationTime() {
return session.getCreationTime(); return session.getCreationTime();
} }
public String getId() { public String getId() {
return session.getId(); return session.getId();
} }
public long getLastAccessedTime() { public long getLastAccessedTime() {
return session.getLastAccessedTime(); return session.getLastAccessedTime();
} }
public int getMaxInactiveInterval() { public int getMaxInactiveInterval() {
return session.getMaxInactiveInterval(); return session.getMaxInactiveInterval();
} }
public ServletContext getServletContext() { public ServletContext getServletContext() {
return session.getServletContext(); return session.getServletContext();
} }
public javax.servlet.http.HttpSessionContext getSessionContext() { public javax.servlet.http.HttpSessionContext getSessionContext() {
return session.getSessionContext(); return session.getSessionContext();
} }
public Object getValue(String key) { public Object getValue(String key) {
return session.getValue(key); return session.getValue(key);
} }
public String[] getValueNames() { public String[] getValueNames() {
return session.getValueNames(); return session.getValueNames();
} }
public void invalidate() { public void invalidate() {
session.invalidate(); session.invalidate();
} }
public boolean isNew() { public boolean isNew() {
return session.isNew(); return session.isNew();
} }
public void putValue(String key, Object value) { public void putValue(String key, Object value) {
session.putValue(key, value); session.putValue(key, value);
} }
public void removeAttribute(String key) { public void removeAttribute(String key) {
session.removeAttribute(key); session.removeAttribute(key);
} }
public void removeValue(String key) { public void removeValue(String key) {
session.removeValue(key); session.removeValue(key);
} }
public void setAttribute(String key, Object value) { public void setAttribute(String key, Object value) {
session.setAttribute(key, value); session.setAttribute(key, value);
} }
public void setMaxInactiveInterval(int maxInactiveInterval) { public void setMaxInactiveInterval(int maxInactiveInterval) {
session.setMaxInactiveInterval(maxInactiveInterval); session.setMaxInactiveInterval(maxInactiveInterval);
}
public String toString() {
return session != null ? session.toString() : "null";
}
} }
} }
@ -162,4 +166,3 @@ class InnerSession extends HashMap<Object, Object> implements HttpSession {

View File

@ -77,6 +77,9 @@ public class FileSource implements ISource {
} }
private String buildFinalFileName(String baseTemplatePath, String fileName) { private String buildFinalFileName(String baseTemplatePath, String fileName) {
if (baseTemplatePath == null) {
return fileName;
}
char firstChar = fileName.charAt(0); char firstChar = fileName.charAt(0);
String finalFileName; String finalFileName;
if (firstChar == '/' || firstChar == '\\') { if (firstChar == '/' || firstChar == '\\') {

View File

@ -25,7 +25,7 @@ import com.jfinal.template.io.Writer;
import com.jfinal.template.stat.Scope; import com.jfinal.template.stat.Scope;
/** /**
* Text 输出纯文本块以及使用 "#[[" "]]#" 指定的非解析 * Text 输出纯文本块以及使用 "#[[" "]]#" 定义的原样输出
*/ */
public class Text extends Stat implements IWritable { public class Text extends Stat implements IWritable {