Java Code Examples for com.intellij.openapi.vfs.VirtualFileManager#constructUrl()

The following examples show how to use com.intellij.openapi.vfs.VirtualFileManager#constructUrl() . 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: BlazeLibrary.java    From intellij with Apache License 2.0 6 votes vote down vote up
protected static String pathToUrl(File path) {
  String name = path.getName();
  boolean isJarFile =
      FileUtilRt.extensionEquals(name, "jar")
          || FileUtilRt.extensionEquals(name, "srcjar")
          || FileUtilRt.extensionEquals(name, "zip");
  // .jar files require an URL with "jar" protocol.
  String protocol =
      isJarFile
          ? StandardFileSystems.JAR_PROTOCOL
          : VirtualFileSystemProvider.getInstance().getSystem().getProtocol();
  String filePath = FileUtil.toSystemIndependentName(path.getPath());
  String url = VirtualFileManager.constructUrl(protocol, filePath);
  if (isJarFile) {
    url += URLUtil.JAR_SEPARATOR;
  }
  return url;
}
 
Example 2
Source File: HaxeFileUtil.java    From intellij-haxe with Apache License 2.0 6 votes vote down vote up
/**
 * Get the canonical name of a file, even when the directories are symlinks.
 *
 * @param file
 * @return
 */
public static VirtualFile getCanonicalFile(VirtualFile file) {
  try {
    java.io.File f = new java.io.File(file.getPath());
    java.io.File absolute = null == f ? null : f.getCanonicalFile();
    if (null != absolute) {
      // Of course, IDEA's notion of a URI requires "://" after the protocol separator
      // (as opposed to Java's ":").  So we can't just use the Java URI.
      VirtualFileManager vfm = VirtualFileManager.getInstance();
      String ideaUri = vfm.constructUrl(absolute.toURI().getScheme(), absolute.getPath());
      return VirtualFileManager.getInstance().findFileByUrl(ideaUri);
    }
  } catch(IOException e) {
    ; // Swallow
  }
  return null;
}
 
Example 3
Source File: ModuleManagerImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
@Nonnull
public Module newModule(@Nonnull @NonNls String name, @Nullable @NonNls String dirPath) {
  assertWritable();

  final String dirUrl = dirPath == null ? null : VirtualFileManager.constructUrl(StandardFileSystems.FILE_PROTOCOL, dirPath);

  ModuleEx moduleEx = null;
  if (dirUrl != null) {
    moduleEx = getModuleByDirUrl(dirUrl);
  }

  if (moduleEx == null) {
    moduleEx = createModule(name, dirUrl, null);
    initModule(moduleEx);
  }
  return moduleEx;
}
 
Example 4
Source File: UrlUtil.java    From intellij with Apache License 2.0 5 votes vote down vote up
public static String pathToUrl(String filePath) {
  filePath = FileUtil.toSystemIndependentName(filePath);
  if (filePath.endsWith(".srcjar") || filePath.endsWith(".jar")) {
    return URLUtil.JAR_PROTOCOL + URLUtil.SCHEME_SEPARATOR + filePath + URLUtil.JAR_SEPARATOR;
  } else if (filePath.contains("src.jar!")) {
    return URLUtil.JAR_PROTOCOL + URLUtil.SCHEME_SEPARATOR + filePath;
  } else {
    return VirtualFileManager.constructUrl(
        VirtualFileSystemProvider.getInstance().getSystem().getProtocol(), filePath);
  }
}
 
Example 5
Source File: HaxelibClasspathUtils.java    From intellij-haxe with Apache License 2.0 5 votes vote down vote up
/**
 * Workhorse for findFileOnClasspath.
 * @param cp
 * @param filePath
 * @return
 */
@Nullable
private static VirtualFile findFileOnOneClasspath(@NotNull HaxeClasspath cp, @NotNull final String filePath) {
  final VirtualFileManager vfmInstance = VirtualFileManager.getInstance();

  class MyLambda implements HaxeClasspath.Lambda {
    public VirtualFile found = null;
    public boolean processEntry(HaxeClasspathEntry entry) {
      String dirUrl = entry.getUrl();
      if (!URLUtil.containsScheme(dirUrl)) {
        dirUrl = vfmInstance.constructUrl(URLUtil.FILE_PROTOCOL, dirUrl);
      }
      VirtualFile dirName = vfmInstance.findFileByUrl(dirUrl);
      if (null != dirName && dirName.isDirectory()) {
        // XXX: This is wrong if filePath is already an absolute file name.
        found = dirName.findFileByRelativePath(filePath);
        if (null != found) {
          return false;  // Stop the search.
        }
      }
      return true;
    }
  }

  MyLambda lambda = new MyLambda();
  cp.iterate(lambda);
  return lambda.found;
}
 
