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