不好意思,我实在看不明白,有人能帮忙解释一下这个方法是干什么用的吗?
/**
* Evaluate @context@=value or @context@!value or @context@^value.
* <pre>
* value: strips ' and " always (no escape or mid stream)
* value: can also be a context variable
* </pre>
* @param source class implementing get_ValueAsString(variable)
* @param logic logic tuple
* @return true or false
*/
private static boolean evaluateLogicTuple (Evaluatee source, String logic)
{
StringTokenizer st = new StringTokenizer (logic.trim(), "!=^><", true);
if (st.countTokens() != 3)
{
log.warning("Logic tuple does not comply with format "
+ "'@context@=value' where operand could be one of '=!^><' => " + logic);
return false;
}
// First Part
String first = st.nextToken().trim(); // get '@tag@'
String firstEval = first.trim();
if (first.indexOf('@') != -1) // variable
{
first = first.replace ('@', ' ').trim (); // strip 'tag'
firstEval = source.get_ValueAsString (first); // replace with it's value
if (firstEval == null)
firstEval = "";
}
firstEval = firstEval.replace('\'', ' ').replace('"', ' ').trim(); // strip ' and " // Comperator
String operand = st.nextToken();
// Second Part
String second = st.nextToken(); // get value
String secondEval = second.trim();
if (second.indexOf('@') != -1) // variable
{
second = second.replace('@', ' ').trim(); // strip tag
secondEval = source.get_ValueAsString (second); // replace with it's value
if (secondEval == null)
secondEval = "";
}
secondEval = secondEval.replace('\'', ' ').replace('"', ' ').trim(); // strip ' and " // Handling of ID compare (null => 0)
if (first.indexOf("_ID") != -1 && firstEval.length() == 0)
firstEval = "0";
if (second.indexOf("_ID") != -1 && secondEval.length() == 0)
secondEval = "0"; // Logical Comparison
boolean result = evaluateLogicTuple (firstEval, operand, secondEval);
//
if (log.isLevelFinest())
log.finest(logic
+ " => \"" + firstEval + "\" " + operand + " \"" + secondEval + "\" => " + result);
//
return result;
} // evaluateLogicTuple
代码来自:https://svn.bitsoftware.ro:8443/dev/SocrateOpen/tags/1.3/common/src/org/compiere/util/Evaluator.java
/**
* Evaluate @context@=value or @context@!value or @context@^value.
* <pre>
* value: strips ' and " always (no escape or mid stream)
* value: can also be a context variable
* </pre>
* @param source class implementing get_ValueAsString(variable)
* @param logic logic tuple
* @return true or false
*/
private static boolean evaluateLogicTuple (Evaluatee source, String logic)
{
StringTokenizer st = new StringTokenizer (logic.trim(), "!=^><", true);
if (st.countTokens() != 3)
{
log.warning("Logic tuple does not comply with format "
+ "'@context@=value' where operand could be one of '=!^><' => " + logic);
return false;
}
// First Part
String first = st.nextToken().trim(); // get '@tag@'
String firstEval = first.trim();
if (first.indexOf('@') != -1) // variable
{
first = first.replace ('@', ' ').trim (); // strip 'tag'
firstEval = source.get_ValueAsString (first); // replace with it's value
if (firstEval == null)
firstEval = "";
}
firstEval = firstEval.replace('\'', ' ').replace('"', ' ').trim(); // strip ' and " // Comperator
String operand = st.nextToken();
// Second Part
String second = st.nextToken(); // get value
String secondEval = second.trim();
if (second.indexOf('@') != -1) // variable
{
second = second.replace('@', ' ').trim(); // strip tag
secondEval = source.get_ValueAsString (second); // replace with it's value
if (secondEval == null)
secondEval = "";
}
secondEval = secondEval.replace('\'', ' ').replace('"', ' ').trim(); // strip ' and " // Handling of ID compare (null => 0)
if (first.indexOf("_ID") != -1 && firstEval.length() == 0)
firstEval = "0";
if (second.indexOf("_ID") != -1 && secondEval.length() == 0)
secondEval = "0"; // Logical Comparison
boolean result = evaluateLogicTuple (firstEval, operand, secondEval);
//
if (log.isLevelFinest())
log.finest(logic
+ " => \"" + firstEval + "\" " + operand + " \"" + secondEval + "\" => " + result);
//
return result;
} // evaluateLogicTuple
代码来自:https://svn.bitsoftware.ro:8443/dev/SocrateOpen/tags/1.3/common/src/org/compiere/util/Evaluator.java
解析成3个字符串
firstEval, operand, secondEval
解析处理完成调用evaluateLogicTuple (firstEval, operand, secondEval);
返回结果
a,b可以是一段形如@var@的占位符,将使用source中定义的var的值来替换;也可以是直接的字符串值,那就不用替换
ab之间是“!=^><”中的某一个操作符
在解析这串表达式并替换变量值之后,再计算这个表达
boolean result = evaluateLogicTuple (firstEval, operand, secondEval);
分析source是否满足logic规则,里面的内容比较复杂,应该和正则表达式的功能差不多,只不过是自己实现的