1 24 package org.ofbiz.minilang.method.otherops; 25 26 import java.text.*; 27 import java.util.*; 28 29 import org.w3c.dom.*; 30 import org.ofbiz.base.util.*; 31 import org.ofbiz.minilang.*; 32 import org.ofbiz.minilang.method.*; 33 34 41 public class PropertyToField extends MethodOperation { 42 43 public static final String module = PropertyToField.class.getName(); 44 45 String resource; 46 String property; 47 ContextAccessor mapAcsr; 48 ContextAccessor fieldAcsr; 49 String defaultVal; 50 boolean noLocale; 51 ContextAccessor argListAcsr; 52 53 public PropertyToField(Element element, SimpleMethod simpleMethod) { 54 super(element, simpleMethod); 55 resource = element.getAttribute("resource"); 56 property = element.getAttribute("property"); 57 mapAcsr = new ContextAccessor(element.getAttribute("map-name")); 58 fieldAcsr = new ContextAccessor(element.getAttribute("field-name")); 59 defaultVal = element.getAttribute("default"); 60 noLocale = "true".equals(element.getAttribute("no-locale")); 62 argListAcsr = new ContextAccessor(element.getAttribute("arg-list-name")); 63 } 64 65 public boolean exec(MethodContext methodContext) { 66 String resource = methodContext.expandString(this.resource); 67 String property = methodContext.expandString(this.property); 68 69 String value = null; 70 if (noLocale) { 71 value = UtilProperties.getPropertyValue(resource, property); 72 } else { 73 value = UtilProperties.getMessage(resource, property, methodContext.getLocale()); 74 } 75 if (value == null || value.length() == 0) { 76 value = defaultVal; 77 } 78 79 value = methodContext.expandString(value); 83 84 if (!argListAcsr.isEmpty()) { 85 List argList = (List) argListAcsr.get(methodContext); 86 if (argList != null && argList.size() > 0) { 87 value = MessageFormat.format(value, argList.toArray()); 88 } 89 } 90 91 if (!mapAcsr.isEmpty()) { 92 Map toMap = (Map) mapAcsr.get(methodContext); 93 94 if (toMap == null) { 95 if (Debug.infoOn()) Debug.logInfo("Map not found with name " + mapAcsr + ", creating new map", module); 96 toMap = new HashMap(); 97 mapAcsr.put(methodContext, toMap); 98 } 99 fieldAcsr.put(toMap, value, methodContext); 100 } else { 101 fieldAcsr.put(methodContext, value); 102 } 103 104 return true; 105 } 106 107 public String rawString() { 108 return "<property-to-field field-name=\"" + this.fieldAcsr + "\" map-name=\"" + this.mapAcsr + "\"/>"; 110 } 111 public String expandedString(MethodContext methodContext) { 112 return this.rawString(); 114 } 115 } 116 | Popular Tags |