Example 6
Source File: HaxeFileUtil.java    From intellij-haxe with Apache License 2.0 5 votes vote down vote up
/**
 * Quick 'n' dirty url fixup, if necessary.
 *
 * @param url
 * @return
 */
@Nullable
public static String fixUrl(@Nullable String url) {
  if (null == url || url.isEmpty())
    return url;
  return url.startsWith(LocalFileSystem.PROTOCOL)
         ? url
         : VirtualFileManager.constructUrl(LocalFileSystem.PROTOCOL, url);
}
 
Example 7
Source File: CompilerConfigurationImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
@Override
public String getCompilerOutputUrl() {
  if (myOutputDirPointer == null) {
    return VirtualFileManager.constructUrl(URLUtil.FILE_PROTOCOL, FileUtil.toSystemIndependentName(myProject.getBasePath()) + "/" + DEFAULT_OUTPUT_URL);
  }
  return myOutputDirPointer.getUrl();
}
 
Example 8
Source File: HttpFileSystemBase.java    From consulo with Apache License 2.0 5 votes vote down vote up
public VirtualFile findFileByPath(@Nonnull String path, boolean isDirectory) {
  try {
    String url = VirtualFileManager.constructUrl(myProtocol, path);
    return getRemoteFileManager().getOrCreateFile(url, path, isDirectory);
  }
  catch (IOException e) {
    return null;
  }
}
 
Example 9
Source File: FileOrDirectoryTreeNode.java    From consulo with Apache License 2.0 5 votes vote down vote up
protected FileOrDirectoryTreeNode(@Nonnull String path,
                                  @Nonnull SimpleTextAttributes invalidAttributes,
                                  @Nonnull Project project,
                                  @javax.annotation.Nullable String parentPath) {
  String preparedPath = path.replace(File.separatorChar, '/');
  String url = VirtualFileManager.constructUrl(LocalFileSystem.getInstance().getProtocol(), preparedPath);
  setUserObject(VirtualFilePointerManager.getInstance().create(url, this, this));
  myFile = new File(getFilePath());
  myInvalidAttributes = invalidAttributes;
  myProject = project;
  myName = parentPath == null ? myFile.getAbsolutePath() : myFile.getName();
}
 
Example 10
Source File: LibraryEditingUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static void copyLibrary(LibraryEx from, Map<String, String> rootMapping, LibraryEx.ModifiableModelEx target) {
  target.setProperties(from.getProperties());
  for (OrderRootType type : OrderRootType.getAllTypes()) {
    final String[] urls = from.getUrls(type);
    for (String url : urls) {
      final String protocol = VirtualFileManager.extractProtocol(url);
      if (protocol == null) continue;
      final String fullPath = VirtualFileManager.extractPath(url);
      final int sep = fullPath.indexOf(ArchiveFileSystem.ARCHIVE_SEPARATOR);
      String localPath;
      String pathInJar;
      if (sep != -1) {
        localPath = fullPath.substring(0, sep);
        pathInJar = fullPath.substring(sep);
      }
      else {
        localPath = fullPath;
        pathInJar = "";
      }
      final String targetPath = rootMapping.get(localPath);
      String targetUrl = targetPath != null ? VirtualFileManager.constructUrl(protocol, targetPath + pathInJar) : url;

      if (from.isJarDirectory(url, type)) {
        target.addJarDirectory(targetUrl, false, type);
      }
      else {
        target.addRoot(targetUrl, type);
      }
    }
  }
}
 
Example 11
Source File: ProjectUpdateSyncTask.java    From intellij with Apache License 2.0 4 votes vote down vote up
private static String pathToUrl(File path) {
  String filePath = FileUtil.toSystemIndependentName(path.getPath());
  return VirtualFileManager.constructUrl(
      VirtualFileSystemProvider.getInstance().getSystem().getProtocol(), filePath);
}
 
Example 12
Source File: MSBaseDotNetCompilerOptionsBuilder.java    From consulo-csharp with Apache License 2.0 4 votes vote down vote up
@Override
public DotNetCompilerMessage convertToMessage(Module module, String line)
{
	if(line.startsWith("error"))
	{
		return new DotNetCompilerMessage(CompilerMessageCategory.ERROR, line, null, -1, 1);
	}
	else
	{
		Matcher matcher = LINE_ERROR_PATTERN.matcher(line);
		if(matcher.matches())
		{
			CompilerMessageCategory category = CompilerMessageCategory.INFORMATION;
			if(matcher.group(4).equals("error"))
			{
				category = CompilerMessageCategory.ERROR;
			}
			else if(matcher.group(4).equals("warning"))
			{
				category = CompilerMessageCategory.WARNING;
			}

			String fileUrl = FileUtil.toSystemIndependentName(matcher.group(1));
			if(!FileUtil.isAbsolute(fileUrl))
			{
				fileUrl = module.getModuleDirUrl() + "/" + fileUrl;
			}
			else
			{
				fileUrl = VirtualFileManager.constructUrl(StandardFileSystems.FILE_PROTOCOL, fileUrl);
			}

			int codeLine = Integer.parseInt(matcher.group(2));
			int codeColumn = Integer.parseInt(matcher.group(3));
			String message = matcher.group(6);
			if(ApplicationProperties.isInSandbox())
			{
				message += "(" + matcher.group(5) + ")";
			}
			return new DotNetCompilerMessage(category, message, fileUrl, codeLine, codeColumn - 1);
		}
	}
	return null;
}
 
Example 13
Source File: HaxelibClasspathUtils.java    From intellij-haxe with Apache License 2.0 4 votes vote down vote up
/**
 * Given a module and a list of files, find the first one that occurs on the
 * classpath.
 *
 * @param module - Module from which to obtain the classpath.
 * @param files - a list of files to check.
 * @return the first of the list of files that occurs on the classpath, or
 *         null if none appear there.
 */
@Nullable
public static VirtualFile findFirstFileOnClasspath(@NotNull final Module module,
                                                   @NotNull final java.util.Collection<VirtualFile> files) {
  if (files.isEmpty()) {
    return null;
  }

  final VirtualFileManager vfmInstance = VirtualFileManager.getInstance();
  final HaxeClasspath cp = ApplicationManager.getApplication().runReadAction(new Computable<HaxeClasspath>() {
    @Override
    public HaxeClasspath compute() {
      return getFullClasspath(module);
    }
  });

  class MyLambda implements HaxeClasspath.Lambda {
    public VirtualFile found;
    public boolean processEntry(HaxeClasspathEntry entry) {
      String dirUrl = entry.getUrl();
      if (!URLUtil.containsScheme(dirUrl)) {
        dirUrl = vfmInstance.constructUrl(URLUtil.FILE_PROTOCOL, dirUrl);
      }
      VirtualFile dirName = vfmInstance.findFileByUrl(dirUrl);
      if (null != dirName && dirName.isDirectory()) {
        String dirPath = dirName.getPath();
        for (VirtualFile f : files) {
          if (f.exists()) {
            // We have a complete path, compare the leading paths.
            String filePath = f.getPath();
            if (filePath.startsWith(dirPath)) {
              found = f;
            }
          } else {
            // We have a partial path, search the path for a matching file.
            found = dirName.findFileByRelativePath(f.toString());
          }
          if (null != found) {
            return false;
          }
        }
      }
      return true;
    }
  }

  MyLambda lambda = new MyLambda();
  cp.iterate(lambda);
  return lambda.found;
}
 
Example 14
Source File: ResourceCompiler.java    From consulo with Apache License 2.0 4 votes vote down vote up
public String getSourceFileUrl() {
  // do not use mySourseFile.getUrl() directly as it requires read action
  return VirtualFileManager.constructUrl(mySourceFile.getFileSystem().getProtocol(), myFromPath);
}
 
Example 15
Source File: HttpFileSystemBase.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
@Override
public String extractPresentableUrl(@Nonnull String path) {
  return VirtualFileManager.constructUrl(myProtocol, path);
}
 
Example 16
Source File: ExternalizablePath.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static String urlValue(String localPath) {
  if (localPath == null) return "";
  localPath = localPath.trim();
  if (localPath.length() == 0) return "";
  return VirtualFileManager.constructUrl(LocalFileSystem.PROTOCOL, localPath.replace(File.separatorChar, '/'));
}