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       * Constructs a new instance.
35       */
36      public LocalFileNameParser() {
37          // empty
38      }
39  
40      /**
41       * Creates a FileName.
42       *
43       * @param scheme The scheme.
44       * @param rootFile the root file.
45       * @param path the path.
46       * @param fileType the file type.
47       * @return a FileName.
48       */
49      protected abstract FileName createFileName(String scheme, String rootFile, String path, FileType fileType);
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(String uri, StringBuilder name) throws FileSystemException;
60  
61      private String[] getSchemes(final VfsComponentContext context, final FileName base, final String uri) {
62          if (context == null) {
63              return new String[] {base != null ? base.getScheme() : URI.create(uri).getScheme()};
64          }
65          return context.getFileSystemManager().getSchemes();
66      }
67  
68      /**
69       * Determines if a name is an absolute file name.
70       *
71       * @param name The file name.
72       * @return true if the name is absolute, false otherwise.
73       */
74      public boolean isAbsoluteName(final String name) {
75          // TODO - this is yucky
76          final StringBuilder b = new StringBuilder(name);
77          try {
78              UriParser.fixSeparators(b);
79              extractRootPrefix(name, b);
80              return true;
81          } catch (final FileSystemException e) {
82              return false;
83          }
84      }
85  
86      @Override
87      public FileName parseUri(final VfsComponentContext context, final FileName base, final String uri)
88              throws FileSystemException {
89          final StringBuilder nameBuilder = new StringBuilder();
90  
91          // Extract the scheme
92          String scheme = UriParser.extractScheme(getSchemes(context, base, uri), uri, nameBuilder);
93          if (scheme == null && base != null) {
94              scheme = base.getScheme();
95          }
96          if (scheme == null) {
97              scheme = "file";
98          }
99  
100         // Remove encoding, and adjust the separators
101         UriParser.canonicalizePath(nameBuilder, 0, nameBuilder.length(), this);
102 
103         UriParser.fixSeparators(nameBuilder);
104 
105         // Extract the root prefix
106         final String rootFile = extractRootPrefix(uri, nameBuilder);
107 
108         // Normalise the path
109         final FileType fileType = UriParser.normalisePath(nameBuilder);
110 
111         final String path = nameBuilder.toString();
112 
113         return createFileName(scheme, rootFile, path, fileType);
114     }
115 }