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