public static IFile LinkFile(String path) { IWorkspace ws = ResourcesPlugin.getWorkspace(); IProject project = ws.getRoot().getProject("tmp"); if (!project.exists()) try { project.create(null); } catch (CoreException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (!project.isOpen()) try { project.open(null); } catch (CoreException e) { // TODO Auto-generated catch block e.printStackTrace(); } IPath location = new Path(path); IFile file = project.getFile(location.lastSegment()); try { file.delete(true, null); } catch (CoreException e1) { } try { file.createLink(location, IResource.NONE, null); } catch (CoreException e) { } return file; }
/** * Creates new eclipse file-link from project root to EFS file. * * @param project - project where to create the file. * @param fileLink - filename of the link being created. * @param realFile - file on the EFS file system, the target of the link. * @return file handle. * @throws CoreException if something goes wrong. */ public static IFile createEfsFile(IProject project, String fileLink, URI realFile) throws CoreException { IFile file = project.getFile(fileLink); file.createLink(realFile, IResource.ALLOW_MISSING_LOCAL, NULL_MONITOR); resourcesCreated.add(file); return file; }
/** * Creates new eclipse file-link from project root to file system file. The filename can include * relative path as a part of the name but the the path has to be present on disk. * * @param project - project where to create the file. * @param fileLink - filename of the link being created. * @param realFile - file on the file system, the target of the link. * @return file handle. * @throws CoreException if something goes wrong. */ public static IFile createLinkedFile(IProject project, String fileLink, IPath realFile) throws CoreException { IFile file = project.getFile(fileLink); file.createLink(realFile, IResource.REPLACE, null); Assert.assertTrue(file.exists()); resourcesCreated.add(file); return file; }
public void testMoveProjectWithVirtualFolder() { IPath fileLocation = getRandomLocation(); IPath folderLocation = getRandomLocation(); IFile file = existingVirtualFolderInExistingProject.getFile(getUniqueString()); IFolder folder = existingVirtualFolderInExistingProject.getFolder(getUniqueString()); IFile childFile = folder.getFile(getUniqueString()); IResource[] oldResources = new IResource[] {existingProject, file, folder, childFile}; try { assertDoesNotExistInWorkspace("1.0", new IResource[] {folder, file, childFile}); try { createFileInFileSystem(fileLocation); folderLocation.toFile().mkdir(); folder.createLink(folderLocation, IResource.NONE, getMonitor()); file.createLink(fileLocation, IResource.NONE, getMonitor()); childFile.create(getRandomContents(), true, getMonitor()); } catch (CoreException e) { fail("2.0", e); } // move the project IProject destinationProject = getWorkspace().getRoot().getProject("MoveTargetProject"); assertDoesNotExistInWorkspace("3.0", destinationProject); try { existingProject.move(destinationProject.getFullPath(), IResource.SHALLOW, getMonitor()); } catch (CoreException e) { fail("4.0", e); } IFile newFile = destinationProject.getFile(file.getProjectRelativePath()); IFolder newFolder = destinationProject.getFolder(folder.getProjectRelativePath()); IFile newChildFile = newFolder.getFile(childFile.getName()); IResource[] newResources = new IResource[] {destinationProject, newFile, newFolder, newChildFile}; assertExistsInWorkspace("5.0", newResources); assertDoesNotExistInWorkspace("6.1", oldResources); assertTrue("7.0", existingProject.isSynchronized(IResource.DEPTH_INFINITE)); assertTrue("8.0", destinationProject.isSynchronized(IResource.DEPTH_INFINITE)); assertTrue("9.0", newFile.getParent().isVirtual()); assertTrue("10.0", newFile.isLinked()); assertTrue("11.0", newFolder.isLinked()); assertTrue("12.0", newFolder.getParent().isVirtual()); } finally { Workspace.clear(fileLocation.toFile()); Workspace.clear(folderLocation.toFile()); } }
public void test01_prepareWorkspace() throws CoreException, URISyntaxException { IWorkspace workspace = ResourcesPlugin.getWorkspace(); IProject project = workspace.getRoot().getProject("project"); ensureExistsInWorkspace(project, true); IFile link = project.getFile("link_to_file"); link.createLink( new URI("bug369177:/dummy_path.txt"), IResource.ALLOW_MISSING_LOCAL, getMonitor()); workspace.save(true, getMonitor()); }
public void testLinkedResourceFiles() throws Exception { IProject[] prjs = new IProject[] {fProject}; fProject.create(new NullProgressMonitor()); fProject.open(new NullProgressMonitor()); createFolder(fProject, "folder1"); File f = createTempFile("extern", ".h"); IPath location = Path.fromOSString(f.getAbsolutePath()); IFile file1 = fProject.getFile("linked1"); IFile file2 = fProject.getFile("linked2.h"); file1.createLink(location, 0, new NullProgressMonitor()); file2.createLink(location, 0, new NullProgressMonitor()); IFile[] files = ResourceLookup.findFilesForLocation(location); assertEquals(2, files.length); files = ResourceLookup.findFilesByName(new Path(location.lastSegment()), prjs, false); assertEquals(2, files.length); files = ResourceLookup.findFilesByName(new Path("linked2.h"), prjs, false); assertEquals(0, files.length); }
/** * Add to the given project a file that is linked to the given file. * * @param project the project to which the linked file should be added * @param file the file that the linked file should be linked with * @param monitor the progress monitor used to provide feedback to the user, or <code>null</code> * if no feedback is desired * @return the file that is linked to the given file * @throws CoreException if the link could not be created for some reason */ public static IFile addLinkToProject(IProject project, File file, IProgressMonitor monitor) throws CoreException { IFile newFile = computeLinkPoint(project, file.getName()); newFile.createLink(new Path(file.getAbsolutePath()), 0, monitor); if (!newFile.exists()) { throw new CoreException( new Status( IStatus.ERROR, DartCore.PLUGIN_ID, IStatus.ERROR, "Failed to create a link to " + file.getAbsolutePath() + " in " + project.getLocation(), null)); } return newFile; }
/** Tests creating a linked file under a virtual folder */ public void testCreateLinkedFileUnderVirtualFolder() { // get a non-existing location IPath location = getRandomLocation(); IFile file = existingVirtualFolderInExistingProject.getFile(getUniqueString()); try { file.createLink(location, IResource.ALLOW_MISSING_LOCAL, getMonitor()); } catch (CoreException e) { fail("1.0", e); } assertTrue("2.0", file.exists()); assertEquals("3.0", location, file.getLocation()); assertTrue("4.0", !location.toFile().exists()); // delete should succeed try { file.delete(IResource.NONE, getMonitor()); } catch (CoreException e) { fail("5.0", e); } }
public void testCopyProjectWithVirtualFolder() { IPath fileLocation = getRandomLocation(); IPath folderLocation = getRandomLocation(); IFile linkedFile = existingVirtualFolderInExistingProject.getFile(getUniqueString()); IFolder linkedFolder = existingVirtualFolderInExistingProject.getFolder(getUniqueString()); try { try { createFileInFileSystem(fileLocation, getRandomContents()); folderLocation.toFile().mkdir(); linkedFolder.createLink(folderLocation, IResource.NONE, getMonitor()); linkedFile.createLink(fileLocation, IResource.NONE, getMonitor()); } catch (CoreException e) { fail("1.0", e); } // copy the project IProject destinationProject = getWorkspace().getRoot().getProject("CopyTargetProject"); try { existingProject.copy(destinationProject.getFullPath(), IResource.SHALLOW, getMonitor()); } catch (CoreException e) { fail("2.0", e); } IFile newFile = destinationProject.getFile(linkedFile.getProjectRelativePath()); assertTrue("3.0", newFile.isLinked()); assertEquals("3.1", linkedFile.getLocation(), newFile.getLocation()); assertTrue("3.2", newFile.getParent().isVirtual()); IFolder newFolder = destinationProject.getFolder(linkedFolder.getProjectRelativePath()); assertTrue("4.0", newFolder.isLinked()); assertEquals("4.1", linkedFolder.getLocation(), newFolder.getLocation()); assertTrue("4.2", newFolder.getParent().isVirtual()); // test project deep copy try { destinationProject.delete(IResource.NONE, getMonitor()); existingProject.copy(destinationProject.getFullPath(), IResource.NONE, getMonitor()); } catch (CoreException e) { fail("5.0", e); } assertTrue("5.1", newFile.isLinked()); assertEquals("5.2", linkedFile.getLocation(), newFile.getLocation()); assertTrue("5.3", newFile.getParent().isVirtual()); assertTrue("5.4", newFolder.isLinked()); assertEquals("5.5", linkedFolder.getLocation(), newFolder.getLocation()); assertTrue("5.6", newFolder.getParent().isVirtual()); try { destinationProject.delete(IResource.NONE, getMonitor()); } catch (CoreException e) { fail("6.0", e); } } finally { Workspace.clear(fileLocation.toFile()); Workspace.clear(folderLocation.toFile()); } }
/** * Updates completions for the BibTeX -data * * @param bibNames Names of the BibTeX -files that the document uses * @param resource The resource of the document */ private void updateBibs(String[] bibNames, boolean biblatexMode, IResource resource) { IProject project = getCurrentProject(); if (project == null) return; if (!biblatexMode) { for (int i = 0; i < bibNames.length; i++) { if (!bibNames[i].endsWith(".bib")) { bibNames[i] += ".bib"; } } } if (bibContainer.checkFreshness(bibNames)) { return; } TexlipseProperties.setSessionProperty(project, TexlipseProperties.BIBFILE_PROPERTY, bibNames); List<String> newBibs = bibContainer.updateBibHash(bibNames); IPath path = resource.getFullPath().removeFirstSegments(1).removeLastSegments(1); if (!path.isEmpty()) path = path.addTrailingSeparator(); KpsewhichRunner filesearch = new KpsewhichRunner(); for (Iterator<String> iter = newBibs.iterator(); iter.hasNext(); ) { String name = iter.next(); try { String filepath = ""; // First try local search IResource res = project.findMember(path + name); // Try searching relative to main file if (res == null) { IContainer sourceDir = TexlipseProperties.getProjectSourceDir(project); res = sourceDir.findMember(name); } if (res != null) { filepath = res.getLocation().toOSString(); } if (res == null) { // Try Kpsewhich filepath = filesearch.getFile(resource, name, "bibtex"); if (filepath.length() > 0 && !(new File(filepath).isAbsolute())) { // filepath is a local path res = project.findMember(path + filepath); if (res != null) { filepath = res.getLocation().toOSString(); } else { filepath = ""; } } else if (filepath.length() > 0) { // Create a link to resource IPath p = new Path(filepath); if (name.indexOf('/') >= 0) { // Remove path from name name = name.substring(name.lastIndexOf('/') + 1); } IFile f = project.getFile(path + name); if (f != null && !f.exists()) { f.createLink(p, IResource.NONE, null); } } } if (filepath.length() > 0) { BibParser parser = new BibParser(filepath); try { List<ReferenceEntry> bibEntriesList = parser.getEntries(); if (bibEntriesList != null && bibEntriesList.size() > 0) { bibContainer.addRefSource(path + name, bibEntriesList); } else if (bibEntriesList == null) { MarkerHandler marker = MarkerHandler.getInstance(); marker.addFatalError( editor, "The BibTeX file " + filepath + " contains fatal errors, parsing aborted."); continue; } } catch (IOException ioe) { TexlipsePlugin.log("Can't read BibTeX file " + filepath, ioe); } } else { MarkerHandler marker = MarkerHandler.getInstance(); marker.addFatalError(editor, "The BibTeX file " + name + " not found."); } } catch (CoreException ce) { TexlipsePlugin.log("Can't run Kpathsea", ce); } } bibContainer.organize(); }