KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: EmptyCondition.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 import org.w3c.dom.*;
28 import org.ofbiz.base.util.*;
29 import org.ofbiz.minilang.*;
30 import org.ofbiz.minilang.method.*;
31
32 /**
33  * Implements compare to a constant condition.
34  *
35  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
36  * @version $Rev: 5462 $
37  * @since 2.1
38  */

39 public class EmptyCondition implements Conditional {
40     
41     public static final String JavaDoc module = EmptyCondition.class.getName();
42     
43     SimpleMethod simpleMethod;
44     
45     ContextAccessor mapAcsr;
46     ContextAccessor fieldAcsr;
47     
48     public EmptyCondition(Element element, SimpleMethod simpleMethod) {
49         this.simpleMethod = simpleMethod;
50         
51         this.mapAcsr = new ContextAccessor(element.getAttribute("map-name"));
52         this.fieldAcsr = new ContextAccessor(element.getAttribute("field-name"));
53     }
54
55     public boolean checkCondition(MethodContext methodContext) {
56         // only run subOps if element is empty/null
57
boolean runSubOps = false;
58         Object JavaDoc fieldVal = getFieldVal(methodContext);
59
60         if (fieldVal == null) {
61             runSubOps = true;
62         } else {
63             if (fieldVal instanceof String JavaDoc) {
64                 String JavaDoc fieldStr = (String JavaDoc) fieldVal;
65
66                 if (fieldStr.length() == 0) {
67                     runSubOps = true;
68                 }
69             } else if (fieldVal instanceof Collection) {
70                 Collection fieldCol = (Collection) fieldVal;
71
72                 if (fieldCol.size() == 0) {
73                     runSubOps = true;
74                 }
75             } else if (fieldVal instanceof Map) {
76                 Map fieldMap = (Map) fieldVal;
77
78                 if (fieldMap.size() == 0) {
79                     runSubOps = true;
80                 }
81             }
82         }
83         
84         return runSubOps;
85     }
86     
87     protected Object JavaDoc getFieldVal(MethodContext methodContext) {
88         Object JavaDoc fieldVal = null;
89         if (!mapAcsr.isEmpty()) {
90             Map fromMap = (Map) mapAcsr.get(methodContext);
91             if (fromMap == null) {
92                 if (Debug.infoOn()) Debug.logInfo("Map not found with name " + mapAcsr + ", running operations", module);
93             } else {
94                 fieldVal = fieldAcsr.get(fromMap, methodContext);
95             }
96         } else {
97             // no map name, try the env
98
fieldVal = fieldAcsr.get(methodContext);
99         }
100         return fieldVal;
101     }
102
103     public void prettyPrint(StringBuffer JavaDoc messageBuffer, MethodContext methodContext) {
104         messageBuffer.append("empty[");
105         if (!this.mapAcsr.isEmpty()) {
106             messageBuffer.append(this.mapAcsr);
107             messageBuffer.append(".");
108         }
109         messageBuffer.append(this.fieldAcsr);
110         if (methodContext != null) {
111             messageBuffer.append("=");
112             messageBuffer.append(getFieldVal(methodContext));
113         }
114         messageBuffer.append("]");
115     }
116 }
117
Popular Tags