Example usage for org.eclipse.jdt.core IJavaProject getPath

List of usage examples for org.eclipse.jdt.core IJavaProject getPath

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaProject getPath.

Prototype

IPath getPath();

Source Link

Document

Returns the path to the innermost resource enclosing this element.

Usage

From source file:org.grails.ide.eclipse.core.internal.classpath.GrailsClasspathContainerInitializer.java

License:Open Source License

/**
 * {@inheritDoc}//from w  w  w  . jav  a2 s. com
 */
public Object getComparisonID(IPath containerPath, IJavaProject project) {
    if (containerPath == null || project == null)
        return null;

    return containerPath.segment(0) + "/" + project.getPath().segment(0); //$NON-NLS-1$
}

From source file:org.gw4e.eclipse.facade.ClasspathManager.java

License:Open Source License

/**
 * Manage source folders exclusion/*from w w w  . j a va  2s.co m*/
 * 
 * @param project
 * @param rootSrcEntry
 * @param relative
 * @return
 * @throws JavaModelException
 */
private static IClasspathEntry ensureExcludedPath(IProject project, IClasspathEntry rootSrcEntry,
        String relative) throws JavaModelException {
    if (rootSrcEntry == null)
        return rootSrcEntry;
    IJavaProject javaProject = JavaCore.create(project);
    IClasspathEntry[] entries = javaProject.getRawClasspath();
    IPath[] excluded = rootSrcEntry.getExclusionPatterns();
    boolean entryFound = false;
    for (int i = 0; i < excluded.length; i++) {
        if (excluded[i].toString().equalsIgnoreCase(relative)) {
            entryFound = true;
            break;
        }
    }
    if (!entryFound) {
        IPath rootSrcPath = javaProject.getPath().append("src");
        IPath[] newEntries = new IPath[excluded.length + 1];
        System.arraycopy(excluded, 0, newEntries, 0, excluded.length);
        newEntries[excluded.length] = new Path(relative);
        rootSrcEntry = JavaCore.newSourceEntry(rootSrcPath, newEntries);
        entries = javaProject.getRawClasspath();
        List<IClasspathEntry> temp = new ArrayList<IClasspathEntry>();
        temp.add(rootSrcEntry);
        for (int i = 0; i < entries.length; i++) {
            if (!(entries[i].getPath().equals(rootSrcPath))) {
                temp.add(entries[i]);
            }
        }
        IClasspathEntry[] array = new IClasspathEntry[temp.size()];
        temp.toArray(array);
        javaProject.setRawClasspath(array, null);
    }
    return rootSrcEntry;
}

From source file:org.gw4e.eclipse.facade.ClasspathManager.java

License:Open Source License

/**
 * Make sure the passed folder is in the classpath
 * /*from w ww . j a  v  a2 s  .c  o  m*/
 * @param project
 * @param folderPath
 * @param monitor
 * @return
 * @throws JavaModelException
 */
public static IClasspathEntry ensureFolderInClasspath(IProject project, String folderPath,
        IProgressMonitor monitor) throws JavaModelException {
    IJavaProject javaProject = JavaCore.create(project);

    if (folderPath.startsWith("src")) {
        handleFolderExclusion(project, folderPath);
    }

    IClasspathEntry[] entries = javaProject.getRawClasspath();

    boolean classpathentryFound = false;
    IPath folder = project.getFolder(folderPath).getFullPath();
    for (int i = 0; i < entries.length; i++) {

        if (entries[i].getPath().equals(folder)) {
            classpathentryFound = true;
            return entries[i];
        }
    }
    if (!classpathentryFound) {
        IClasspathEntry[] newEntries = new IClasspathEntry[entries.length + 1];
        System.arraycopy(entries, 0, newEntries, 0, entries.length);
        IPath srcPath = javaProject.getPath().append(folderPath);
        IClasspathEntry srcEntry = JavaCore.newSourceEntry(srcPath, null);
        newEntries[entries.length] = JavaCore.newSourceEntry(srcEntry.getPath());
        javaProject.setRawClasspath(newEntries, monitor);
        return srcEntry;
    }
    return null;
}

From source file:org.gw4e.eclipse.facade.ClasspathManager.java

License:Open Source License

/**
 * Manage source folders exclusion/* ww w. jav  a  2  s .c o  m*/
 * 
 * @param project
 * @param folderPath
 * @throws JavaModelException
 */
