001: /*
002: * $Id: PropertyToField.java,v 1.1 2003/08/17 06:06:13 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.otherops;
025:
026: import java.text.*;
027: import java.util.*;
028:
029: import org.w3c.dom.*;
030: import org.ofbiz.base.util.*;
031: import org.ofbiz.minilang.*;
032: import org.ofbiz.minilang.method.*;
033:
034: /**
035: * Copies an properties file property value to a field
036: *
037: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
038: * @version $Revision: 1.1 $
039: * @since 2.0
040: */
041: public class PropertyToField extends MethodOperation {
042:
043: public static final String module = PropertyToField.class.getName();
044:
045: String resource;
046: String property;
047: ContextAccessor mapAcsr;
048: ContextAccessor fieldAcsr;
049: String defaultVal;
050: boolean noLocale;
051: ContextAccessor argListAcsr;
052:
053: public PropertyToField(Element element, SimpleMethod simpleMethod) {
054: super (element, simpleMethod);
055: resource = element.getAttribute("resource");
056: property = element.getAttribute("property");
057: mapAcsr = new ContextAccessor(element.getAttribute("map-name"));
058: fieldAcsr = new ContextAccessor(element
059: .getAttribute("field-name"));
060: defaultVal = element.getAttribute("default");
061: // defaults to false, ie anything but true is false
062: noLocale = "true".equals(element.getAttribute("no-locale"));
063: argListAcsr = new ContextAccessor(element
064: .getAttribute("arg-list-name"));
065: }
066:
067: public boolean exec(MethodContext methodContext) {
068: String resource = methodContext.expandString(this .resource);
069: String property = methodContext.expandString(this .property);
070:
071: String value = null;
072: if (noLocale) {
073: value = UtilProperties.getPropertyValue(resource, property);
074: } else {
075: value = UtilProperties.getMessage(resource, property,
076: methodContext.getLocale());
077: }
078: if (value == null || value.length() == 0) {
079: value = defaultVal;
080: }
081:
082: // note that expanding the value string here will handle defaultValue and the string from
083: // the properties file; if we decide later that we don't want the string from the properties
084: // file to be expanded we should just expand the defaultValue at the beginning of this method.
085: value = methodContext.expandString(value);
086:
087: if (!argListAcsr.isEmpty()) {
088: List argList = (List) argListAcsr.get(methodContext);
089: if (argList != null && argList.size() > 0) {
090: value = MessageFormat.format(value, argList.toArray());
091: }
092: }
093:
094: if (!mapAcsr.isEmpty()) {
095: Map toMap = (Map) mapAcsr.get(methodContext);
096:
097: if (toMap == null) {
098: if (Debug.infoOn())
099: Debug.logInfo("Map not found with name " + mapAcsr
100: + ", creating new map", module);
101: toMap = new HashMap();
102: mapAcsr.put(methodContext, toMap);
103: }
104: fieldAcsr.put(toMap, value, methodContext);
105: } else {
106: fieldAcsr.put(methodContext, value);
107: }
108:
109: return true;
110: }
111: }
|