KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > minilang > method > callops > FlexibleMessage


1 /*
2  * $Id: FlexibleMessage.java 5720 2005-09-13 03:10:59Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.minilang.method.callops;
25
26 import java.io.Serializable JavaDoc;
27
28 import org.w3c.dom.*;
29 import org.ofbiz.base.util.*;
30
31 import org.ofbiz.minilang.method.*;
32
33 /**
34  * Simple class to wrap messages that come either from a straight string or a properties file
35  *
36  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
37  * @version $Rev: 5720 $
38  * @since 2.0
39  */

40 public class FlexibleMessage implements Serializable JavaDoc {
41     
42     public static final String JavaDoc module = FlexibleMessage.class.getName();
43     
44     String JavaDoc message = null;
45     String JavaDoc propertyResource = null;
46     boolean isProperty = false;
47
48     public FlexibleMessage(Element element, String JavaDoc defaultProperty) {
49         String JavaDoc resAttr = null;
50         String JavaDoc propAttr = null;
51         String JavaDoc elVal = null;
52
53         if (element != null) {
54             resAttr = element.getAttribute("resource");
55             propAttr = element.getAttribute("property");
56             elVal = UtilXml.elementValue(element);
57         }
58
59         if (resAttr != null && resAttr.length() > 0) {
60             propertyResource = resAttr;
61             message = propAttr;
62             isProperty = true;
63         } else if (elVal != null && elVal.length() > 0) {
64             message = elVal;
65             isProperty = false;
66         } else {
67             // put in default property
68
propertyResource = "DefaultMessages";
69             message = defaultProperty;
70             isProperty = true;
71         }
72     }
73
74     public String JavaDoc getMessage(ClassLoader JavaDoc loader, MethodContext methodContext) {
75         String JavaDoc message = methodContext.expandString(this.message);
76         String JavaDoc propertyResource = methodContext.expandString(this.propertyResource);
77         
78         // if (Debug.infoOn()) Debug.logInfo("[FlexibleMessage.getMessage] isProperty: " + isProperty + ", message: " + message + ", propertyResource: " + propertyResource, module);
79
if (!isProperty && message != null) {
80             // if (Debug.infoOn()) Debug.logInfo("[FlexibleMessage.getMessage] Adding message: " + message, module);
81
return message;
82         } else if (isProperty && propertyResource != null && message != null) {
83             // URL propertyURL = UtilURL.fromResource(propertyResource, loader);
84
//String propMsg = UtilProperties.getPropertyValue(propertyResource, message);
85
String JavaDoc propMsg = UtilProperties.getMessage(propertyResource, message, methodContext.getEnvMap(), methodContext.getLocale());
86
87             // if (Debug.infoOn()) Debug.logInfo("[FlexibleMessage.getMessage] Got property message: " + propMsg, module);
88
if (propMsg == null) {
89                 return "In Simple Map Processing property message could not be found in resource [" + propertyResource + "] with name [" + message + "]. ";
90             } else {
91                 return propMsg;
92             }
93         } else {
94             Debug.logInfo("[FlexibleMessage.getMessage] No message found, returning empty string", module);
95             return "";
96         }
97     }
98 }
99
Popular Tags