Index: editor/tools/plugins/com.google.dart.tools.ui/src/com/google/dart/tools/ui/internal/util/DirectoryVerification.java |
=================================================================== |
--- editor/tools/plugins/com.google.dart.tools.ui/src/com/google/dart/tools/ui/internal/util/DirectoryVerification.java (revision 35204) |
+++ editor/tools/plugins/com.google.dart.tools.ui/src/com/google/dart/tools/ui/internal/util/DirectoryVerification.java (working copy) |
@@ -25,7 +25,8 @@ |
import java.io.File; |
import java.io.IOException; |
-import java.net.URISyntaxException; |
+import java.net.URI; |
+import java.net.URL; |
/** |
* A utility class to handle checking to see if a directory is legal to open in the editor. |
@@ -36,8 +37,9 @@ |
// disallow parent directories of the workspace |
try { |
Location workspaceLocation = Platform.getInstanceLocation(); |
- File workspaceDirectory = new File(workspaceLocation.getURL().toURI()); |
+ File workspaceDirectory = toFile(workspaceLocation.getURL()); |
+ |
String dirPath = directory.getCanonicalPath(); |
String workspacePath = workspaceDirectory.getCanonicalPath(); |
@@ -47,8 +49,6 @@ |
} |
} catch (IOException e) { |
- } catch (URISyntaxException e) { |
- |
} |
// discourage user home |
@@ -79,6 +79,57 @@ |
return status.isOK(); |
} |
+ /** |
+ * Ensures the given path string starts with exactly four leading slashes. |
+ */ |
+ private static String ensureUNCPath(String path) { |
+ int len = path.length(); |
+ StringBuffer result = new StringBuffer(len); |
+ for (int i = 0; i < 4; i++) { |
+ // if we have hit the first non-slash character, add another leading slash |
+ if (i >= len || result.length() > 0 || path.charAt(i) != '/') { |
+ result.append('/'); |
+ } |
+ } |
+ result.append(path); |
+ return result.toString(); |
+ } |
+ |
+ /** |
+ * Returns the URL as a local file, or <code>null</code> if the given URL does not represent a |
+ * local file. |
+ * |
+ * @param url The url to return the file for |
+ * @return The local file corresponding to the given url, or <code>null</code> |
+ */ |
+ private static File toFile(URL url) { |
+ |
+ if (!"file".equalsIgnoreCase(url.getProtocol())) { |
+ return null; |
+ //assume all illegal characters have been properly encoded, so use URI class to unencode |
+ } |
+ |
+ String externalForm = url.toExternalForm(); |
+ String pathString = externalForm.substring(5); |
+ |
+ try { |
+ if (pathString.indexOf('/') == 0) { |
+ if (pathString.indexOf("//") == 0) { |
+ externalForm = "file:" + ensureUNCPath(pathString); //$NON-NLS-1$ |
+ } |
+ return new File(new URI(externalForm)); |
+ } |
+ if (pathString.indexOf(':') == 1) { |
+ return new File(new URI("file:/" + pathString)); //$NON-NLS-1$ |
+ } |
+ |
+ return new File(new URI(pathString).getSchemeSpecificPart()); |
+ } catch (Exception e) { |
+ //URL contains unencoded characters |
+ return new File(pathString); |
+ } |
+ } |
+ |
private DirectoryVerification() { |
} |