KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > formview > util > FormulaUtil


1 package net.sourceforge.formview.util;
2
3 import java.util.Map JavaDoc;
4
5 public class FormulaUtil {
6
7     public static boolean executeCondition(String JavaDoc formula, Map JavaDoc attributeMap) {
8         // TODO : this method support JUST formula
9
// a == VALUE and a != VALUE
10
boolean equalCondition = true;
11         int indexBeforeOp = formula.indexOf("==");
12         if (indexBeforeOp == - 1) {
13             indexBeforeOp = formula.indexOf("!=");
14             equalCondition = false;
15         }
16
17         if (indexBeforeOp != -1) {
18             String JavaDoc leftOp = formula.substring(0, indexBeforeOp);
19             
20             
21             String JavaDoc rightOp = formula.substring(indexBeforeOp + 2, formula.length());
22             
23             if (leftOp != null && leftOp.length() > 0 &&
24                 rightOp != null && rightOp.length() > 0 ) {
25                 
26                 leftOp = leftOp.replace('{', ' ');
27                 leftOp = leftOp.replace('$', ' ');
28                 leftOp = leftOp.trim();
29                 
30                 String JavaDoc valueOfLeftMap = (String JavaDoc)attributeMap.get(leftOp);
31                 if (valueOfLeftMap != null)
32                     valueOfLeftMap = valueOfLeftMap.toUpperCase();
33                 else
34                     valueOfLeftMap = "NULL";
35                 
36                 rightOp = rightOp.replace('}', ' ');
37                 rightOp = rightOp.replace('$', ' ');
38                 
39                 rightOp = rightOp.trim().toUpperCase();
40                 
41                 if (equalCondition) {
42                     return rightOp.equals(valueOfLeftMap);
43                 }
44                 else {
45                     return !rightOp.equals(valueOfLeftMap);
46                 }
47             }
48         }
49         return false;
50     }
51
52
53 }
54
Popular Tags