KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: Assert.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2004 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.Iterator JavaDoc;
27 import java.util.LinkedList JavaDoc;
28 import java.util.List JavaDoc;
29
30 import org.ofbiz.base.util.UtilValidate;
31 import org.ofbiz.base.util.UtilXml;
32 import org.ofbiz.base.util.string.FlexibleStringExpander;
33 import org.ofbiz.minilang.SimpleMethod;
34 import org.ofbiz.minilang.method.ContextAccessor;
35 import org.ofbiz.minilang.method.MethodContext;
36 import org.ofbiz.minilang.method.MethodOperation;
37 import org.w3c.dom.Element JavaDoc;
38
39 /**
40  * Operation used to check each sub-condition independently and for each one that fails (does not evaluate to true), adds an error to the error message list.
41  *
42  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
43  * @version $Rev: 5462 $
44  * @since 3.3
45  */

46 public class Assert extends MethodOperation {
47
48     public static final String JavaDoc module = Assert.class.getName();
49
50     protected ContextAccessor errorListAcsr;
51     protected FlexibleStringExpander titleExdr;
52
53     /** List of Conditional objects */
54     protected List JavaDoc conditionalList = new LinkedList JavaDoc();
55
56     public Assert(Element JavaDoc element, SimpleMethod simpleMethod) {
57         super(element, simpleMethod);
58
59         errorListAcsr = new ContextAccessor(element.getAttribute("error-list-name"), "error_list");
60         titleExdr = new FlexibleStringExpander(element.getAttribute("title"));
61         
62         List JavaDoc conditionalElementList = UtilXml.childElementList(element);
63         Iterator JavaDoc conditionalElementIter = conditionalElementList.iterator();
64         while (conditionalElementIter.hasNext()) {
65             Element JavaDoc conditionalElement = (Element JavaDoc) conditionalElementIter.next();
66             this.conditionalList.add(ConditionalFactory.makeConditional(conditionalElement, simpleMethod));
67         }
68     }
69
70     public boolean exec(MethodContext methodContext) {
71         List JavaDoc messages = (List JavaDoc) errorListAcsr.get(methodContext);
72         String JavaDoc title = this.titleExdr.expandString(methodContext.getEnvMap());
73         
74         // check each conditional and if fails generate a message to add to the error list
75
Iterator JavaDoc conditionalIter = conditionalList.iterator();
76         while (conditionalIter.hasNext()) {
77             Conditional condition = (Conditional) conditionalIter.next();
78             boolean conditionTrue = condition.checkCondition(methodContext);
79             
80             if (!conditionTrue) {
81                 // pretty print condition
82
StringBuffer JavaDoc messageBuffer = new StringBuffer JavaDoc();
83                 messageBuffer.append("Assertion ");
84                 if (UtilValidate.isNotEmpty(title)) {
85                     messageBuffer.append("[");
86                     messageBuffer.append(title);
87                     messageBuffer.append("] ");
88                 }
89                 messageBuffer.append("failed: ");
90                 condition.prettyPrint(messageBuffer, methodContext);
91                 messages.add(messageBuffer.toString());
92             }
93         }
94
95         return true;
96     }
97
98     public String JavaDoc rawString() {
99         return expandedString(null);
100     }
101     
102     public String JavaDoc expandedString(MethodContext methodContext) {
103         String JavaDoc title = this.titleExdr.expandString(methodContext.getEnvMap());
104
105         StringBuffer JavaDoc messageBuf = new StringBuffer JavaDoc();
106         messageBuf.append("<assert");
107         if (UtilValidate.isNotEmpty(title)) {
108             messageBuf.append(" title=\"");
109             messageBuf.append(title);
110             messageBuf.append("\"");
111         }
112         messageBuf.append(">");
113         Iterator JavaDoc conditionalIter = conditionalList.iterator();
114         while (conditionalIter.hasNext()) {
115             Conditional condition = (Conditional) conditionalIter.next();
116             condition.prettyPrint(messageBuf, methodContext);
117         }
118         messageBuf.append("</assert>");
119         return messageBuf.toString();
120     }
121 }
122
Popular Tags