KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > webclient > URLUtil


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.webclient;
21
22 import java.net.URL JavaDoc;
23 import java.net.MalformedURLException JavaDoc;
24 import java.util.Iterator JavaDoc;
25
26 import org.openide.ErrorManager;
27 import org.openide.filesystems.FileObject;
28 import org.openide.filesystems.URLMapper;
29 import org.openide.util.Lookup;
30
31 /** Utility class for various useful URL-related tasks.
32  * !!! COPIED from extbrowser.
33  *
34  * @author Petr Jiricka
35  */

36 public class URLUtil {
37
38     /** results with URLMapper instances*/
39     private static Lookup.Result result;
40
41     static {
42         result = Lookup.getDefault().lookup(new Lookup.Template (URLMapper.class));
43     }
44     
45     /** Creates a URL that is suitable for using in a different process on the
46      * same node, similarly to URLMapper.EXTERNAL. May just return the original
47      * URL if that's good enough.
48      */

49     public static URL JavaDoc createExternalURL(URL JavaDoc url) {
50         if (url == null)
51             return null;
52
53         // return if the protocol is fine
54
if (isAcceptableProtocol(url.getProtocol().toLowerCase()))
55             return url;
56         
57         // remove the anchor
58
String JavaDoc anchor = url.getRef();
59         String JavaDoc urlString = url.toString ();
60         int ind = urlString.indexOf('#');
61         if (ind >= 0) {
62             urlString = urlString.substring(0, ind);
63         }
64         
65         // map to an external URL using the anchor-less URL
66
try {
67             FileObject fos[] = URLMapper.findFileObjects(new URL JavaDoc(urlString));
68             if ((fos != null) && (fos.length > 0)) {
69                 URL JavaDoc newUrl = getURLOfAppropriateType(fos[0]);
70                 if (newUrl != null) {
71                     // re-add the anchor if exists
72
urlString = newUrl.toString();
73                     if (ind >=0) {
74                         urlString = urlString + "#" + anchor; // NOI18N
75
}
76                     return new URL JavaDoc(urlString);
77                 }
78             }
79         }
80         catch (MalformedURLException JavaDoc e) {
81             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
82         }
83         
84         return url;
85     }
86     
87     /** Returns a URL for the given file object that can be correctly interpreted
88      * by usual web browsers (including Netscape 4.71, IE and Mozilla).
89      * First attempts to get an EXTERNAL URL, if that is a suitable URL, it is used;
90      * otherwise a NETWORK URL is used.
91      */

92     private static URL JavaDoc getURLOfAppropriateType(FileObject fo) {
93         // PENDING - there is still the problem that the HTTP server will be started
94
// (because the HttpServerURLMapper.getURL(...) method starts it),
95
// even when it is not needed
96
URL JavaDoc retVal;
97         URL JavaDoc suitable = null;
98         
99         Iterator JavaDoc instances = result.allInstances ().iterator();
100         while (instances.hasNext()) {
101             URLMapper mapper = (URLMapper) instances.next();
102             retVal = mapper.getURL (fo, URLMapper.EXTERNAL);
103             if ((retVal != null) && isAcceptableProtocol(retVal.getProtocol().toLowerCase())) {
104                 // return if this is a 'file' URL
105
if ("file".equals(retVal.getProtocol().toLowerCase())) { // NOI18N
106
return retVal;
107                 }
108                 suitable = retVal;
109             }
110         }
111         
112         // if we found a suitable URL, return it
113
if (suitable != null) {
114             return suitable;
115         }
116         
117         return URLMapper.findURL(fo, URLMapper.NETWORK);
118     }
119         
120     /** Returns true if the protocol is acceptable for usual web browsers.
121      * Specifically, returns true for file, http and ftp protocols.
122      */

123     private static boolean isAcceptableProtocol(String JavaDoc protocol) {
124         if ("http".equals(protocol) // NOI18N
125
|| "ftp".equals(protocol) // NOI18N
126
|| "file".equals(protocol)) // NOI18N
127
return true;
128         
129         return false;
130     }
131
132 }
133
Popular Tags