001: /*
002: * $Id: MasterIf.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:
028: import org.w3c.dom.*;
029: import org.ofbiz.base.util.*;
030: import org.ofbiz.minilang.*;
031: import org.ofbiz.minilang.method.*;
032:
033: /**
034: * Represents the top-level element and only mounted operation for the more flexible if structure.
035: *
036: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
037: * @version $Revision: 1.1 $
038: * @since 2.1
039: */
040: public class MasterIf extends MethodOperation {
041:
042: Conditional condition;
043:
044: List thenSubOps = new LinkedList();
045: List elseSubOps = null;
046:
047: List elseIfs = null;
048:
049: public MasterIf(Element element, SimpleMethod simpleMethod) {
050: super (element, simpleMethod);
051:
052: Element conditionElement = UtilXml.firstChildElement(element,
053: "condition");
054: Element conditionChildElement = UtilXml.firstChildElement(
055: conditionElement, null);
056: this .condition = ConditionalFactory.makeConditional(
057: conditionChildElement, simpleMethod);
058:
059: Element thenElement = UtilXml
060: .firstChildElement(element, "then");
061: SimpleMethod.readOperations(thenElement, thenSubOps,
062: simpleMethod);
063:
064: List elseIfElements = UtilXml.childElementList(element,
065: "else-if");
066: if (elseIfElements != null && elseIfElements.size() > 0) {
067: elseIfs = new LinkedList();
068: Iterator eieIter = elseIfElements.iterator();
069: while (eieIter.hasNext()) {
070: Element elseIfElement = (Element) eieIter.next();
071: elseIfs.add(new ElseIf(elseIfElement, simpleMethod));
072: }
073: }
074:
075: Element elseElement = UtilXml
076: .firstChildElement(element, "else");
077: if (elseElement != null) {
078: elseSubOps = new LinkedList();
079: SimpleMethod.readOperations(elseElement, elseSubOps,
080: simpleMethod);
081: }
082: }
083:
084: public boolean exec(MethodContext methodContext) {
085: // if conditions fails, always return true; if a sub-op returns false
086: // return false and stop, otherwise return true
087: // return true;
088:
089: // only run subOps if element is empty/null
090: boolean runSubOps = condition.checkCondition(methodContext);
091:
092: if (runSubOps) {
093: return SimpleMethod.runSubOps(thenSubOps, methodContext);
094: } else {
095:
096: // try the else-ifs
097: if (elseIfs != null && elseIfs.size() > 0) {
098: Iterator elseIfIter = elseIfs.iterator();
099: while (elseIfIter.hasNext()) {
100: ElseIf elseIf = (ElseIf) elseIfIter.next();
101: if (elseIf.checkCondition(methodContext)) {
102: return elseIf.runSubOps(methodContext);
103: }
104: }
105: }
106:
107: if (elseSubOps != null) {
108: return SimpleMethod
109: .runSubOps(elseSubOps, methodContext);
110: } else {
111: return true;
112: }
113: }
114: }
115: }
|