01: // Copyright 2004, 2005 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // Unless required by applicable law or agreed to in writing, software
10: // distributed under the License is distributed on an "AS IS" BASIS,
11: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: // See the License for the specific language governing permissions and
13: // limitations under the License.
14:
15: package org.apache.hivemind.util;
16:
17: import java.net.URL;
18: import java.util.Locale;
19:
20: import org.apache.hivemind.ClassResolver;
21: import org.apache.hivemind.Resource;
22:
23: /**
24: * Implementation of {@link org.apache.hivemind.Resource}
25: * for resources found within the classpath.
26: *
27: *
28: * @author Howard Lewis Ship
29: */
30:
31: public class ClasspathResource extends AbstractResource {
32: private ClassResolver _resolver;
33:
34: public ClasspathResource(ClassResolver resolver, String path) {
35: this (resolver, path, null);
36: }
37:
38: public ClasspathResource(ClassResolver resolver, String path,
39: Locale locale) {
40: super (path, locale);
41:
42: _resolver = resolver;
43: }
44:
45: /**
46: * Locates the localization of the
47: * resource using {@link LocalizedResourceFinder}.
48: */
49:
50: public Resource getLocalization(Locale locale) {
51: String path = getPath();
52: LocalizedResourceFinder finder = new LocalizedResourceFinder(
53: _resolver);
54:
55: LocalizedResource localizedResource = finder.resolve(path,
56: locale);
57:
58: if (localizedResource == null)
59: return null;
60:
61: String localizedPath = localizedResource.getResourcePath();
62: Locale pathLocale = localizedResource.getResourceLocale();
63:
64: if (localizedPath == null)
65: return null;
66:
67: if (path.equals(localizedPath))
68: return this ;
69:
70: return new ClasspathResource(_resolver, localizedPath,
71: pathLocale);
72: }
73:
74: /**
75: * Invokes {@link ClassResolver#getResource(String)} with the path.
76: */
77:
78: public URL getResourceURL() {
79: return _resolver.getResource(getPath());
80: }
81:
82: public String toString() {
83: return "classpath:" + getPath();
84: }
85:
86: public int hashCode() {
87: return 4783 & getPath().hashCode();
88: }
89:
90: protected Resource newResource(String path) {
91: return new ClasspathResource(_resolver, path);
92: }
93:
94: }
|