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

The following examples show how to use com.intellij.util.io.URLUtil#splitJarUrl() . 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: 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;
}