1 19 20 package org.netbeans.modules.masterfs.filebasedfs; 21 22 import java.net.URISyntaxException ; 23 import org.netbeans.modules.masterfs.filebasedfs.fileobjects.BaseFileObj; 24 import org.openide.filesystems.FileObject; 25 import org.openide.filesystems.FileUtil; 26 import org.openide.filesystems.URLMapper; 27 28 import java.io.File ; 29 import java.net.MalformedURLException ; 30 import java.net.URI ; 31 import java.net.URL ; 32 import org.openide.util.Exceptions; 33 34 64 65 public final class FileBasedURLMapper extends URLMapper { 66 public final URL getURL(final FileObject fo, final int type) { 67 URL retVal = null; 68 try { 69 if (fo instanceof BaseFileObj) { 70 final BaseFileObj bfo = (BaseFileObj) fo; 71 retVal = FileBasedURLMapper.fileToURL(bfo.getFileName().getFile(), fo); 72 } 73 } catch (MalformedURLException e) { 74 retVal = null; 75 } 76 return retVal; 77 } 78 79 public final FileObject[] getFileObjects(final URL url) { 80 if (!"file".equals(url.getProtocol())) return null; FileObject retVal = null; 83 File file; 84 try { 85 final String host = url.getHost(); 86 final String f = url.getFile(); 87 if (host != null && host.trim().length() != 0) { 90 file = new File ("////" + host + f); } else { 92 if (f.startsWith("//")) { 93 file = new File (f); 94 } else { 95 file = FileUtil.normalizeFile(new File (new URI (url.toExternalForm()))); 96 } 97 } 98 } catch (URISyntaxException e) { 99 file = new File (url.getFile()); 100 if (!file.exists()) { 101 final StringBuffer sb = new StringBuffer (); 102 sb.append(e.getLocalizedMessage()).append(" [").append(url.toExternalForm()).append(']'); Exceptions.printStackTrace(new IllegalArgumentException (sb.toString())); 104 return null; 105 } 106 } 107 108 final FileBasedFileSystem instance = FileBasedFileSystem.getInstance(file); 109 110 if (instance != null) { 111 retVal = instance.findFileObject(file); 112 } 113 114 return new FileObject[]{retVal}; 115 } 116 117 private static URL fileToURL(final File file, final FileObject fo) throws MalformedURLException { 118 URL retVal = null; 119 if (fo.isFolder() && (!fo.isValid() || fo.isVirtual())) { 120 final String urlDef = file.toURI().toURL().toExternalForm(); 121 final String pathSeparator = "/"; if (!urlDef.endsWith(pathSeparator)) { 123 retVal = new URL (urlDef + pathSeparator); 125 } 126 } 127 retVal = (retVal == null) ? file.toURI().toURL() : retVal; 128 return retVal; 129 } 130 131 132 } 133 | Popular Tags |