KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > provider > AbstractLayeredFileProvider


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.provider;
17
18 import org.apache.commons.vfs.FileName;
19 import org.apache.commons.vfs.FileObject;
20 import org.apache.commons.vfs.FileSystem;
21 import org.apache.commons.vfs.FileSystemException;
22 import org.apache.commons.vfs.FileSystemOptions;
23
24 /**
25  * A {@link FileProvider} that is layered on top of another, such as the
26  * contents of a zip or tar file.
27  *
28  * @author <a HREF="mailto:adammurdoch@apache.org">Adam Murdoch</a>
29  */

30 public abstract class AbstractLayeredFileProvider
31     extends AbstractFileProvider
32     implements FileProvider
33 {
34     public AbstractLayeredFileProvider()
35     {
36         super();
37         setFileNameParser(LayeredFileNameParser.getInstance());
38     }
39
40     /**
41      * Locates a file object, by absolute URI.
42      */

43     public FileObject findFile(final FileObject baseFile,
44                                final String JavaDoc uri,
45                                final FileSystemOptions properties) throws FileSystemException
46     {
47         // Split the URI up into its parts
48
final LayeredFileName name = (LayeredFileName) parseUri(baseFile!=null?baseFile.getName():null, uri);
49
50         // Make the URI canonical
51

52         // Resolve the outer file name
53
final FileName fileName = name.getOuterName();
54         final FileObject file = getContext().resolveFile(baseFile, fileName.getURI(), properties);
55
56         // Create the file system
57
final FileObject rootFile = createFileSystem(name.getScheme(), file, properties);
58
59         // Resolve the file
60
return rootFile.resolveFile(name.getPath());
61     }
62
63     /**
64      * Creates a layered file system.
65      */

66     public synchronized FileObject createFileSystem(final String JavaDoc scheme,
67                                                     final FileObject file,
68                                                     final FileSystemOptions fileSystemOptions)
69         throws FileSystemException
70     {
71         // Check if cached
72
final FileName rootName = file.getName();
73         FileSystem fs = findFileSystem(rootName, null);
74         if (fs == null)
75         {
76             // Create the file system
77
fs = doCreateFileSystem(scheme, file, fileSystemOptions);
78             addFileSystem(rootName, fs);
79         }
80         return fs.getRoot();
81     }
82
83     /**
84      * Creates a layered file system. This method is called if the file system
85      * is not cached. The file system may implement {@link VfsComponent}.
86      *
87      * @param scheme The URI scheme.
88      * @param file The file to create the file system on top of.
89      * @return The file system.
90      */

91     protected abstract FileSystem doCreateFileSystem(final String JavaDoc scheme,
92                                                      final FileObject file,
93                                                      final FileSystemOptions fileSystemOptions)
94         throws FileSystemException;
95
96 }
97
Popular Tags