KickJava   Java API By Example, From Geeks To Geeks.

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


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.FileObject;
19 import org.apache.commons.vfs.FileSystemException;
20 import org.apache.commons.vfs.FileSystemOptions;
21 import org.apache.commons.vfs.FileType;
22
23 import java.io.IOException JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.net.URLConnection JavaDoc;
26 import java.net.URLStreamHandler JavaDoc;
27
28 /**
29  * A default URL stream handler that will work for most file systems.
30  *
31  * @author <a HREF="mailto:brian@mmmanager.org">Brian Olsen</a>
32  */

33 public class DefaultURLStreamHandler
34     extends URLStreamHandler JavaDoc
35 {
36     private final VfsComponentContext context;
37     private final FileSystemOptions fileSystemOptions;
38
39     public DefaultURLStreamHandler(final VfsComponentContext context)
40     {
41         this(context, null);
42     }
43
44     public DefaultURLStreamHandler(final VfsComponentContext context, final FileSystemOptions fileSystemOptions)
45     {
46         this.context = context;
47         this.fileSystemOptions = fileSystemOptions;
48     }
49
50     protected URLConnection JavaDoc openConnection(final URL JavaDoc url)
51         throws IOException JavaDoc
52     {
53         final FileObject entry = context.resolveFile(url.toExternalForm(), fileSystemOptions);
54         return new DefaultURLConnection(url, entry.getContent());
55     }
56
57     protected void parseURL(final URL JavaDoc u,
58                             final String JavaDoc spec,
59                             final int start,
60                             final int limit)
61     {
62         try
63         {
64             FileObject old = context.resolveFile(u.toExternalForm(), fileSystemOptions);
65
66             FileObject newURL;
67             if (start > 0 && spec.charAt(start - 1) == ':')
68             {
69                 newURL = context.resolveFile(old, spec, fileSystemOptions);
70             }
71             else
72             {
73                 if (old.getType() == FileType.FILE && old.getParent() != null)
74                 {
75                     // for files we have to resolve relative
76
newURL = old.getParent().resolveFile(spec);
77                 }
78                 else
79                 {
80                     newURL = old.resolveFile(spec);
81                 }
82             }
83
84             final String JavaDoc url = newURL.getName().getURI();
85             final StringBuffer JavaDoc filePart = new StringBuffer JavaDoc();
86             final String JavaDoc protocolPart = UriParser.extractScheme(url, filePart);
87
88             setURL(u, protocolPart, "", -1, null, null, filePart.toString(), null, null);
89         }
90         catch (FileSystemException fse)
91         {
92             // This is rethrown to MalformedURLException in URL anyway
93
throw new RuntimeException JavaDoc(fse.getMessage());
94         }
95     }
96
97     protected String JavaDoc toExternalForm(final URL JavaDoc u)
98     {
99         return u.getProtocol() + ":" + u.getFile();
100     }
101 }
102
Popular Tags