浏览代码

jfinal 3.2 release ^_^

James 8 年之前
父节点
当前提交
91575bb650

+ 56 - 45
src/main/java/com/jfinal/kit/Base64Kit.java

@@ -19,22 +19,26 @@ package com.jfinal.kit;
 import java.nio.charset.Charset;
 
 public class Base64Kit {
-	
-	public static final Charset UTF_8 = Charset.forName("UTF-8");
-	private static IBase64 delegate;
-	private Base64Kit() {}
-	
-	static {
-		if (isPresent("java.util.Base64", Base64Kit.class.getClassLoader())) {
-			delegate = new Java8Base64();
+
+    public static final Charset UTF_8 = Charset.forName("UTF-8");
+    private static IBase64 delegate;
+
+    private Base64Kit() {
+    }
+
+    static {
+        if (isPresent("java.util.Base64", Base64Kit.class.getClassLoader())) {
+            delegate = new Java8Base64();
         } else {
-        	delegate = new Java67Base64();
+            delegate = new Java67Base64();
         }
-	}
-	
-	/**
+    }
+
+    /**
      * 编码
-     * @param value byte数组
+     * 
+     * @param value
+     *            byte数组
      * @return {String}
      */
     public static String encode(byte[] value) {
@@ -43,7 +47,9 @@ public class Base64Kit {
 
     /**
      * 编码
-     * @param value 字符串
+     * 
+     * @param value
+     *            字符串
      * @return {String}
      */
     public static String encode(String value) {
@@ -53,8 +59,11 @@ public class Base64Kit {
 
     /**
      * 编码
-     * @param value 字符串
-     * @param charsetName charSet
+     * 
+     * @param value
+     *            字符串
+     * @param charsetName
+     *            charSet
      * @return {String}
      */
     public static String encode(String value, String charsetName) {
@@ -64,7 +73,9 @@ public class Base64Kit {
 
     /**
      * 解码
-     * @param value 字符串
+     * 
+     * @param value
+     *            字符串
      * @return {byte[]}
      */
     public static byte[] decode(String value) {
@@ -73,7 +84,9 @@ public class Base64Kit {
 
     /**
      * 解码
-     * @param value 字符串
+     * 
+     * @param value
+     *            字符串
      * @return {String}
      */
     public static String decodeToStr(String value) {
@@ -83,54 +96,52 @@ public class Base64Kit {
 
     /**
      * 解码
-     * @param value 字符串
-     * @param charsetName 字符集
+     * 
+     * @param value
+     *            字符串
+     * @param charsetName
+     *            字符集
      * @return {String}
      */
     public static String decodeToStr(String value, String charsetName) {
         byte[] decodedValue = delegate.decode(value);
         return new String(decodedValue, Charset.forName(charsetName));
     }
-    
-    private  static boolean isPresent(String className, ClassLoader classLoader) {
+
+    private static boolean isPresent(String className, ClassLoader classLoader) {
         try {
             Class.forName(className, true, classLoader);
             return true;
-        }catch (Throwable ex) {
+        } catch (Throwable ex) {
             return false;
         }
     }
-    
+
     static interface IBase64 {
-    	public String encode(byte[] value);
-    	public byte[] decode(String value);
+        public String encode(byte[] value);
+
+        public byte[] decode(String value);
     }
-    
+
     static class Java8Base64 implements IBase64 {
-    	@Override
-    	public String encode(byte[] value) {
-    		return java.util.Base64.getEncoder().encodeToString(value);
-    	}
-    	
-    	@Override
-    	public byte[] decode(String value) {
-    		return java.util.Base64.getDecoder().decode(value);
-    	}
+        @Override
+        public String encode(byte[] value) {
+            return java.util.Base64.getEncoder().encodeToString(value);
+        }
+
+        @Override
+        public byte[] decode(String value) {
+            return java.util.Base64.getDecoder().decode(value);
+        }
     }
-    
+
     static class Java67Base64 implements IBase64 {
-    	public String encode(byte[] data) {
+        public String encode(byte[] data) {
             return javax.xml.bind.DatatypeConverter.printBase64Binary(data);
         }
-    	
+
         public byte[] decode(String base64) {
             return javax.xml.bind.DatatypeConverter.parseBase64Binary(base64);
         }
     }
 }
-
-
-
-
-
-

+ 16 - 0
src/main/java/com/jfinal/kit/FileKit.java

@@ -38,4 +38,20 @@ public class FileKit {
 			}
 		}
 	}
+	
+	public static String getFileExtension(String fileFullName) {
+	    if (StrKit.isBlank(fileFullName)) {
+            throw new RuntimeException("fileFullName is empty");
+        }
+	    return  getFileExtension(new File(fileFullName));
+	}
+	
+	public static String getFileExtension(File file) {
+	    if (null == file) {
+	        throw new NullPointerException();
+	    }
+	    String fileName = file.getName();
+	    int dotIdx = fileName.lastIndexOf('.');
+	    return (dotIdx == -1) ? "" : fileName.substring(dotIdx + 1);
+    }
 }

+ 82 - 0
src/main/java/com/jfinal/kit/ImageKit.java

@@ -0,0 +1,82 @@
+/**
+ * Copyright (c) 2011-2017, 玛雅牛 (myaniu AT gmail dot com).
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.jfinal.kit;
+
+import java.awt.image.BufferedImage;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.IOException;
+
+import javax.imageio.ImageIO;
+
+public class ImageKit {
+    private ImageKit() {
+        
+    }
+    
+    /**
+     * 生成形如data:image/jpeg;base64,iVBORw0KGgoA……的字符串,将图片文件Data URI化
+     *
+     * @param imageFilePath 图片文件路径
+     * @return 形如data:image/jpeg;base64,iVBORw0KGgoA……的字符串
+     * @throws IOException
+     */
+    public static String encodeDataUri(String imageFilePath) throws IOException{
+        return encodeDataUri(new File(imageFilePath));
+    }
+    
+    /**
+     * 生成形如data:image/jpeg;base64,iVBORw0KGgoA……的字符串,将图片文件Data URI化
+     *
+     * @param imageFile 图片文件对象
+     * @return 形如data:image/jpeg;base64,iVBORw0KGgoA……的字符串
+     * @throws IOException
+     */
+    public static String encodeDataUri(File imageFile) throws IOException{
+        String type = FileKit.getFileExtension(imageFile).toLowerCase();
+        if("jpg".equals(type)) {
+            type = "jpeg";
+        }
+        return "data:image/"+type+";base64," + encodeBase64(imageFile);
+    }
+    
+    /**
+     * 将文件编码成base64格式
+     *
+     * @param imageFilePath 图片文件路径
+     * @return base64编码格式的字符串
+     * @throws IOException
+     */
+    public static String encodeBase64(String imageFilePath) throws IOException{
+        return encodeBase64(new File(imageFilePath));
+    }
+    
+    /**
+     * 将文件编码成base64格式
+     *
+     * @param imageFile 图片文件对象
+     * @return base64编码格式的字符串
+     * @throws IOException
+     */
+    public static String encodeBase64(File imageFile) throws IOException{
+        BufferedImage bi = ImageIO.read(imageFile);
+        String type = FileKit.getFileExtension(imageFile).toLowerCase();
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ImageIO.write(bi,type, baos);
+        return Base64Kit.encode(baos.toByteArray());
+    }
+}

+ 46 - 40
src/main/java/com/jfinal/kit/Kv.java

@@ -21,41 +21,20 @@ import java.util.Map;
 import com.jfinal.json.Json;
 
 /**
- * Kv ---> Key Value 
- * Kv 用于取代 JMap,前者输入量少,且输入更顺滑
- * 
- * 参数或者返回值封装,常用于业务层传参与返回值
+ * Kv (Key Value)
  * 
  * Example:
- * 1:Kv para = Kv.by("id", 123);
+ *    Kv para = Kv.by("id", 123);
  *    User user = user.findFirst(getSqlPara("find", para));
- * 
- * 2:return Kv.fail("msg", "用户名或密码错误");	// 登录失败返回
- *   return Kv.ok("loginUser", user);			// 登录成功返回
- * 
- * 3:Kv kv = loginService.login(...);
- *   renderJson(kv);
  */
 @SuppressWarnings({"serial", "rawtypes", "unchecked"})
 public class Kv extends HashMap {
-
+	
+	@Deprecated
 	private static final String STATE_OK = "isOk";
+	@Deprecated
 	private static final String STATE_FAIL = "isFail";
 	
-	// 状态联动,与 Ret 不同,这里的默认值为 false
-	private static boolean stateLinkage = false;
-	
-	/**
-	 * 设置状态联动
-	 * <pre>
-	 * 1:设置为 true,则在 setOk() 与 setFail() 中,同时处理 isOk 与 isFail 两个状态
-	 * 2:设置为 false,则 setOk() 与 setFile() 只处理与其相关的一个状态
-	 * </pre>
-	 */
-	public static void setStateLinkage(boolean stateLinkage) {
-		Kv.stateLinkage = stateLinkage;
-	}
-	
 	public Kv() {
 	}
 	
@@ -67,46 +46,66 @@ public class Kv extends HashMap {
 		return new Kv();
 	}
 	
+	@Deprecated
 	public static Kv ok() {
 		return new Kv().setOk();
 	}
 	
+	@Deprecated
 	public static Kv ok(Object key, Object value) {
 		return ok().set(key, value);
 	}
 	
+	@Deprecated
 	public static Kv fail() {
 		return new Kv().setFail();
 	}
 	
+	@Deprecated
 	public static Kv fail(Object key, Object value) {
 		return fail().set(key, value);
 	}
 	
+	@Deprecated
 	public Kv setOk() {
 		super.put(STATE_OK, Boolean.TRUE);
-		if (stateLinkage) {
-			super.put(STATE_FAIL, Boolean.FALSE);
-		}
+		super.put(STATE_FAIL, Boolean.FALSE);
 		return this;
 	}
 	
+	@Deprecated
 	public Kv setFail() {
 		super.put(STATE_FAIL, Boolean.TRUE);
-		if (stateLinkage) {
-			super.put(STATE_OK, Boolean.FALSE);
-		}
+		super.put(STATE_OK, Boolean.FALSE);
 		return this;
 	}
 	
+	@Deprecated
 	public boolean isOk() {
 		Boolean isOk = (Boolean)get(STATE_OK);
-		return isOk != null && isOk;
+		if (isOk != null) {
+			return isOk;
+		}
+		Boolean isFail = (Boolean)get(STATE_FAIL);
+		if (isFail != null) {
+			return !isFail;
+		}
+		
+		throw new IllegalStateException("调用 isOk() 之前,必须先调用 ok()、fail() 或者 setOk()、setFail() 方法");
 	}
 	
+	@Deprecated
 	public boolean isFail() {
 		Boolean isFail = (Boolean)get(STATE_FAIL);
-		return isFail != null && isFail;
+		if (isFail != null) {
+			return isFail;
+		}
+		Boolean isOk = (Boolean)get(STATE_OK);
+		if (isOk != null) {
+			return !isOk;
+		}
+		
+		throw new IllegalStateException("调用 isFail() 之前,必须先调用 ok()、fail() 或者 setOk()、setFail() 方法");
 	}
 	
 	public Kv set(Object key, Object value) {
@@ -134,17 +133,24 @@ public class Kv extends HashMap {
 	}
 	
 	public String getStr(Object key) {
-		return (String)get(key);
+		Object s = get(key);
+		return s != null ? s.toString() : null;
 	}
-
+	
 	public Integer getInt(Object key) {
-		return (Integer)get(key);
+		Number n = (Number)get(key);
+		return n != null ? n.intValue() : null;
 	}
-
+	
 	public Long getLong(Object key) {
-		return (Long)get(key);
+		Number n = (Number)get(key);
+		return n != null ? n.longValue() : null;
 	}
-
+	
+	public Number getNumber(Object key) {
+		return (Number)get(key);
+	}
+	
 	public Boolean getBoolean(Object key) {
 		return (Boolean)get(key);
 	}

+ 15 - 75
src/main/java/com/jfinal/kit/Okv.java

@@ -21,42 +21,17 @@ import java.util.Map;
 import com.jfinal.json.Json;
 
 /**
- * Okv ---> Ordered Key Value 
+ * Okv (Ordered Key Value) 
  * 
  * Okv 与 Kv 的唯一区别在于 Okv 继承自 LinkedHashMap,而 Kv 继承自 HashMap
  * 所以对 Okv 中的数据进行迭代输出的次序与数据插入的先后次序一致
  * 
- * 参数或者返回值封装,常用于业务层传参与返回值
- * 
  * Example:
- * 1:Okv para = Okv.by("id", 123);
+ *    Okv para = Okv.by("id", 123);
  *    User user = user.findFirst(getSqlPara("find", para));
- * 
- * 2:return Okv.fail("msg", "用户名或密码错误");	// 登录失败返回
- *   return Okv.ok("loginUser", user);			// 登录成功返回
- * 
- * 3:Okv okv = loginService.login(...);
- *   renderJson(okv);
  */
 @SuppressWarnings({"serial", "rawtypes", "unchecked"})
 public class Okv extends LinkedHashMap {
-
-	private static final String STATE_OK = "isOk";
-	private static final String STATE_FAIL = "isFail";
-	
-	// 状态联动,与 Ret 不同,这里的默认值为 false
-	private static boolean stateLinkage = false;
-	
-	/**
-	 * 设置状态联动
-	 * <pre>
-	 * 1:设置为 true,则在 setOk() 与 setFail() 中,同时处理 isOk 与 isFail 两个状态
-	 * 2:设置为 false,则 setOk() 与 setFile() 只处理与其相关的一个状态
-	 * </pre>
-	 */
-	public static void setStateLinkage(boolean stateLinkage) {
-		Okv.stateLinkage = stateLinkage;
-	}
 	
 	public Okv() {
 	}
@@ -69,48 +44,6 @@ public class Okv extends LinkedHashMap {
 		return new Okv();
 	}
 	
-	public static Okv ok() {
-		return new Okv().setOk();
-	}
-	
-	public static Okv ok(Object key, Object value) {
-		return ok().set(key, value);
-	}
-	
-	public static Okv fail() {
-		return new Okv().setFail();
-	}
-	
-	public static Okv fail(Object key, Object value) {
-		return fail().set(key, value);
-	}
-	
-	public Okv setOk() {
-		super.put(STATE_OK, Boolean.TRUE);
-		if (stateLinkage) {
-			super.put(STATE_FAIL, Boolean.FALSE);
-		}
-		return this;
-	}
-	
-	public Okv setFail() {
-		super.put(STATE_FAIL, Boolean.TRUE);
-		if (stateLinkage) {
-			super.put(STATE_OK, Boolean.FALSE);
-		}
-		return this;
-	}
-	
-	public boolean isOk() {
-		Boolean isOk = (Boolean)get(STATE_OK);
-		return isOk != null && isOk;
-	}
-	
-	public boolean isFail() {
-		Boolean isFail = (Boolean)get(STATE_FAIL);
-		return isFail != null && isFail;
-	}
-	
 	public Okv set(Object key, Object value) {
 		super.put(key, value);
 		return this;
@@ -136,17 +69,24 @@ public class Okv extends LinkedHashMap {
 	}
 	
 	public String getStr(Object key) {
-		return (String)get(key);
+		Object s = get(key);
+		return s != null ? s.toString() : null;
 	}
-
+	
 	public Integer getInt(Object key) {
-		return (Integer)get(key);
+		Number n = (Number)get(key);
+		return n != null ? n.intValue() : null;
 	}
-
+	
 	public Long getLong(Object key) {
-		return (Long)get(key);
+		Number n = (Number)get(key);
+		return n != null ? n.longValue() : null;
 	}
-
+	
+	public Number getNumber(Object key) {
+		return (Number)get(key);
+	}
+	
 	public Boolean getBoolean(Object key) {
 		return (Boolean)get(key);
 	}

+ 75 - 28
src/main/java/com/jfinal/kit/Ret.java

@@ -25,22 +25,22 @@ import com.jfinal.json.Json;
  */
 @SuppressWarnings({"serial", "rawtypes", "unchecked"})
 public class Ret extends HashMap {
-
-	private static final String STATE_OK = "isOk";
-	private static final String STATE_FAIL = "isFail";
 	
-	// 状态联动
-	private static boolean stateLinkage = false;
+	private static final String STATE = "state";
+	private static final String STATE_OK = "ok";
+	private static final String STATE_FAIL = "fail";
+	
+	private static final String OLD_STATE_OK = "isOk";
+	private static final String OLD_STATE_FAIL = "isFail";
+	
+	// 默认为新工作模式
+	private static boolean newWorkMode = true;
 	
 	/**
-	 * 设置状态联动
-	 * <pre>
-	 * 1:设置为 true,则在 setOk() 与 setFail() 中,同时处理 isOk 与 isFail 两个状态
-	 * 2:设置为 false,则 setOk() 与 setFile() 只处理与其相关的一个状态
-	 * </pre>
+	 * 设置为旧工作模式,为了兼容 jfinal 3.2 之前的版本,在 jfinal 4.x 版本之后会移除对旧工作模式的支持
 	 */
-	public static void setStateLinkage(boolean stateLinkage) {
-		Ret.stateLinkage = stateLinkage;
+	public static void setToOldWorkMode() {
+		newWorkMode = false;
 	}
 	
 	public Ret() {
@@ -75,29 +75,69 @@ public class Ret extends HashMap {
 	}
 	
 	public Ret setOk() {
-		super.put(STATE_OK, Boolean.TRUE);
-		if (stateLinkage) {
-			super.put(STATE_FAIL, Boolean.FALSE);
+		if (newWorkMode) {
+			super.put(STATE, STATE_OK);
+		} else {
+			super.put(OLD_STATE_OK, Boolean.TRUE);
+			super.put(OLD_STATE_FAIL, Boolean.FALSE);
 		}
 		return this;
 	}
 	
 	public Ret setFail() {
-		super.put(STATE_FAIL, Boolean.TRUE);
-		if (stateLinkage) {
-			super.put(STATE_OK, Boolean.FALSE);
+		if (newWorkMode) {
+			super.put(STATE, STATE_FAIL);
+		} else {
+			super.put(OLD_STATE_FAIL, Boolean.TRUE);
+			super.put(OLD_STATE_OK, Boolean.FALSE);
 		}
 		return this;
 	}
 	
 	public boolean isOk() {
-		Boolean isOk = (Boolean)get(STATE_OK);
-		return isOk != null && isOk;
+		if (newWorkMode) {
+			Object state = get(STATE);
+			if (STATE_OK.equals(state)) {
+				return true;
+			}
+			if (STATE_FAIL.equals(state)) {
+				return false;
+			}
+		} else {
+			Boolean isOk = (Boolean)get(OLD_STATE_OK);
+			if (isOk != null) {
+				return isOk;
+			}
+			Boolean isFail = (Boolean)get(OLD_STATE_FAIL);
+			if (isFail != null) {
+				return !isFail;
+			}
+		}
+		
+		throw new IllegalStateException("调用 isOk() 之前,必须先调用 ok()、fail() 或者 setOk()、setFail() 方法");
 	}
 	
 	public boolean isFail() {
-		Boolean isFail = (Boolean)get(STATE_FAIL);
-		return isFail != null && isFail;
+		if (newWorkMode) {
+			Object state = get(STATE);
+			if (STATE_FAIL.equals(state)) {
+				return true;
+			}
+			if (STATE_OK.equals(state)) {
+				return false;
+			}
+		} else {
+			Boolean isFail = (Boolean)get(OLD_STATE_FAIL);
+			if (isFail != null) {
+				return isFail;
+			}
+			Boolean isOk = (Boolean)get(OLD_STATE_OK);
+			if (isOk != null) {
+				return !isOk;
+			}
+		}
+		
+		throw new IllegalStateException("调用 isFail() 之前,必须先调用 ok()、fail() 或者 setOk()、setFail() 方法");
 	}
 	
 	public Ret set(Object key, Object value) {
@@ -125,17 +165,24 @@ public class Ret extends HashMap {
 	}
 	
 	public String getStr(Object key) {
-		return (String)get(key);
+		Object s = get(key);
+		return s != null ? s.toString() : null;
 	}
-
+	
 	public Integer getInt(Object key) {
-		return (Integer)get(key);
+		Number n = (Number)get(key);
+		return n != null ? n.intValue() : null;
 	}
-
+	
 	public Long getLong(Object key) {
-		return (Long)get(key);
+		Number n = (Number)get(key);
+		return n != null ? n.longValue() : null;
 	}
-
+	
+	public Number getNumber(Object key) {
+		return (Number)get(key);
+	}
+	
 	public Boolean getBoolean(Object key) {
 		return (Boolean)get(key);
 	}

+ 6 - 0
src/main/java/com/jfinal/validate/Validator.java

@@ -44,6 +44,12 @@ public abstract class Validator implements Interceptor {
 	protected static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd";
 	protected static final String emailAddressPattern = "\\b(^['_A-Za-z0-9-]+(\\.['_A-Za-z0-9-]+)*@([A-Za-z0-9-])+(\\.[A-Za-z0-9-]+)*((\\.[A-Za-z0-9]{2,})|(\\.[A-Za-z0-9]{2,}\\.[A-Za-z0-9]{2,}))$)\\b";
 	
+	/**
+	 * 设置短路验证. 默认值为 false
+	 * 短路验证是指在验证过程中,只要碰到验证失败则立即停止后续验证并返回
+	 * 非短路验证是指验证操作一直持续到结束,无论中途有没有碰到验证失败
+	 * @param shortCircuit true 表示短路型验证
+	 */
 	protected void setShortCircuit(boolean shortCircuit) {
 		this.shortCircuit = shortCircuit;
 	}