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.FileSystemException;
21  import org.apache.commons.vfs2.FileType;
22  
23  /**
24   * Implementation for layered file systems.
25   * <p>
26   * Additionally encodes the '!' character.
27   * </p>
28   */
29  public class LayeredFileNameParser extends AbstractFileNameParser {
30      private static final LayeredFileNameParserleNameParser.html#LayeredFileNameParser">LayeredFileNameParser INSTANCE = new LayeredFileNameParser();
31  
32      /**
33       * Returns the Parser.
34       *
35       * @return The Parser.
36       */
37      public static LayeredFileNameParser getInstance() {
38          return INSTANCE;
39      }
40  
41      /**
42       * Determines if a character should be encoded.
43       *
44       * @param ch The character to check.
45       * @return true if the character should be encoded.
46       */
47      @Override
48      public boolean encodeCharacter(final char ch) {
49          return super.encodeCharacter(ch) || ch == '!';
50      }
51  
52      /**
53       * Parses the base and name into a FileName.
54       *
55       * @param context The component context.
56       * @param baseFileName The base FileName.
57       * @param fileName name The target file name.
58       * @return The constructed FileName.
59       * @throws FileSystemException if an error occurs.
60       */
61      @Override
62      public FileName parseUri(FileNameal VfsComponentContext context, final FileName baseFileName, final String fileName)
63              throws FileSystemException {
64          final StringBuilder name = new StringBuilder();
65  
66          // Extract the scheme
67          final String scheme = UriParser.extractScheme(context.getFileSystemManager().getSchemes(), fileName, name);
68  
69          // Extract the Layered file URI
70          final String rootUriName = extractRootName(name);
71          FileName rootUri = null;
72          if (rootUriName != null) {
73              rootUri = context.parseURI(rootUriName);
74          }
75  
76          // Decode and normalise the path
77          UriParser.canonicalizePath(name, 0, name.length(), this);
78          UriParser.fixSeparators(name);
79          final FileType fileType = UriParser.normalisePath(name);
80          final String path = name.toString();
81  
82          return new LayeredFileName(scheme, rootUri, path, fileType);
83      }
84  
85      /**
86       * Pops the root prefix off a URI, which has had the scheme removed.
87       *
88       * @param uri string builder which gets modified.
89       * @return the extracted root name.
90       * @throws FileSystemException if error occurs.
91       */
92      protected String extractRootName(final StringBuilder uri) throws FileSystemException {
93          // Looking for <name>!<abspath> (staring at the end)
94          final int maxlen = uri.length();
95          int pos = maxlen - 1;
96          for (; pos > 0 && uri.charAt(pos) != '!'; pos--) {
97          }
98  
99          if (pos == 0 && uri.charAt(pos) != '!') {
100             // not ! found, so take the whole path a root
101             // e.g. zip:/my/zip/file.zip
102             pos = maxlen;
103         }
104 
105         // Extract the name
106         final String prefix = uri.substring(0, pos);
107         if (pos < maxlen) {
108             uri.delete(0, pos + 1);
109         } else {
110             uri.setLength(0);
111         }
112 
113         return prefix;
114     }
115 
116 }