KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > velocity > tools > view > i18n > MultiViewsTool


1 /*
2  * Copyright 2003 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.view.i18n;
18
19
20 import java.util.Locale JavaDoc;
21 import javax.servlet.ServletContext JavaDoc;
22
23 import org.apache.velocity.app.Velocity;
24 import org.apache.velocity.context.Context;
25
26 import org.apache.velocity.tools.view.context.ViewContext;
27 import org.apache.velocity.tools.view.tools.ViewTool;
28
29 /**
30  * Allows for transparent content negotiation in a manner mimicking
31  * Apache httpd's <a
32  * HREF="http://httpd.apache.org/docs-2.0/content-negotiation.html">MultiViews</a>.
33  *
34  * <p>Reads the default language out of the ViewContext as
35  * <code>org.apache.velocity.tools.view.i18n.defaultLanguage</code>.
36  * See {@link #findLocalizedResource(String, String)} and {@link
37  * #findLocalizedResource(String, Locale)} for usage.</p>
38  *
39  * @version $Id: MultiViewsTool.java,v 1.3.2.1 2004/03/12 20:16:28 nbubna Exp $
40  * @author <a HREF="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
41  */

42 public class MultiViewsTool implements ViewTool
43 {
44     /**
45      * The key used to search initialization, context, and JVM
46      * parameters for the default language to use.
47      */

48     protected static final String JavaDoc DEFAULT_LANGUAGE_KEY =
49         "org.apache.velocity.tools.view.i18n.defaultLanguage";
50
51     /**
52      * The two character abbreviation for the request's default
53      * language.
54      */

55     protected String JavaDoc defaultLanguage;
56
57     /**
58      * Creates a new uninitialized instance. Call {@link #init}
59      * to initialize it.
60      */

61     public MultiViewsTool()
62     {
63     }
64
65     /**
66      * Extracts the default language from the specified
67      * <code>ViewContext</code>, looking first at the Velocity
68      * context, then the servlet context, then lastly at the JVM
69      * default. This "narrow scope to wide scope" pattern makes it
70      * easy to setup language overrides at different levels within
71      * your application.
72      *
73      * @param obj the current ViewContext
74      * @throws IllegalArgumentException if the param is not a ViewContext
75      */

76     public void init(Object JavaDoc obj)
77     {
78         if (!(obj instanceof ViewContext))
79         {
80             throw new IllegalArgumentException JavaDoc("Tool can only be initialized with a ViewContext");
81         }
82
83         ViewContext context = (ViewContext)obj;
84         Context JavaDoc vc = context.getVelocityContext();
85         defaultLanguage = (String JavaDoc) vc.get(DEFAULT_LANGUAGE_KEY);
86         if (defaultLanguage == null || defaultLanguage.trim().equals(""))
87         {
88             ServletContext JavaDoc sc = context.getServletContext();
89             defaultLanguage = (String JavaDoc) sc.getAttribute(DEFAULT_LANGUAGE_KEY);
90             if (defaultLanguage == null || defaultLanguage.trim().equals(""))
91             {
92                 // Use JVM default.
93
defaultLanguage = Locale.getDefault().getLanguage();
94             }
95         }
96     }
97
98     /**
99      * Calls {@link #findLocalizedResource(String, String)} using the
100      * language extracted from <code>locale</code>.
101      *
102      * @see #findLocalizedResource(String, String)
103      */

104     public String JavaDoc findLocalizedResource(String JavaDoc name, Locale JavaDoc locale)
105     {
106         return findLocalizedResource(name, locale.getLanguage());
107     }
108
109     /**
110      * Calls {@link #findLocalizedResource(String, String)} using the
111      * default language.
112      *
113      * @see #findLocalizedResource(String, String)
114      */

115     public String JavaDoc findLocalizedResource(String JavaDoc name)
116     {
117         return findLocalizedResource(defaultLanguage);
118     }
119
120     /**
121      * <p>Finds the a localized version of the requested Velocity
122      * resource (such as a file or template) which is most appropriate
123      * for the locale of the current request. Use in conjuction with
124      * Apache httpd's <code>MultiViews</code>, or by itself.</p>
125      *
126      * <p>Usage from a template would be something like the following:
127      * <blockquote><code><pre>
128      * #parse ($multiviews.findLocalizedResource("header.vm", "en"))
129      * #include ($multiviews.findLocalizedResource("my_page.html", "en"))
130      * #parse ($multiviews.findLocalizedResource("footer.vm", "en"))
131      * </pre></code></blockquote>
132      *
133      * You might also wrap this method using another pull/view tool
134      * which does internationalization/localization/content negation
135      * for a single point of access.</p>
136      *
137      * @param name The unlocalized name of the file to find.
138      * @param language The language to find localized context for.
139      * @return The localized file name, or <code>name</code> if it is
140      * not localizable.
141      */

142     public String JavaDoc findLocalizedResource(String JavaDoc name, String JavaDoc language)
143     {
144         String JavaDoc localizedName = name + '.' + language;
145         // templateExists() checks for static content as well
146
if (!Velocity.templateExists(localizedName))
147         {
148             // Fall back to the default lanaguage.
149
String JavaDoc defaultLangSuffix = '.' + defaultLanguage;
150             if (localizedName.endsWith(defaultLangSuffix))
151             {
152                 // Assume no localized version of the resource.
153
localizedName = name;
154             }
155             else
156             {
157                 localizedName = name + defaultLangSuffix;
158                 if (!Velocity.templateExists(localizedName))
159                 {
160                     localizedName = name;
161                 }
162             }
163         }
164         return localizedName;
165     }
166
167
168 }
169
Popular Tags