KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > core > io > support > LocalizedResourceHelper


1 /*
2  * Copyright 2002-2006 the original author or authors.
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.springframework.core.io.support;
18
19 import java.util.Locale JavaDoc;
20
21 import org.springframework.core.io.DefaultResourceLoader;
22 import org.springframework.core.io.Resource;
23 import org.springframework.core.io.ResourceLoader;
24 import org.springframework.util.Assert;
25
26 /**
27  * Helper class for loading a localized resource,
28  * specified through name, extension and current locale.
29  *
30  * @author Juergen Hoeller
31  * @since 1.2.5
32  */

33 public class LocalizedResourceHelper {
34
35     /** The default separator to use inbetween file name parts: an underscore */
36     public static final String JavaDoc DEFAULT_SEPARATOR = "_";
37
38
39     private final ResourceLoader resourceLoader;
40
41     private String JavaDoc separator = DEFAULT_SEPARATOR;
42
43
44     /**
45      * Create a new LocalizedResourceHelper with a DefaultResourceLoader.
46      * @see org.springframework.core.io.DefaultResourceLoader
47      */

48     public LocalizedResourceHelper() {
49         this.resourceLoader = new DefaultResourceLoader();
50     }
51
52     /**
53      * Create a new LocalizedResourceHelper with the given ResourceLoader.
54      * @param resourceLoader the ResourceLoader to use
55      */

56     public LocalizedResourceHelper(ResourceLoader resourceLoader) {
57         Assert.notNull(resourceLoader, "ResourceLoader must not be null");
58         this.resourceLoader = resourceLoader;
59     }
60
61     /**
62      * Set the separator to use inbetween file name parts.
63      * Default is an underscore ("_").
64      */

65     public void setSeparator(String JavaDoc separator) {
66         this.separator = (separator != null ? separator : DEFAULT_SEPARATOR);
67     }
68
69
70     /**
71      * Find the most specific localized resource for the given name,
72      * extension and locale:
73      * <p>The file will be searched with locations in the following order,
74      * similar to <code>java.util.ResourceBundle</code>'s search order:
75      * <ul>
76      * <li>[name]_[language]_[country]_[variant][extension]
77      * <li>[name]_[language]_[country][extension]
78      * <li>[name]_[language][extension]
79      * <li>[name][extension]
80      * </ul>
81      * <p>If none of the specific files can be found, a resource
82      * descriptor for the default location will be returned.
83      * @param name the name of the file, without localization part nor extension
84      * @param extension the file extension (e.g. ".xls")
85      * @param locale the current locale (may be <code>null</code>)
86      * @return the most specific localized resource found
87      * @see java.util.ResourceBundle
88      */

89     public Resource findLocalizedResource(String JavaDoc name, String JavaDoc extension, Locale JavaDoc locale) {
90         Assert.notNull(name, "Name must not be null");
91         Assert.notNull(extension, "Extension must not be null");
92
93         Resource resource = null;
94
95         if (locale != null) {
96             String JavaDoc lang = locale.getLanguage();
97             String JavaDoc country = locale.getCountry();
98             String JavaDoc variant = locale.getVariant();
99
100             // Check for file with language, country and variant localization.
101
if (variant.length() > 0) {
102                 String JavaDoc location =
103                         name + this.separator + lang + this.separator + country + this.separator + variant + extension;
104                 resource = this.resourceLoader.getResource(location);
105             }
106
107             // Check for file with language and country localization.
108
if ((resource == null || !resource.exists()) && country.length() > 0) {
109                 String JavaDoc location = name + this.separator + lang + this.separator + country + extension;
110                 resource = this.resourceLoader.getResource(location);
111             }
112
113             // Check for document with language localization.
114
if ((resource == null || !resource.exists()) && lang.length() > 0) {
115                 String JavaDoc location = name + this.separator + lang + extension;
116                 resource = this.resourceLoader.getResource(location);
117             }
118         }
119
120         // Check for document without localization.
121
if (resource == null || !resource.exists()) {
122             String JavaDoc location = name + extension;
123             resource = this.resourceLoader.getResource(location);
124         }
125
126         return resource;
127     }
128
129 }
130
Popular Tags