1 24 package org.ofbiz.minilang.method.envops; 25 26 import java.text.*; 27 import java.util.*; 28 29 import org.w3c.dom.*; 30 31 import org.ofbiz.base.util.*; 32 import org.ofbiz.minilang.*; 33 import org.ofbiz.minilang.method.*; 34 35 42 public class StringAppend extends MethodOperation { 43 44 public static final String module = StringAppend.class.getName(); 45 46 String string; 47 String prefix; 48 String suffix; 49 ContextAccessor mapAcsr; 50 ContextAccessor fieldAcsr; 51 ContextAccessor argListAcsr; 52 53 public StringAppend(Element element, SimpleMethod simpleMethod) { 54 super(element, simpleMethod); 55 string = element.getAttribute("string"); 56 prefix = element.getAttribute("prefix"); 57 suffix = element.getAttribute("suffix"); 58 mapAcsr = new ContextAccessor(element.getAttribute("map-name")); 59 fieldAcsr = new ContextAccessor(element.getAttribute("field-name")); 60 argListAcsr = new ContextAccessor(element.getAttribute("arg-list-name")); 61 } 62 63 public boolean exec(MethodContext methodContext) { 64 if (!mapAcsr.isEmpty()) { 65 Map toMap = (Map) mapAcsr.get(methodContext); 66 67 if (toMap == null) { 68 if (Debug.verboseOn()) Debug.logVerbose("Map not found with name " + mapAcsr + ", creating new map", module); 69 toMap = new HashMap(); 70 mapAcsr.put(methodContext, toMap); 71 } 72 73 String oldValue = (String ) fieldAcsr.get(toMap, methodContext); 74 fieldAcsr.put(toMap, this.appendString(oldValue, methodContext), methodContext); 75 } else { 76 String oldValue = (String ) fieldAcsr.get(methodContext); 77 fieldAcsr.put(methodContext, this.appendString(oldValue, methodContext)); 78 } 79 80 return true; 81 } 82 83 public String appendString(String oldValue, MethodContext methodContext) { 84 String value = methodContext.expandString(string); 85 String prefixValue = methodContext.expandString(prefix); 86 String suffixValue = methodContext.expandString(suffix); 87 88 if (!argListAcsr.isEmpty()) { 89 List argList = (List) argListAcsr.get(methodContext); 90 if (argList != null && argList.size() > 0) { 91 value = MessageFormat.format(value, argList.toArray()); 92 } 93 } 94 95 StringBuffer newValue = new StringBuffer (); 96 if (value != null && value.length() > 0) { 97 if (oldValue == null || oldValue.length() == 0) { 98 newValue.append(value); 99 } else { 100 newValue.append(oldValue); 101 if (prefixValue != null) newValue.append(prefixValue); 102 newValue.append(value); 103 if (suffixValue != null) 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 return "<string-append string=\"" + this.string + "\" prefix=\"" + this.prefix + "\" suffix=\"" + this.suffix + "\" field-name=\"" + this.fieldAcsr + "\" map-name=\"" + this.mapAcsr + "\"/>"; 117 } 118 public String expandedString(MethodContext methodContext) { 119 return this.rawString(); 121 } 122 } 123 | Popular Tags |