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.url;
18  
19  import org.apache.commons.vfs.FileName;
20  import org.apache.commons.vfs.FileSystemException;
21  import org.apache.commons.vfs.provider.AbstractFileNameParser;
22  import org.apache.commons.vfs.provider.URLFileName;
23  import org.apache.commons.vfs.provider.URLFileNameParser;
24  import org.apache.commons.vfs.provider.VfsComponentContext;
25  import org.apache.commons.vfs.provider.local.GenericFileNameParser;
26  
27  /***
28   * Implementation for any java.net.url based filesystem.<br />
29   * Composite of URLFilenameParser and GenericFilenameParser
30   *
31   * @author imario@apache.org
32   * @version $Revision: 480428 $ $Date: 2006-11-29 07:15:24 +0100 (Mi, 29 Nov 2006) $
33   */
34  public class UrlFileNameParser extends AbstractFileNameParser
35  {
36      private URLFileNameParser url = new URLFileNameParser(80);
37      private GenericFileNameParser generic = new GenericFileNameParser();
38  
39      public UrlFileNameParser()
40      {
41          super();
42      }
43  
44      public boolean encodeCharacter(char ch)
45      {
46          return super.encodeCharacter(ch) || ch == '?';
47      }
48  
49      public FileName parseUri(final VfsComponentContext context, final FileName base, final String filename) throws FileSystemException
50      {
51          if (isUrlBased(base, filename))
52          {
53              return url.parseUri(context, base, filename);
54          }
55  
56          return generic.parseUri(context, base, filename);
57      }
58  
59      /***
60       * Guess is the given filename is a url with host or not. VFS treats such urls differently.<br />
61       * A filename is url-based if the base is a <code>URLFileName</code> or there are only 2 slashes
62       * after the scheme.<br/>
63       * e.g: http://host/path, file:/path/to/file, file:///path/to/file
64       *
65       */
66      protected boolean isUrlBased(final FileName base, final String filename)
67      {
68          if (base instanceof URLFileName)
69          {
70              return true;
71          }
72  
73          int nuofSlash = countSlashes(filename);
74          return nuofSlash == 2;
75      }
76  
77      /***
78       * This method counts the slashes after the scheme.
79       *
80       * @param filename
81       * @return nuof slashes
82       */
83      protected int countSlashes(final String filename)
84      {
85          int state = 0;
86          int nuofSlash = 0;
87          for (int pos = 0; pos<filename.length(); pos++)
88          {
89              char c = filename.charAt(pos);
90              if (state == 0)
91              {
92                  if (c >= 'a' && c <= 'z')
93                  {
94                      continue;
95                  }
96                  if (c == ':')
97                  {
98                      state++;
99                      continue;
100                 }
101             }
102             else if (state == 1)
103             {
104                 if (c == '/')
105                 {
106                     nuofSlash++;
107                 }
108                 else
109                 {
110                     return nuofSlash;
111                 }
112             }
113         }
114         return nuofSlash;
115     }
116 }