01: /*******************************************************************************
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: *******************************************************************************/package org.ofbiz.minilang.method.envops;
19:
20: import java.util.*;
21:
22: import org.w3c.dom.*;
23: import org.ofbiz.base.util.*;
24: import org.ofbiz.minilang.*;
25: import org.ofbiz.minilang.method.*;
26:
27: /**
28: * Copies an environment field to a map field
29: */
30: public class EnvToField extends MethodOperation {
31:
32: public static final String module = EnvToField.class.getName();
33:
34: ContextAccessor envAcsr;
35: ContextAccessor mapAcsr;
36: ContextAccessor fieldAcsr;
37:
38: public EnvToField(Element element, SimpleMethod simpleMethod) {
39: super (element, simpleMethod);
40: envAcsr = new ContextAccessor(element.getAttribute("env-name"));
41: mapAcsr = new ContextAccessor(element.getAttribute("map-name"));
42: fieldAcsr = new ContextAccessor(element
43: .getAttribute("field-name"));
44:
45: // set fieldAcsr to their defualt value of envAcsr if empty
46: if (fieldAcsr.isEmpty()) {
47: fieldAcsr = envAcsr;
48: }
49: }
50:
51: public boolean exec(MethodContext methodContext) {
52: Object envVar = envAcsr.get(methodContext);
53:
54: if (envVar == null) {
55: Debug.logWarning("Environment field not found with name "
56: + envAcsr + ", not copying env field", module);
57: return true;
58: }
59:
60: if (!mapAcsr.isEmpty()) {
61: Map toMap = (Map) mapAcsr.get(methodContext);
62:
63: if (toMap == null) {
64: if (Debug.verboseOn())
65: Debug.logVerbose("Map not found with name "
66: + mapAcsr + ", creating new map", module);
67: toMap = new HashMap();
68: mapAcsr.put(methodContext, toMap);
69: }
70: fieldAcsr.put(toMap, envVar, methodContext);
71: } else {
72: // no to-map, so put in env
73: fieldAcsr.put(methodContext, envVar);
74: }
75: return true;
76: }
77:
78: public String rawString() {
79: return "<env-to-field env-name=\"" + this .envAcsr
80: + "\" field-name=\"" + this .fieldAcsr
81: + "\" map-name=\"" + this .mapAcsr + "\"/>";
82: }
83:
84: public String expandedString(MethodContext methodContext) {
85: // TODO: something more than a stub/dummy
86: return this.rawString();
87: }
88: }
|