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.text.*;
021: import java.util.*;
022:
023: import org.w3c.dom.*;
024:
025: import org.ofbiz.base.util.*;
026: import org.ofbiz.minilang.*;
027: import org.ofbiz.minilang.method.*;
028:
029: /**
030: * Copies the specified String to a field
031: */
032: public class StringToField extends MethodOperation {
033:
034: public static final String module = StringToField.class.getName();
035:
036: String string;
037: ContextAccessor mapAcsr;
038: ContextAccessor fieldAcsr;
039: ContextAccessor argListAcsr;
040: String messageFieldName;
041:
042: public StringToField(Element element, SimpleMethod simpleMethod) {
043: super (element, simpleMethod);
044: string = element.getAttribute("string");
045: mapAcsr = new ContextAccessor(element.getAttribute("map-name"));
046: fieldAcsr = new ContextAccessor(element
047: .getAttribute("field-name"));
048: argListAcsr = new ContextAccessor(element
049: .getAttribute("arg-list-name"));
050: messageFieldName = element.getAttribute("message-field-name");
051: }
052:
053: public boolean exec(MethodContext methodContext) {
054: String valueStr = methodContext.expandString(string);
055:
056: if (!argListAcsr.isEmpty()) {
057: List argList = (List) argListAcsr.get(methodContext);
058: if (argList != null && argList.size() > 0) {
059: valueStr = MessageFormat.format(valueStr, argList
060: .toArray());
061: }
062: }
063:
064: Object value;
065: if (this .messageFieldName != null
066: && this .messageFieldName.length() > 0) {
067: value = new MessageString(valueStr, this .messageFieldName,
068: true);
069: } else {
070: value = valueStr;
071: }
072:
073: if (!mapAcsr.isEmpty()) {
074: Map toMap = (Map) mapAcsr.get(methodContext);
075:
076: if (toMap == null) {
077: if (Debug.verboseOn())
078: Debug.logVerbose("Map not found with name "
079: + mapAcsr + ", creating new map", module);
080: toMap = new HashMap();
081: mapAcsr.put(methodContext, toMap);
082: }
083: fieldAcsr.put(toMap, value, methodContext);
084: } else {
085: fieldAcsr.put(methodContext, value);
086: }
087:
088: return true;
089: }
090:
091: public String rawString() {
092: // TODO: something more than the empty tag
093: return "<string-to-field string=\"" + this .string
094: + "\" field-name=\"" + this .fieldAcsr
095: + "\" map-name=\"" + this .mapAcsr + "\"/>";
096: }
097:
098: public String expandedString(MethodContext methodContext) {
099: // TODO: something more than a stub/dummy
100: return this.rawString();
101: }
102: }
|