KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > provider > url > UrlFileNameParser


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.vfs.provider.url;
17
18 import org.apache.commons.vfs.FileName;
19 import org.apache.commons.vfs.FileSystemException;
20 import org.apache.commons.vfs.provider.AbstractFileNameParser;
21 import org.apache.commons.vfs.provider.URLFileName;
22 import org.apache.commons.vfs.provider.URLFileNameParser;
23 import org.apache.commons.vfs.provider.VfsComponentContext;
24 import org.apache.commons.vfs.provider.local.GenericFileNameParser;
25
26 /**
27  * Implementation for any java.net.url based filesystem.<br />
28  * Composite of URLFilenameParser and GenericFilenameParser
29  *
30  * @author imario@apache.org
31  */

32 public class UrlFileNameParser extends AbstractFileNameParser
33 {
34     private URLFileNameParser url = new URLFileNameParser(80);
35     private GenericFileNameParser generic = new GenericFileNameParser();
36
37     public UrlFileNameParser()
38     {
39         super();
40     }
41
42     public boolean encodeCharacter(char ch)
43     {
44         return super.encodeCharacter(ch) || ch == '?';
45     }
46
47     public FileName parseUri(final VfsComponentContext context, final FileName base, final String JavaDoc filename) throws FileSystemException
48     {
49         if (isUrlBased(base, filename))
50         {
51             return url.parseUri(context, base, filename);
52         }
53
54         return generic.parseUri(context, base, filename);
55     }
56
57     /**
58      * Guess is the given filename is a url with host or not. VFS treats such urls differently.<br />
59      * A filename is url-based if the base is a <code>URLFileName</code> or there are only 2 slashes
60      * after the scheme.<br/>
61      * e.g: http://host/path, file:/path/to/file, file:///path/to/file
62      *
63      */

64     protected boolean isUrlBased(final FileName base, final String JavaDoc filename)
65     {
66         if (base instanceof URLFileName)
67         {
68             return true;
69         }
70
71         int nuofSlash = countSlashes(filename);
72         return nuofSlash == 2;
73     }
74
75     /**
76      * This method counts the slashes after the scheme.
77      *
78      * @param filename
79      * @return nuof slashes
80      */

81     protected int countSlashes(final String JavaDoc filename)
82     {
83         int state = 0;
84         int nuofSlash = 0;
85         for (int pos = 0; pos<filename.length(); pos++)
86         {
87             char c = filename.charAt(pos);
88             if (state == 0)
89             {
90                 if (c >= 'a' && c <= 'z')
91                 {
92                     continue;
93                 }
94                 if (c == ':')
95                 {
96                     state++;
97                     continue;
98                 }
99             }
100             else if (state == 1)
101             {
102                 if (c == '/')
103                 {
104                     nuofSlash++;
105                 }
106                 else
107                 {
108                     return nuofSlash;
109                 }
110             }
111         }
112         return nuofSlash;
113     }
114 }
115
Popular Tags