1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs2.provider.res;
18
19 import java.net.URL;
20 import java.util.Arrays;
21 import java.util.Collection;
22 import java.util.Collections;
23
24 import org.apache.commons.vfs2.Capability;
25 import org.apache.commons.vfs2.FileName;
26 import org.apache.commons.vfs2.FileObject;
27 import org.apache.commons.vfs2.FileSystem;
28 import org.apache.commons.vfs2.FileSystemConfigBuilder;
29 import org.apache.commons.vfs2.FileSystemException;
30 import org.apache.commons.vfs2.FileSystemOptions;
31 import org.apache.commons.vfs2.provider.AbstractFileProvider;
32
33
34
35
36 public class ResourceFileProvider extends AbstractFileProvider {
37
38
39 protected static final Collection<Capability> capabilities = Collections
40 .unmodifiableCollection(Arrays.asList(Capability.DISPATCHER));
41
42 private static final int BUFFER_SIZE = 80;
43
44 public ResourceFileProvider() {
45 setFileNameParser(ResourceFileNameParser.getInstance());
46 }
47
48
49
50
51
52
53
54
55
56
57 @Override
58 public FileObjectObject.html#FileObject">FileObject findFile(final FileObject baseFile, final String uri, final FileSystemOptions fileSystemOptions)
59 throws FileSystemException {
60 final FileName fileName;
61 if (baseFile != null) {
62 fileName = parseUri(baseFile.getName(), uri);
63 }
64 else {
65 fileName = parseUri(null, uri);
66 }
67 final String resourceName = fileName.getPath();
68
69 ClassLoader classLoader = ResourceFileSystemConfigBuilder.getInstance().getClassLoader(fileSystemOptions);
70 if (classLoader == null) {
71 classLoader = getClass().getClassLoader();
72 }
73 FileSystemException.requireNonNull(classLoader, "vfs.provider.url/badly-formed-uri.error", uri);
74 final URL url = classLoader.getResource(resourceName);
75
76 FileSystemException.requireNonNull(url, "vfs.provider.url/badly-formed-uri.error", uri);
77
78 return getContext().getFileSystemManager().resolveFile(url.toExternalForm());
79 }
80
81 @Override
82 public FileSystemConfigBuilder getConfigBuilder() {
83 return org.apache.commons.vfs2.provider.res.ResourceFileSystemConfigBuilder.getInstance();
84 }
85
86 @Override
87 public void closeFileSystem(final FileSystem filesystem) {
88
89 }
90
91 @Override
92 public Collection<Capability> getCapabilities() {
93 return capabilities;
94 }
95 }