001: /*
002: * $Id: FlexibleMessage.java,v 1.2 2003/10/10 09:55:53 jonesde 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.callops;
025:
026: import org.w3c.dom.*;
027: import org.ofbiz.base.util.*;
028:
029: import org.ofbiz.minilang.method.*;
030:
031: /**
032: * Simple class to wrap messages that come either from a straight string or a properties file
033: *
034: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
035: * @version $Revision: 1.2 $
036: * @since 2.0
037: */
038: public class FlexibleMessage {
039:
040: public static final String module = FlexibleMessage.class.getName();
041:
042: String message = null;
043: String propertyResource = null;
044: boolean isProperty = false;
045:
046: public FlexibleMessage(Element element, String defaultProperty) {
047: String resAttr = null;
048: String propAttr = null;
049: String elVal = null;
050:
051: if (element != null) {
052: resAttr = element.getAttribute("resource");
053: propAttr = element.getAttribute("property");
054: elVal = UtilXml.elementValue(element);
055: }
056:
057: if (resAttr != null && resAttr.length() > 0) {
058: propertyResource = resAttr;
059: message = propAttr;
060: isProperty = true;
061: } else if (elVal != null && elVal.length() > 0) {
062: message = elVal;
063: isProperty = false;
064: } else {
065: // put in default property
066: propertyResource = "DefaultMessages";
067: message = defaultProperty;
068: isProperty = true;
069: }
070: }
071:
072: public String getMessage(ClassLoader loader,
073: MethodContext methodContext) {
074: String message = methodContext.expandString(this .message);
075: String propertyResource = methodContext
076: .expandString(this .propertyResource);
077:
078: // if (Debug.infoOn()) Debug.logInfo("[FlexibleMessage.getMessage] isProperty: " + isProperty + ", message: " + message + ", propertyResource: " + propertyResource, module);
079: if (!isProperty && message != null) {
080: // if (Debug.infoOn()) Debug.logInfo("[FlexibleMessage.getMessage] Adding message: " + message, module);
081: return message;
082: } else if (isProperty && propertyResource != null
083: && message != null) {
084: // URL propertyURL = UtilURL.fromResource(propertyResource, loader);
085: //String propMsg = UtilProperties.getPropertyValue(propertyResource, message);
086: String propMsg = UtilProperties.getMessage(
087: propertyResource, message, methodContext
088: .getEnvMap(), methodContext.getLocale());
089:
090: // if (Debug.infoOn()) Debug.logInfo("[FlexibleMessage.getMessage] Got property message: " + propMsg, module);
091: if (propMsg == null || propMsg.length() == 0) {
092: return "In Simple Map Processing property message could not be found in resource ["
093: + propertyResource
094: + "] with name ["
095: + message
096: + "]. ";
097: } else {
098: return propMsg;
099: }
100: } else {
101: Debug
102: .logInfo(
103: "[FlexibleMessage.getMessage] No message found, returning empty string",
104: module);
105: return "";
106: }
107: }
108: }
|