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          super();
32          setFileNameParser(LayeredFileNameParser.getInstance());
33      }
34  
35      /**
36       * Locates a file object, by absolute URI.
37       *
38       * @param baseFile The base FileObject.
39       * @param uri The name of the file to locate.
40       * @param fileSystemOptions The FileSystemOptions.
41       * @return The FileObject if it is located, null otherwise.
42       * @throws FileSystemException if an error occurs.
43       */
44      @Override
45      public FileObjectect.html#FileObject">FileObject findFile(final FileObject baseFile, final String uri, final FileSystemOptions fileSystemOptions)
46              throws FileSystemException {
47          // Split the URI up into its parts
48          final LayeredFileName./../../org/apache/commons/vfs2/provider/LayeredFileName.html#LayeredFileName">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(), fileSystemOptions);
55  
56          // Create the file system
57          final FileObject rootFile = createFileSystem(name.getScheme(), file, fileSystemOptions);
58  
59          // Resolve the file
60          return rootFile.resolveFile(name.getPath());
61      }
62  
63      /**
64       * Creates a layered file system.
65       *
66       * @param scheme The protocol to use.
67       * @param file a FileObject.
68       * @param fileSystemOptions Options to access the FileSystem.
69       * @return A FileObject associated with the new FileSystem.
70       * @throws FileSystemException if an error occurs.
71       */
72      @Override
73      public synchronized FileObject createFileSystem(FileObject="jxr_keyword">final String scheme, final FileObject file,
74              final FileSystemOptions fileSystemOptions) throws FileSystemException {
75          // Check if cached
76          final FileName rootName = file.getName();
77          FileSystem fs = findFileSystem(rootName, fileSystemOptions);
78          if (fs == null) {
79              // Create the file system
80              fs = doCreateFileSystem(scheme, file, fileSystemOptions);
81              addFileSystem(rootName, fs);
82          }
83          return fs.getRoot();
84      }
85  
86      /**
87       * Creates a layered file system.
88       * <p>
89       * This method is called if the file system is not cached.
90       * </p>
91       *
92       * @param scheme The URI scheme.
93       * @param file The file to create the file system on top of.
94       * @param fileSystemOptions options for new and underlying file systems.
95       * @return The file system, never null. Might implement {@link VfsComponent}.
96       * @throws FileSystemException if the file system cannot be created.
97       */
98      protected abstract FileSystem doCreateFileSystem(final String scheme, final FileObject file,
99              final FileSystemOptions fileSystemOptions) throws FileSystemException;
100 
101 }