KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > masterfs > MasterURLMapper


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.masterfs;
21
22 import java.net.URISyntaxException JavaDoc;
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 JavaDoc;
30 import java.net.MalformedURLException JavaDoc;
31 import java.net.URL JavaDoc;
32 import java.net.URI JavaDoc;
33 import org.openide.util.Exceptions;
34
35 /**
36  * Implements URLMapper for MasterFileSystem.
37  * @author rm111737
38  */

39 public final class MasterURLMapper extends URLMapper {
40     /** Creates a new instance of MasterURLMapper */
41     public MasterURLMapper() {
42     }
43
44     public FileObject[] getFileObjects(final URL JavaDoc url) {
45         final FileSystem hfs = MasterFileSystem.getDefault();
46         if (!url.getProtocol().equals("file")) return null; //NOI18N
47
//TODO: review and simplify
48
FileObject retVal = null;
49         String JavaDoc filePath = null;
50         try {
51             filePath = FileUtil.normalizeFile(new File JavaDoc(new URI JavaDoc(url.toExternalForm()))).getAbsolutePath();
52         } catch (URISyntaxException JavaDoc e) {
53             StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
54             sb.append(e.getLocalizedMessage()).append(" [").append(url.toExternalForm()).append(']');//NOI18N
55
IllegalArgumentException JavaDoc iax = new IllegalArgumentException JavaDoc(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.");//NOI18N
59
}
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 JavaDoc getURL(final FileObject fo, final int type) {
71         if (type == URLMapper.NETWORK || !(fo instanceof MasterFileObject)) return null;
72         MasterFileObject hfo = (MasterFileObject) fo;
73         File JavaDoc f = (hfo != null) ? hfo.getResource().getFile() : null;
74
75         try {
76             return (f != null) ? fileToURL(f, fo) : null;
77         } catch (MalformedURLException JavaDoc mfx) {
78             return null;
79         }
80     }
81     
82     private static boolean isWindowsDriveRoot(File JavaDoc file) {
83         return (Utilities.isWindows () || (Utilities.getOperatingSystem () == Utilities.OS_OS2)) && file.getParent() == null;
84     }
85     
86     static URL JavaDoc fileToURL(File JavaDoc file, FileObject fo) throws MalformedURLException JavaDoc {
87         URL JavaDoc retVal = null;
88         if (isWindowsDriveRoot(file)) {
89             retVal = new URL JavaDoc ("file:/"+file.getAbsolutePath ());//NOI18N
90
} else {
91             if (fo.isFolder() && (!fo.isValid() || fo.isVirtual())) {
92                 String JavaDoc urlDef = file.toURI().toURL().toExternalForm();
93                 String JavaDoc pathSeparator = "/";//NOI18N
94
if (!urlDef.endsWith(pathSeparator)) {
95                     retVal = new URL JavaDoc (urlDef + pathSeparator);
96                 }
97             }
98             retVal = (retVal == null) ? file.toURI().toURL() : retVal;
99         }
100         return retVal;
101     }
102
103 }
104
Popular Tags