朋友们,
我们正在编写一个验证框架...
我们有一个如下的配置文件...
<root>
<property name="Premium">
<xmlTag>//Message/Request/Product/Benefit/Premium/amount</xmlTag>
<valueType>float</valueType>
<validation condition=">" value="0">Premium Amount cannot be less than Zero.</validation>
</property>
我使用XPath获取XML值,并通过<valueType>
元素的值将其转换为浮点数...
不,我也把value="0"
转换为浮点数。
现在,我必须应用被指定为condition=">"
的条件。
我不想在IF ELSEIF...ELSE循环中执行此操作。
还有其他方法可以将"<"转换为运算符<
或在字符串上使用比较运算符吗?
这样,我的代码将更简单,对未来的更多运算符也更有用。
=============================================================================
感谢所有的建议和答案...
我决定使用BeanShell的bsh.Interpreter。它为我完成了工作...
以下是给大家的示例代码...
System.out.println(new bsh.Interpreter().eval("1 < 0"));
System.out.println(new bsh.Interpreter().eval("1 > 0"));
System.out.println(new bsh.Interpreter().eval("1 >= 0"));
System.out.println(new bsh.Interpreter().eval("0 >= 0"));
System.out.println(new bsh.Interpreter().eval("1 != 0"));
System.out.println(new bsh.Interpreter().eval("0 != 0"));
System.out.println(new bsh.Interpreter().eval("1 == 0"));
System.out.println(new bsh.Interpreter().eval("0 == 0"));
返回给我 true/false。
谢谢 & 祝好运...
if-then-else
链,可以使用哈希映射。但是动态执行它没有捷径,所以你需要编写大量代码。 - Sergey KalinichenkoSystem.out.println(new bsh.Interpreter().eval("1 < 0")); System.out.println(new bsh.Interpreter().eval("1 > 0")); System.out.println(new bsh.Interpreter().eval("1 >= 0")); System.out.println(new bsh.Interpreter().eval("0 >= 0")); System.out.println(new bsh.Interpreter().eval("1 != 0")); System.out.println(new bsh.Interpreter().eval("0 == 0"));
BeanShell 的bsh.Interpreter完成了魔法。 - SANSSAN