Browse Source

Merge branch 'master' into jfinal-java8

James 8 years ago
parent
commit
cb0278a4f5

+ 1 - 1
src/main/java/com/jfinal/plugin/activerecord/ActiveRecordPlugin.java

@@ -109,7 +109,7 @@ public class ActiveRecordPlugin implements IPlugin {
 		return this;
 	}
 	
-	public ActiveRecordPlugin addSqlTemplate(com.jfinal.template.IStringSource sqlTemplate) {
+	public ActiveRecordPlugin addSqlTemplate(com.jfinal.template.source.ISource sqlTemplate) {
 		config.sqlKit.addSqlTemplate(sqlTemplate);
 		return this;
 	}

+ 3 - 3
src/main/java/com/jfinal/plugin/activerecord/sql/SqlKit.java

@@ -23,8 +23,8 @@ import java.util.Map;
 import com.jfinal.kit.StrKit;
 import com.jfinal.plugin.activerecord.SqlPara;
 import com.jfinal.template.Engine;
-import com.jfinal.template.IStringSource;
 import com.jfinal.template.Template;
+import com.jfinal.template.source.ISource;
 
 /**
  * SqlKit
@@ -79,7 +79,7 @@ public class SqlKit {
 		sqlSourceList.add(new SqlSource(sqlTemplate));
 	}
 	
-	public void addSqlTemplate(IStringSource sqlTemplate) {
+	public void addSqlTemplate(ISource sqlTemplate) {
 		if (sqlTemplate == null) {
 			throw new IllegalArgumentException("sqlTemplate can not be null");
 		}
@@ -89,7 +89,7 @@ public class SqlKit {
 	public synchronized void parseSqlTemplate() {
 		Map<String, Template> sqlTemplateMap = new HashMap<String, Template>();
 		for (SqlSource ss : sqlSourceList) {
-			Template template = ss.isFile() ? engine.getTemplate(ss.file) : engine.getTemplate(ss.stringSource);
+			Template template = ss.isFile() ? engine.getTemplate(ss.file) : engine.getTemplate(ss.source);
 			Map<Object, Object> data = new HashMap<Object, Object>();
 			data.put(SQL_TEMPLATE_MAP_KEY, sqlTemplateMap);
 			template.renderToString(data);

+ 5 - 5
src/main/java/com/jfinal/plugin/activerecord/sql/SqlSource.java

@@ -1,6 +1,6 @@
 package com.jfinal.plugin.activerecord.sql;
 
-import com.jfinal.template.IStringSource;
+import com.jfinal.template.source.ISource;
 
 /**
  * 封装 sql 模板源
@@ -8,16 +8,16 @@ import com.jfinal.template.IStringSource;
 class SqlSource {
 	
 	String file;
-	IStringSource stringSource;
+	ISource source;
 	
 	SqlSource(String file) {
 		this.file = file;
-		this.stringSource = null;
+		this.source = null;
 	}
 	
-	SqlSource(IStringSource stringSource) {
+	SqlSource(ISource source) {
 		this.file = null;
-		this.stringSource = stringSource;
+		this.source = source;
 	}
 	
 	boolean isFile() {

+ 26 - 23
src/main/java/com/jfinal/template/Engine.java

@@ -22,6 +22,9 @@ import java.util.Map;
 import com.jfinal.kit.HashKit;
 import com.jfinal.kit.StrKit;
 import com.jfinal.template.expr.ast.MethodKit;
+import com.jfinal.template.source.FileSource;
+import com.jfinal.template.source.ISource;
+import com.jfinal.template.source.StringSource;
 import com.jfinal.template.stat.Parser;
 import com.jfinal.template.stat.ast.Stat;
 
@@ -131,23 +134,23 @@ public class Engine {
 		
 		Template template = templateCache.get(fileName);
 		if (template == null) {
-			template = buildTemplateByFileStringSource(fileName);
+			template = buildTemplateByFileSource(fileName);
 			templateCache.put(fileName, template);
 		} else if (devMode) {
 			if (template.isModified()) {
-				template = buildTemplateByFileStringSource(fileName);
+				template = buildTemplateByFileSource(fileName);
 				templateCache.put(fileName, template);
 			}
 		}
 		return template;
 	}
 	
-	private Template buildTemplateByFileStringSource(String fileName) {
-		FileStringSource fileStringSource = new FileStringSource(config.getBaseTemplatePath(), fileName, config.getEncoding());
+	private Template buildTemplateByFileSource(String fileName) {
+		FileSource fileSource = new FileSource(config.getBaseTemplatePath(), fileName, config.getEncoding());
 		Env env = new Env(config);
-		Parser parser = new Parser(env, fileStringSource.getContent(), fileName);
+		Parser parser = new Parser(env, fileSource.getContent(), fileName);
 		if (devMode) {
-			env.addStringSource(fileStringSource);
+			env.addSource(fileSource);
 		}
 		Stat stat = parser.parse();
 		Template template = new Template(env, stat);
@@ -157,7 +160,7 @@ public class Engine {
 	/**
 	 * Get template by string content and do not cache the template
 	 * 
-	 * 重要:MemoryStringSource 中的 key = HashKit.md5(content),也即 key
+	 * 重要:StringSource 中的 key = HashKit.md5(content),也即 key
 	 *     与 content 有紧密的对应关系,当 content 发生变化时 key 值也相应变化
 	 *     因此,原先 key 所对应的 Template 缓存对象已无法被获取,当 getTemplateByString(String)
 	 *     的 String 参数的数量不确定时会引发内存泄漏
@@ -176,17 +179,17 @@ public class Engine {
 	 */
 	public Template getTemplateByString(String content, boolean cache) {
 		if (!cache) {
-			return buildTemplateByStringSource(new MemoryStringSource(content, cache));
+			return buildTemplateBySource(new StringSource(content, cache));
 		}
 		
 		String key = HashKit.md5(content);
 		Template template = templateCache.get(key);
 		if (template == null) {
-			template = buildTemplateByStringSource(new MemoryStringSource(content, cache));
+			template = buildTemplateBySource(new StringSource(content, cache));
 			templateCache.put(key, template);
 		} else if (devMode) {
 			if (template.isModified()) {
-				template = buildTemplateByStringSource(new MemoryStringSource(content, cache));
+				template = buildTemplateBySource(new StringSource(content, cache));
 				templateCache.put(key, template);
 			}
 		}
@@ -194,32 +197,32 @@ public class Engine {
 	}
 	
 	/**
-	 * Get template with implementation of IStringSource
+	 * Get template with implementation of ISource
 	 */
-	public Template getTemplate(IStringSource stringSource) {
-		String key = stringSource.getKey();
-		if (key == null) {	// key 为 null 则不缓存,详见 IStringSource.getKey() 注释
-			return buildTemplateByStringSource(stringSource);
+	public Template getTemplate(ISource source) {
+		String key = source.getKey();
+		if (key == null) {	// key 为 null 则不缓存,详见 ISource.getKey() 注释
+			return buildTemplateBySource(source);
 		}
 		
 		Template template = templateCache.get(key);
 		if (template == null) {
-			template = buildTemplateByStringSource(stringSource);
+			template = buildTemplateBySource(source);
 			templateCache.put(key, template);
 		} else if (devMode) {
 			if (template.isModified()) {
-				template = buildTemplateByStringSource(stringSource);
+				template = buildTemplateBySource(source);
 				templateCache.put(key, template);
 			}
 		}
 		return template;
 	}
 	
-	private Template buildTemplateByStringSource(IStringSource stringSource) {
+	private Template buildTemplateBySource(ISource source) {
 		Env env = new Env(config);
-		Parser parser = new Parser(env, stringSource.getContent(), null);
+		Parser parser = new Parser(env, source.getContent(), null);
 		if (devMode) {
-			env.addStringSource(stringSource);
+			env.addSource(source);
 		}
 		Stat stat = parser.parse();
 		Template template = new Template(env, stat);
@@ -235,10 +238,10 @@ public class Engine {
 	}
 	
 	/**
-	 * Add shared function by IStringSource
+	 * Add shared function by ISource
 	 */
-	public Engine addSharedFunction(IStringSource stringSource) {
-		config.addSharedFunction(stringSource);
+	public Engine addSharedFunction(ISource source) {
+		config.addSharedFunction(source);
 		return this;
 	}
 	

+ 26 - 18
src/main/java/com/jfinal/template/EngineConfig.java

@@ -27,6 +27,9 @@ import com.jfinal.template.expr.ast.ExprList;
 import com.jfinal.template.expr.ast.SharedMethodKit;
 import com.jfinal.template.ext.directive.*;
 import com.jfinal.template.ext.sharedmethod.Json;
+import com.jfinal.template.source.FileSource;
+import com.jfinal.template.source.ISource;
+import com.jfinal.template.source.StringSource;
 import com.jfinal.template.stat.Location;
 import com.jfinal.template.stat.Parser;
 import com.jfinal.template.stat.ast.Define;
@@ -41,7 +44,7 @@ public class EngineConfig {
 	public static final String DEFAULT_ENCODING = "UTF-8";
 	
 	private Map<String, Define> sharedFunctionMap = new HashMap<String, Define>();
-	private List<IStringSource> sharedFunctionSourceList = new ArrayList<IStringSource>();		// for devMode only
+	private List<ISource> sharedFunctionSourceList = new ArrayList<ISource>();		// for devMode only
 	
 	Map<String, Object> sharedObjectMap = null;
 	
@@ -71,17 +74,17 @@ public class EngineConfig {
 	 * Add shared function with file
 	 */
 	public void addSharedFunction(String fileName) {
-		FileStringSource fileStringSource = new FileStringSource(baseTemplatePath, fileName, encoding);
-		doAddSharedFunction(fileStringSource, fileName);
+		FileSource fileSource = new FileSource(baseTemplatePath, fileName, encoding);
+		doAddSharedFunction(fileSource, fileName);
 	}
 	
-	private synchronized void doAddSharedFunction(IStringSource stringSource, String fileName) {
+	private synchronized void doAddSharedFunction(ISource source, String fileName) {
 		Env env = new Env(this);
-		new Parser(env, stringSource.getContent(), fileName).parse();
+		new Parser(env, source.getContent(), fileName).parse();
 		addToSharedFunctionMap(sharedFunctionMap, env);
 		if (devMode) {
-			sharedFunctionSourceList.add(stringSource);
-			env.addStringSource(stringSource);
+			sharedFunctionSourceList.add(source);
+			env.addSource(source);
 		}
 	}
 	
@@ -98,18 +101,18 @@ public class EngineConfig {
 	 * Add shared function by string content
 	 */
 	public void addSharedFunctionByString(String content) {
-		// content 中的内容被解析后会存放在 Env 之中,而 MemoryStringSource 所对应的
+		// content 中的内容被解析后会存放在 Env 之中,而 StringSource 所对应的
 		// Template 对象 isModified() 始终返回 false,所以没有必要对其缓存
-		MemoryStringSource memoryStringSource = new MemoryStringSource(content, false);
-		doAddSharedFunction(memoryStringSource, null);
+		StringSource stringSource = new StringSource(content, false);
+		doAddSharedFunction(stringSource, null);
 	}
 	
 	/**
-	 * Add shared function by IStringSource
+	 * Add shared function by ISource
 	 */
-	public void addSharedFunction(IStringSource stringSource) {
-		String fileName = stringSource instanceof FileStringSource ? ((FileStringSource)stringSource).getFileName() : null;
-		doAddSharedFunction(stringSource, fileName);
+	public void addSharedFunction(ISource source) {
+		String fileName = source instanceof FileSource ? ((FileSource)source).getFileName() : null;
+		doAddSharedFunction(source, fileName);
 	}
 	
 	private void addToSharedFunctionMap(Map<String, Define> sharedFunctionMap, Env env) {
@@ -169,14 +172,14 @@ public class EngineConfig {
 	private synchronized void reloadSharedFunctionSourceList() {
 		Map<String, Define> newMap = new HashMap<String, Define>();
 		for (int i = 0, size = sharedFunctionSourceList.size(); i < size; i++) {
-			IStringSource ss = sharedFunctionSourceList.get(i);
-			String fileName = ss instanceof FileStringSource ? ((FileStringSource)ss).getFileName() : null;
+			ISource source = sharedFunctionSourceList.get(i);
+			String fileName = source instanceof FileSource ? ((FileSource)source).getFileName() : null;
 			
 			Env env = new Env(this);
-			new Parser(env, ss.getContent(), fileName).parse();
+			new Parser(env, source.getContent(), fileName).parse();
 			addToSharedFunctionMap(newMap, env);
 			if (devMode) {
-				env.addStringSource(ss);
+				env.addSource(source);
 			}
 		}
 		this.sharedFunctionMap = newMap;
@@ -221,6 +224,11 @@ public class EngineConfig {
 	}
 	
 	public void setBaseTemplatePath(String baseTemplatePath) {
+		// 使用 ClassPathSourceFactory 时,允许 baseTemplatePath 为 null 值
+		if (baseTemplatePath == null) {
+			this.baseTemplatePath = null;
+			return ;
+		}
 		if (StrKit.isBlank(baseTemplatePath)) {
 			throw new IllegalArgumentException("baseTemplatePath can not be blank");
 		}

+ 15 - 14
src/main/java/com/jfinal/template/Env.java

@@ -20,6 +20,7 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import com.jfinal.template.source.ISource;
 import com.jfinal.template.stat.Location;
 import com.jfinal.template.stat.ParseException;
 import com.jfinal.template.stat.ast.Define;
@@ -36,8 +37,8 @@ public class Env {
 	protected EngineConfig engineConfig;
 	protected Map<String, Define> functionMap = new HashMap<String, Define>();
 	
-	// 代替 Template 持有该属性,便于在 #include 指令中调用 Env.addStringSource()
-	protected List<IStringSource> stringSourceList = null;
+	// 代替 Template 持有该属性,便于在 #include 指令中调用 Env.addSource()
+	protected List<ISource> sourceList = null;
 	
 	public Env(EngineConfig engineConfig) {
 		this.engineConfig = engineConfig;
@@ -90,15 +91,15 @@ public class Env {
 	
 	/**
 	 * 本方法用于在 devMode 之下,判断当前 Template 以及其下 #include 指令
-	 * 所涉及的所有 IStringSource 对象是否被修改,以便于在 devMode 下重新加载
+	 * 所涉及的所有 ISource 对象是否被修改,以便于在 devMode 下重新加载
 	 * 
-	 * stringSourceList 属性用于存放主模板以及 #include 进来的模板所对应的
-	 * IStringSource 对象
+	 * sourceList 属性用于存放主模板以及 #include 进来的模板所对应的
+	 * ISource 对象
 	 */
-	public boolean isStringSourceListModified() {
-		if (stringSourceList != null) {
-			for (int i = 0, size = stringSourceList.size(); i < size; i++) {
-				if (stringSourceList.get(i).isModified()) {
+	public boolean isSourceListModified() {
+		if (sourceList != null) {
+			for (int i = 0, size = sourceList.size(); i < size; i++) {
+				if (sourceList.get(i).isModified()) {
 					return true;
 				}
 			}
@@ -107,14 +108,14 @@ public class Env {
 	}
 	
 	/**
-	 * 添加本 Template 的 IStringSource,以及该 Template 使用 include 包含进来的所有 IStringSource
+	 * 添加本 Template 的 ISource,以及该 Template 使用 include 包含进来的所有 ISource
 	 * 以便于在 devMode 之下判断该 Template 是否被 modified,进而 reload 该 Template
 	 */
-	public void addStringSource(IStringSource stringSource) {
-		if (stringSourceList == null) {
-			stringSourceList = new ArrayList<IStringSource>();
+	public void addSource(ISource source) {
+		if (sourceList == null) {
+			sourceList = new ArrayList<ISource>();
 		}
-		stringSourceList.add(stringSource);
+		sourceList.add(source);
 	}
 }
 

+ 1 - 1
src/main/java/com/jfinal/template/Template.java

@@ -82,7 +82,7 @@ public class Template {
 	}
 	
 	public boolean isModified() {
-		return env.isStringSourceListModified();
+		return env.isSourceListModified();
 	}
 }
 

+ 10 - 10
src/main/java/com/jfinal/template/ext/directive/RenderDirective.java

@@ -22,11 +22,11 @@ import java.util.Map;
 import com.jfinal.template.Directive;
 import com.jfinal.template.EngineConfig;
 import com.jfinal.template.Env;
-import com.jfinal.template.FileStringSource;
-import com.jfinal.template.IStringSource;
 import com.jfinal.template.TemplateException;
 import com.jfinal.template.expr.ast.Assign;
 import com.jfinal.template.expr.ast.ExprList;
+import com.jfinal.template.source.FileSource;
+import com.jfinal.template.source.ISource;
 import com.jfinal.template.stat.Ctrl;
 import com.jfinal.template.stat.ParseException;
 import com.jfinal.template.stat.Parser;
@@ -113,8 +113,8 @@ public class RenderDirective extends Directive {
 			statInfo = parseStatInfo(env, subFileName);
 			statInfoCache.put(subFileName, statInfo);
 		} else if (env.getEngineConfig().isDevMode()) {
-			// statInfo.env.isStringSourceModified() 逻辑可以支持 #render 子模板中的 #include 过来的子模板在 devMode 下在修改后可被重加载
-			if (statInfo.stringSource.isModified() || statInfo.env.isStringSourceListModified()) {
+			// statInfo.env.isSourceListModified() 逻辑可以支持 #render 子模板中的 #include 过来的子模板在 devMode 下在修改后可被重加载
+			if (statInfo.source.isModified() || statInfo.env.isSourceListModified()) {
 				statInfo = parseStatInfo(env, subFileName);
 				statInfoCache.put(subFileName, statInfo);
 			}
@@ -126,12 +126,12 @@ public class RenderDirective extends Directive {
 	
 	private StatInfo parseStatInfo(Env env, String subFileName) {
 		EngineConfig config = env.getEngineConfig();
-		FileStringSource fileStringSource = new FileStringSource(config.getBaseTemplatePath(), subFileName, config.getEncoding());
+		FileSource fileSource = new FileSource(config.getBaseTemplatePath(), subFileName, config.getEncoding());
 		
 		try {
 			EnvSub envSub = new EnvSub(env);
-			Stat stat = new Parser(envSub, fileStringSource.getContent(), subFileName).parse();
-			return new StatInfo(envSub, stat, fileStringSource);
+			Stat stat = new Parser(envSub, fileSource.getContent(), subFileName).parse();
+			return new StatInfo(envSub, stat, fileSource);
 		} catch (Exception e) {
 			throw new ParseException(e.getMessage(), location, e);
 		}
@@ -140,12 +140,12 @@ public class RenderDirective extends Directive {
 	private static class StatInfo {
 		EnvSub env;
 		Stat stat;
-		IStringSource stringSource;
+		ISource source;
 		
-		StatInfo(EnvSub env, Stat stat, IStringSource stringSource) {
+		StatInfo(EnvSub env, Stat stat, ISource source) {
 			this.env = env;
 			this.stat = stat;
-			this.stringSource = stringSource;
+			this.source = source;
 		}
 	}
 	

+ 7 - 5
src/main/java/com/jfinal/template/FileStringSource.java

@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-package com.jfinal.template;
+package com.jfinal.template.source;
 
 import java.io.BufferedReader;
 import java.io.File;
@@ -22,10 +22,12 @@ import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStreamReader;
 
+import com.jfinal.template.EngineConfig;
+
 /**
- * FileStringSource
+ * FileSource
  */
-public class FileStringSource implements IStringSource {
+public class FileSource implements ISource {
 	
 	private String finalFileName;
 	private String fileName;
@@ -33,13 +35,13 @@ public class FileStringSource implements IStringSource {
 	
 	private long lastModified;
 	
-	public FileStringSource(String baseTemplatePath, String fileName, String encoding) {
+	public FileSource(String baseTemplatePath, String fileName, String encoding) {
 		this.finalFileName = buildFinalFileName(baseTemplatePath, fileName);
 		this.fileName = fileName;
 		this.encoding= encoding;
 	}
 	
-	public FileStringSource(String baseTemplatePath, String fileName) {
+	public FileSource(String baseTemplatePath, String fileName) {
 		this(baseTemplatePath, fileName, EngineConfig.DEFAULT_ENCODING);
 	}
 	

+ 5 - 5
src/main/java/com/jfinal/template/IStringSource.java

@@ -14,12 +14,12 @@
  * limitations under the License.
  */
 
-package com.jfinal.template;
+package com.jfinal.template.source;
 
 /**
- * IStringSource
+ * ISource
  */
-public interface IStringSource {
+public interface ISource {
 	
 	/**
 	 * reload template if modified on devMode
@@ -29,13 +29,13 @@ public interface IStringSource {
 	/**
 	 * key used to cache, return null if do not cache the template
 	 * 
-	 * 注意:如果不希望缓存从该 IStreamSource 解析出来的 Template 对象
+	 * 注意:如果不希望缓存从该 ISource 解析出来的 Template 对象
 	 *      让 getKey() 返回 null 值即可  
 	 */
 	String getKey();
 	
 	/**
-	 * content of StringSource
+	 * content of ISource
 	 */
 	StringBuilder getContent();
 	

+ 7 - 6
src/main/java/com/jfinal/template/MemoryStringSource.java

@@ -14,25 +14,26 @@
  * limitations under the License.
  */
 
-package com.jfinal.template;
+package com.jfinal.template.source;
 
 import com.jfinal.kit.HashKit;
 import com.jfinal.kit.StrKit;
+import com.jfinal.template.EngineConfig;
 
 /**
- * MemoryStringSource
+ * StringSource
  */
-public class MemoryStringSource implements IStringSource {
+public class StringSource implements ISource {
 	
 	private String key;
 	private StringBuilder content;
 	
 	/**
-	 * 构造 MemoryStringSource
+	 * 构造 StringSource
 	 * @param content 模板内容
 	 * @param cache true 则缓存 Template,否则不缓存
 	 */
-	public MemoryStringSource(String content, boolean cache) {
+	public StringSource(String content, boolean cache) {
 		if (StrKit.isBlank(content)) {
 			throw new IllegalArgumentException("content can not be blank");
 		}
@@ -40,7 +41,7 @@ public class MemoryStringSource implements IStringSource {
 		this.key = cache ? HashKit.md5(content) : null;	// 不缓存只要将 key 值赋为 null 即可
 	}
 	
-	public MemoryStringSource(StringBuilder content, boolean cache) {
+	public StringSource(StringBuilder content, boolean cache) {
 		if (content == null || content.length() == 0) {
 			throw new IllegalArgumentException("content can not be blank");
 		}

+ 1 - 1
src/main/java/com/jfinal/template/stat/ast/Define.java

@@ -133,7 +133,7 @@ public class Define extends Stat {
 		if (envForDevMode == null) {
 			throw new IllegalStateException("Check engine config: setDevMode(...) must be invoked before addSharedFunction(...)");
 		}
-		return envForDevMode.isStringSourceListModified();
+		return envForDevMode.isSourceListModified();
 	}
 }
 

+ 4 - 4
src/main/java/com/jfinal/template/stat/ast/Include.java

@@ -19,11 +19,11 @@ package com.jfinal.template.stat.ast;
 import java.io.Writer;
 import com.jfinal.template.EngineConfig;
 import com.jfinal.template.Env;
-import com.jfinal.template.FileStringSource;
 import com.jfinal.template.expr.ast.Assign;
 import com.jfinal.template.expr.ast.Const;
 import com.jfinal.template.expr.ast.Expr;
 import com.jfinal.template.expr.ast.ExprList;
+import com.jfinal.template.source.FileSource;
 import com.jfinal.template.stat.Ctrl;
 import com.jfinal.template.stat.Location;
 import com.jfinal.template.stat.ParseException;
@@ -87,11 +87,11 @@ public class Include extends Stat {
 	private void parseSubTemplate(Env env, String fileName, String parentFileName, Location location) {
 		String subFileName = getSubFileName(fileName, parentFileName);
 		EngineConfig config = env.getEngineConfig();
-		FileStringSource fileStringSource = new FileStringSource(config.getBaseTemplatePath(), subFileName, config.getEncoding());
+		FileSource fileSource = new FileSource(config.getBaseTemplatePath(), subFileName, config.getEncoding());
 		try {
-			Parser parser = new Parser(env, fileStringSource.getContent(), subFileName);
+			Parser parser = new Parser(env, fileSource.getContent(), subFileName);
 			if (config.isDevMode()) {
-				env.addStringSource(fileStringSource);
+				env.addSource(fileSource);
 			}
 			this.stat = parser.parse();
 		} catch (Exception e) {