001: /*
002: * $Id: CombinedCondition.java,v 1.1 2003/08/17 06:06:12 ajzeneski Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.minilang.method.conditional;
025:
026: import java.util.*;
027: import org.w3c.dom.*;
028: import org.ofbiz.base.util.*;
029: import org.ofbiz.minilang.*;
030: import org.ofbiz.minilang.method.*;
031:
032: /**
033: * Implements generic combining conditions such as or, and, etc.
034: *
035: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
036: * @version $Revision: 1.1 $
037: * @since 2.1
038: */
039: public class CombinedCondition implements Conditional {
040:
041: public static final int OR = 1;
042: public static final int XOR = 2;
043: public static final int AND = 3;
044: public static final int NOT = 4;
045:
046: SimpleMethod simpleMethod;
047: int conditionType;
048: List subConditions = new LinkedList();
049:
050: public CombinedCondition(Element element, int conditionType,
051: SimpleMethod simpleMethod) {
052: this .simpleMethod = simpleMethod;
053: this .conditionType = conditionType;
054: List subElements = UtilXml.childElementList(element, null);
055: Iterator subElIter = subElements.iterator();
056: while (subElIter.hasNext()) {
057: Element subElement = (Element) subElIter.next();
058: subConditions.add(ConditionalFactory.makeConditional(
059: subElement, simpleMethod));
060: }
061: }
062:
063: public boolean checkCondition(MethodContext methodContext) {
064: if (subConditions.size() == 0)
065: return true;
066:
067: Iterator subCondIter = subConditions.iterator();
068: switch (this .conditionType) {
069: case OR:
070: while (subCondIter.hasNext()) {
071: Conditional subCond = (Conditional) subCondIter.next();
072: if (subCond.checkCondition(methodContext)) {
073: return true;
074: }
075: }
076: return false;
077: case XOR:
078: boolean trueFound = false;
079: while (subCondIter.hasNext()) {
080: Conditional subCond = (Conditional) subCondIter.next();
081: if (subCond.checkCondition(methodContext)) {
082: if (trueFound) {
083: return false;
084: } else {
085: trueFound = true;
086: }
087: }
088: }
089: return trueFound;
090: case AND:
091: while (subCondIter.hasNext()) {
092: Conditional subCond = (Conditional) subCondIter.next();
093: if (!subCond.checkCondition(methodContext)) {
094: return false;
095: }
096: }
097: return true;
098: case NOT:
099: Conditional subCond = (Conditional) subCondIter.next();
100: return !subCond.checkCondition(methodContext);
101: default:
102: return false;
103: }
104: }
105:
106: }
|