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.local;
    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   * A parser for Windows file names.
    25   *
    26   * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
    27   * @version $Revision: 480428 $ $Date: 2006-11-29 07:15:24 +0100 (Mi, 29 Nov 2006) $
    28   */
    29  public class WindowsFileNameParser
    30      extends LocalFileNameParser
    31  {
    32      /***
    33       * Pops the root prefix off a URI, which has had the scheme removed.
    34       */
    35      protected String extractRootPrefix(final String uri,
    36                                         final StringBuffer name)
    37          throws FileSystemException
    38      {
    39          return extractWindowsRootPrefix(uri, name);
    40      }
    41
    42      protected FileName createFileName(String scheme, final String rootFile, final String path, final FileType type)
    43      {
    44          return new WindowsFileName(scheme, rootFile, path, type);
    45      }
    46
    47      /***
    48       * Extracts a Windows root prefix from a name.
    49       */
    50      private String extractWindowsRootPrefix(final String uri,
    51                                              final StringBuffer name)
    52          throws FileSystemException
    53      {
    54          // Looking for:
    55          // ('/'){0, 3} <letter> ':' '/'
    56          // ['/'] '//' <name> '/' <name> ( '/' | <end> )
    57
    58          // Skip over first 4 (unc) leading '/' chars
    59          int startPos = 0;
    60          int maxlen = Math.min(4, name.length());
    61          for (; startPos < maxlen && name.charAt(startPos) == '/'; startPos++)
    62          {
    63          }
    64          if (startPos == maxlen && name.length() > startPos && name.charAt(startPos + 1) == '/')
    65          {
    66              // Too many '/'
    67              throw new FileSystemException("vfs.provider.local/not-absolute-file-name.error", uri);
    68          }
    69          name.delete(0, startPos);
    70
    71          // Look for drive name
    72          String driveName = extractDrivePrefix(name);
    73          if (driveName != null)
    74          {
    75              return driveName;
    76          }
    77
    78          // Look for UNC name
    79          if (startPos < 2)
    80          {
    81              throw new FileSystemException("vfs.provider.local/not-absolute-file-name.error", uri);
    82          }
    83
    84          return "//" + extractUNCPrefix(uri, name);
    85      }
    86
    87      /***
    88       * Extracts a drive prefix from a path.  Leading '/' chars have been removed.
    89       */
    90      private String extractDrivePrefix(final StringBuffer name)
    91      {
    92          // Looking for <letter> ':' '/'
    93          if (name.length() < 3)
    94          {
    95              // Too short
    96              return null;
    97          }
    98          char ch = name.charAt(0);
    99          if (ch == '/' || ch == ':')
    100         {
    101             // Missing drive letter
    102             return null;
    103         }
    104         if (name.charAt(1) != ':')
    105         {
    106             // Missing ':'
    107             return null;
    108         }
    109         if (name.charAt(2) != '/')
    110         {
    111             // Missing separator
    112             return null;
    113         }
    114
    115         String prefix = name.substring(0, 2);
    116         name.delete(0, 2);
    117
    118         return prefix.intern();
    119     }
    120
    121     /***
    122      * Extracts a UNC name from a path.  Leading '/' chars have been removed.
    123      */
    124     private String extractUNCPrefix(final String uri,
    125                                     final StringBuffer name)
    126         throws FileSystemException
    127     {
    128         // Looking for <name> '/' <name> ( '/' | <end> )
    129
    130         // Look for first separator
    131         int maxpos = name.length();
    132         int pos = 0;
    133         for (; pos < maxpos && name.charAt(pos) != '/'; pos++)
    134         {
    135         }
    136         pos++;
    137         if (pos >= maxpos)
    138         {
    139             throw new FileSystemException("vfs.provider.local/missing-share-name.error", uri);
    140         }
    141
    142         // Now have <name> '/'
    143         int startShareName = pos;
    144         for (; pos < maxpos && name.charAt(pos) != '/'; pos++)
    145         {
    146         }
    147         if (pos == startShareName)
    148         {
    149             throw new FileSystemException("vfs.provider.local/missing-share-name.error", uri);
    150         }
    151
    152         // Now have <name> '/' <name> ( '/' | <end> )
    153         String prefix = name.substring(0, pos);
    154         name.delete(0, pos);
    155         return prefix;
    156     }
    157 }