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