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.local;
18  
19  import org.apache.commons.vfs2.FileName;
20  import org.apache.commons.vfs2.FileSystemException;
21  import org.apache.commons.vfs2.FileType;
22  import org.apache.commons.vfs2.VFS;
23  import org.apache.commons.vfs2.provider.AbstractFileNameParser;
24  import org.apache.commons.vfs2.provider.UriParser;
25  import org.apache.commons.vfs2.provider.VfsComponentContext;
26  
27  /**
28   * A name parser.
29   */
30  public abstract class LocalFileNameParser extends AbstractFileNameParser {
31  
32      /**
33       * Determines if a name is an absolute file name.
34       *
35       * @param name The file name.
36       * @return true if the name is absolute, false otherwise.
37       */
38      public boolean isAbsoluteName(final String name) {
39          // TODO - this is yucky
40          final StringBuilder b = new StringBuilder(name);
41          try {
42              UriParser.fixSeparators(b);
43              extractRootPrefix(name, b);
44              return true;
45          } catch (final FileSystemException e) {
46              return false;
47          }
48      }
49  
50      /**
51       * Pops the root prefix off a URI, which has had the scheme removed.
52       *
53       * @param name the URI to modify.
54       * @param uri the whole URI for error reporting.
55       * @return the root prefix extracted.
56       * @throws FileSystemException if an error occurs.
57       */
58      protected abstract String extractRootPrefix(final String uri, final StringBuilder name) throws FileSystemException;
59  
60      @Override
61      public FileName parseUri(FileNamefinal VfsComponentContext context, final FileName base, final String uri)
62              throws FileSystemException {
63          final StringBuilder name = new StringBuilder();
64  
65          // Extract the scheme
66          String scheme = UriParser.extractScheme(context.getFileSystemManager().getSchemes(), uri, name);
67          if (scheme == null) {
68              scheme = "file";
69          }
70  
71          // Remove encoding, and adjust the separators
72          UriParser.canonicalizePath(name, 0, name.length(), this);
73  
74          UriParser.fixSeparators(name);
75  
76          // Extract the root prefix
77          final String rootFile = extractRootPrefix(uri, name);
78  
79          // Normalise the path
80          final FileType fileType = UriParser.normalisePath(name);
81  
82          final String path = name.toString();
83  
84          return createFileName(scheme, rootFile, path, fileType);
85      }
86  
87      protected abstract FileName createFileName(String scheme, final String rootFile, final String path,
88              final FileType type);
89  }