KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > minilang > method > conditional > CombinedCondition


1 /*
2  * $Id: CombinedCondition.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.minilang.method.conditional;
25
26 import java.util.*;
27 import org.w3c.dom.*;
28 import org.ofbiz.base.util.*;
29 import org.ofbiz.minilang.*;
30 import org.ofbiz.minilang.method.*;
31
32 /**
33  * Implements generic combining conditions such as or, and, etc.
34  *
35  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
36  * @version $Rev: 5462 $
37  * @since 2.1
38  */

39 public class CombinedCondition implements Conditional {
40     
41     public static final int OR = 1;
42     public static final int XOR = 2;
43     public static final int AND = 3;
44     public static final int NOT = 4;
45
46     SimpleMethod simpleMethod;
47     int conditionType;
48     List subConditions = new LinkedList();
49     
50     public CombinedCondition(Element element, int conditionType, SimpleMethod simpleMethod) {
51         this.simpleMethod = simpleMethod;
52         this.conditionType = conditionType;
53         List subElements = UtilXml.childElementList(element);
54         Iterator subElIter = subElements.iterator();
55         while (subElIter.hasNext()) {
56             Element subElement = (Element) subElIter.next();
57             subConditions.add(ConditionalFactory.makeConditional(subElement, simpleMethod));
58         }
59     }
60
61     public boolean checkCondition(MethodContext methodContext) {
62         if (subConditions.size() == 0) return true;
63         
64         Iterator subCondIter = subConditions.iterator();
65         switch (this.conditionType) {
66             case OR:
67                 while (subCondIter.hasNext()) {
68                     Conditional subCond = (Conditional) subCondIter.next();
69                     if (subCond.checkCondition(methodContext)) {
70                         return true;
71                     }
72                 }
73                 return false;
74             case XOR:
75                 boolean trueFound = false;
76                 while (subCondIter.hasNext()) {
77                     Conditional subCond = (Conditional) subCondIter.next();
78                     if (subCond.checkCondition(methodContext)) {
79                         if (trueFound) {
80                             return false;
81                         } else {
82                             trueFound = true;
83                         }
84                     }
85                 }
86                 return trueFound;
87             case AND:
88                 while (subCondIter.hasNext()) {
89                     Conditional subCond = (Conditional) subCondIter.next();
90                     if (!subCond.checkCondition(methodContext)) {
91                         return false;
92                     }
93                 }
94                 return true;
95             case NOT:
96                 Conditional subCond = (Conditional) subCondIter.next();
97                 return !subCond.checkCondition(methodContext);
98             default:
99                 return false;
100         }
101     }
102
103     public void prettyPrint(StringBuffer JavaDoc messageBuffer, MethodContext methodContext) {
104         messageBuffer.append("(");
105         Iterator subCondIter = subConditions.iterator();
106         while (subCondIter.hasNext()) {
107             Conditional subCond = (Conditional) subCondIter.next();
108             subCond.prettyPrint(messageBuffer, methodContext);
109             if (subCondIter.hasNext()) {
110                 switch (this.conditionType) {
111                 case OR:
112                     messageBuffer.append(" OR ");
113                     break;
114                 case XOR:
115                     messageBuffer.append(" XOR ");
116                     break;
117                 case AND:
118                     messageBuffer.append(" AND ");
119                     break;
120                 case NOT:
121                     messageBuffer.append(" NOT ");
122                     break;
123                 default:
124                     messageBuffer.append("?");
125                 }
126             }
127         }
128         messageBuffer.append(")");
129     }
130 }
131
Popular Tags