KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > velocity > tools > struts > MessageResourcesTool


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
17 package org.apache.velocity.tools.struts;
18
19 import java.util.Locale JavaDoc;
20 import javax.servlet.http.HttpServletRequest JavaDoc;
21 import javax.servlet.ServletContext JavaDoc;
22 import org.apache.struts.util.MessageResources;
23 import org.apache.velocity.app.Velocity;
24 import org.apache.velocity.tools.view.context.ViewContext;
25 import org.apache.velocity.tools.view.tools.ViewTool;
26
27 /**
28  * <p>Abstract view tool that provides access to Struts' message resources.</p>
29  *
30  * @author <a HREF="mailto:nbubna@apache.org">Nathan Bubna</a>
31  * @since VelocityTools 1.1
32  * @version $Id: MessageResourcesTool.java,v 1.3 2004/02/18 20:09:51 nbubna Exp $
33  */

34 public abstract class MessageResourcesTool implements ViewTool
35 {
36
37     protected ServletContext JavaDoc application;
38     protected HttpServletRequest JavaDoc request;
39     protected Locale JavaDoc locale;
40     protected MessageResources resources;
41
42
43     /**
44      * Initializes this tool.
45      *
46      * @param obj the current ViewContext
47      * @throws IllegalArgumentException if the param is not a ViewContext
48      */

49     public void init(Object JavaDoc obj)
50     {
51         if (!(obj instanceof ViewContext))
52         {
53             throw new IllegalArgumentException JavaDoc("Tool can only be initialized with a ViewContext");
54         }
55
56         ViewContext context = (ViewContext)obj;
57         this.request = context.getRequest();
58         this.application = context.getServletContext();
59         this.resources =
60             StrutsUtils.getMessageResources(request, application);
61         this.locale =
62             StrutsUtils.getLocale(request, request.getSession(false));
63     }
64
65
66     /**
67      * Retrieves the specified {@link MessageResources} bundle, or the
68      * application's default MessageResources if no bundle is specified.
69      * @since VelocityTools 1.1
70      */

71     protected MessageResources getResources(String JavaDoc bundle)
72     {
73         if (bundle == null)
74         {
75             if (resources == null)
76             {
77                 Velocity.error("MessageResourcesTool: Message resources are not available.");
78             }
79             return resources;
80         }
81         
82         MessageResources res =
83             StrutsUtils.getMessageResources(request, application, bundle);
84         if (res == null)
85         {
86             Velocity.error("MessageResourcesTool: MessageResources bundle '"
87                            + bundle + "' is not available.");
88         }
89         return res;
90     }
91
92 }
93
Popular Tags