View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.vfs2.provider;
18  
19  import org.apache.commons.vfs2.FileName;
20  import org.apache.commons.vfs2.FileObject;
21  import org.apache.commons.vfs2.FileSystem;
22  import org.apache.commons.vfs2.FileSystemException;
23  import org.apache.commons.vfs2.FileSystemOptions;
24  
25  /**
26   * A {@link FileProvider} that is layered on top of another, such as the contents of a zip or tar file.
27   */
28  public abstract class AbstractLayeredFileProvider extends AbstractFileProvider {
29  
30      public AbstractLayeredFileProvider() {
31          setFileNameParser(LayeredFileNameParser.getInstance());
32      }
33  
34      /**
35       * Locates a file object, by absolute URI.
36       *
37       * @param baseFile The base FileObject.
38       * @param uri The name of the file to locate.
39       * @param fileSystemOptions The FileSystemOptions.
40       * @return The FileObject if it is located, null otherwise.
41       * @throws FileSystemException if an error occurs.
42       */
43      @Override
44      public FileObjectect.html#FileObject">FileObject findFile(final FileObject baseFile, final String uri, final FileSystemOptions fileSystemOptions)
45              throws FileSystemException {
46          // Split the URI up into its parts
47          final LayeredFileName./../../org/apache/commons/vfs2/provider/LayeredFileName.html#LayeredFileName">LayeredFileName name = (LayeredFileName) parseUri(baseFile != null ? baseFile.getName() : null, uri);
48  
49          // Make the URI canonical
50  
51          // Resolve the outer file name
52          final FileName fileName = name.getOuterName();
53          final FileObject file = getContext().resolveFile(baseFile, fileName.getURI(), fileSystemOptions);
54  
55          // Create the file system
56          final FileObject rootFile = createFileSystem(name.getScheme(), file, fileSystemOptions);
57  
58          // Resolve the file
59          return rootFile.resolveFile(name.getPath());
60      }
61  
62      /**
63       * Creates a layered file system.
64       *
65       * @param scheme The protocol to use.
66       * @param file a FileObject.
67       * @param fileSystemOptions Options to access the FileSystem.
68       * @return A FileObject associated with the new FileSystem.
69       * @throws FileSystemException if an error occurs.
70       */
71      @Override
72      public synchronized FileObject createFileSystem(FileObject="jxr_keyword">final String scheme, final FileObject file,
73              final FileSystemOptions fileSystemOptions) throws FileSystemException {
74          // Check if cached
75          final FileName rootName = file.getName();
76          FileSystem fs = findFileSystem(rootName, fileSystemOptions);
77          if (fs == null) {
78              // Create the file system
79              fs = doCreateFileSystem(scheme, file, fileSystemOptions);
80              addFileSystem(rootName, fs);
81          }
82          return fs.getRoot();
83      }
84  
85      /**
86       * Creates a layered file system.
87       * <p>
88       * This method is called if the file system is not cached.
89       * </p>
90       *
91       * @param scheme The URI scheme.
92       * @param file The file to create the file system on top of.
93       * @param fileSystemOptions options for new and underlying file systems.
94       * @return The file system, never null. Might implement {@link VfsComponent}.
95       * @throws FileSystemException if the file system cannot be created.
96       */
97      protected abstract FileSystem doCreateFileSystem(final String scheme, final FileObject file,
98              final FileSystemOptions fileSystemOptions) throws FileSystemException;
99  
100 }