private static void handleFolderExclusion(IProject project, String folderPath) throws JavaModelException {
    IJavaProject javaProject = JavaCore.create(project);
    IClasspathEntry[] entries = javaProject.getRawClasspath();
    IClasspathEntry rootSrcEntry = null;
    IPath srcPath = javaProject.getPath().append("src");
    for (int i = 0; i < entries.length; i++) {
        if ((entries[i].getPath().equals(srcPath))) {
            rootSrcEntry = entries[i];
            break;
        }
    }

    // 'src' folder by itslef is not in the build path ...
    if (rootSrcEntry == null)
        return;

    String relative = folderPath.substring("src/".length()).concat("/");

    StringTokenizer st = new StringTokenizer(relative, "/");
    StringBuffer sb = new StringBuffer();
    while (st.hasMoreTokens()) {
        String temp = st.nextToken();
        sb.append(temp).append("/");
        rootSrcEntry = ClasspathManager.ensureExcludedPath(project, rootSrcEntry, sb.toString());
    }

}

From source file:org.gw4e.eclipse.facade.ResourceManager.java

License:Open Source License

/**
 * Return whether the resource is in of the passed folders
 * /*w  w  w  . jav  a2  s .  co  m*/
 * @param resource
 * @param folders
 * @return
 */
public static boolean isFileInFolders(IFile resource, String[] folders) {
    if (resource == null)
        return false;
    IProject p = resource.getProject();
    IJavaProject javaProject = JavaCore.create(p);
    for (int i = 0; i < folders.length; i++) {
        IPath folderPath = javaProject.getPath().append(folders[i]);
        String allowedPAth = folderPath.toString();
        String resourcePath = resource.getFullPath().toString();
        if (resourcePath.startsWith(allowedPAth)) {
            return true;
        }
    }
    return false;
}

From source file:org.gw4e.eclipse.test.facade.ResourceManagerTest.java

License:Open Source License

@Test
public void testToResource() throws Exception {
    IJavaProject project = ProjectHelper.getOrCreateSimpleGW4EProject(PROJECT_NAME, true, true);
    IResource r = ResourceManager.toResource(project.getPath().append("/src/test/java/SimpleImpl.java"));
    assertNotNull(r);//from   w w  w  . j ava  2  s  . com
    IFile f = (IFile) r;
    assertTrue(f.exists());
}

From source file:org.gw4e.eclipse.test.facade.ResourceManagerTest.java

License:Open Source License

@Test
public void testUpdateBuildPolicyFileFor() throws Exception {
    IJavaProject project = ProjectHelper.createSimpleCompleteProject(PROJECT_NAME);
    IFile file = (IFile) ResourceManager.toResource(project.getPath().append("/src/test/java/SimpleImpl.java"));
    IFile policies = (IFile) ResourceManager.getResource(
            project.getProject().getFullPath().append("src/test/resources/build.policies").toString());

    Properties p = ResourceManager.getProperties(ResourceManager.toFile(policies.getFullPath()));
    String generators = (String) p.get("Simple.json");
    assertEquals("sync", generators);
    ResourceManager.updateBuildPolicyFileFor(file);

    Waiter.waitUntil(new ICondition() {
        @Override/*from w  ww.ja va2s . c  o  m*/
        public boolean checkCondition() throws Exception {
            Properties p = ResourceManager.getProperties(ResourceManager.toFile(policies.getFullPath()));
            String generators = (String) p.get("Simple.json");
            return "RandomPath(EdgeCoverage(100));I;random(edge_coverage(100));I;".equals(generators);
        }

        @Override
        public String getFailureMessage() {
            return "Build policies not updated";
        }
    });
}

From source file:org.gw4e.eclipse.test.fwk.ProjectHelper.java

License:Open Source License

public static boolean isFolderInClassPath(IJavaProject jproject, String f) throws CoreException {
    IClasspathEntry[] entries = jproject.getRawClasspath();
    IPath folder = jproject.getPath().append(f);
    for (int i = 0; i < entries.length; i++) {
        if (entries[i].getPath().equals(folder)) {
            return true;
        }/*from w  w  w .ja  v a 2 s .c  o m*/
    }
    return false;
}

From source file:org.hibernate.eclipse.console.utils.OpenMappingUtils.java

License:Open Source License

/**
 * Trying to find hibernate console config ejb3 mapping file,
 * which is corresponding to provided element.
 *   /*www . j ava2  s.c  o  m*/
 * @param consoleConfig
 * @param element
 * @return
 */
