1 11 package org.eclipse.core.filesystem; 12 13 import java.io.File ; 14 import java.net.URI ; 15 import java.net.URISyntaxException ; 16 import org.eclipse.core.runtime.*; 17 18 30 public class URIUtil { 31 32 43 public static boolean equals(URI one, URI two) { 44 try { 45 return EFS.getStore(one).equals(EFS.getStore(two)); 46 } catch (CoreException e) { 47 return one.equals(two); 49 } 50 } 51 52 56 private static String escapeColons(String string) { 57 final String COLON_STRING = "%3A"; if (string.indexOf(':') == -1) 59 return string; 60 int length = string.length(); 61 StringBuffer result = new StringBuffer (length); 62 for (int i = 0; i < length; i++) { 63 char c = string.charAt(i); 64 if (c == ':') 65 result.append(COLON_STRING); 66 else 67 result.append(c); 68 } 69 return result.toString(); 70 } 71 72 80 public static IPath toPath(URI uri) { 81 Assert.isNotNull(uri); 82 if (EFS.SCHEME_FILE.equals(uri.getScheme())) 83 return new Path(uri.getSchemeSpecificPart()); 84 return null; 85 } 86 87 93 public static URI toURI(IPath path) { 94 if (path == null) 95 return null; 96 if (path.isAbsolute()) 97 return toURI(path.toFile().getAbsolutePath()); 98 try { 99 return new URI (escapeColons(path.toString())); 101 } catch (URISyntaxException e) { 102 return toURI(path.toFile().getAbsolutePath()); 103 } 104 } 105 106 114 public static URI toURI(String pathString) { 115 if (File.separatorChar != '/') 116 pathString = pathString.replace(File.separatorChar, '/'); 117 final int length = pathString.length(); 118 StringBuffer pathBuf = new StringBuffer (length + 1); 119 if (length > 0 && (pathString.charAt(0) != '/')) 121 pathBuf.append('/'); 122 if (pathString.startsWith("//")) pathBuf.append('/').append('/'); 125 pathBuf.append(pathString); 126 try { 127 return new URI (EFS.SCHEME_FILE, null, pathBuf.toString(), null); 128 } catch (URISyntaxException e) { 129 return new File (pathString).toURI(); 131 } 132 } 133 134 137 private URIUtil() { 138 super(); 139 } 140 } 141 | Popular Tags |