001: /*
002: * $Id: StringAppend.java,v 1.1 2003/08/17 06:06:12 ajzeneski Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.minilang.method.envops;
025:
026: import java.text.*;
027: import java.util.*;
028:
029: import org.w3c.dom.*;
030:
031: import org.ofbiz.base.util.*;
032: import org.ofbiz.minilang.*;
033: import org.ofbiz.minilang.method.*;
034:
035: /**
036: * Appends the specified String to a field
037: *
038: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
039: * @version $Revision: 1.1 $
040: * @since 2.2
041: */
042: public class StringAppend extends MethodOperation {
043:
044: public static final String module = StringAppend.class.getName();
045:
046: String string;
047: String prefix;
048: String suffix;
049: ContextAccessor mapAcsr;
050: ContextAccessor fieldAcsr;
051: ContextAccessor argListAcsr;
052:
053: public StringAppend(Element element, SimpleMethod simpleMethod) {
054: super (element, simpleMethod);
055: string = element.getAttribute("string");
056: prefix = element.getAttribute("prefix");
057: suffix = element.getAttribute("suffix");
058: mapAcsr = new ContextAccessor(element.getAttribute("map-name"));
059: fieldAcsr = new ContextAccessor(element
060: .getAttribute("field-name"));
061: argListAcsr = new ContextAccessor(element
062: .getAttribute("arg-list-name"));
063: }
064:
065: public boolean exec(MethodContext methodContext) {
066: if (!mapAcsr.isEmpty()) {
067: Map toMap = (Map) mapAcsr.get(methodContext);
068:
069: if (toMap == null) {
070: if (Debug.verboseOn())
071: Debug.logVerbose("Map not found with name "
072: + mapAcsr + ", creating new map", module);
073: toMap = new HashMap();
074: mapAcsr.put(methodContext, toMap);
075: }
076:
077: String oldValue = (String) fieldAcsr.get(toMap,
078: methodContext);
079: fieldAcsr.put(toMap, this .appendString(oldValue,
080: methodContext), methodContext);
081: } else {
082: String oldValue = (String) fieldAcsr.get(methodContext);
083: fieldAcsr.put(methodContext, this .appendString(oldValue,
084: methodContext));
085: }
086:
087: return true;
088: }
089:
090: public String appendString(String oldValue,
091: MethodContext methodContext) {
092: String value = methodContext.expandString(string);
093: String prefixValue = methodContext.expandString(prefix);
094: String suffixValue = methodContext.expandString(suffix);
095:
096: if (!argListAcsr.isEmpty()) {
097: List argList = (List) argListAcsr.get(methodContext);
098: if (argList != null && argList.size() > 0) {
099: value = MessageFormat.format(value, argList.toArray());
100: }
101: }
102:
103: StringBuffer newValue = new StringBuffer();
104: if (value != null && value.length() > 0) {
105: if (oldValue == null || oldValue.length() == 0) {
106: newValue.append(value);
107: } else {
108: newValue.append(oldValue);
109: if (prefixValue != null)
110: newValue.append(prefixValue);
111: newValue.append(value);
112: if (suffixValue != null)
113: newValue.append(suffixValue);
114: }
115: } else {
116: if (oldValue == null || oldValue.length() == 0) {
117: newValue.append(oldValue);
118: }
119: }
120:
121: return newValue.toString();
122: }
123: }
|