1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs.impl;
18
19 import org.apache.commons.vfs.FileObject;
20 import org.apache.commons.vfs.FileSystemException;
21 import org.apache.commons.vfs.FileUtil;
22
23 import java.io.IOException;
24 import java.net.URL;
25 import java.util.jar.Attributes;
26
27 /***
28 * Helper class for VFSClassLoader. This represents a resource loaded with
29 * the classloader.
30 *
31 * @author <a href="mailto:brian@mmmanager.org">Brian Olsen</a>
32 * @version $Revision: 480428 $ $Date: 2006-11-29 07:15:24 +0100 (Mi, 29 Nov 2006) $
33 * @see VFSClassLoader
34 */
35 class Resource
36 {
37 private final FileObject root;
38 private final FileObject resource;
39 private final FileObject packageFolder/package-summary.html">ong> final FileObject packageFolder;
40 private final String packageName/package-summary.html">ong> final String packageName;
41
42 /***
43 * Creates a new instance.
44 *
45 * @param root The code source FileObject.
46 * @param resource The resource of the FileObject.
47 */
48 public Resource(final String name,
49 final FileObject root,
50 final FileObject resource)
51 throws FileSystemException
52 {
53 this.root = root;
54 this.resource = resource;
55 packageFolder = resource.getParent();
56 final int pos = name.lastIndexOf('/');
57 if (pos == -1)
58 {
59 packageName = null;
60 }
61 else
62 {
63 packageName = name.substring(0, pos).replace('/', '.');
64 }
65 }
66
67 /***
68 * Returns the URL of the resource.
69 */
70 public URL getURL() throws FileSystemException
71 {
72 return resource.getURL();
73 }
74
75 /***
76 * Returns the name of the package containing the resource.
77 */
78 public String getPackageName()
79 {
80 return</strong> packageName;
81 }
82
83 /***
84 * Returns an attribute of the package containing the resource.
85 */
86 public String getPackageAttribute(final Attributes.Name attrName) throws FileSystemException
87 {
88 return</strong> (String) packageFolder.getContent().getAttribute(attrName.toString());
89 }
90
91 /***
92 * Returns the folder for the package containing the resource.
93 */
94 public FileObject getPackageFolder()
95 {
96 return</strong> packageFolder;
97 }
98
99 /***
100 * Returns the FileObject of the resource.
101 */
102 public FileObject getFileObject()
103 {
104 return resource;
105 }
106
107 /***
108 * Returns the code source as an URL.
109 */
110 public URL getCodeSourceURL() throws FileSystemException
111 {
112 return root.getURL();
113 }
114
115 /***
116 * Returns the data for this resource as a byte array.
117 */
118 public byte[] getBytes() throws IOException
119 {
120 return FileUtil.getContent(resource);
121 }
122 }