1 19 20 package org.netbeans.modules.extbrowser; 21 22 import java.net.InetAddress ; 23 import java.net.UnknownHostException ; 24 import java.util.Iterator ; 25 import java.net.URL ; 26 import java.net.MalformedURLException ; 27 28 import org.openide.ErrorManager; 29 import org.openide.filesystems.FileObject; 30 import org.openide.filesystems.URLMapper; 31 import org.openide.util.Lookup; 32 33 37 public class URLUtil { 38 39 40 private static Lookup.Result result; 41 42 static { 43 result = Lookup.getDefault().lookup(new Lookup.Template (URLMapper.class)); 44 } 45 46 53 public static URL createExternalURL(URL url, boolean allowJar) { 54 if (url == null) 55 return null; 56 57 if (isAcceptableProtocol(url, allowJar)) 59 return url; 60 61 String anchor = url.getRef(); 63 String urlString = url.toString (); 64 int ind = urlString.indexOf('#'); 65 if (ind >= 0) { 66 urlString = urlString.substring(0, ind); 67 } 68 69 try { 71 FileObject fo = URLMapper.findFileObject(new URL (urlString)); 72 if (fo != null) { 73 URL newUrl = getURLOfAppropriateType(fo, allowJar); 74 if (newUrl != null) { 75 urlString = newUrl.toString(); 77 if (ind >=0) { 78 urlString = urlString + "#" + anchor; } 80 return new URL (urlString); 81 } 82 } 83 } 84 catch (MalformedURLException e) { 85 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 86 } 87 88 return url; 89 } 90 91 96 private static URL getURLOfAppropriateType(FileObject fo, boolean allowJar) { 97 URL retVal; 101 URL suitable = null; 102 103 Iterator instances = result.allInstances ().iterator(); 104 while (instances.hasNext()) { 105 URLMapper mapper = (URLMapper) instances.next(); 106 retVal = mapper.getURL (fo, URLMapper.EXTERNAL); 107 if ((retVal != null) && isAcceptableProtocol(retVal, allowJar)) { 108 String p = retVal.getProtocol().toLowerCase(); 110 if ("file".equals(p) || "jar".equals(p)) { return retVal; 112 } 113 suitable = retVal; 114 } 115 } 116 117 if (suitable != null) { 119 return suitable; 120 } 121 122 return makeURLLocal(URLMapper.findURL(fo, URLMapper.NETWORK)); 123 } 124 125 private static URL makeURLLocal(URL input) { 126 String host = input.getHost(); 127 try { 128 if (host.equals(InetAddress.getLocalHost().getHostName())) { 129 host = "127.0.0.1"; return new URL (input.getProtocol(), host, input.getPort(), input.getFile()); 131 } 132 else return input; 133 } catch (UnknownHostException e) { 134 return input; 135 } catch (MalformedURLException e) { 136 return input; 137 } 138 } 139 140 143 private static boolean isAcceptableProtocol(URL url, boolean allowJar) { 144 String protocol = url.getProtocol().toLowerCase(); 145 if ("http".equals(protocol) || "ftp".equals(protocol) || "file".equals(protocol)) return true; 149 if (allowJar && "jar".equals(protocol)) { String urlString = url.toString(); 151 if (!urlString.toLowerCase().startsWith("jar:nbinst:")) return true; 153 } 154 155 return false; 156 } 157 158 163 public static boolean browserHandlesJarURLs(String browser) { 164 return (ExtWebBrowser.MOZILLA.equals(browser) || 165 ExtWebBrowser.FIREFOX.equals(browser)); 166 } 167 168 } 169 | Popular Tags |