001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.minilang.method.envops;
019:
020: import java.util.Iterator;
021: import java.util.LinkedList;
022: import java.util.List;
023: import java.util.Map;
024:
025: import org.ofbiz.minilang.SimpleMethod;
026: import org.ofbiz.minilang.method.ContextAccessor;
027: import org.ofbiz.minilang.method.MethodContext;
028: import org.ofbiz.minilang.method.MethodOperation;
029: import org.ofbiz.base.util.Debug;
030: import org.w3c.dom.Element;
031:
032: /**
033: * Process sub-operations for each entry in the map
034: */
035: public class IterateMap extends MethodOperation {
036:
037: public static final String module = IterateMap.class.getName();
038:
039: List subOps = new LinkedList();
040:
041: ContextAccessor keyAcsr;
042: ContextAccessor valueAcsr;
043: ContextAccessor mapAcsr;
044:
045: public IterateMap(Element element, SimpleMethod simpleMethod) {
046: super (element, simpleMethod);
047: this .keyAcsr = new ContextAccessor(element
048: .getAttribute("key-name"));
049: this .valueAcsr = new ContextAccessor(element
050: .getAttribute("value-name"));
051: this .mapAcsr = new ContextAccessor(element
052: .getAttribute("map-name"));
053:
054: SimpleMethod.readOperations(element, subOps, simpleMethod);
055: }
056:
057: public boolean exec(MethodContext methodContext) {
058:
059: if (mapAcsr.isEmpty()) {
060: Debug.logWarning(
061: "No map-name specified in iterate tag, doing nothing: "
062: + rawString(), module);
063: return true;
064: }
065:
066: Object oldKey = keyAcsr.get(methodContext);
067: Object oldValue = valueAcsr.get(methodContext);
068: if (oldKey != null) {
069: Debug
070: .logWarning(
071: "In iterate-map the key had a non-null value before entering the loop for the operation: "
072: + this .rawString(), module);
073: }
074: if (oldValue != null) {
075: Debug
076: .logWarning(
077: "In iterate-map the value had a non-null value before entering the loop for the operation: "
078: + this .rawString(), module);
079: }
080:
081: Map theMap = (Map) mapAcsr.get(methodContext);
082: if (theMap == null) {
083: if (Debug.infoOn())
084: Debug.logInfo("Map not found with name " + mapAcsr
085: + ", doing nothing: " + rawString(), module);
086: return true;
087: }
088: if (theMap.size() == 0) {
089: if (Debug.verboseOn())
090: Debug.logVerbose("Map with name " + mapAcsr
091: + " has zero entries, doing nothing: "
092: + rawString(), module);
093: return true;
094: }
095:
096: Iterator theIterator = theMap.entrySet().iterator();
097: while (theIterator.hasNext()) {
098: Map.Entry theEntry = (Map.Entry) theIterator.next();
099: keyAcsr.put(methodContext, theEntry.getKey());
100: valueAcsr.put(methodContext, theEntry.getValue());
101:
102: if (!SimpleMethod.runSubOps(subOps, methodContext)) {
103: // only return here if it returns false, otherwise just carry on
104: return false;
105: }
106: }
107:
108: return true;
109: }
110:
111: public String rawString() {
112: return "<iterate-map map-name=\"" + this .mapAcsr + "\" key=\""
113: + this .keyAcsr + "\" value=\"" + this .valueAcsr
114: + "\"/>";
115: }
116:
117: public String expandedString(MethodContext methodContext) {
118: // TODO: something more than a stub/dummy
119: return this.rawString();
120: }
121: }
|