KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > pageflow > internal > ExpressionAwareMessageResources


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.pageflow.internal;
19
20 import org.apache.struts.util.MessageResources;
21 import org.apache.beehive.netui.util.logging.Logger;
22
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24 import javax.servlet.ServletContext JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Locale JavaDoc;
27
28 /**
29  * Message resources extension that knows how to evaluate JSP 2.0-style expressions (set in the message keys) that are
30  * prefixed with a special indicator string.
31  */

32 public class ExpressionAwareMessageResources
33         extends MessageResources
34 {
35     private static final Logger _log = Logger.getInstance( ExpressionAwareMessageResources.class );
36     
37     private MessageResources _delegate;
38     private Object JavaDoc _formBean;
39     private transient HttpServletRequest JavaDoc _request;
40     private transient ServletContext JavaDoc _servletContext;
41
42     public ExpressionAwareMessageResources( Object JavaDoc formBean, HttpServletRequest JavaDoc request, ServletContext JavaDoc servletContext )
43     {
44         super( defaultFactory, null, true );
45         _formBean = formBean;
46         _request = request;
47         _servletContext = servletContext;
48     }
49     
50     public ExpressionAwareMessageResources( MessageResources delegate, Object JavaDoc formBean, HttpServletRequest JavaDoc request,
51                                             ServletContext JavaDoc servletContext )
52     {
53         super( delegate.getFactory(), delegate.getConfig(), delegate.getReturnNull() );
54         _delegate = delegate;
55         _formBean = formBean;
56         _request = request;
57         _servletContext = servletContext;
58     }
59
60     public String JavaDoc getMessage( Locale JavaDoc locale, String JavaDoc key )
61     {
62         if ( key.startsWith( InternalConstants.MESSAGE_IS_EXPRESSION_PREFIX ) )
63         {
64             String JavaDoc messageExpr = key.substring( InternalConstants.MESSAGE_IS_EXPRESSION_PREFIX_LENGTH );
65         
66             try
67             {
68                 return InternalExpressionUtils.evaluateMessage( messageExpr, _formBean, _request, _servletContext );
69             }
70             catch ( Exception JavaDoc e )
71             {
72                 _log.error( "Could not evaluate message expression " + messageExpr, e );
73             }
74                 
75             return null;
76         }
77         
78         return _delegate != null ? _delegate.getMessage( locale, key ) : null;
79     }
80     
81     public String JavaDoc getMessage( Locale JavaDoc locale, String JavaDoc key, Object JavaDoc args[] )
82     {
83         for ( int i = 0; i < args.length; i++ )
84         {
85             Object JavaDoc arg = args[i];
86             
87             if ( arg instanceof String JavaDoc )
88             {
89                 String JavaDoc argStr = ( String JavaDoc ) arg;
90                 
91                 if ( argStr.startsWith( InternalConstants.MESSAGE_IS_EXPRESSION_PREFIX ) )
92                 {
93                     String JavaDoc argExpr = argStr.substring( InternalConstants.MESSAGE_IS_EXPRESSION_PREFIX_LENGTH );
94                 
95                     try
96                     {
97                         args[i] =
98                             InternalExpressionUtils.evaluateMessage( argExpr, _formBean, _request, _servletContext );
99                     }
100                     catch ( Exception JavaDoc e )
101                     {
102                         _log.error( "Could not evaluate message arg expression " + argExpr, e );
103                     }
104                 }
105             }
106         }
107         
108         return super.getMessage( locale, key, args );
109     }
110
111     protected void setFormBean( Object JavaDoc formBean )
112     {
113         _formBean = formBean;
114     }
115
116     public static void update( MessageResources resources, Object JavaDoc formBean )
117     {
118         if ( resources instanceof ExpressionAwareMessageResources )
119         {
120             ( ( ExpressionAwareMessageResources ) resources ).setFormBean( formBean );
121         }
122     }
123 }
124
125
126
Popular Tags