001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.minilang.method.conditional;
019:
020: import java.util.*;
021:
022: import org.w3c.dom.*;
023: import org.ofbiz.base.util.*;
024: import org.ofbiz.minilang.*;
025: import org.ofbiz.minilang.method.*;
026:
027: /**
028: * Represents the top-level element and only mounted operation for the more flexible if structure.
029: */
030: public class MasterIf extends MethodOperation {
031:
032: Conditional condition;
033:
034: List thenSubOps = new LinkedList();
035: List elseSubOps = null;
036:
037: List elseIfs = null;
038:
039: public MasterIf(Element element, SimpleMethod simpleMethod) {
040: super (element, simpleMethod);
041:
042: Element conditionElement = UtilXml.firstChildElement(element,
043: "condition");
044: Element conditionChildElement = UtilXml
045: .firstChildElement(conditionElement);
046: this .condition = ConditionalFactory.makeConditional(
047: conditionChildElement, simpleMethod);
048:
049: Element thenElement = UtilXml
050: .firstChildElement(element, "then");
051: SimpleMethod.readOperations(thenElement, thenSubOps,
052: simpleMethod);
053:
054: List elseIfElements = UtilXml.childElementList(element,
055: "else-if");
056: if (elseIfElements != null && elseIfElements.size() > 0) {
057: elseIfs = new LinkedList();
058: Iterator eieIter = elseIfElements.iterator();
059: while (eieIter.hasNext()) {
060: Element elseIfElement = (Element) eieIter.next();
061: elseIfs.add(new ElseIf(elseIfElement, simpleMethod));
062: }
063: }
064:
065: Element elseElement = UtilXml
066: .firstChildElement(element, "else");
067: if (elseElement != null) {
068: elseSubOps = new LinkedList();
069: SimpleMethod.readOperations(elseElement, elseSubOps,
070: simpleMethod);
071: }
072: }
073:
074: public boolean exec(MethodContext methodContext) {
075: // if conditions fails, always return true; if a sub-op returns false
076: // return false and stop, otherwise return true
077: // return true;
078:
079: // only run subOps if element is empty/null
080: boolean runSubOps = condition.checkCondition(methodContext);
081:
082: if (runSubOps) {
083: return SimpleMethod.runSubOps(thenSubOps, methodContext);
084: } else {
085:
086: // try the else-ifs
087: if (elseIfs != null && elseIfs.size() > 0) {
088: Iterator elseIfIter = elseIfs.iterator();
089: while (elseIfIter.hasNext()) {
090: ElseIf elseIf = (ElseIf) elseIfIter.next();
091: if (elseIf.checkCondition(methodContext)) {
092: return elseIf.runSubOps(methodContext);
093: }
094: }
095: }
096:
097: if (elseSubOps != null) {
098: return SimpleMethod
099: .runSubOps(elseSubOps, methodContext);
100: } else {
101: return true;
102: }
103: }
104: }
105:
106: public String rawString() {
107: return expandedString(null);
108: }
109:
110: public String expandedString(MethodContext methodContext) {
111: // TODO: fill in missing details, if needed
112: StringBuffer messageBuf = new StringBuffer();
113: this .condition.prettyPrint(messageBuf, methodContext);
114: return "<if><condition>" + messageBuf + "</condition></if>";
115: }
116: }
|