KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > filesystems > NbfsUtil


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.openide.filesystems;
21
22 import java.io.UnsupportedEncodingException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.net.URLDecoder JavaDoc;
25 import java.net.URLEncoder JavaDoc;
26 import java.security.AccessController JavaDoc;
27 import java.security.PrivilegedActionException JavaDoc;
28 import java.security.PrivilegedExceptionAction JavaDoc;
29 import java.util.StringTokenizer JavaDoc;
30
31 /**
32  * @author Radek Matous
33  */

34 @SuppressWarnings JavaDoc("deprecation") // needs to use old Repository methods for compat
35
final class NbfsUtil {
36     /** url separator */
37     private static final char SEPARATOR = '/';
38
39     /**
40      * Gets URL with nbfs protocol for passes fo
41      * @param fo
42      * @return url with nbfs protocol
43      * @throws FileStateInvalidException if FileObject somehow corrupted
44      */

45     static URL JavaDoc getURL(FileObject fo) throws FileStateInvalidException {
46         final String JavaDoc fsPart = encodeFsPart(fo);
47         final String JavaDoc foPart = encodeFoPart(fo);
48
49         final String JavaDoc host = "nbhost"; //NOI18N
50
final String JavaDoc file = combine(fsPart, foPart);
51
52         // #13038: the URL constructor accepting a handler is a security-sensitive
53
// operation. Sometimes a user class loaded internally (customized bean...),
54
// which has no privileges, needs to make and use an nbfs: URL, since this
55
// may be the URL used by e.g. ClassLoader.getResource for resources.
56
try {
57             return AccessController.doPrivileged(
58                 new PrivilegedExceptionAction JavaDoc<URL JavaDoc>() {
59                     public URL JavaDoc run() throws Exception JavaDoc {
60                         // #30397: the fsPart name cannot be null
61
return new URL JavaDoc(FileURL.PROTOCOL, host, -1, file, FileURL.HANDLER); // NOI18N
62
}
63                 }
64             );
65         } catch (PrivilegedActionException JavaDoc pae) {
66             // MalformedURLException is declared but should not happen.
67
IllegalStateException JavaDoc ise = new IllegalStateException JavaDoc(pae.toString());
68             ExternalUtil.annotate(ise, pae);
69             throw ise;
70         }
71     }
72
73     private static String JavaDoc combine(final String JavaDoc host, final String JavaDoc file) {
74         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
75         sb.append(SEPARATOR).append(host);
76         sb.append(file);
77
78         return sb.toString();
79     }
80
81     private static String JavaDoc[] split(URL JavaDoc url) {
82         String JavaDoc file = url.getFile();
83         int idx = file.indexOf("/", 1);
84         String JavaDoc fsPart = "";
85         String JavaDoc foPart = file;
86
87         if (idx > 1) {
88             fsPart = file.substring(1, idx);
89             foPart = file.substring(idx + 1);
90         }
91
92         return new String JavaDoc[] { fsPart, foPart };
93     }
94
95     /**
96      * Gets FileObject for passed url.
97      * @param url
98      * @return appropriate FileObject. Can return null for other protocol than nbfs or
99      * if such FileObject isn't reachable via Repository.
100      */

101     static FileObject getFileObject(URL JavaDoc url) {
102         if (!url.getProtocol().equals(FileURL.PROTOCOL)) {
103             return null;
104         }
105
106         if (isOldEncoding(url)) {
107             return oldDecode(url);
108         }
109
110         String JavaDoc[] urlParts = split(url);
111
112         String JavaDoc fsName = decodeFsPart(urlParts[0]);
113         String JavaDoc foName = decodeFoPart(urlParts[1]);
114
115         FileSystem fsys = ExternalUtil.getRepository().findFileSystem(fsName);
116
117         return (fsys == null) ? null : fsys.findResource(foName);
118     }
119
120     private static String JavaDoc encodeFsPart(FileObject fo) throws FileStateInvalidException {
121         FileSystem fs = fo.getFileSystem();
122
123         return encoder(fs.getSystemName());
124     }
125
126     private static String JavaDoc encodeFoPart(FileObject fo) {
127         StringTokenizer JavaDoc elemsEnum;
128         StringBuffer JavaDoc sBuff = new StringBuffer JavaDoc();
129         elemsEnum = new StringTokenizer JavaDoc(fo.getPath(), String.valueOf(SEPARATOR));
130
131         while (elemsEnum.hasMoreElements()) {
132             sBuff.append(SEPARATOR);
133             sBuff.append(encoder((String JavaDoc) elemsEnum.nextElement()));
134         }
135
136         String JavaDoc retVal = sBuff.toString();
137
138         if ((retVal.length() == 0) || (fo.isFolder() && (retVal.charAt(retVal.length() - 1) != SEPARATOR))) {
139             retVal += SEPARATOR;
140         }
141
142         return retVal;
143     }
144
145     private static String JavaDoc decodeFsPart(String JavaDoc encodedStr) {
146         return decoder(encodedStr);
147     }
148
149     private static String JavaDoc decodeFoPart(String JavaDoc encodedStr) {
150         if (encodedStr == null) {
151             return ""; //NOI18N
152
}
153
154         StringTokenizer JavaDoc elemsEnum;
155         StringBuffer JavaDoc sBuff = new StringBuffer JavaDoc();
156         elemsEnum = new StringTokenizer JavaDoc(encodedStr, String.valueOf(SEPARATOR));
157
158         while (elemsEnum.hasMoreElements()) {
159             sBuff.append(SEPARATOR);
160             sBuff.append(decoder((String JavaDoc) elemsEnum.nextElement()));
161         }
162
163         return sBuff.toString();
164     }
165
166     private static String JavaDoc encoder(String JavaDoc elem) {
167         try {
168             return URLEncoder.encode(elem, "UTF-8"); // NOI18N
169
} catch (UnsupportedEncodingException JavaDoc e) {
170             throw new AssertionError JavaDoc(e);
171         }
172     }
173
174     private static String JavaDoc decoder(String JavaDoc elem) {
175         try {
176             return URLDecoder.decode(elem, "UTF-8"); // NOI18N
177
} catch (UnsupportedEncodingException JavaDoc e) {
178             throw new AssertionError JavaDoc(e);
179         }
180     }
181
182     // backward compatibility
183
private static boolean isOldEncoding(URL JavaDoc url) {
184         String JavaDoc host = url.getHost();
185
186         return (host == null) || (host.length() == 0);
187     }
188
189     private static FileObject oldDecode(URL JavaDoc u) {
190         String JavaDoc resourceName = u.getFile();
191
192         if (resourceName.startsWith("/")) {
193             resourceName = resourceName.substring(1); // NOI18N
194
}
195
196         // first part is FS name
197
int first = resourceName.indexOf('/'); // NOI18N
198

199         if (first == -1) {
200             return null;
201         }
202
203         String JavaDoc fileSystemName = oldDecodeFSName(resourceName.substring(0, first));
204         resourceName = resourceName.substring(first);
205
206         FileSystem fsys = ExternalUtil.getRepository().findFileSystem(fileSystemName);
207
208         return (fsys == null) ? null : fsys.findResource(resourceName);
209     }
210
211     /** Decodes name to FS one.
212      * @param name encoded name
213      * @return original name of the filesystem
214      */

215     private static String JavaDoc oldDecodeFSName(String JavaDoc name) {
216         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
217         int i = 0;
218         int len = name.length();
219
220         while (i < len) {
221             char ch = name.charAt(i++);
222
223             if ((ch == 'Q') && (i < len)) {
224                 switch (name.charAt(i++)) {
225                 case 'B':
226                     sb.append('/');
227
228                     break;
229
230                 case 'C':
231                     sb.append(':');
232
233                     break;
234
235                 case 'D':
236                     sb.append('\\');
237
238                     break;
239
240                 case 'E':
241                     sb.append('#');
242
243                     break;
244
245                 default:
246                     sb.append('Q');
247
248                     break;
249                 }
250             } else {
251                 // not Q
252
sb.append(ch);
253             }
254         }
255
256         return sb.toString();
257     }
258 }
259
Popular Tags