📄 paramchecker.java
字号:
package cn.js.fan.util;import java.util.Vector;import java.util.Iterator;import javax.servlet.http.HttpServletRequest;import com.redmoon.kit.util.FileUpload;import java.util.HashMap;import cn.js.fan.security.SecurityUtil;import java.text.SimpleDateFormat;import cn.js.fan.web.SkinUtil;import java.util.Date;import org.apache.log4j.Logger;import com.cloudwebsoft.framework.base.ISequence;public class ParamChecker { int sourceType; HttpServletRequest request; FileUpload fu; String formResource = ""; String res = "res.checker"; Logger logger = Logger.getLogger(ParamConfig.class.getName()); public static String TYPE_DATE = "Date"; public static String TYPE_INT = "int"; public static String TYPE_STRING = "String"; public static String TYPE_STRING_ARRAY = "String[]"; public static String TYPE_INT_ARRAY = "int[]"; public static String TYPE_BOOLEAN = "boolean"; public static String TYPE_LONG = "long"; public static String TYPE_DOUBLE = "double"; public static String TYPE_FLOAT = "float"; public static int SOURCE_TYPE_REQUEST = 0; public static int SOURCE_TYPE_FILEUPLOAD = 1; HashMap fields = new HashMap(); boolean onErrorExit = false; public Vector msgs = new Vector(); public ParamChecker(HttpServletRequest request) { this.request = request; this.sourceType = SOURCE_TYPE_REQUEST; } public ParamChecker(HttpServletRequest request, Object sourceObj) { this.request = request; if (sourceObj instanceof HttpServletRequest) { this.sourceType = SOURCE_TYPE_REQUEST; } else { this.sourceType = SOURCE_TYPE_FILEUPLOAD; this.fu = (FileUpload) sourceObj; } } public String getFieldValue(String field) { if (sourceType == SOURCE_TYPE_REQUEST) { return ParamUtil.get(request, field, false); } else { return fu.getFieldValue(field); } } public String[] getFieldValues(String field) { if (sourceType == SOURCE_TYPE_REQUEST) { return ParamUtil.getParameters(request, "field"); } else { return fu.getFieldValues(field); } } public static String[] split(String str) { str = str.replaceAll(",", ","); String[] r = StrUtil.split(str, ","); if (r!=null) { int len = r.length; for (int i=0; i<len; i++) { r[i] = r[i].trim().replaceAll("\\\\comma", ","); } } return r; } public Object getValue(String fieldName) throws ErrMsgException { if (fields.containsKey(fieldName)) { return ((Field) fields.get(fieldName)).value; } else throw new ErrMsgException(fieldName + " is not found in the request's form fields."); } public void setValue(String fieldName, String desc, Object value) { Field f = new Field(fieldName, desc, value); fields.put(fieldName, f); } public String getString(String fieldName) throws ErrMsgException { return (String)getValue(fieldName); } public int getInt(String fieldName) throws ErrMsgException { Integer it = (Integer)getValue(fieldName); if (it==null) return -1; else return it.intValue(); } public Date getDate(String fieldName) throws ErrMsgException { Date d = (Date)getValue(fieldName); return d; } public String[] getStringValues(String fieldName) throws ErrMsgException { return (String[])getValue(fieldName); } public int[] getIntValues(String fieldName) throws ErrMsgException { return (int[])getValue(fieldName); } public boolean getBoolean(String fieldName) throws ErrMsgException { Boolean b = (Boolean)getValue(fieldName); if (b==null) return false; else return b.booleanValue(); } public long getLong(String fieldName) throws ErrMsgException { Long v = (Long)getValue(fieldName); if (v==null) return -1; else return v.longValue(); } public boolean checkUnionCond(String rulePairStr, String token) { boolean isValid = true; String[] pairs = rulePairStr.split(token); if (pairs.length < 2) { addMsg("err_format", new String[] {rulePairStr}); isValid = false; } String leftFieldName = pairs[0].trim(); String rightFieldName = pairs[1].trim(); Field leftField = (Field) fields.get(leftFieldName); Field rightField = (Field) fields.get(rightFieldName); if (leftField == null) { addMsg("err_not_in_result", new String[] {leftFieldName}); isValid = false; return false; } if (rightField == null) { addMsg("err_not_in_result", new String[] {rightFieldName}); isValid = false; return false; } String leftFieldDesc = leftField.desc; String rightFieldDesc = rightField.desc; if (leftField.value==null || rightField.value==null) return false; if (!leftField.type.equals(rightField.type)) { isValid = false; addMsg("err_type_not_match", new String[] {leftFieldDesc, "" +rightFieldDesc}); } if (leftField.type.equals(this.TYPE_STRING) || leftField.type.equals(this.TYPE_INT) || leftField.type.equals(this.TYPE_DATE) || leftField.type.equals(this.TYPE_LONG)) ; else { addMsg("err_can_not_compare", new String[] {leftFieldDesc, "" +rightFieldDesc}); isValid = false; return isValid; } if (token.equals(">=")) { if (leftField.type.equals(this.TYPE_INT)) { int a = ((Integer) leftField.value).intValue(); int b = ((Integer) rightField.value).intValue(); if (a >= b) ; else { isValid = false; addMsg("err_need_more_equal", new String[] {leftFieldDesc, "" +rightFieldDesc}); } } else if (leftField.type.equals(this.TYPE_DATE)) { Date a = (Date)leftField.value; Date b = (Date)rightField.value; if (DateUtil.compare(a, b)==1 || DateUtil.compare(a, b)==0) ; else { isValid = false; addMsg("err_need_more_equal", new String[] {leftFieldDesc, "" +rightFieldDesc}); } } else if (leftField.type.equals(this.TYPE_LONG)) { long a = ((Long) leftField.value).longValue(); long b = ((Long) rightField.value).longValue(); if (a >= b) ; else { isValid = false; addMsg("err_need_more_equal", new String[] {leftFieldDesc, "" +rightFieldDesc}); } } else if (leftField.type.equals(this.TYPE_DOUBLE)) { double a = ((Double) leftField.value).doubleValue(); double b = ((Double) rightField.value).doubleValue(); if (a >= b) ; else { isValid = false; addMsg("err_need_more_equal", new String[] {leftFieldDesc, "" +rightFieldDesc}); } } else if (leftField.type.equals(this.TYPE_FLOAT)) { float a = ((Float) leftField.value).floatValue(); float b = ((Float) rightField.value).floatValue(); if (a >= b) ; else { isValid = false; addMsg("err_need_more_equal", new String[] {leftFieldDesc, "" +rightFieldDesc}); } } } else if (token.equals(">")) { if (leftField.type.equals(this.TYPE_INT)) { int a = ((Integer) leftField.value).intValue(); int b = ((Integer) rightField.value).intValue(); if (a > b) ; else { isValid = false; addMsg("err_need_more", new String[] {leftFieldDesc, "" +rightFieldDesc}); } } else if (leftField.type.equals(this.TYPE_DATE)) { Date a = (Date)leftField.value; Date b = (Date)rightField.value; if (DateUtil.compare(a, b)==1) ; else { isValid = false; addMsg("err_need_more", new String[] {leftFieldDesc, "" +rightFieldDesc}); } } else if (leftField.type.equals(this.TYPE_LONG)) { long a = ((Long) leftField.value).longValue(); long b = ((Long) rightField.value).longValue(); if (a > b) ; else { isValid = false; addMsg("err_need_more", new String[] {leftFieldDesc, "" +rightFieldDesc}); } } else if (leftField.type.equals(this.TYPE_DOUBLE)) { double a = ((Double) leftField.value).doubleValue(); double b = ((Double) rightField.value).doubleValue(); if (a > b) ; else { isValid = false; addMsg("err_need_more", new String[] {leftFieldDesc, "" +rightFieldDesc}); } } else if (leftField.type.equals(this.TYPE_FLOAT)) { float a = ((Float) leftField.value).floatValue(); float b = ((Float) rightField.value).floatValue(); if (a > b) ; else { isValid = false; addMsg("err_need_more", new String[] {leftFieldDesc, "" +rightFieldDesc}); } } } else if (token.equals("<=")) { if (leftField.type.equals(this.TYPE_INT)) { int a = ((Integer) leftField.value).intValue(); int b = ((Integer) rightField.value).intValue(); if (a <= b) ; else { isValid = false; addMsg("err_need_less_equal", new String[] {leftFieldDesc, "" +rightFieldDesc}); } } else if (leftField.type.equals(this.TYPE_DATE)) { Date a = (Date)leftField.value; Date b = (Date)rightField.value; if (DateUtil.compare(a, b)==2 || DateUtil.compare(a, b)==0) ; else { isValid = false; addMsg("err_need_less_equal", new String[] {leftFieldDesc, "" +rightFieldDesc}); } } else if (leftField.type.equals(this.TYPE_LONG)) { long a = ((Long) leftField.value).longValue(); long b = ((Long) rightField.value).longValue(); if (a <= b) ; else { isValid = false; addMsg("err_need_less_equal", new String[] {leftFieldDesc, "" +rightFieldDesc}); } } else if (leftField.type.equals(this.TYPE_DOUBLE)) { double a = ((Double) leftField.value).doubleValue(); double b = ((Double) rightField.value).doubleValue(); if (a <= b) ; else { isValid = false; addMsg("err_need_less_equal", new String[] {leftFieldDesc, "" +rightFieldDesc}); } } else if (leftField.type.equals(this.TYPE_FLOAT)) { float a = ((Float) leftField.value).floatValue(); float b = ((Float) rightField.value).floatValue(); if (a <= b) ; else { isValid = false; addMsg("err_need_less_equal", new String[] {leftFieldDesc, "" +rightFieldDesc}); } } } else if (token.equals("<")) { if (leftField.type.equals(this.TYPE_INT)) { int a = ((Integer) leftField.value).intValue(); int b = ((Integer) rightField.value).intValue(); if (a < b) ; else { isValid = false; addMsg("err_need_less", new String[] {leftFieldDesc, "" +rightFieldDesc}); } } else if (leftField.type.equals(this.TYPE_DATE)) { Date a = (Date)leftField.value; Date b = (Date)rightField.value; if (DateUtil.compare(a, b)==2) ; else { isValid = false; addMsg("err_need_less", new String[] {leftFieldDesc, "" +rightFieldDesc}); } } else if (leftField.type.equals(this.TYPE_LONG)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -