James 7 年 前
コミット
fdb344e535

+ 8 - 1
src/main/java/com/jfinal/captcha/CaptchaRender.java

@@ -48,13 +48,20 @@ public class CaptchaRender extends Render {
 	protected static final char[] charArray = "3456789ABCDEFGHJKMNPQRSTUVWXY".toCharArray();
 	// 验证码字体
 	protected static final Font[] RANDOM_FONT = new Font[] {
+		new Font(Font.DIALOG, Font.BOLD, 33),
+		new Font(Font.DIALOG_INPUT, Font.BOLD, 34),
+		new Font(Font.SERIF, Font.BOLD, 33),
+		new Font(Font.SANS_SERIF, Font.BOLD, 34),
+		new Font(Font.MONOSPACED, Font.BOLD, 34)
+	};
+	/*protected static final Font[] RANDOM_FONT = new Font[] {
 		new Font("nyala", Font.BOLD, 38),
 		new Font("Arial", Font.BOLD, 32),
 		new Font("Bell MT", Font.BOLD, 32),
 		new Font("Credit valley", Font.BOLD, 34),
 		new Font("Impact", Font.BOLD, 32),
 		new Font(Font.MONOSPACED, Font.BOLD, 40)
-	};
+	};*/
 	
 	/**
 	 * 设置 captchaName

+ 11 - 8
src/main/java/com/jfinal/plugin/activerecord/generator/BaseModelGenerator.java

@@ -26,13 +26,13 @@ import com.jfinal.kit.JavaKeyword;
 import com.jfinal.kit.Kv;
 import com.jfinal.kit.StrKit;
 import com.jfinal.template.Engine;
-import com.jfinal.template.source.ClassPathSourceFactory;
 
 /**
  * Base model 生成器
  */
 public class BaseModelGenerator {
 	
+	protected Engine engine;
 	protected String template = "/com/jfinal/plugin/activerecord/generator/base_model_template.jf";
 	
 	protected String baseModelPackageName;
@@ -71,6 +71,16 @@ public class BaseModelGenerator {
 		
 		this.baseModelPackageName = baseModelPackageName;
 		this.baseModelOutputDir = baseModelOutputDir;
+		
+		initEngine();
+	}
+	
+	protected void initEngine() {
+		engine = new Engine();
+		engine.setToClassPathSourceFactory();	// 从 class path 内读模板文件
+		engine.addSharedMethod(new StrKit());
+		engine.addSharedObject("getterTypeMap", getterTypeMap);
+		engine.addSharedObject("javaKeyword", javaKeyword);
 	}
 	
 	/**
@@ -88,12 +98,6 @@ public class BaseModelGenerator {
 		System.out.println("Generate base model ...");
 		System.out.println("Base Model Output Dir: " + baseModelOutputDir);
 		
-		Engine engine = Engine.create("forBaseModel");
-		engine.setSourceFactory(new ClassPathSourceFactory());
-		engine.addSharedMethod(new StrKit());
-		engine.addSharedObject("getterTypeMap", getterTypeMap);
-		engine.addSharedObject("javaKeyword", javaKeyword);
-		
 		for (TableMeta tableMeta : tableMetas) {
 			genBaseModelContent(tableMeta);
 		}
@@ -105,7 +109,6 @@ public class BaseModelGenerator {
 		data.set("generateChainSetter", generateChainSetter);
 		data.set("tableMeta", tableMeta);
 		
-		Engine engine = Engine.use("forBaseModel");
 		tableMeta.baseModelContent = engine.getTemplate(template).renderToString(data);
 	}
 	

+ 9 - 5
src/main/java/com/jfinal/plugin/activerecord/generator/MappingKitGenerator.java

@@ -24,13 +24,13 @@ import com.jfinal.kit.Kv;
 import com.jfinal.kit.LogKit;
 import com.jfinal.kit.StrKit;
 import com.jfinal.template.Engine;
-import com.jfinal.template.source.ClassPathSourceFactory;
 
 /**
  * MappingKit 文件生成器
  */
 public class MappingKitGenerator {
 	
+	protected Engine engine;
 	protected String template = "/com/jfinal/plugin/activerecord/generator/mapping_kit_template.jf";
 	
 	protected String mappingKitPackageName;
@@ -40,6 +40,14 @@ public class MappingKitGenerator {
 	public MappingKitGenerator(String mappingKitPackageName, String mappingKitOutputDir) {
 		this.mappingKitPackageName = mappingKitPackageName;
 		this.mappingKitOutputDir = mappingKitOutputDir;
+		
+		initEngine();
+	}
+	
+	protected void initEngine() {
+		engine = new Engine();
+		engine.setToClassPathSourceFactory();
+		engine.addSharedMethod(new StrKit());
 	}
 	
 	/**
@@ -71,10 +79,6 @@ public class MappingKitGenerator {
 		System.out.println("Generate MappingKit file ...");
 		System.out.println("MappingKit Output Dir: " + mappingKitOutputDir);
 		
-		Engine engine = Engine.create("forMappingKit");
-		engine.setSourceFactory(new ClassPathSourceFactory());
-		engine.addSharedMethod(new StrKit());
-		
 		Kv data = Kv.by("mappingKitPackageName", mappingKitPackageName);
 		data.set("mappingKitClassName", mappingKitClassName);
 		data.set("tableMetas", tableMetas);

+ 10 - 6
src/main/java/com/jfinal/plugin/activerecord/generator/ModelGenerator.java

@@ -23,13 +23,13 @@ import java.util.List;
 import com.jfinal.kit.Kv;
 import com.jfinal.kit.StrKit;
 import com.jfinal.template.Engine;
-import com.jfinal.template.source.ClassPathSourceFactory;
 
 /**
  * Model 生成器
  */
 public class ModelGenerator {
 	
+	protected Engine engine;
 	protected String template = "/com/jfinal/plugin/activerecord/generator/model_template.jf";
 	
 	protected String modelPackageName;
@@ -57,6 +57,14 @@ public class ModelGenerator {
 		this.modelPackageName = modelPackageName;
 		this.baseModelPackageName = baseModelPackageName;
 		this.modelOutputDir = modelOutputDir;
+		
+		initEngine();
+	}
+	
+	protected void initEngine() {
+		engine = new Engine();
+		engine.setToClassPathSourceFactory();
+		engine.addSharedMethod(new StrKit());
 	}
 	
 	/**
@@ -74,10 +82,6 @@ public class ModelGenerator {
 		System.out.println("Generate model ...");
 		System.out.println("Model Output Dir: " + modelOutputDir);
 		
-		Engine engine = Engine.create("forModel");
-		engine.setSourceFactory(new ClassPathSourceFactory());
-		engine.addSharedMethod(new StrKit());
-		
 		for (TableMeta tableMeta : tableMetas) {
 			genModelContent(tableMeta);
 		}
@@ -90,7 +94,7 @@ public class ModelGenerator {
 		data.set("generateDaoInModel", generateDaoInModel);
 		data.set("tableMeta", tableMeta);
 		
-		String ret = Engine.use("forModel").getTemplate(template).renderToString(data);
+		String ret = engine.getTemplate(template).renderToString(data);
 		tableMeta.modelContent = ret;
 	}
 	

+ 12 - 12
src/main/java/com/jfinal/render/TemplateRender.java

@@ -56,18 +56,18 @@ public class TemplateRender extends Render {
 			data.put(attrName, request.getAttribute(attrName));
 		}
 		
-        try {
-        	OutputStream os = response.getOutputStream();
-        	engine.getTemplate(view).render(data, os);
-        } catch (RuntimeException e) {	// 捕获 ByteWriter.close() 抛出的 RuntimeException
-        	Throwable cause = e.getCause();
-        	if (cause instanceof IOException) {	// ClientAbortException、EofException 直接或间接继承自 IOException
-	        	String name = cause.getClass().getSimpleName();
-	        	if ("ClientAbortException".equals(name) || "EofException".equals(name)) {
-	        		return ;
-	        	}
-	        }
-        	throw e;
+		try {
+			OutputStream os = response.getOutputStream();
+			engine.getTemplate(view).render(data, os);
+		} catch (RuntimeException e) {	// 捕获 ByteWriter.close() 抛出的 RuntimeException
+			Throwable cause = e.getCause();
+			if (cause instanceof IOException) {	// ClientAbortException、EofException 直接或间接继承自 IOException
+				String name = cause.getClass().getSimpleName();
+				if ("ClientAbortException".equals(name) || "EofException".equals(name)) {
+					return ;
+				}
+			}
+			throw e;
 		} catch (IOException e) {
 			throw new RenderException(e);
 		}

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

+ 8 - 2
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 形参数

+ 3 - 0
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 == '\\') {

+ 1 - 1
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 {