@SuppressWarnings("unchecked")
public static IFile searchInEjb3MappingFiles(ConsoleConfiguration consoleConfig, Object element) {
    IFile file = null;
    if (consoleConfig == null) {
        return file;
    }
    final ConsoleConfiguration cc2 = consoleConfig;
    List<String> documentPaths = (List<String>) consoleConfig.execute(new ExecutionContext.Command() {
        public Object execute() {
            String persistenceUnitName = cc2.getPreferences().getPersistenceUnitName();
            EntityResolver entityResolver = cc2.getConfiguration().getEntityResolver();
            IService service = cc2.getHibernateExtension().getHibernateService();
            return service.getJPAMappingFilePaths(persistenceUnitName, entityResolver);
        }
    });
    if (documentPaths == null) {
        return file;
    }
    IJavaProject[] projs = ProjectUtils.findJavaProjects(consoleConfig);
    ArrayList<IPath> pathsSrc = new ArrayList<IPath>();
    ArrayList<IPath> pathsOut = new ArrayList<IPath>();
    ArrayList<IPath> pathsFull = new ArrayList<IPath>();
    for (int i = 0; i < projs.length; i++) {
        IJavaProject proj = projs[i];
        IPath projPathFull = proj.getResource().getLocation();
        IPath projPath = proj.getPath();
        IPath projPathOut = null;
        try {
            projPathOut = proj.getOutputLocation();
            projPathOut = projPathOut.makeRelativeTo(projPath);
        } catch (JavaModelException e) {
            // just ignore
        }
        IPackageFragmentRoot[] pfrs = new IPackageFragmentRoot[0];
        try {
            pfrs = proj.getAllPackageFragmentRoots();
        } catch (JavaModelException e) {
            // just ignore
        }
        for (int j = 0; j < pfrs.length; j++) {
            // TODO: think about possibility to open resources from jar files
            if (pfrs[j].isArchive() || pfrs[j].isExternal()) {
                continue;
            }
            final IPath pathSrc = pfrs[j].getPath();
            final IPath pathOut = projPathOut;
            final IPath pathFull = projPathFull;
            pathsSrc.add(pathSrc);
            pathsOut.add(pathOut);
            pathsFull.add(pathFull);
        }
    }
    int scanSize = Math.min(pathsSrc.size(), pathsOut.size());
    scanSize = Math.min(pathsFull.size(), scanSize);
    for (int i = 0; i < scanSize && file == null; i++) {
        final IPath pathSrc = pathsSrc.get(i);
        final IPath pathOut = pathsOut.get(i);
        final IPath pathFull = pathsFull.get(i);
        Iterator<String> it = documentPaths.iterator();
        while (it.hasNext() && file == null) {
            String docPath = it.next();
            IPath path2DocFull = Path.fromOSString(docPath);
            IPath resPath = path2DocFull.makeRelativeTo(pathFull);
            if (pathOut != null) {
                resPath = resPath.makeRelativeTo(pathOut);
            }
            resPath = pathSrc.append(resPath);
            file = ResourcesPlugin.getWorkspace().getRoot().getFile(resPath);
            if (file == null || !file.exists()) {
                file = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(resPath);
            }
            if (file != null && file.exists()) {
                if (elementInFile(consoleConfig, file, element)) {
                    break;
                }
            }
            file = null;
        }
    }
    return file;
}

From source file:org.hibernate.eclipse.console.utils.ProjectUtils.java

License:Open Source License

private static String[] projectPersistenceUnits(IJavaProject javaProject) {
    if (javaProject == null || javaProject.getResource() == null) {
        return new String[0];
    }/*  w w  w. ja  va2  s.  c  om*/
    IPath projPathFull = javaProject.getResource().getLocation();
    IPath projPath = javaProject.getPath();
    IPath path = javaProject.readOutputLocation().append("META-INF/persistence.xml"); //$NON-NLS-1$
    path = path.makeRelativeTo(projPath);
    path = projPathFull.append(path);
    IFile file = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
    if (!exists(file)) {
        return new String[0];
    }
    InputStream is = null;
    org.dom4j.Document doc = null;
    try {
        is = file.getContents();
        SAXReader saxReader = new SAXReader();
        doc = saxReader.read(new InputSource(is));
    } catch (CoreException e) {
        HibernateConsolePlugin.getDefault().logErrorMessage("CoreException: ", e); //$NON-NLS-1$
    } catch (DocumentException e) {
        HibernateConsolePlugin.getDefault().logErrorMessage("DocumentException: ", e); //$NON-NLS-1$
    } finally {
        try {
            if (is != null)
                is.close();
        } catch (IOException ioe) {
            //ignore
        }
    }
    if (doc == null || doc.getRootElement() == null) {
        return new String[0];
    }
    Iterator<?> it = doc.getRootElement().elements("persistence-unit").iterator(); //$NON-NLS-1$
    ArrayList<String> res = new ArrayList<String>();
    while (it.hasNext()) {
        Element el = (Element) it.next();
        res.add(el.attributeValue("name")); //$NON-NLS-1$
    }
    return res.toArray(new String[0]);
}