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.vfs.provider;
    18
    19  import org.apache.commons.vfs.FileName;
    20  import org.apache.commons.vfs.FileSystemException;
    21  import org.apache.commons.vfs.FileType;
    22
    23  /***
    24   * Implementation for layered filesystems.
    25   * <p/>
    26   * Additionally encodes the '!' character.
    27   */
    28  public class LayeredFileNameParser extends AbstractFileNameParser
    29  {
    30      private final static LayeredFileNameParser INSTANCE = new LayeredFileNameParser();
    31
    32      public static LayeredFileNameParser getInstance()
    33      {
    34          return INSTANCE;
    35      }
    36
    37      public boolean encodeCharacter(char ch)
    38      {
    39          return super.encodeCharacter(ch) || ch == '!';
    40      }
    41
    42      public FileName parseUri(final VfsComponentContext context, FileName base, final String filename) throws FileSystemException
    43      {
    44          final StringBuffer name = new StringBuffer();
    45
    46          // Extract the scheme
    47          final String scheme = UriParser.extractScheme(filename, name);
    48
    49          // Extract the Layered file URI
    50          final String rootUriName = extractRootName(name);
    51          FileName rootUri = null;
    52          if (rootUriName != null)
    53          {
    54              rootUri = context.parseURI(rootUriName);
    55          }
    56
    57          // Decode and normalise the path
    58          UriParser.canonicalizePath(name, 0, name.length(), this);
    59          UriParser.fixSeparators(name);
    60          FileType fileType = UriParser.normalisePath(name);
    61          final String path = name.toString();
    62
    63          return new LayeredFileName(scheme, rootUri, path, fileType);
    64      }
    65
    66      /***
    67       * Pops the root prefix off a URI, which has had the scheme removed.
    68       */
    69      protected String extractRootName(final StringBuffer uri)
    70          throws FileSystemException
    71      {
    72          // Looking for <name>!<abspath> (staring at the end)
    73          int maxlen = uri.length();
    74          int pos = maxlen - 1;
    75          for (; pos > 0 && uri.charAt(pos) != '!'; pos--)
    76          {
    77          }
    78
    79          if (pos == 0 && uri.charAt(pos) != '!')
    80          {
    81              // not ! found, so take the whole path a root
    82              // e.g. zip:/my/zip/file.zip
    83              pos = maxlen;
    84          }
    85
    86          // Extract the name
    87          String prefix = uri.substring(0, pos);
    88          if (pos < maxlen)
    89          {
    90              uri.delete(0, pos + 1);
    91          }
    92          else
    93          {
    94              uri.setLength(0);
    95          }
    96
    97          return prefix;
    98      }
    99
    100 }