ソースを参照

IStringSource 重构为 ISource

James 8 年 前
コミット
8eb6a47a30

+ 18 - 18
src/main/java/com/jfinal/template/Engine.java

@@ -22,7 +22,7 @@ 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.IStringSource;
+import com.jfinal.template.source.ISource;
 import com.jfinal.template.stat.Parser;
 import com.jfinal.template.stat.ast.Stat;
 
@@ -148,7 +148,7 @@ public class Engine {
 		Env env = new Env(config);
 		Parser parser = new Parser(env, fileStringSource.getContent(), fileName);
 		if (devMode) {
-			env.addStringSource(fileStringSource);
+			env.addSource(fileStringSource);
 		}
 		Stat stat = parser.parse();
 		Template template = new Template(env, stat);
@@ -177,17 +177,17 @@ public class Engine {
 	 */
 	public Template getTemplateByString(String content, boolean cache) {
 		if (!cache) {
-			return buildTemplateByStringSource(new MemoryStringSource(content, cache));
+			return buildTemplateBySource(new MemoryStringSource(content, cache));
 		}
 		
 		String key = HashKit.md5(content);
 		Template template = templateCache.get(key);
 		if (template == null) {
-			template = buildTemplateByStringSource(new MemoryStringSource(content, cache));
+			template = buildTemplateBySource(new MemoryStringSource(content, cache));
 			templateCache.put(key, template);
 		} else if (devMode) {
 			if (template.isModified()) {
-				template = buildTemplateByStringSource(new MemoryStringSource(content, cache));
+				template = buildTemplateBySource(new MemoryStringSource(content, cache));
 				templateCache.put(key, template);
 			}
 		}
@@ -195,32 +195,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);
@@ -236,10 +236,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;
 	}
 	

+ 14 - 14
src/main/java/com/jfinal/template/EngineConfig.java

@@ -27,7 +27,7 @@ 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.IStringSource;
+import com.jfinal.template.source.ISource;
 import com.jfinal.template.stat.Location;
 import com.jfinal.template.stat.Parser;
 import com.jfinal.template.stat.ast.Define;
@@ -42,7 +42,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;
 	
@@ -76,13 +76,13 @@ public class EngineConfig {
 		doAddSharedFunction(fileStringSource, 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);
 		}
 	}
 	
@@ -106,11 +106,11 @@ public class EngineConfig {
 	}
 	
 	/**
-	 * 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 FileStringSource ? ((FileStringSource)source).getFileName() : null;
+		doAddSharedFunction(source, fileName);
 	}
 	
 	private void addToSharedFunctionMap(Map<String, Define> sharedFunctionMap, Env env) {
@@ -170,14 +170,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 FileStringSource ? ((FileStringSource)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;

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

@@ -21,7 +21,7 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
-import com.jfinal.template.source.IStringSource;
+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;
@@ -38,8 +38,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;
@@ -92,15 +92,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;
 				}
 			}
@@ -109,14 +109,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);
 	}
 }
 

+ 2 - 3
src/main/java/com/jfinal/template/FileStringSource.java

@@ -21,13 +21,12 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStreamReader;
-
-import com.jfinal.template.source.IStringSource;
+import com.jfinal.template.source.ISource;
 
 /**
  * FileStringSource
  */
-public class FileStringSource implements IStringSource {
+public class FileStringSource implements ISource {
 	
 	private String finalFileName;
 	private String fileName;

+ 2 - 2
src/main/java/com/jfinal/template/MemoryStringSource.java

@@ -18,12 +18,12 @@ package com.jfinal.template;
 
 import com.jfinal.kit.HashKit;
 import com.jfinal.kit.StrKit;
-import com.jfinal.template.source.IStringSource;
+import com.jfinal.template.source.ISource;
 
 /**
  * MemoryStringSource
  */
-public class MemoryStringSource implements IStringSource {
+public class MemoryStringSource implements ISource {
 	
 	private String key;
 	private StringBuilder content;

+ 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();
 	}
 }
 

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

@@ -26,7 +26,7 @@ import com.jfinal.template.FileStringSource;
 import com.jfinal.template.TemplateException;
 import com.jfinal.template.expr.ast.Assign;
 import com.jfinal.template.expr.ast.ExprList;
-import com.jfinal.template.source.IStringSource;
+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);
 			}
@@ -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;
 		}
 	}
 	

+ 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();
 	}
 }
 

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

@@ -91,7 +91,7 @@ public class Include extends Stat {
 		try {
 			Parser parser = new Parser(env, fileStringSource.getContent(), subFileName);
 			if (config.isDevMode()) {
-				env.addStringSource(fileStringSource);
+				env.addSource(fileStringSource);
 			}
 			this.stat = parser.parse();
 		} catch (Exception e) {