Search in sources :

Example 66 with IWorkspace

use of org.eclipse.core.resources.IWorkspace in project eclipse-pmd by acanda.

the class LocationResolverTest method resolveIfExistsWorkspaceLocationWithMissingFile.

/**
 * Verifies that {@link LocationResolver#resolveIfExists(Location, IProject)} does not throw an exception in a
 * workspace context if the rule set file does not exist.
 */
@Test
public void resolveIfExistsWorkspaceLocationWithMissingFile() throws URISyntaxException {
    final Location location = new Location("project/pmd.xml", LocationContext.WORKSPACE);
    final IProject project = mock(IProject.class);
    final IWorkspace workspace = mock(IWorkspace.class);
    final IWorkspaceRoot workspaceRoot = mock(IWorkspaceRoot.class);
    when(project.getWorkspace()).thenReturn(workspace);
    when(workspace.getRoot()).thenReturn(workspaceRoot);
    when(workspaceRoot.getProject("project")).thenReturn(project);
    when(project.getLocationURI()).thenReturn(new URI("file:///workspace/project"));
    final Optional<String> result = LocationResolver.resolveIfExists(location, project);
    assertFalse("The location should not resolve", result.isPresent());
}
Also used : IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) IWorkspace(org.eclipse.core.resources.IWorkspace) URI(java.net.URI) IProject(org.eclipse.core.resources.IProject) Location(ch.acanda.eclipse.pmd.domain.Location) Test(org.junit.Test)

Example 67 with IWorkspace

use of org.eclipse.core.resources.IWorkspace in project eclipse-pmd by acanda.

the class LocationResolverTest method resolveIfExistsWorkspaceLocationWithProjectNameOnly.

/**
 * Verifies that {@link LocationResolver#resolveIfExists(Location, IProject)} does not throw an exception in a
 * workspace context if the path consists only of the project name.
 */
@Test
public void resolveIfExistsWorkspaceLocationWithProjectNameOnly() throws URISyntaxException {
    final Location location = new Location("project", LocationContext.WORKSPACE);
    final IProject project = mock(IProject.class);
    final IWorkspace workspace = mock(IWorkspace.class);
    final IWorkspaceRoot workspaceRoot = mock(IWorkspaceRoot.class);
    when(project.getWorkspace()).thenReturn(workspace);
    when(workspace.getRoot()).thenReturn(workspaceRoot);
    when(workspaceRoot.getProject("project")).thenReturn(project);
    when(project.getLocationURI()).thenReturn(new URI("file:///workspace/project/"));
    final Optional<String> result = LocationResolver.resolveIfExists(location, project);
    assertFalse("The location should not resolve", result.isPresent());
}
Also used : IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) IWorkspace(org.eclipse.core.resources.IWorkspace) URI(java.net.URI) IProject(org.eclipse.core.resources.IProject) Location(ch.acanda.eclipse.pmd.domain.Location) Test(org.junit.Test)

Example 68 with IWorkspace

use of org.eclipse.core.resources.IWorkspace in project eclipse-pmd by acanda.

the class LocationResolverTest method resolveIfExistsWorkspaceLocation.

/**
 * Verifies that {@link LocationResolver#resolveIfExists(Location, IProject)} resolves the location in a workspace
 * context correctly.
 */
@Test
public void resolveIfExistsWorkspaceLocation() throws URISyntaxException, IOException {
    final Path ruleSetFile = Files.createTempFile(LocationResolverTest.class.getSimpleName(), ".xml");
    try {
        final Location location = new Location("project/" + ruleSetFile.getFileName().toString(), LocationContext.WORKSPACE);
        final IProject project = mock(IProject.class);
        final IWorkspace workspace = mock(IWorkspace.class);
        final IWorkspaceRoot workspaceRoot = mock(IWorkspaceRoot.class);
        when(project.getWorkspace()).thenReturn(workspace);
        when(workspace.getRoot()).thenReturn(workspaceRoot);
        when(workspaceRoot.getProject("project")).thenReturn(project);
        when(project.getLocationURI()).thenReturn(ruleSetFile.getParent().toUri());
        final Optional<String> result = LocationResolver.resolveIfExists(location, project);
        assertTrue("A valid workspace location should resolve", result.isPresent());
        assertEquals("The resolved location in a workspace context should be the provided location appended to the workspace location", ruleSetFile.toString(), result.get());
    } finally {
        Files.deleteIfExists(ruleSetFile);
    }
}
Also used : Path(java.nio.file.Path) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) IWorkspace(org.eclipse.core.resources.IWorkspace) IProject(org.eclipse.core.resources.IProject) Location(ch.acanda.eclipse.pmd.domain.Location) Test(org.junit.Test)

Example 69 with IWorkspace

use of org.eclipse.core.resources.IWorkspace in project yamcs-studio by yamcs.

the class FileUtil method writeTextFile.

/**
 * Write a text file.
 *
 * @param filePath
 *            path of the file. It can be an absolute path or a relative path to the OPI that contains the specified
 *            widget. If it is an absolute path, it can be either<br>
 *            a workspace path such as <code>/BOY Examples/Scripts/myfile.xml</code><br>
 *            a local file system path such as <code>C:\myfile.xml</code><br>
 *            or an URL path such as <code>http://mysite.com/myfile.xml</code>.
 * @param inWorkspace
 *            true if the file path is a workspace file path. Otherwise, it will be recognized as a local file
 *            system file.
 * @param widget
 *            a widget in the OPI, which is used to provide relative path reference. It can be null if the path is
 *            an absolute path.
 * @param text
 *            the text to be written to the file.
 * @param append
 *            true if the text should be appended to the end of the file.
 * @throws Exception
 *             if error happens.
 */
public static void writeTextFile(String filePath, boolean inWorkspace, AbstractBaseEditPart widget, String text, boolean append) throws Exception {
    IPath path = FileUtil.buildAbsolutePath(filePath, widget);
    if (inWorkspace) {
        IWorkspace workspace = ResourcesPlugin.getWorkspace();
        IWorkspaceRoot root = workspace.getRoot();
        String projectName = path.segment(0);
        IProject project = root.getProject(projectName);
        if (!(project.exists())) {
            project.create(new NullProgressMonitor());
        }
        project.open(new NullProgressMonitor());
        IFolder folder = null;
        for (int i = 1; i < path.segmentCount() - 1; i++) {
            if (i == 1)
                folder = project.getFolder(path.segment(i));
            else
                folder = folder.getFolder(path.segment(i));
            if (!(folder.exists())) {
                folder.create(true, true, null);
            }
        }
        IContainer container;
        if (folder == null)
            container = project;
        else
            container = folder;
        IFile file = container.getFile(ResourceUtil.getPathFromString(path.lastSegment()));
        if (file.exists()) {
            StringBuilder sb = new StringBuilder();
            if (append) {
                sb.append(FileUtil.readTextFile(filePath, widget));
            }
            sb.append(text);
            file.setContents(new ByteArrayInputStream(sb.toString().getBytes("UTF-8")), true, false, // $NON-NLS-1$
            null);
        } else {
            File sysFile = file.getLocation().toFile();
            BufferedWriter writer = new BufferedWriter(// $NON-NLS-1$
            new OutputStreamWriter(new FileOutputStream(sysFile, append), "UTF-8"));
            writer.write(text);
            writer.flush();
            writer.close();
        }
    } else {
        BufferedWriter writer = new BufferedWriter(// $NON-NLS-1$
        new OutputStreamWriter(new FileOutputStream(path.toString(), append), "UTF-8"));
        writer.write(text);
        writer.flush();
        writer.close();
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) IProject(org.eclipse.core.resources.IProject) BufferedWriter(java.io.BufferedWriter) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) ByteArrayInputStream(java.io.ByteArrayInputStream) IWorkspace(org.eclipse.core.resources.IWorkspace) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) IContainer(org.eclipse.core.resources.IContainer) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IFolder(org.eclipse.core.resources.IFolder)

Example 70 with IWorkspace

use of org.eclipse.core.resources.IWorkspace in project yamcs-studio by yamcs.

the class ResourceAndContainerGroup method validateContainer.

/**
 * Returns a <code>boolean</code> indicating whether a container name
 * represents a valid container resource in the workbench. An error message
 * is stored for future reference if the name does not represent a valid
 * container.
 *
 * @return <code>boolean</code> indicating validity of the container name
 */
private boolean validateContainer() {
    IPath path = _containerGroup.getFullPath();
    if (path == null) {
        _problemType = PROBLEM_CONTAINER_EMPTY;
        _problemMessage = Messages.ResourceAndContainerGroup_PROBLEM_EMPTY;
        return false;
    }
    IWorkspace workspace = ResourcesPlugin.getWorkspace();
    String projectName = path.segment(0);
    if (projectName == null || !workspace.getRoot().getProject(projectName).exists()) {
        _problemType = PROBLEM_PROJECT_DOES_NOT_EXIST;
        _problemMessage = Messages.ResourceAndContainerGroup_PROBLEM_DOES_NOT_EXIST;
        return false;
    }
    // path is invalid if any prefix is occupied by a file
    IWorkspaceRoot root = workspace.getRoot();
    while (path.segmentCount() > 1) {
        if (root.getFile(path).exists()) {
            _problemType = PROBLEM_PATH_OCCUPIED;
            _problemMessage = NLS.bind(Messages.ResourceAndContainerGroup_PROBLEM_FILE_ALREADY_EXISTS_AT_LOCATION, path.makeRelative());
            return false;
        }
        path = path.removeLastSegments(1);
    }
    return true;
}
Also used : IPath(org.eclipse.core.runtime.IPath) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) IWorkspace(org.eclipse.core.resources.IWorkspace)

Aggregations

IWorkspace (org.eclipse.core.resources.IWorkspace)118 IProject (org.eclipse.core.resources.IProject)55 CoreException (org.eclipse.core.runtime.CoreException)54 IWorkspaceRoot (org.eclipse.core.resources.IWorkspaceRoot)37 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)36 IPath (org.eclipse.core.runtime.IPath)35 IWorkspaceRunnable (org.eclipse.core.resources.IWorkspaceRunnable)29 IStatus (org.eclipse.core.runtime.IStatus)27 IFile (org.eclipse.core.resources.IFile)24 PersistenceException (org.talend.commons.exception.PersistenceException)23 IProjectDescription (org.eclipse.core.resources.IProjectDescription)21 File (java.io.File)19 InvocationTargetException (java.lang.reflect.InvocationTargetException)19 Path (org.eclipse.core.runtime.Path)19 ISchedulingRule (org.eclipse.core.runtime.jobs.ISchedulingRule)19 IResource (org.eclipse.core.resources.IResource)18 Status (org.eclipse.core.runtime.Status)14 IOException (java.io.IOException)13 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)13 ArrayList (java.util.ArrayList)12