001 // Copyright 2004, 2005 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package org.apache.hivemind.util;
016
017 import java.io.File;
018 import java.net.MalformedURLException;
019 import java.net.URL;
020 import java.util.Locale;
021
022 import org.apache.commons.logging.Log;
023 import org.apache.commons.logging.LogFactory;
024 import org.apache.hivemind.Resource;
025
026 /**
027 * An implementation of {@link org.apache.hivemind.Resource} built around
028 * {@link java.io.File}.
029 *
030 * @author Howard Lewis Ship
031 */
032 public class FileResource extends AbstractResource
033 {
034 private static final Log LOG = LogFactory.getLog(FileResource.class);
035
036 public FileResource(String path)
037 {
038 super(path);
039 }
040
041 public FileResource(String path, Locale locale)
042 {
043 super(path, locale);
044 }
045
046 protected Resource newResource(String path)
047 {
048 return new FileResource(path);
049 }
050
051 private File getFile()
052 {
053 return new File(getPath());
054 }
055
056 public URL getResourceURL()
057 {
058 File file = getFile();
059
060 try
061 {
062 if (file == null || !file.exists())
063 return null;
064
065 return file.toURL();
066 }
067 catch (MalformedURLException ex)
068 {
069 LOG.error(UtilMessages.badFileURL(getPath(), ex), ex);
070 return null;
071 }
072 }
073
074 public Resource getLocalization(Locale locale)
075 {
076 LocalizedFileResourceFinder f = new LocalizedFileResourceFinder();
077
078 String path = getPath();
079
080 String finalPath = f.findLocalizedPath(path, locale);
081
082 if (finalPath.equals(path))
083 return this;
084
085 return newResource(finalPath);
086 }
087
088 public String toString()
089 {
090 return getPath();
091 }
092
093 }