public void testFindRootShouldNotBeFooledByRelativePath() throws IOException { File tmp = createTempDirectory(); File x = new File(tmp, "x.jar"); x.createNewFile(); LocalFileSystem lfs = LocalFileSystem.getInstance(); VirtualFile vx = lfs.refreshAndFindFileByIoFile(x); assertNotNull(vx); ArchiveFileSystem jfs = (ArchiveFileSystem)StandardFileSystems.jar(); VirtualFile root = ArchiveVfsUtil.getArchiveRootForLocalFile(vx); PersistentFS fs = PersistentFS.getInstance(); String path = vx.getPath() + "/../" + vx.getName() + ArchiveFileSystem.ARCHIVE_SEPARATOR; NewVirtualFile root1 = fs.findRoot(path, (NewVirtualFileSystem)jfs); assertSame(root1, root); }
@Nonnull public static Pair<InputStream, Long> getArchiveEntryInputStream(VirtualFile sourceFile, final CompileContext context) throws IOException { final String fullPath = sourceFile.getPath(); final int jarEnd = fullPath.indexOf(ArchiveFileSystem.ARCHIVE_SEPARATOR); LOG.assertTrue(jarEnd != -1, fullPath); String pathInJar = fullPath.substring(jarEnd + ArchiveFileSystem.ARCHIVE_SEPARATOR.length()); String jarPath = fullPath.substring(0, jarEnd); final ZipFile jarFile = new ZipFile(new File(FileUtil.toSystemDependentName(jarPath))); final ZipEntry entry = jarFile.getEntry(pathInJar); if (entry == null) { context.addMessage(CompilerMessageCategory.ERROR, "Cannot extract '" + pathInJar + "' from '" + jarFile.getName() + "': entry not found", null, -1, -1); return Pair.empty(); } BufferedInputStream bufferedInputStream = new BufferedInputStream(jarFile.getInputStream(entry)) { @Override public void close() throws IOException { super.close(); jarFile.close(); } }; return Pair.<InputStream, Long>create(bufferedInputStream, entry.getSize()); }
@Nullable private static FileType findFileType(final VirtualFile file) { return ApplicationManager.getApplication().runReadAction(new Computable<FileType>() { @Override public FileType compute() { VirtualFile tempFile = file; if ((file.getFileSystem() instanceof ArchiveFileSystem) && file.getParent() == null) { //[myakovlev] It was bug - directories with *.jar extensions was saved as files of JarFileSystem. // so we can not just return true, we should filter such directories. String path = file.getPath().substring(0, file.getPath().length() - ArchiveFileSystem.ARCHIVE_SEPARATOR.length()); tempFile = LocalFileSystem.getInstance().findFileByPath(path); } if (tempFile != null && !tempFile.isDirectory()) { return tempFile.getFileType(); } return null; } }); }
@Nonnull @Override public CellAppearanceEx forLibrary(Project project, @Nonnull final Library library, final boolean hasInvalidRoots) { final StructureConfigurableContext context = ProjectStructureConfigurable.getInstance(project).getContext(); final Icon icon = LibraryPresentationManager.getInstance().getCustomIcon(library, context); final String name = library.getName(); if (name != null) { return normalOrRedWaved(name, (icon != null ? icon : AllIcons.Nodes.PpLib), hasInvalidRoots); } final String[] files = library.getUrls(BinariesOrderRootType.getInstance()); if (files.length == 0) { return SimpleTextCellAppearance.invalid(ProjectBundle.message("library.empty.library.item"), AllIcons.Nodes.PpLib); } else if (files.length == 1) { return forVirtualFilePointer(new LightFilePointer(files[0])); } final String url = StringUtil.trimEnd(files[0], ArchiveFileSystem.ARCHIVE_SEPARATOR); return SimpleTextCellAppearance.regular(PathUtil.getFileName(url), AllIcons.Nodes.PpLib); }
@Override @Nullable public Icon getIcon(@NotNull PsiElement element, int flags) { if (element instanceof PsiDirectory) { final PsiDirectory psiDirectory = (PsiDirectory)element; final VirtualFile vFile = psiDirectory.getVirtualFile(); final Project project = psiDirectory.getProject(); SourceFolder sourceFolder; Icon symbolIcon; if (vFile.getParent() == null && vFile.getFileSystem() instanceof ArchiveFileSystem) { symbolIcon = PlatformIcons.JAR_ICON; } else if (ProjectRootsUtil.isModuleContentRoot(vFile, project)) { Module module = ProjectRootManager.getInstance(project).getFileIndex().getModuleForFile(vFile); symbolIcon = module != null ? ModuleType.get(module).getIcon() : PlatformIcons.CONTENT_ROOT_ICON_CLOSED; } else if ((sourceFolder = ProjectRootsUtil.getModuleSourceRoot(vFile, project)) != null) { symbolIcon = SourceRootPresentation.getSourceRootIcon(sourceFolder); } else if (JavaDirectoryService.getInstance().getPackage(psiDirectory) != null) { symbolIcon = PlatformIcons.PACKAGE_ICON; } else if (!Registry.is("ide.hide.excluded.files") && ProjectRootManager.getInstance(project).getFileIndex().isExcluded(vFile)) { symbolIcon = AllIcons.Modules.ExcludeRoot; } else { symbolIcon = PlatformIcons.DIRECTORY_CLOSED_ICON; } return ElementBase.createLayeredIcon(element, symbolIcon, 0); } return null; }
/** * Return correct instance of directory icon (taken straight from the source code) * * @param element */ private Icon getDirectoryIcon(final PsiDirectory element) { final VirtualFile vFile = element.getVirtualFile(); final Project project = element.getProject(); final SourceFolder sourceFolder; Icon symbolIcon = null; if (vFile.getParent() == null && vFile.getFileSystem() instanceof ArchiveFileSystem) { symbolIcon = PlatformIcons.JAR_ICON; } else if (ProjectRootsUtil.isModuleContentRoot(vFile, project)) { final Module module = ProjectRootManager.getInstance(project).getFileIndex().getModuleForFile(vFile); symbolIcon = module != null ? ModuleType.get(module).getIcon() : PlatformIcons.CONTENT_ROOT_ICON_CLOSED; } else if ((sourceFolder = ProjectRootsUtil.getModuleSourceRoot(vFile, project)) != null) { symbolIcon = SourceRootPresentation.getSourceRootIcon(sourceFolder); } else if (hasJFS && JrtFileSystem.isModuleRoot(vFile)) { symbolIcon = AllIcons.Nodes.JavaModuleRoot; } else if (hasJDS && JavaDirectoryService.getInstance().getPackage(element) != null) { symbolIcon = PlatformIcons.PACKAGE_ICON; } else if (!Registry.is("ide.hide.excluded.files") && ProjectRootManager.getInstance(project).getFileIndex().isExcluded(vFile)) { symbolIcon = AllIcons.Modules.ExcludeRoot; } try { if (ProjectRootsUtil.findUnloadedModuleByContentRoot(vFile, project) != null) { symbolIcon = AllIcons.Modules.UnloadedModule; } } catch (NoSuchMethodError e) { // till android studio implements this shit; } if (symbolIcon != null) { return ElementBase.createLayeredIcon(element, symbolIcon, 0); } else { return TintedIconsService.getIcon("/icons/nodes/folderClosed.png", "ff00cc"); } }
public static String toPresentableUrl(String url) { String path = VirtualFileManager.extractPath(url); if (path.endsWith(ArchiveFileSystem.ARCHIVE_SEPARATOR)) { path = path.substring(0, path.length() - ArchiveFileSystem.ARCHIVE_SEPARATOR.length()); } return path.replace('/', File.separatorChar); }
@Nonnull @Override public PackagingElement<?> createExtractedDirectory(@Nonnull VirtualFile jarEntry) { LOG.assertTrue(jarEntry.getFileSystem() instanceof ArchiveFileSystem, "Expected file from jar but file from " + jarEntry.getFileSystem() + " found"); final String fullPath = jarEntry.getPath(); final int jarEnd = fullPath.indexOf(ArchiveFileSystem.ARCHIVE_SEPARATOR); return new ExtractedDirectoryPackagingElement(fullPath.substring(0, jarEnd), fullPath.substring(jarEnd + 1)); }
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); } } } }
public static File getArchiveFile(VirtualFile file) { String fullPath = file.getPath(); return new File(FileUtil.toSystemDependentName(fullPath.substring(fullPath.indexOf(ArchiveFileSystem.ARCHIVE_SEPARATOR)))); }
private static String appendPathInJar(String outputPath, String pathInJar) { LOGGER.assertTrue(outputPath.length() > 0 && outputPath.charAt(outputPath.length() - 1) != '/'); LOGGER.assertTrue(pathInJar.length() > 0 && pathInJar.charAt(0) != '/'); return outputPath + ArchiveFileSystem.ARCHIVE_SEPARATOR + pathInJar; }
@SuppressWarnings({"ConstantConditions"}) public ArchiveFileDiffElement(@Nonnull VirtualFile file) { super(file.getFileSystem() instanceof ArchiveFileSystem ? file : ArchiveVfsUtil.getArchiveRootForLocalFile(file)); }
@Override protected int getSplitUrlIndex(String url) { int jarNameEnd = url.lastIndexOf(ArchiveFileSystem.ARCHIVE_SEPARATOR.charAt(0)); String jarUrl = jarNameEnd >= 0 ? url.substring(0, jarNameEnd) : url; return super.getSplitUrlIndex(jarUrl); }