KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > utils > ResourceLocator


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.wcf.utils;
14
15 import java.net.MalformedURLException JavaDoc;
16 import java.net.URL JavaDoc;
17 import java.util.Hashtable JavaDoc;
18 import java.util.Locale JavaDoc;
19 import java.util.MissingResourceException JavaDoc;
20
21 import javax.servlet.ServletContext JavaDoc;
22
23 /**
24  * searches for resources in a locale specific way
25  *
26  * @author av
27  */

28 public class ResourceLocator {
29   private static final String JavaDoc WEBKEY = ResourceLocator.class.getName();
30   private Hashtable JavaDoc urlCache = new Hashtable JavaDoc();
31
32   private ResourceLocator() {
33   }
34
35   static ResourceLocator instance(ServletContext JavaDoc context) {
36     ResourceLocator loc = (ResourceLocator) context.getAttribute(WEBKEY);
37     if (loc == null) {
38       loc = new ResourceLocator();
39       context.setAttribute(WEBKEY, loc);
40     }
41     return loc;
42   }
43
44   /**
45    * Searches for a resource in a way similar to ResourceBundle.
46    * It tries to match the language and country
47    * codes. Example: with path "/test.xml", language "en" and country "US"
48    * the following resources will be searched:
49    * <ol>
50    * <li>test_en_US.xml</li>
51    * <li>test_en.xml</li>
52    * <li>test.xml</li>
53    * </ol>
54    * <p>
55    * Implementation note: the search results will be cached, so
56    * you have to restart the servlet after adding or removing files.
57    * @throws MissingResourceException if no resource is found
58    */

59
60   public static URL JavaDoc getResource(ServletContext JavaDoc context, Locale JavaDoc locale, String JavaDoc uri)
61     throws MalformedURLException JavaDoc, MissingResourceException JavaDoc {
62     return instance(context).findResource(context, locale, uri);
63   }
64
65   URL JavaDoc findResource(ServletContext JavaDoc context, Locale JavaDoc locale, String JavaDoc path)
66     throws MalformedURLException JavaDoc, MissingResourceException JavaDoc {
67     String JavaDoc ext = "";
68
69     // check if we have an extension to remove
70
if (path.lastIndexOf("/") < path.lastIndexOf('.')) {
71       int pos = path.lastIndexOf('.');
72       ext = path.substring(pos, path.length()); // including the dot
73
path = path.substring(0, pos);
74     }
75
76     String JavaDoc test1 = path + "_" + locale.getLanguage() + "_" + locale.getCountry() + ext;
77     if (urlCache.containsKey(test1)) {
78       return (URL JavaDoc) urlCache.get(test1);
79     }
80
81     URL JavaDoc url = context.getResource(test1);
82     if (url != null) {
83       urlCache.put(test1, url);
84       return url;
85     }
86
87     String JavaDoc test = path + "_" + locale.getLanguage() + ext;
88     url = context.getResource(test);
89     if (url != null) {
90       urlCache.put(test1, url);
91       return url;
92     }
93
94     test = path + ext;
95     url = context.getResource(test);
96     if (url != null) {
97       urlCache.put(test1, url);
98       return url;
99     }
100
101     throw new MissingResourceException JavaDoc("Resource \"" + path + "\" not found", ResourceLocator.class.getName(), path + ext);
102   }
103
104 }
105
Popular Tags