KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > util > FileResource


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// 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 JavaDoc;
18 import java.net.MalformedURLException JavaDoc;
19 import java.net.URL JavaDoc;
20 import java.util.Locale JavaDoc;
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 {
34     private static final Log LOG = LogFactory.getLog(FileResource.class);
35
36     public FileResource(String JavaDoc path)
37     {
38         super(path);
39     }
40
41     public FileResource(String JavaDoc path, Locale JavaDoc locale)
42     {
43         super(path, locale);
44     }
45
46     protected Resource newResource(String JavaDoc path)
47     {
48         return new FileResource(path);
49     }
50
51     private File JavaDoc getFile()
52     {
53         return new File JavaDoc(getPath());
54     }
55
56     public URL JavaDoc getResourceURL()
57     {
58         File JavaDoc file = getFile();
59
60         try
61         {
62             if (file == null || !file.exists())
63                 return null;
64
65             return file.toURL();
66         }
67         catch (MalformedURLException JavaDoc ex)
68         {
69             LOG.error(UtilMessages.badFileURL(getPath(), ex), ex);
70             return null;
71         }
72     }
73
74     public Resource getLocalization(Locale JavaDoc locale)
75     {
76         LocalizedFileResourceFinder f = new LocalizedFileResourceFinder();
77
78         String JavaDoc path = getPath();
79
80         String JavaDoc finalPath = f.findLocalizedPath(path, locale);
81
82         if (finalPath.equals(path))
83             return this;
84
85         return newResource(finalPath);
86     }
87
88     public String JavaDoc toString()
89     {
90         return getPath();
91     }
92
93 }
94
Popular Tags