1 19 20 package org.openide.filesystems; 21 22 import java.io.UnsupportedEncodingException ; 23 import java.net.URL ; 24 import java.net.URLDecoder ; 25 import java.net.URLEncoder ; 26 import java.security.AccessController ; 27 import java.security.PrivilegedActionException ; 28 import java.security.PrivilegedExceptionAction ; 29 import java.util.StringTokenizer ; 30 31 34 @SuppressWarnings ("deprecation") final class NbfsUtil { 36 37 private static final char SEPARATOR = '/'; 38 39 45 static URL getURL(FileObject fo) throws FileStateInvalidException { 46 final String fsPart = encodeFsPart(fo); 47 final String foPart = encodeFoPart(fo); 48 49 final String host = "nbhost"; final String file = combine(fsPart, foPart); 51 52 try { 57 return AccessController.doPrivileged( 58 new PrivilegedExceptionAction <URL >() { 59 public URL run() throws Exception { 60 return new URL (FileURL.PROTOCOL, host, -1, file, FileURL.HANDLER); } 63 } 64 ); 65 } catch (PrivilegedActionException pae) { 66 IllegalStateException ise = new IllegalStateException (pae.toString()); 68 ExternalUtil.annotate(ise, pae); 69 throw ise; 70 } 71 } 72 73 private static String combine(final String host, final String file) { 74 StringBuffer sb = new StringBuffer (); 75 sb.append(SEPARATOR).append(host); 76 sb.append(file); 77 78 return sb.toString(); 79 } 80 81 private static String [] split(URL url) { 82 String file = url.getFile(); 83 int idx = file.indexOf("/", 1); 84 String fsPart = ""; 85 String 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 [] { fsPart, foPart }; 93 } 94 95 101 static FileObject getFileObject(URL 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 [] urlParts = split(url); 111 112 String fsName = decodeFsPart(urlParts[0]); 113 String 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 encodeFsPart(FileObject fo) throws FileStateInvalidException { 121 FileSystem fs = fo.getFileSystem(); 122 123 return encoder(fs.getSystemName()); 124 } 125 126 private static String encodeFoPart(FileObject fo) { 127 StringTokenizer elemsEnum; 128 StringBuffer sBuff = new StringBuffer (); 129 elemsEnum = new StringTokenizer (fo.getPath(), String.valueOf(SEPARATOR)); 130 131 while (elemsEnum.hasMoreElements()) { 132 sBuff.append(SEPARATOR); 133 sBuff.append(encoder((String ) elemsEnum.nextElement())); 134 } 135 136 String 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 decodeFsPart(String encodedStr) { 146 return decoder(encodedStr); 147 } 148 149 private static String decodeFoPart(String encodedStr) { 150 if (encodedStr == null) { 151 return ""; } 153 154 StringTokenizer elemsEnum; 155 StringBuffer sBuff = new StringBuffer (); 156 elemsEnum = new StringTokenizer (encodedStr, String.valueOf(SEPARATOR)); 157 158 while (elemsEnum.hasMoreElements()) { 159 sBuff.append(SEPARATOR); 160 sBuff.append(decoder((String ) elemsEnum.nextElement())); 161 } 162 163 return sBuff.toString(); 164 } 165 166 private static String encoder(String elem) { 167 try { 168 return URLEncoder.encode(elem, "UTF-8"); } catch (UnsupportedEncodingException e) { 170 throw new AssertionError (e); 171 } 172 } 173 174 private static String decoder(String elem) { 175 try { 176 return URLDecoder.decode(elem, "UTF-8"); } catch (UnsupportedEncodingException e) { 178 throw new AssertionError (e); 179 } 180 } 181 182 private static boolean isOldEncoding(URL url) { 184 String host = url.getHost(); 185 186 return (host == null) || (host.length() == 0); 187 } 188 189 private static FileObject oldDecode(URL u) { 190 String resourceName = u.getFile(); 191 192 if (resourceName.startsWith("/")) { 193 resourceName = resourceName.substring(1); } 195 196 int first = resourceName.indexOf('/'); 199 if (first == -1) { 200 return null; 201 } 202 203 String 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 215 private static String oldDecodeFSName(String name) { 216 StringBuffer sb = new StringBuffer (); 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 sb.append(ch); 253 } 254 } 255 256 return sb.toString(); 257 } 258 } 259 | Popular Tags |