📄 condition.java
字号:
* Will evaluate a calendar condition.
*
* @param calField the calendar field from Calendar
*/
private ConditionMatch evaluateCalendarCondition(final int calField) {
return evaluateNumericCondition((Calendar.getInstance()).get(calField));
}
/**
* Will evaluate usign operator.
*
* @param compareWith what to compare with
* @return true or false
*/
private ConditionMatch evaluateNumericCondition(final long compareWith) {
if (log.isDebugEnabled()) {
log.debug("evaluating with operator, is " + compareWith + " " + getOperator() + " " + numericValue);
}
switch (operator) {
case OPERATOR_NOT_EQUAL:
return compareWith != numericValue ? new ConditionMatch() : null;
case OPERATOR_GREATER_THAN:
return compareWith > numericValue ? new ConditionMatch() : null;
case OPERATOR_LESS_THAN:
return compareWith < numericValue ? new ConditionMatch() : null;
case OPERATOR_GREATER_THAN_OR_EQUAL:
return compareWith >= numericValue ? new ConditionMatch() : null;
case OPERATOR_LESS_THAN_OR_EQUAL:
return compareWith <= numericValue ? new ConditionMatch() : null;
default:
return compareWith == numericValue ? new ConditionMatch() : null;
}
}
/**
* Returns false on failure. Use getError to get the description of the error.
*
* @return weather or not the condition was successful in initialisation.
*/
public boolean initialise() {
initialised = true;
if (error != null) {
return false;
}
// make sure we default to header if not set
if (type == 0) {
type = TYPE_HEADER;
}
switch (type) {
// note, only numeric specified others handled by default:
case TYPE_SERVER_PORT:
initNumericValue();
break;
case TYPE_TIME:
initNumericValue();
break;
case TYPE_TIME_YEAR:
initNumericValue();
break;
case TYPE_TIME_MONTH:
initNumericValue();
break;
case TYPE_TIME_DAY_OF_MONTH:
initNumericValue();
break;
case TYPE_TIME_DAY_OF_WEEK:
initNumericValue();
break;
case TYPE_TIME_AMPM:
initNumericValue();
break;
case TYPE_TIME_HOUR_OF_DAY:
initNumericValue();
break;
case TYPE_TIME_MINUTE:
initNumericValue();
break;
case TYPE_TIME_SECOND:
initNumericValue();
break;
case TYPE_TIME_MILLISECOND:
initNumericValue();
break;
case TYPE_CONTENT_LENGTH:
initNumericValue();
break;
case TYPE_LOCAL_PORT:
initNumericValue();
break;
case TYPE_USER_IN_ROLE:
// we only care to make sure the user has entered a name (if no name use value)
// note regexps cannot be entered against this due to limitations in servlet spec
if (StringUtils.isBlank(name)) {
name = strValue;
}
break;
case TYPE_SESSION_ATTRIBUTE:
if (StringUtils.isBlank(name)) {
setError("you must set a name for session attributes");
}
initStringValue();
break;
case TYPE_ATTRIBUTE:
if (StringUtils.isBlank(name)) {
setError("you must set a name for attributes");
}
initStringValue();
break;
case TYPE_HEADER:
if (StringUtils.isBlank(name)) {
setError("you must set a name for a header");
}
initStringValue();
break;
default:
// other generic types
initStringValue();
}
if (log.isDebugEnabled()) {
log.debug("loaded condition " + getType() + " " + name + " " + strValue);
}
valid = error == null;
return valid;
}
private void initStringValue() {
if (StringUtils.isBlank(strValue)) {
log.debug("value is blank initing pattern to null");
pattern = null;
return;
}
try {
if (caseSensitive) {
pattern = Pattern.compile(strValue);
} else {
pattern = Pattern.compile(strValue, Pattern.CASE_INSENSITIVE);
}
} catch (PatternSyntaxException e) {
//e.printStackTrace();
setError("Problem compiling regular expression " + strValue + " (" + e.getMessage() + ")");
return;
}
if (pattern == null) {
setError("value \"" + strValue + "\" did not compile as a regular expression");
}
}
/**
* Will init a numeric value type ie port.
*/
private void initNumericValue() {
if (numericValue == 0) {
numericValue = NumberUtils.stringToLong(StringUtils.trim(strValue));
if (numericValue == 0 && !"0".equals(strValue)) {
setError("Value " + strValue + " is not a valid number (tried to cast to java type long)");
}
}
}
protected void setError(String s) {
super.setError(s);
log.error("Condition " + id + " had error: " + s);
}
/**
* Will get the operator type.
*
* @return notequal, greater etc.
*/
public String getOperator() {
switch (operator) {
case OPERATOR_NOT_EQUAL:
return "notequal";
case OPERATOR_GREATER_THAN:
return "greater";
case OPERATOR_LESS_THAN:
return "less";
case OPERATOR_GREATER_THAN_OR_EQUAL:
return "greaterorequal";
case OPERATOR_LESS_THAN_OR_EQUAL:
return "lessorequal";
case OPERATOR_EQUAL:
return "equal";
default:
return "";
}
}
/**
* Will ste the operator.
*
* @param operator type
*/
public void setOperator(final String operator) {
if ("notequal".equals(operator)) {
this.operator = OPERATOR_NOT_EQUAL;
} else if ("greater".equals(operator)) {
this.operator = OPERATOR_GREATER_THAN;
} else if ("less".equals(operator)) {
this.operator = OPERATOR_LESS_THAN;
} else if ("greaterorequal".equals(operator)) {
this.operator = OPERATOR_GREATER_THAN_OR_EQUAL;
} else if ("lessorequal".equals(operator)) {
this.operator = OPERATOR_LESS_THAN_OR_EQUAL;
} else if ("equal".equals(operator) || StringUtils.isBlank(operator)) {
this.operator = OPERATOR_EQUAL;
} else {
setError("Operator " + operator + " is not valid");
}
}
/**
* Will get the name.
*
* @return String
*/
public String getName() {
return name;
}
/**
* Will set the name.
*
* @param name the name
*/
public void setName(final String name) {
this.name = name;
}
/**
* Will return "add" or "or".
*
* @return "add" or "or"
*/
public String getNext() {
if (processNextOr) return "or";
return "and";
}
/**
* Will set next.
*
* @param next "or" or "and"
*/
public void setNext(final String next) {
if ("or".equals(next)) {
this.processNextOr = true;
} else if ("and".equals(next) || StringUtils.isBlank(next)) {
this.processNextOr = false;
} else {
setError("Next " + next + " is not valid (can be 'and', 'or')");
}
}
/**
* Will get the value.
*
* @return String
*/
public String getValue() {
return strValue;
}
/**
* Will set the value.
*
* @param value the value
*/
public void setValue(final String value) {
this.strValue = value;
}
/**
* True if process next is or.
*
* @return boolean
*/
public boolean isProcessNextOr() {
return processNextOr;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public boolean isCaseSensitive() {
return caseSensitive;
}
public void setCaseSensitive(boolean caseSensitive) {
this.caseSensitive = caseSensitive;
}
public String getDisplayName() {
return "Condtition " + id;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -