Java Code Examples for com.intellij.util.io.URLUtil#unescapePercentSequences()

The following examples show how to use com.intellij.util.io.URLUtil#unescapePercentSequences() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PathManager.java    From dynkt with GNU Affero General Public License v3.0 5 votes vote down vote up
@Nullable
private static String extractRoot(URL resourceURL, String resourcePath) {
	if ((!StringUtil.startsWithChar(resourcePath, '/')) && (!StringUtil.startsWithChar(resourcePath, '\\'))) {
		log("precondition failed: " + resourcePath);
		return null;
	}
	String resultPath = null;
	String protocol = resourceURL.getProtocol();
	if ("file".equals(protocol)) {
		String path = resourceURL.getFile();
		String testPath = path.replace('\\', '/');
		String testResourcePath = resourcePath.replace('\\', '/');
		if (StringUtil.endsWithIgnoreCase(testPath, testResourcePath)) {
			resultPath = path.substring(0, path.length() - resourcePath.length());
		}
	} else if ("jar".equals(protocol)) {
		Pair<String, String> paths = URLUtil.splitJarUrl(resourceURL.getFile());
		if (paths != null) {
			resultPath = paths.first;
		}
	} else if ("bundle".equals(protocol)) {
		resultPath = (new File(
				(PathManager.class.getProtectionDomain().getCodeSource().getLocation() + "").replace("file:/", "")))
						.getAbsolutePath().replace('\\', '/');
	} else {
		throw new UnsupportedOperationException(
				"No '" + protocol + "' known! (" + resourceURL + ", " + resourcePath + ")");
	}
	if (resultPath == null) {
		log("cannot extract: " + resourcePath + " from " + resourceURL);
		return null;
	}
	if ((SystemInfo.isWindows) && (resultPath.startsWith("/"))) {
		resultPath = resultPath.substring(1);
	}
	resultPath = StringUtil.trimEnd(resultPath, File.separator);
	resultPath = URLUtil.unescapePercentSequences(resultPath);

	return resultPath;
}
 
Example 2
Source File: VfsUtilCore.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static String convertFromUrl(@Nonnull URL url) {
  String protocol = url.getProtocol();
  String path = url.getPath();
  if (protocol.equals(URLUtil.JAR_PROTOCOL)) {
    if (StringUtil.startsWithConcatenation(path, URLUtil.FILE_PROTOCOL, PROTOCOL_DELIMITER)) {
      try {
        URL subURL = new URL(path);
        path = subURL.getPath();
      }
      catch (MalformedURLException e) {
        throw new RuntimeException(VfsBundle.message("url.parse.unhandled.exception"), e);
      }
    }
    else {
      throw new RuntimeException(new IOException(VfsBundle.message("url.parse.error", url.toExternalForm())));
    }
  }
  if (SystemInfo.isWindows || SystemInfo.isOS2) {
    while (!path.isEmpty() && path.charAt(0) == '/') {
      path = path.substring(1, path.length());
    }
  }

  path = URLUtil.unescapePercentSequences(path);
  return protocol + "://" + path;
}
 
Example 3
Source File: CustomProtocolHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public List<String> getOpenArgs(URI uri) {
  final List<String> args = new ArrayList<String>();
  final String query = uri.getQuery();
  String file = null;
  String line = null;
  if (query != null) {
    for (String param : query.split("&")) {
      String[] pair = param.split("=");
      String key = URLUtil.unescapePercentSequences(pair[0]);
      if (pair.length > 1) {
        if ("file".equals(key)) {
          file = URLUtil.unescapePercentSequences(pair[1]);
        } else if ("line".equals(key)) {
          line = URLUtil.unescapePercentSequences(pair[1]);
        }
      }
    }
  }

  if (file != null) {
    if (line != null) {
      args.add(LINE_NUMBER_ARG_NAME);
      args.add(line);
    }
    args.add(file);
  }
  return args;
}
 
Example 4
Source File: PathManager.java    From consulo with Apache License 2.0 5 votes vote down vote up
/**
 * Attempts to extract classpath entry part from passed URL.
 */
@Nullable
@NonNls
private static String extractRoot(URL resourceURL, String resourcePath) {
  if (!(StringUtil.startsWithChar(resourcePath, '/') || StringUtil.startsWithChar(resourcePath, '\\'))) {
    //noinspection HardCodedStringLiteral,UseOfSystemOutOrSystemErr
    System.err.println("precondition failed: " + resourcePath);
    return null;
  }

  String resultPath = null;
  String protocol = resourceURL.getProtocol();
  if (URLUtil.FILE_PROTOCOL.equals(protocol)) {
    String path = resourceURL.getFile();
    String testPath = path.replace('\\', '/');
    String testResourcePath = resourcePath.replace('\\', '/');
    if (StringUtil.endsWithIgnoreCase(testPath, testResourcePath)) {
      resultPath = path.substring(0, path.length() - resourcePath.length());
    }
  }
  else if (URLUtil.JAR_PROTOCOL.equals(protocol)) {
    Pair<String, String> paths = URLUtil.splitJarUrl(resourceURL.getFile());
    if (paths != null) {
      resultPath = paths.first;
    }
  }

  if (resultPath == null) {
    //noinspection HardCodedStringLiteral,UseOfSystemOutOrSystemErr
    System.err.println("cannot extract: " + resourcePath + " from " + resourceURL);
    return null;
  }

  resultPath = StringUtil.trimEnd(resultPath, File.separator);
  resultPath = URLUtil.unescapePercentSequences(resultPath);

  return resultPath;
}
 
Example 5
Source File: UrlImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public String getPath() {
  if (decodedPath == null) {
    decodedPath = URLUtil.unescapePercentSequences(path);
  }
  return decodedPath;
}
 
Example 6
Source File: FileUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
public static String unquote(@Nonnull String urlString) {
  urlString = urlString.replace('/', File.separatorChar);
  return URLUtil.unescapePercentSequences(urlString);
}