KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > impl > Resource


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.vfs.impl;
17
18 import org.apache.commons.vfs.FileObject;
19 import org.apache.commons.vfs.FileSystemException;
20 import org.apache.commons.vfs.FileUtil;
21
22 import java.io.IOException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.jar.Attributes JavaDoc;
25
26 /**
27  * Helper class for VFSClassLoader. This represents a resource loaded with
28  * the classloader.
29  *
30  * @author <a HREF="mailto:brian@mmmanager.org">Brian Olsen</a>
31  * @see VFSClassLoader
32  */

33 class Resource
34 {
35     private final FileObject root;
36     private final FileObject resource;
37     private final FileObject packageFolder;
38     private final String JavaDoc packageName;
39
40     /**
41      * Creates a new instance.
42      *
43      * @param root The code source FileObject.
44      * @param resource The resource of the FileObject.
45      */

46     public Resource(final String JavaDoc name,
47                     final FileObject root,
48                     final FileObject resource)
49         throws FileSystemException
50     {
51         this.root = root;
52         this.resource = resource;
53         packageFolder = resource.getParent();
54         final int pos = name.lastIndexOf('/');
55         if (pos == -1)
56         {
57             packageName = null;
58         }
59         else
60         {
61             packageName = name.substring(0, pos).replace('/', '.');
62         }
63     }
64
65     /**
66      * Returns the URL of the resource.
67      */

68     public URL JavaDoc getURL() throws FileSystemException
69     {
70         return resource.getURL();
71     }
72
73     /**
74      * Returns the name of the package containing the resource.
75      */

76     public String JavaDoc getPackageName()
77     {
78         return packageName;
79     }
80
81     /**
82      * Returns an attribute of the package containing the resource.
83      */

84     public String JavaDoc getPackageAttribute(final Attributes.Name JavaDoc attrName) throws FileSystemException
85     {
86         return (String JavaDoc) packageFolder.getContent().getAttribute(attrName.toString());
87     }
88
89     /**
90      * Returns the folder for the package containing the resource.
91      */

92     public FileObject getPackageFolder()
93     {
94         return packageFolder;
95     }
96
97     /**
98      * Returns the FileObject of the resource.
99      */

100     public FileObject getFileObject()
101     {
102         return resource;
103     }
104
105     /**
106      * Returns the code source as an URL.
107      */

108     public URL JavaDoc getCodeSourceURL() throws FileSystemException
109     {
110         return root.getURL();
111     }
112
113     /**
114      * Returns the data for this resource as a byte array.
115      */

116     public byte[] getBytes() throws IOException JavaDoc
117     {
118         return FileUtil.getContent(resource);
119     }
120 }
121
Popular Tags