KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > minilang > method > envops > IterateMap


1 /*
2  * $Id: IterateMap.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2002-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.envops;
25
26 import java.util.Iterator JavaDoc;
27 import java.util.LinkedList JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import org.ofbiz.minilang.SimpleMethod;
32 import org.ofbiz.minilang.method.ContextAccessor;
33 import org.ofbiz.minilang.method.MethodContext;
34 import org.ofbiz.minilang.method.MethodOperation;
35 import org.ofbiz.base.util.Debug;
36 import org.w3c.dom.Element JavaDoc;
37
38 /**
39  * Process sub-operations for each entry in the map
40  *
41  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
42  * @version $Rev: 5462 $
43  * @since 2.0
44  */

45 public class IterateMap extends MethodOperation {
46     
47     public static final String JavaDoc module = IterateMap.class.getName();
48
49     List JavaDoc subOps = new LinkedList JavaDoc();
50
51     ContextAccessor keyAcsr;
52     ContextAccessor valueAcsr;
53     ContextAccessor mapAcsr;
54
55     public IterateMap(Element JavaDoc element, SimpleMethod simpleMethod) {
56         super(element, simpleMethod);
57         this.keyAcsr = new ContextAccessor(element.getAttribute("key-name"));
58         this.valueAcsr = new ContextAccessor(element.getAttribute("value-name"));
59         this.mapAcsr = new ContextAccessor(element.getAttribute("map-name"));
60
61         SimpleMethod.readOperations(element, subOps, simpleMethod);
62     }
63
64     public boolean exec(MethodContext methodContext) {
65         Object JavaDoc fieldVal = null;
66
67         if (mapAcsr.isEmpty()) {
68             Debug.logWarning("No map-name specified in iterate tag, doing nothing: " + rawString(), module);
69             return true;
70         }
71
72         Object JavaDoc oldKey = keyAcsr.get(methodContext);
73         Object JavaDoc oldValue = valueAcsr.get(methodContext);
74         if (oldKey != null) {
75             Debug.logWarning("In iterate-map the key had a non-null value before entering the loop for the operation: " + this.rawString(), module);
76         }
77         if (oldValue != null) {
78             Debug.logWarning("In iterate-map the value had a non-null value before entering the loop for the operation: " + this.rawString(), module);
79         }
80         
81         Map JavaDoc theMap = (Map JavaDoc) mapAcsr.get(methodContext);
82         if (theMap == null) {
83             if (Debug.infoOn()) Debug.logInfo("Map not found with name " + mapAcsr + ", doing nothing: " + rawString(), module);
84             return true;
85         }
86         if (theMap.size() == 0) {
87             if (Debug.verboseOn()) Debug.logVerbose("Map with name " + mapAcsr + " has zero entries, doing nothing: " + rawString(), module);
88             return true;
89         }
90
91         Iterator JavaDoc theIterator = theMap.entrySet().iterator();
92         while (theIterator.hasNext()) {
93             Map.Entry JavaDoc theEntry = (Map.Entry JavaDoc) theIterator.next();
94             keyAcsr.put(methodContext, theEntry.getKey());
95             valueAcsr.put(methodContext, theEntry.getValue());
96
97             if (!SimpleMethod.runSubOps(subOps, methodContext)) {
98                 // only return here if it returns false, otherwise just carry on
99
return false;
100             }
101         }
102
103         return true;
104     }
105
106     public String JavaDoc rawString() {
107         return "<iterate-map map-name=\"" + this.mapAcsr + "\" key=\"" + this.keyAcsr + "\" value=\"" + this.valueAcsr + "\"/>";
108     }
109     public String JavaDoc expandedString(MethodContext methodContext) {
110         // TODO: something more than a stub/dummy
111
return this.rawString();
112     }
113 }
114
Popular Tags