📄 rangevalidator.java
字号:
package com.exp.web.util.validator;
import com.exp.fcl.xml.EXPXMLNode;
import com.exp.web.util.FormBean;
public class RangeValidator extends BaseValidator {
protected double min = Double.MIN_VALUE;
protected double max = Double.MAX_VALUE;
protected boolean conainMin = false;
protected boolean containMax = false;
protected boolean bInt = false;
protected boolean bFloat = false;
protected boolean failFlag = false;
protected int precision = -1;
protected void initConfig(EXPXMLNode validator) {
try {
String temp = validator.getAttributeValue("float");
if (temp.equals("true")) {
this.bFloat = true;
}
temp = validator.getAttributeValue("integer");
if (temp.equals("true")) {
this.bInt = true;
}
temp = validator.getAttributeValue("precision");
if (!temp.equals("")) {
this.precision = Integer.valueOf(temp).intValue();
}
//FOR MIN
temp = validator.getAttributeValue("min");
if (!temp.equals("")) {
this.min = Double.valueOf(temp).doubleValue();
} else {
if (this.bFloat) {
this.min = Float.MIN_VALUE;
} else if (this.bInt) {
this.min = Integer.MIN_VALUE;
}
}
//FOR MAX
temp = validator.getAttributeValue("max");
if (!temp.equals("")) {
this.max = Double.valueOf(temp).doubleValue();
} else {
if (this.bFloat) {
this.max = Float.MAX_VALUE;
} else if (this.bInt) {
this.max = Integer.MAX_VALUE;
}
}
temp = validator.getAttributeValue("min-contain");
if (temp.equals("true")) {
this.conainMin = true;
}
temp = validator.getAttributeValue("max-contain");
if (temp.equals("true")) {
this.containMax = true;
}
} catch (Exception e) {
this.failFlag = true;
}
}
public boolean checkValid(FormBean formBean) {
if (this.failFlag) {
return false;
}
try {
String value = formBean.getString(this.getTarget());
double dbValue = Double.valueOf(value).doubleValue();
if (this.bInt) {
if ((dbValue % 1) != 0) {
return false;
}
}
boolean bMin = false, bMax = false;
if (this.conainMin) {
bMin = dbValue >= this.min;
} else {
bMin = dbValue > this.min;
}
if (this.containMax) {
bMax = dbValue <= this.max;
} else {
bMax = dbValue < this.max;
}
boolean bRet = bMax && bMin;
if (bRet) {
bRet = this.checkPrecision(dbValue);
}
return bRet;
} catch (Exception e) {
}
return false;
}
protected boolean checkPrecision(double value) {
if (this.precision >= 0) {
String temp = "" + value;
int pos = temp.indexOf(".");
int length = temp.length() - 1;
if (pos >= 0) {
while (length > pos) {
if (temp.endsWith("0")) {
length--;
} else {
break;
}
}
return length - pos <= this.precision;
} else {
return true;
}
} else {
return true;
}
}
protected String genCheckCondition() {
StringBuffer buf = new StringBuffer();
buf.append("if(");
buf.append("!isRange(");
buf.append(this.getTargetValueWithForm());
buf.append(",");
if (this.bInt) {
buf.append("true");
} else {
buf.append("false");
}
buf.append(",");
buf.append(this.min);
buf.append(",");
buf.append(this.max);
buf.append(",");
buf.append(this.precision);
buf.append(",");
if (this.conainMin) {
buf.append("true");
} else {
buf.append("false");
}
if (this.containMax) {
buf.append("true");
} else {
buf.append("false");
}
buf.append(")");
buf.append(this.getEmptyJumpScript());
buf.append(")");
return buf.toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -