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