1 19 20 package org.netbeans.modules.webclient; 21 22 import java.net.URL ; 23 import java.net.MalformedURLException ; 24 import java.util.Iterator ; 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 36 public class URLUtil { 37 38 39 private static Lookup.Result result; 40 41 static { 42 result = Lookup.getDefault().lookup(new Lookup.Template (URLMapper.class)); 43 } 44 45 49 public static URL createExternalURL(URL url) { 50 if (url == null) 51 return null; 52 53 if (isAcceptableProtocol(url.getProtocol().toLowerCase())) 55 return url; 56 57 String anchor = url.getRef(); 59 String urlString = url.toString (); 60 int ind = urlString.indexOf('#'); 61 if (ind >= 0) { 62 urlString = urlString.substring(0, ind); 63 } 64 65 try { 67 FileObject fos[] = URLMapper.findFileObjects(new URL (urlString)); 68 if ((fos != null) && (fos.length > 0)) { 69 URL newUrl = getURLOfAppropriateType(fos[0]); 70 if (newUrl != null) { 71 urlString = newUrl.toString(); 73 if (ind >=0) { 74 urlString = urlString + "#" + anchor; } 76 return new URL (urlString); 77 } 78 } 79 } 80 catch (MalformedURLException e) { 81 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 82 } 83 84 return url; 85 } 86 87 92 private static URL getURLOfAppropriateType(FileObject fo) { 93 URL retVal; 97 URL suitable = null; 98 99 Iterator 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 if ("file".equals(retVal.getProtocol().toLowerCase())) { return retVal; 107 } 108 suitable = retVal; 109 } 110 } 111 112 if (suitable != null) { 114 return suitable; 115 } 116 117 return URLMapper.findURL(fo, URLMapper.NETWORK); 118 } 119 120 123 private static boolean isAcceptableProtocol(String protocol) { 124 if ("http".equals(protocol) || "ftp".equals(protocol) || "file".equals(protocol)) return true; 128 129 return false; 130 } 131 132 } 133 | Popular Tags |