KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > masterfs > filebasedfs > FileBasedURLMapper


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.filebasedfs;
21
22 import java.net.URISyntaxException JavaDoc;
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 JavaDoc;
29 import java.net.MalformedURLException JavaDoc;
30 import java.net.URI JavaDoc;
31 import java.net.URL JavaDoc;
32 import org.openide.util.Exceptions;
33
34 //TODO: JDK problems with URL, URI, File conversion for UNC
35
/*
36 There must be consistently called conversion from FileUtil and URLMapper.
37 new File (URI.create (fo.getURL ().toExternalForm ())) is typical scenario that leads to this
38 bug: java.lang.IllegalArgumentException: URI has an authority component
39         at java.io.File.<init>(File.java:326)
40
41
42 Maybe there would be also possible to return a little special URL from FileBasedURLMapper that
43 would get special subclass of URLStreamHandler in constructor. This subclass of URLStreamHandler
44 would provided external form (method toExternalForm) that would be suitable for above mentioned
45 conversion from URL to File.
46         
47 Known problems :
48 1/ at java.io.File.<init>(File.java:326)
49      at org.netbeans.modules.javacore.parser.ASTProvider.getClassPath(ASTProvider.java:477)
50      at org.netbeans.lib.gjast.ASParser$BridgeContext.getClassPath(ASParser.java:421)
51
52 2/
53        at java.io.File.<init>(File.java:326)
54        at org.netbeans.modules.javacore.scanning.FileScanner.<init>(FileScanner.java:85)
55 catch] at org.netbeans.modules.javacore.JMManager.scanFiles(JMManager.java:1112)
56
57 3/ org.netbeans.modules.javacore.parser.ECRequestDescImpl.getFileName(FileObject fo,StringBuffer buf)
58     at java.io.File.<init>(File.java:326)
59      
60     
61     
62     
63 */

64
65 public final class FileBasedURLMapper extends URLMapper {
66     public final URL JavaDoc getURL(final FileObject fo, final int type) {
67         URL JavaDoc 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 JavaDoc e) {
74             retVal = null;
75         }
76         return retVal;
77     }
78
79     public final FileObject[] getFileObjects(final URL JavaDoc url) {
80         if (!"file".equals(url.getProtocol())) return null; //NOI18N
81
//TODO: review and simplify
82
FileObject retVal = null;
83         File JavaDoc file;
84         try {
85             final String JavaDoc host = url.getHost();
86             final String JavaDoc f = url.getFile();
87             //TODO: UNC workaround
88
//TODO: string concatenation
89
if (host != null && host.trim().length() != 0) {
90                 file = new File JavaDoc("////" + host + f);//NOI18N
91
} else {
92                 if (f.startsWith("//")) {
93                     file = new File JavaDoc(f);
94                 } else {
95                     file = FileUtil.normalizeFile(new File JavaDoc(new URI JavaDoc(url.toExternalForm())));
96                 }
97             }
98         } catch (URISyntaxException JavaDoc e) {
99             file = new File JavaDoc(url.getFile());
100             if (!file.exists()) {
101                 final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
102                 sb.append(e.getLocalizedMessage()).append(" [").append(url.toExternalForm()).append(']');//NOI18N
103
Exceptions.printStackTrace(new IllegalArgumentException JavaDoc(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 JavaDoc fileToURL(final File JavaDoc file, final FileObject fo) throws MalformedURLException JavaDoc {
118         URL JavaDoc retVal = null;
119         if (fo.isFolder() && (!fo.isValid() || fo.isVirtual())) {
120             final String JavaDoc urlDef = file.toURI().toURL().toExternalForm();
121             final String JavaDoc pathSeparator = "/";//NOI18N
122
if (!urlDef.endsWith(pathSeparator)) {
123                 //TODO: string concatenation
124
retVal = new URL JavaDoc(urlDef + pathSeparator);
125             }
126         }
127         retVal = (retVal == null) ? file.toURI().toURL() : retVal;
128         return retVal;
129     }
130
131
132 }
133
Popular Tags