From 6df84deb857b2274ff95bc5fd9db3e79334ff22a Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Fri, 7 Dec 2012 18:26:22 +0400 Subject: [PATCH] WEB-703 (firefox is not yet tested, windows is also untested) --- .../src/com/intellij/openapi/vfs/VfsUtil.java | 30 +++++++++++++++---- .../src/com/intellij/util/io/URLUtil.java | 16 ++++------ platform/util/util.iml | 9 ++++++ 3 files changed, 39 insertions(+), 16 deletions(-) diff --git a/platform/platform-api/src/com/intellij/openapi/vfs/VfsUtil.java b/platform/platform-api/src/com/intellij/openapi/vfs/VfsUtil.java index 4317fb122eb7..533354d939e7 100644 --- a/platform/platform-api/src/com/intellij/openapi/vfs/VfsUtil.java +++ b/platform/platform-api/src/com/intellij/openapi/vfs/VfsUtil.java @@ -50,6 +50,8 @@ public class VfsUtil extends VfsUtilCore { private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.vfs.VfsUtil"); public static final char VFS_PATH_SEPARATOR = '/'; + private static final String LOCALHOST_URI_PATH_PREFIX = "localhost/"; + public static void saveText(@NotNull VirtualFile file, @NotNull String text) throws IOException { Charset charset = file.getCharset(); file.setBinaryContent(text.getBytes(charset.name())); @@ -464,16 +466,32 @@ public class VfsUtil extends VfsUtilCore { } @NotNull - public static String fixURLforIDEA(@NotNull String url ) { + public static String fixURLforIDEA(@NotNull String url) { + // removeLocalhostPrefix - false due to backward compatibility reasons + return toIdeaUrl(url, false); + } + + @NotNull + public static String toIdeaUrl(@NotNull String url) { + return toIdeaUrl(url, true); + } + + @NotNull + public static String toIdeaUrl(@NotNull String url, boolean removeLocalhostPrefix) { int idx = url.indexOf(":/"); - if( idx >= 0 && idx+2 < url.length() && url.charAt(idx+2) != '/' ) { + if (idx >= 0 && idx + 2 < url.length() && url.charAt(idx + 2) != '/') { String prefix = url.substring(0, idx); - String suffix = url.substring(idx+2); + String suffix = url.substring(idx + 2); if (SystemInfo.isWindows) { - url = prefix+"://"+suffix; - } else { - url = prefix+":///"+suffix; + return prefix + "://" + suffix; + } + else if (removeLocalhostPrefix && prefix.equals(StandardFileSystems.FILE_PROTOCOL) && suffix.startsWith(LOCALHOST_URI_PATH_PREFIX)) { + // sometimes (e.g. in Google Chrome for Mac) local file url is prefixed with 'localhost' so we need to remove it + return prefix + ":///" + suffix.substring(LOCALHOST_URI_PATH_PREFIX.length()); + } + else { + return prefix + ":///" + suffix; } } return url; diff --git a/platform/util/src/com/intellij/util/io/URLUtil.java b/platform/util/src/com/intellij/util/io/URLUtil.java index fad1eca2cc4d..231047e086a0 100644 --- a/platform/util/src/com/intellij/util/io/URLUtil.java +++ b/platform/util/src/com/intellij/util/io/URLUtil.java @@ -16,14 +16,14 @@ package com.intellij.util.io; +import com.google.common.base.Charsets; import com.intellij.openapi.util.io.FileUtil; +import gnu.trove.TIntArrayList; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; import java.io.*; import java.net.URL; -import java.util.ArrayList; -import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; @@ -103,7 +103,7 @@ public class URLUtil { while (i < len) { char c = s.charAt(i); if (c == '%') { - List bytes = new ArrayList(); + TIntArrayList bytes = new TIntArrayList(); while (i + 2 < len && s.charAt(i) == '%') { final int d1 = decode(s.charAt(i + 1)); final int d2 = decode(s.charAt(i + 2)); @@ -118,14 +118,10 @@ public class URLUtil { if (!bytes.isEmpty()) { final byte[] bytesArray = new byte[bytes.size()]; for (int j = 0; j < bytes.size(); j++) { - bytesArray[j] = (byte)bytes.get(j).intValue(); - } - try { - decoded.append(new String(bytesArray, "UTF-8")); - continue; - } - catch (UnsupportedEncodingException ignored) { + bytesArray[j] = (byte)bytes.getQuick(j); } + decoded.append(new String(bytesArray, Charsets.UTF_8)); + continue; } } diff --git a/platform/util/util.iml b/platform/util/util.iml index 3e482bfe30e0..f94a232b957e 100644 --- a/platform/util/util.iml +++ b/platform/util/util.iml @@ -20,6 +20,15 @@ + + + + + + + + +