KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > minilang > method > ifops > IfInstanceOf


1 /*
2  * $Id: IfInstanceOf.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  */

25 package org.ofbiz.minilang.method.ifops;
26
27 import java.util.List JavaDoc;
28 import java.util.LinkedList JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import org.ofbiz.minilang.method.MethodOperation;
32 import org.ofbiz.minilang.method.MethodContext;
33 import org.ofbiz.minilang.method.ContextAccessor;
34 import org.ofbiz.minilang.SimpleMethod;
35 import org.ofbiz.base.util.Debug;
36 import org.ofbiz.base.util.ObjectType;
37 import org.ofbiz.base.util.UtilXml;
38
39 import org.w3c.dom.Element JavaDoc;
40
41 /**
42  *
43  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
44  * @version $Rev: 5462 $
45  * @since 3.2
46  */

47 public class IfInstanceOf extends MethodOperation {
48
49     public static final String JavaDoc module = IfInstanceOf.class.getName();
50
51     protected List JavaDoc subOps = new LinkedList JavaDoc();
52     protected List JavaDoc elseSubOps = null;
53
54     protected ContextAccessor mapAcsr = null;
55     protected ContextAccessor fieldAcsr = null;
56     protected String JavaDoc className = null;
57
58     public IfInstanceOf(Element JavaDoc element, SimpleMethod simpleMethod) {
59         super(element, simpleMethod);
60         this.mapAcsr = new ContextAccessor(element.getAttribute("map-name"));
61         this.fieldAcsr = new ContextAccessor(element.getAttribute("field-name"));
62         this.className = element.getAttribute("class");
63
64         SimpleMethod.readOperations(element, subOps, simpleMethod);
65
66         Element JavaDoc elseElement = UtilXml.firstChildElement(element, "else");
67         if (elseElement != null) {
68             elseSubOps = new LinkedList JavaDoc();
69             SimpleMethod.readOperations(elseElement, elseSubOps, simpleMethod);
70         }
71     }
72
73     public boolean exec(MethodContext methodContext) {
74         // only run subOps if element is instanceOf
75
boolean runSubOps = false;
76         Object JavaDoc fieldVal = null;
77
78         if (!mapAcsr.isEmpty()) {
79             Map JavaDoc fromMap = (Map JavaDoc) mapAcsr.get(methodContext);
80             if (fromMap == null) {
81                 if (Debug.infoOn()) Debug.logInfo("Map not found with name " + mapAcsr + ", running operations", module);
82             } else {
83                 fieldVal = fieldAcsr.get(fromMap, methodContext);
84             }
85         } else {
86             // no map name, try the env
87
fieldVal = fieldAcsr.get(methodContext);
88         }
89
90         runSubOps = ObjectType.instanceOf(fieldVal, className);
91
92         if (runSubOps) {
93             return SimpleMethod.runSubOps(subOps, methodContext);
94         } else {
95             if (elseSubOps != null) {
96                 return SimpleMethod.runSubOps(elseSubOps, methodContext);
97             } else {
98                 return true;
99             }
100         }
101     }
102
103     public String JavaDoc rawString() {
104         // TODO: add all attributes and other info
105
return "<if-instance-of field-name=\"" + this.fieldAcsr + "\" map-name=\"" + this.mapAcsr + "\"/>";
106     }
107     public String JavaDoc expandedString(MethodContext methodContext) {
108         // TODO: something more than a stub/dummy
109
return this.rawString();
110     }
111 }
112
Popular Tags