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.io.File;
18: import java.net.MalformedURLException;
19: import java.net.URL;
20: import java.util.Locale;
21:
22: import org.apache.commons.logging.Log;
23: import org.apache.commons.logging.LogFactory;
24: import org.apache.hivemind.Resource;
25:
26: /**
27: * An implementation of {@link org.apache.hivemind.Resource} built around
28: * {@link java.io.File}.
29: *
30: * @author Howard Lewis Ship
31: */
32: public class FileResource extends AbstractResource {
33: private static final Log LOG = LogFactory
34: .getLog(FileResource.class);
35:
36: public FileResource(String path) {
37: super (path);
38: }
39:
40: public FileResource(String path, Locale locale) {
41: super (path, locale);
42: }
43:
44: protected Resource newResource(String path) {
45: return new FileResource(path);
46: }
47:
48: private File getFile() {
49: return new File(getPath());
50: }
51:
52: public URL getResourceURL() {
53: File file = getFile();
54:
55: try {
56: if (file == null || !file.exists())
57: return null;
58:
59: return file.toURL();
60: } catch (MalformedURLException ex) {
61: LOG.error(UtilMessages.badFileURL(getPath(), ex), ex);
62: return null;
63: }
64: }
65:
66: public Resource getLocalization(Locale locale) {
67: LocalizedFileResourceFinder f = new LocalizedFileResourceFinder();
68:
69: String path = getPath();
70:
71: String finalPath = f.findLocalizedPath(path, locale);
72:
73: if (finalPath.equals(path))
74: return this ;
75:
76: return newResource(finalPath);
77: }
78:
79: public String toString() {
80: return getPath();
81: }
82:
83: }
|