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.callops;
019:
020: import java.util.*;
021:
022: import org.w3c.dom.*;
023: import org.ofbiz.base.util.*;
024: import org.ofbiz.minilang.*;
025: import org.ofbiz.minilang.method.*;
026: import org.ofbiz.minilang.operation.*;
027:
028: /**
029: * An event operation that calls a simple map processor inlined or from a separate file
030: */
031: public class CallSimpleMapProcessor extends MethodOperation {
032:
033: String xmlResource;
034: String processorName;
035: ContextAccessor inMapAcsr;
036: ContextAccessor outMapAcsr;
037: ContextAccessor errorListAcsr;
038:
039: MapProcessor inlineMapProcessor = null;
040:
041: public CallSimpleMapProcessor(Element element,
042: SimpleMethod simpleMethod) {
043: super (element, simpleMethod);
044: xmlResource = element.getAttribute("xml-resource");
045: processorName = element.getAttribute("processor-name");
046: inMapAcsr = new ContextAccessor(element
047: .getAttribute("in-map-name"));
048: outMapAcsr = new ContextAccessor(element
049: .getAttribute("out-map-name"));
050: errorListAcsr = new ContextAccessor(element
051: .getAttribute("error-list-name"), "error_list");
052:
053: Element simpleMapProcessorElement = UtilXml.firstChildElement(
054: element, "simple-map-processor");
055: if (simpleMapProcessorElement != null) {
056: inlineMapProcessor = new MapProcessor(
057: simpleMapProcessorElement);
058: }
059: }
060:
061: public boolean exec(MethodContext methodContext) {
062: List messages = (List) errorListAcsr.get(methodContext);
063: if (messages == null) {
064: messages = new LinkedList();
065: errorListAcsr.put(methodContext, messages);
066: }
067:
068: Map inMap = (Map) inMapAcsr.get(methodContext);
069: if (inMap == null) {
070: inMap = new HashMap();
071: inMapAcsr.put(methodContext, inMap);
072: }
073:
074: Map outMap = (Map) outMapAcsr.get(methodContext);
075: if (outMap == null) {
076: outMap = new HashMap();
077: outMapAcsr.put(methodContext, outMap);
078: }
079:
080: // run external map processor first
081: if (this .xmlResource != null && this .xmlResource.length() > 0
082: && this .processorName != null
083: && this .processorName.length() > 0) {
084: String xmlResource = methodContext
085: .expandString(this .xmlResource);
086: String processorName = methodContext
087: .expandString(this .processorName);
088: try {
089: org.ofbiz.minilang.SimpleMapProcessor
090: .runSimpleMapProcessor(xmlResource,
091: processorName, inMap, outMap, messages,
092: methodContext.getLocale(),
093: methodContext.getLoader());
094: } catch (MiniLangException e) {
095: messages
096: .add("Error running SimpleMapProcessor in XML file \""
097: + xmlResource + "\": " + e.toString());
098: }
099: }
100:
101: // run inlined map processor last so it can override the external map processor
102: if (inlineMapProcessor != null) {
103: inlineMapProcessor.exec(inMap, outMap, messages,
104: (methodContext.getRequest() != null ? methodContext
105: .getRequest().getLocale() : null),
106: methodContext.getLoader());
107: }
108:
109: return true;
110: }
111:
112: public String rawString() {
113: // TODO: something more than the empty tag
114: return "<call-simple-map-processor/>";
115: }
116:
117: public String expandedString(MethodContext methodContext) {
118: // TODO: something more than a stub/dummy
119: return this.rawString();
120: }
121: }
|