KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > provider > HostFileNameParser


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;
17
18 import org.apache.commons.vfs.FileName;
19 import org.apache.commons.vfs.FileSystemException;
20 import org.apache.commons.vfs.FileType;
21
22 /**
23  * Implementation for any url based filesystem.<br />
24  * Parses the url into user/password/host/port/path<br />
25  * Does not handle a query string (after ?)
26  *
27  * @author imario@apache.org
28  * @see URLFileNameParser URLFileNameParser for the implementation which also handles the query string too
29  */

30 public class HostFileNameParser extends AbstractFileNameParser
31 {
32     private final int defaultPort;
33
34     public HostFileNameParser(final int defaultPort)
35     {
36         this.defaultPort = defaultPort;
37     }
38
39     public int getDefaultPort()
40     {
41         return defaultPort;
42     }
43
44     public boolean encodeCharacter(char ch)
45     {
46         return super.encodeCharacter(ch);
47     }
48
49     public FileName parseUri(final VfsComponentContext context, FileName base, final String JavaDoc filename) throws FileSystemException
50     {
51         // FTP URI are generic URI (as per RFC 2396)
52
final StringBuffer JavaDoc name = new StringBuffer JavaDoc();
53
54         // Extract the scheme and authority parts
55
final Authority auth = extractToPath(filename, name);
56
57         // Decode and normalise the file name
58
UriParser.canonicalizePath(name, 0, name.length(), this);
59         UriParser.fixSeparators(name);
60         FileType fileType = UriParser.normalisePath(name);
61         final String JavaDoc path = name.toString();
62
63         return new GenericFileName(
64             auth.scheme,
65             auth.hostName,
66             auth.port,
67             defaultPort,
68             auth.userName,
69             auth.password,
70             path,
71             fileType);
72     }
73
74     /**
75      * Extracts the scheme, userinfo, hostname and port components of a
76      * generic URI.
77      *
78      * @param uri The absolute URI to parse.
79      * @param name Used to return the remainder of the URI.
80      */

81     protected Authority extractToPath(final String JavaDoc uri,
82                                       final StringBuffer JavaDoc name)
83         throws FileSystemException
84     {
85         final Authority auth = new Authority();
86
87         // Extract the scheme
88
auth.scheme = UriParser.extractScheme(uri, name);
89
90         // Expecting "//"
91
if (name.length() < 2 || name.charAt(0) != '/' || name.charAt(1) != '/')
92         {
93             throw new FileSystemException("vfs.provider/missing-double-slashes.error", uri);
94         }
95         name.delete(0, 2);
96
97         // Extract userinfo, and split into username and password
98
final String JavaDoc userInfo = extractUserInfo(name);
99         final String JavaDoc userName;
100         final String JavaDoc password;
101         if (userInfo != null)
102         {
103             int idx = userInfo.indexOf(':');
104             if (idx == -1)
105             {
106                 userName = userInfo;
107                 password = "";
108             }
109             else
110             {
111                 userName = userInfo.substring(0, idx);
112                 password = userInfo.substring(idx + 1);
113             }
114         }
115         else
116         {
117             userName = "";
118             password = "";
119         }
120         auth.userName = UriParser.decode(userName);
121         auth.password = UriParser.decode(password);
122
123         // Extract hostname, and normalise (lowercase)
124
final String JavaDoc hostName = extractHostName(name);
125         if (hostName == null)
126         {
127             throw new FileSystemException("vfs.provider/missing-hostname.error", uri);
128         }
129         auth.hostName = hostName; //.toLowerCase();
130

131         // Extract port
132
auth.port = extractPort(name, uri);
133
134         // Expecting '/' or empty name
135
if (name.length() > 0 && name.charAt(0) != '/')
136         {
137             throw new FileSystemException("vfs.provider/missing-hostname-path-sep.error", uri);
138         }
139
140         return auth;
141     }
142
143     /**
144      * Extracts the user info from a URI. The scheme:// part has been removed
145      * already.
146      */

147     protected String JavaDoc extractUserInfo(final StringBuffer JavaDoc name)
148     {
149         final int maxlen = name.length();
150         for (int pos = 0; pos < maxlen; pos++)
151         {
152             final char ch = name.charAt(pos);
153             if (ch == '@')
154             {
155                 // Found the end of the user info
156
String JavaDoc userInfo = name.substring(0, pos);
157                 name.delete(0, pos + 1);
158                 return userInfo;
159             }
160             if (ch == '/' || ch == '?')
161             {
162                 // Not allowed in user info
163
break;
164             }
165         }
166
167         // Not found
168
return null;
169     }
170
171     /**
172      * Extracts the hostname from a URI. The scheme://userinfo@ part has
173      * been removed.
174      */

175     protected String JavaDoc extractHostName(final StringBuffer JavaDoc name)
176     {
177         final int maxlen = name.length();
178         int pos = 0;
179         for (; pos < maxlen; pos++)
180         {
181             final char ch = name.charAt(pos);
182             if (ch == '/' || ch == ';' || ch == '?' || ch == ':'
183                 || ch == '@' || ch == '&' || ch == '=' || ch == '+'
184                 || ch == '$' || ch == ',')
185             {
186                 break;
187             }
188         }
189         if (pos == 0)
190         {
191             return null;
192         }
193
194         final String JavaDoc hostname = name.substring(0, pos);
195         name.delete(0, pos);
196         return hostname;
197     }
198
199     /**
200      * Extracts the port from a URI. The scheme://userinfo@hostname
201      * part has been removed.
202      *
203      * @return The port, or -1 if the URI does not contain a port.
204      */

205     protected int extractPort(final StringBuffer JavaDoc name, final String JavaDoc uri) throws FileSystemException
206     {
207         if (name.length() < 1 || name.charAt(0) != ':')
208         {
209             return -1;
210         }
211
212         final int maxlen = name.length();
213         int pos = 1;
214         for (; pos < maxlen; pos++)
215         {
216             final char ch = name.charAt(pos);
217             if (ch < '0' || ch > '9')
218             {
219                 break;
220             }
221         }
222
223         final String JavaDoc port = name.substring(1, pos);
224         name.delete(0, pos);
225         if (port.length() == 0)
226         {
227             throw new FileSystemException("vfs.provider/missing-port.error", uri);
228         }
229
230         return Integer.parseInt(port);
231     }
232
233     /**
234      * Parsed authority info (scheme, hostname, userinfo, port)
235      */

236     protected static class Authority
237     {
238         public String JavaDoc scheme;
239         public String JavaDoc hostName;
240         public String JavaDoc userName;
241         public String JavaDoc password;
242         public int port;
243     }
244 }
245
Popular Tags