KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: MasterIf.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
28 import org.w3c.dom.*;
29 import org.ofbiz.base.util.*;
30 import org.ofbiz.minilang.*;
31 import org.ofbiz.minilang.method.*;
32
33 /**
34  * Represents the top-level element and only mounted operation for the more flexible if structure.
35  *
36  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
37  * @version $Rev: 5462 $
38  * @since 2.1
39  */

40 public class MasterIf extends MethodOperation {
41
42     Conditional condition;
43
44     List thenSubOps = new LinkedList();
45     List elseSubOps = null;
46
47     List elseIfs = null;
48
49     public MasterIf(Element element, SimpleMethod simpleMethod) {
50         super(element, simpleMethod);
51         
52         Element conditionElement = UtilXml.firstChildElement(element, "condition");
53         Element conditionChildElement = UtilXml.firstChildElement(conditionElement);
54         this.condition = ConditionalFactory.makeConditional(conditionChildElement, simpleMethod);
55         
56         Element thenElement = UtilXml.firstChildElement(element, "then");
57         SimpleMethod.readOperations(thenElement, thenSubOps, simpleMethod);
58         
59         List elseIfElements = UtilXml.childElementList(element, "else-if");
60         if (elseIfElements != null && elseIfElements.size() > 0) {
61             elseIfs = new LinkedList();
62             Iterator eieIter = elseIfElements.iterator();
63             while (eieIter.hasNext()) {
64                 Element elseIfElement = (Element) eieIter.next();
65                 elseIfs.add(new ElseIf(elseIfElement, simpleMethod));
66             }
67         }
68         
69         Element elseElement = UtilXml.firstChildElement(element, "else");
70         if (elseElement != null) {
71             elseSubOps = new LinkedList();
72             SimpleMethod.readOperations(elseElement, elseSubOps, simpleMethod);
73         }
74     }
75
76     public boolean exec(MethodContext methodContext) {
77         // if conditions fails, always return true; if a sub-op returns false
78
// return false and stop, otherwise return true
79
// return true;
80

81         // only run subOps if element is empty/null
82
boolean runSubOps = condition.checkCondition(methodContext);
83
84         if (runSubOps) {
85             return SimpleMethod.runSubOps(thenSubOps, methodContext);
86         } else {
87             
88             // try the else-ifs
89
if (elseIfs != null && elseIfs.size() > 0) {
90                 Iterator elseIfIter = elseIfs.iterator();
91                 while (elseIfIter.hasNext()) {
92                     ElseIf elseIf = (ElseIf) elseIfIter.next();
93                     if (elseIf.checkCondition(methodContext)) {
94                         return elseIf.runSubOps(methodContext);
95                     }
96                 }
97             }
98             
99             if (elseSubOps != null) {
100                 return SimpleMethod.runSubOps(elseSubOps, methodContext);
101             } else {
102                 return true;
103             }
104         }
105     }
106
107     public String JavaDoc rawString() {
108         return expandedString(null);
109     }
110
111     public String JavaDoc expandedString(MethodContext methodContext) {
112         // TODO: fill in missing details, if needed
113
StringBuffer JavaDoc messageBuf = new StringBuffer JavaDoc();
114         this.condition.prettyPrint(messageBuf, methodContext);
115         return "<if><condition>" + messageBuf + "</condition></if>";
116     }
117 }
118
Popular Tags