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: * Appends the specified String to a field
031: */
032: public class StringAppend extends MethodOperation {
033:
034: public static final String module = StringAppend.class.getName();
035:
036: String string;
037: String prefix;
038: String suffix;
039: ContextAccessor mapAcsr;
040: ContextAccessor fieldAcsr;
041: ContextAccessor argListAcsr;
042:
043: public StringAppend(Element element, SimpleMethod simpleMethod) {
044: super (element, simpleMethod);
045: string = element.getAttribute("string");
046: prefix = element.getAttribute("prefix");
047: suffix = element.getAttribute("suffix");
048: mapAcsr = new ContextAccessor(element.getAttribute("map-name"));
049: fieldAcsr = new ContextAccessor(element
050: .getAttribute("field-name"));
051: argListAcsr = new ContextAccessor(element
052: .getAttribute("arg-list-name"));
053: }
054:
055: public boolean exec(MethodContext methodContext) {
056: if (!mapAcsr.isEmpty()) {
057: Map toMap = (Map) mapAcsr.get(methodContext);
058:
059: if (toMap == null) {
060: if (Debug.verboseOn())
061: Debug.logVerbose("Map not found with name "
062: + mapAcsr + ", creating new map", module);
063: toMap = new HashMap();
064: mapAcsr.put(methodContext, toMap);
065: }
066:
067: String oldValue = (String) fieldAcsr.get(toMap,
068: methodContext);
069: fieldAcsr.put(toMap, this .appendString(oldValue,
070: methodContext), methodContext);
071: } else {
072: String oldValue = (String) fieldAcsr.get(methodContext);
073: fieldAcsr.put(methodContext, this .appendString(oldValue,
074: methodContext));
075: }
076:
077: return true;
078: }
079:
080: public String appendString(String oldValue,
081: MethodContext methodContext) {
082: String value = methodContext.expandString(string);
083: String prefixValue = methodContext.expandString(prefix);
084: String suffixValue = methodContext.expandString(suffix);
085:
086: if (!argListAcsr.isEmpty()) {
087: List argList = (List) argListAcsr.get(methodContext);
088: if (argList != null && argList.size() > 0) {
089: value = MessageFormat.format(value, argList.toArray());
090: }
091: }
092:
093: StringBuffer newValue = new StringBuffer();
094: if (value != null && value.length() > 0) {
095: if (oldValue == null || oldValue.length() == 0) {
096: newValue.append(value);
097: } else {
098: newValue.append(oldValue);
099: if (prefixValue != null)
100: newValue.append(prefixValue);
101: newValue.append(value);
102: if (suffixValue != null)
103: newValue.append(suffixValue);
104: }
105: } else {
106: if (oldValue == null || oldValue.length() == 0) {
107: newValue.append(oldValue);
108: }
109: }
110:
111: return newValue.toString();
112: }
113:
114: public String rawString() {
115: // TODO: something more than the empty tag
116: return "<string-append string=\"" + this .string
117: + "\" prefix=\"" + this .prefix + "\" suffix=\""
118: + this .suffix + "\" field-name=\"" + this .fieldAcsr
119: + "\" map-name=\"" + this .mapAcsr + "\"/>";
120: }
121:
122: public String expandedString(MethodContext methodContext) {
123: // TODO: something more than a stub/dummy
124: return this.rawString();
125: }
126: }
|