1 19 20 package org.netbeans.modules.masterfs; 21 22 import java.net.URISyntaxException ; 23 import org.openide.filesystems.FileObject; 24 import org.openide.filesystems.FileSystem; 25 import org.openide.filesystems.URLMapper; 26 import org.openide.filesystems.FileUtil; 27 import org.openide.util.Utilities; 28 29 import java.io.File ; 30 import java.net.MalformedURLException ; 31 import java.net.URL ; 32 import java.net.URI ; 33 import org.openide.util.Exceptions; 34 35 39 public final class MasterURLMapper extends URLMapper { 40 41 public MasterURLMapper() { 42 } 43 44 public FileObject[] getFileObjects(final URL url) { 45 final FileSystem hfs = MasterFileSystem.getDefault(); 46 if (!url.getProtocol().equals("file")) return null; FileObject retVal = null; 49 String filePath = null; 50 try { 51 filePath = FileUtil.normalizeFile(new File (new URI (url.toExternalForm()))).getAbsolutePath(); 52 } catch (URISyntaxException e) { 53 StringBuilder sb = new StringBuilder (); 54 sb.append(e.getLocalizedMessage()).append(" [").append(url.toExternalForm()).append(']'); IllegalArgumentException iax = new IllegalArgumentException (sb.toString()); 56 if (Utilities.isWindows() && url.getAuthority() != null) { 57 Exceptions.attachLocalizedMessage(iax, 58 "; might be because your user directory is on a Windows UNC path (issue #46813)? If so, try using mapped drive letters."); } 60 Exceptions.printStackTrace(iax); 61 return null; 62 } 63 64 retVal = hfs.findResource(filePath); 65 if (!(retVal instanceof MasterFileObject)) return null; 66 if (!retVal.isValid()) return null; 67 return new FileObject[]{retVal}; 68 } 69 70 public URL getURL(final FileObject fo, final int type) { 71 if (type == URLMapper.NETWORK || !(fo instanceof MasterFileObject)) return null; 72 MasterFileObject hfo = (MasterFileObject) fo; 73 File f = (hfo != null) ? hfo.getResource().getFile() : null; 74 75 try { 76 return (f != null) ? fileToURL(f, fo) : null; 77 } catch (MalformedURLException mfx) { 78 return null; 79 } 80 } 81 82 private static boolean isWindowsDriveRoot(File file) { 83 return (Utilities.isWindows () || (Utilities.getOperatingSystem () == Utilities.OS_OS2)) && file.getParent() == null; 84 } 85 86 static URL fileToURL(File file, FileObject fo) throws MalformedURLException { 87 URL retVal = null; 88 if (isWindowsDriveRoot(file)) { 89 retVal = new URL ("file:/"+file.getAbsolutePath ()); } else { 91 if (fo.isFolder() && (!fo.isValid() || fo.isVirtual())) { 92 String urlDef = file.toURI().toURL().toExternalForm(); 93 String pathSeparator = "/"; if (!urlDef.endsWith(pathSeparator)) { 95 retVal = new URL (urlDef + pathSeparator); 96 } 97 } 98 retVal = (retVal == null) ? file.toURI().toURL() : retVal; 99 } 100 return retVal; 101 } 102 103 } 104 | Popular Tags |