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

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

Introduction

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

Prototype

IPath getPath();

Source Link

Document

Returns the path of this classpath entry.

Usage

From source file:net.rim.ejde.internal.util.LegacyModelUtil.java

License:Open Source License

static public void setSource(Project proj, IJavaProject eclipseJavaProject, String source) {
    if (null == proj)// Don't process for a non existing legacy project
        return;//from   w ww. ja v a2s  . co m

    if (StringUtils.isBlank(source))// Don't process for a non existing
        // source folder
        return;

    if (null == eclipseJavaProject)// Don't process for a non existing
        // Eclipse equivalent
        return;

    try {
        IClasspathEntry[] classpathEntries = eclipseJavaProject.getRawClasspath();

        IWorkspace workspace = ResourcesPlugin.getWorkspace();
        IWorkspaceRoot workspaceRoot = workspace.getRoot();

        IPath classpathEntryPath;
        String classpathEntryLastSegment;
        IFolder folder;

        for (IClasspathEntry classpathEntry : classpathEntries) {
            if (IClasspathEntry.CPE_SOURCE == classpathEntry.getEntryKind()) {
                classpathEntryPath = classpathEntry.getPath();
                classpathEntryLastSegment = classpathEntryPath.lastSegment();

                if (source.equalsIgnoreCase(classpathEntryLastSegment)) {// if
                    // the
                    // string
                    // can't
                    // be
                    // matched
                    // to
                    // an
                    // existing
                    // classpath
                    // entry
                    // why
                    // should
                    // we
                    // add
                    // it
                    // to
                    // the
                    // legacy
                    // metadata?!
                    if (ImportUtils.getImportPref(ResourceBuilder.LOCALE_INTERFACES_FOLDER_NAME)
                            .equalsIgnoreCase(classpathEntryLastSegment)) {
                        return;
                    }
                    if (!classpathEntryPath.toOSString().equals(IConstants.EMPTY_STRING)) {

                        folder = workspaceRoot.getFolder(classpathEntryPath);

                        if (folder.isDerived())// Don't process for
                            // Eclipse
                            // derived directories
                            return;
                    }

                }
            }
        }
    } catch (JavaModelException e) {
        log.error(e.getMessage(), e);
    }

    String udata = proj.getUserData();

    if (StringUtils.isNotBlank(udata)) {
        int idx1 = udata.indexOf(DELIM_SECTION);
        if (idx1 >= 0) {
            int idx2 = udata.indexOf(DELIM_SECTION, idx1 + 1);
            String udata_new = (idx1 > 0 ? udata.substring(0, idx1) : EMPTY_STRING) + DELIM_SECTION + source
                    + (idx2 > idx1 ? udata.substring(idx2) : EMPTY_STRING);
            if (!udata.equals(udata_new)) {
                proj.setUserData(udata_new);
            }
        }
    } else {
        proj.setUserData(DELIM_SECTION + source);
    }
}

From source file:net.rim.ejde.internal.util.LegacyModelUtil.java

License:Open Source License

static public void syncSources(Project proj, IJavaProject eclipseJavaProject) {
    if (null == proj)// Don't process for a non existing legacy project
        return;/*  w  ww. ja v a  2  s  . c om*/

    if (null == eclipseJavaProject)// Don't process for a non existing
        // Eclipse equivalent
        return;

    String sources = "";
    StringBuffer buf = new StringBuffer();

    try {
        IClasspathEntry[] classpathEntries = eclipseJavaProject.getRawClasspath();

        IWorkspace workspace = ResourcesPlugin.getWorkspace();
        IWorkspaceRoot workspaceRoot = workspace.getRoot();

        IPath classpathEntryPath;
        String classpathEntryLastSegment;
        IFolder folder;

        for (IClasspathEntry classpathEntry : classpathEntries) {
            if (IClasspathEntry.CPE_SOURCE == classpathEntry.getEntryKind()) {
                classpathEntryPath = classpathEntry.getPath();
                classpathEntryLastSegment = classpathEntryPath.lastSegment();

                if (ImportUtils.getImportPref(ResourceBuilder.LOCALE_INTERFACES_FOLDER_NAME)
                        .equalsIgnoreCase(classpathEntryLastSegment)) {
                    continue;
                }

                if (classpathEntryPath.toOSString().equals(IConstants.EMPTY_STRING)) {
                    continue;
                }

                folder = workspaceRoot.getFolder(classpathEntryPath);

                if (folder.isDerived()) {// Don't process for Eclipse
                    // derived directories
                    continue;
                }

                buf.append(DELIM_SECTION + classpathEntryLastSegment);
            }
        }
    } catch (JavaModelException e) {
        log.error(e.getMessage(), e);
    }

    sources = buf.toString();

    String udata = proj.getUserData();

    if (StringUtils.isNotBlank(udata)) {

        int idx1 = udata.indexOf(DELIM_SECTION);

        if (idx1 >= 0) {
            int idx2 = udata.indexOf(DELIM_SECTION, idx1 + 1);
            String udata_new = (idx1 > 0 ? udata.substring(0, idx1) : EMPTY_STRING) + DELIM_SECTION + sources
                    + (idx2 > idx1 ? udata.substring(idx2) : EMPTY_STRING);
            if (!udata.equals(udata_new)) {
                proj.setUserData(udata_new);
            }
        }
    } else {
        proj.setUserData(sources);
    }
}

From source file:net.rim.ejde.internal.util.PackageUtils.java

License:Open Source License

/**
 * Return the absolute path of the given entry.
 *
 * @param currentEntry/*from   w  ww  .  ja  v a2 s  . c  om*/
 * @return the absolute path of the give entry. Return <code>null<code> if the entry can not be found.
 */
public static IPath getAbsolutePath(IClasspathEntry currentEntry) {
    if (currentEntry == null) {
        return null;
    }
    if (currentEntry.getEntryKind() != IClasspathEntry.CPE_LIBRARY) {
        return currentEntry.getPath();
    }
    IPath absolutePath = null;
    IPath path = currentEntry.getPath();
    Object target = JavaModel.getTarget(path, true);
    if (target instanceof IResource) {
        // if the target was found inside workspace
        IResource resource = (IResource) target;
        absolutePath = resource.getLocation();
    } else if (target instanceof File) {
        // if the target was found outside of the workspace
        absolutePath = new Path(((File) target).getAbsolutePath());
    }
    return absolutePath;
}

From source file:net.rim.ejde.internal.util.PackageUtils.java

License:Open Source License

/**
 * Gets all the source folders of the given <code>iJavaProject</code>.
 *
 * @param iJavaProject//w  w  w  .  ja v a2 s.c  o  m
 * @return
 */
public static ArrayList<IContainer> getAllSrcFolders(IJavaProject iJavaProject) {
    ArrayList<IContainer> result = new ArrayList<IContainer>();
    IProject iProject = iJavaProject.getProject();
    IClasspathEntry[] classPathEntries = null;
    IFolder srcFolder = null;
    IPath srcFolderPath = null;
    IPath relativeSrcFolderPath = null;
    try {
        classPathEntries = iJavaProject.getRawClasspath();
        for (IClasspathEntry classPathEntry : classPathEntries) {
            if (classPathEntry.getEntryKind() != IClasspathEntry.CPE_SOURCE) {
                continue;
            }
            srcFolderPath = classPathEntry.getPath();
            if (srcFolderPath.segment(0).equals(iProject.getName())) {
                // remove the project name from the path
                relativeSrcFolderPath = srcFolderPath.removeFirstSegments(1);
            }
            if (relativeSrcFolderPath == null || relativeSrcFolderPath.isEmpty()) {
                result.add(iProject);
            } else {
                srcFolder = iProject.getFolder(relativeSrcFolderPath);
                if (srcFolder.exists()) {
                    result.add(srcFolder);
                }
            }
        }
    } catch (Throwable e) {
        _logger.error(e.getMessage(), e);
    }
    return result;
}

From source file:net.rim.ejde.internal.util.ProjectUtils.java

License:Open Source License

/**
 * Gets a file that exists in an Eclipse project.
 * <p>/*from   w  ww.j  a v  a  2s . c  o  m*/
 * TODO: Someone can probably optimize this method better. Like using some of the IWorkspaceRoot.find*() methods...
 *
 * @param project
 *            the Eclipse project the file belongs to
 * @param file
 *            the File which is in the Eclipse project
 * @return the Eclipse resource file associated with the file
 */
public static IResource getResource(IProject project, File file) {
    IJavaProject javaProject = JavaCore.create(project);
    IPath filePath = new Path(file.getAbsolutePath());
    try {
        IClasspathEntry[] classpathEntries = javaProject.getResolvedClasspath(true);

        IFile input = null;
        // Look for a source folder
        for (IClasspathEntry classpathEntry : classpathEntries) {
            if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {

                // Try to resolve the source container
                IWorkspaceRoot workspaceRoot = project.getWorkspace().getRoot();
                IResource resource = workspaceRoot.findMember(classpathEntry.getPath());
                if (resource instanceof IContainer) {
                    IContainer sourceContainer = (IContainer) resource;
                    File sourceContainerFile = resource.getLocation().toFile();
                    IPath sourceFolderPath = new Path(sourceContainerFile.getAbsolutePath());

                    // See if the file path is within this source folder
                    // path
                    if (sourceFolderPath.isPrefixOf(filePath)) {
                        int segmentCount = sourceFolderPath.segmentCount();
                        IPath relativePath = filePath.removeFirstSegments(segmentCount);
                        input = sourceContainer.getFile(relativePath);
                        break;
                    }
                }
            }
        }
        return input;
    } catch (JavaModelException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:net.sf.eclipse.tomcat.TomcatBootstrap.java

License:Open Source License

private void getClassPathEntries(IClasspathEntry[] entries, IJavaProject prj, ArrayList data,
        List selectedPaths, ArrayList visitedProjects, IPath outputPath) {
    for (IClasspathEntry entrie : entries) {
        IClasspathEntry entry = entrie;
        IPath path = entry.getPath();
        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            path = entry.getOutputLocation();
            if (path == null) {
                continue;
            }/*from  w  w w.j  av  a 2  s. c  om*/
        }
        if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
            String prjName = entry.getPath().lastSegment();
            if (!visitedProjects.contains(prjName)) {
                visitedProjects.add(prjName);
                getClassPathEntries(prj.getJavaModel().getJavaProject(prjName), data, selectedPaths,
                        visitedProjects);
            }
            continue;
        } else if (!selectedPaths.contains(path.toFile().toString().replace('\\', '/'))) {
            if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER
                    && !entry.getPath().toString().equals("org.eclipse.jdt.launching.JRE_CONTAINER")) {

                // entires in container are only processed individually
                // if container itself is not selected

                IClasspathContainer container;
                try {
                    container = JavaCore.getClasspathContainer(path, prj);
                } catch (JavaModelException e1) {
                    TomcatLauncherPlugin.log(e1);
                    container = null;
                }

                if (container != null) {
                    getClassPathEntries(container.getClasspathEntries(), prj, data, selectedPaths,
                            visitedProjects, outputPath);
                }
            }
            continue;
        }

        IClasspathEntry[] tmpEntry = null;
        if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
            try {
                tmpEntry = JavaCore.getClasspathContainer(path, prj).getClasspathEntries();
            } catch (JavaModelException e1) {
                TomcatLauncherPlugin.log(e1);
                continue;
            }
        } else {
            tmpEntry = new IClasspathEntry[1];
            tmpEntry[0] = JavaCore.getResolvedClasspathEntry(entry);
        }

        for (IClasspathEntry element : tmpEntry) {
            if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                IResource res = prj.getProject().getWorkspace().getRoot().findMember(element.getPath());
                if (res != null) {
                    add(data, res);
                } else {
                    add(data, element.getPath());
                }
            } else if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                IPath srcPath = entry.getOutputLocation();
                if (srcPath != null && !srcPath.equals(outputPath)) {
                    add(data, prj.getProject().getWorkspace().getRoot().findMember(srcPath));
                }
            } else {
                TomcatLauncherPlugin.log(">>> " + element);
                if (element.getPath() != null) {
                    add(data, element.getPath());
                }
            }
        }
    }
}

From source file:net.sf.eclipse.tomcat.TomcatBootstrap.java

License:Open Source License

private void collectMavenDependencies(IClasspathEntry[] entries, IJavaProject prj, List data,
        List visitedProjects) {/*from w w w.  j a va  2s .  com*/
    for (int i = 0; i < entries.length; i++) {
        IClasspathEntry entry = entries[i];
        IPath path = entry.getPath();
        if ((entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER)
                && entry.getPath().toString().endsWith("MAVEN2_CLASSPATH_CONTAINER")) {
            IClasspathEntry[] tmpEntry = null;
            try {
                tmpEntry = JavaCore.getClasspathContainer(path, prj).getClasspathEntries();
            } catch (JavaModelException e1) {
                TomcatLauncherPlugin.log(e1);
                continue;
            }
            for (int j = 0; j < tmpEntry.length; j++) {
                if (tmpEntry[j].getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                    if (tmpEntry[j].getPath().lastSegment().matches(".*servlet-api[^\\/]{0,10}\\.jar$")) {
                        continue;
                    }
                    if (tmpEntry[j].getPath().lastSegment().matches(".*jasper[^\\/]{0,10}\\.jar$")) {
                        continue;
                    }
                    if (tmpEntry[j].getPath().lastSegment().matches(".*annotations-api[^\\/]{0,10}.\\.jar$")) {
                        continue;
                    }
                    if (tmpEntry[j].getPath().lastSegment().matches(".*el-api[^\\/]{0,10}\\.jar$")) {
                        continue;
                    }
                    if (tmpEntry[j].getPath().lastSegment().matches(".*jsp-api[^\\/]{0,10}\\.jar$")) {
                        continue;
                    }
                    IResource res = prj.getProject().getWorkspace().getRoot().findMember(tmpEntry[j].getPath());
                    if (res != null) {
                        add(data, res);
                    } else {
                        add(data, tmpEntry[j].getPath());
                    }
                } else if (tmpEntry[j].getEntryKind() == IClasspathEntry.CPE_PROJECT) {
                    String prjName = tmpEntry[j].getPath().lastSegment();
                    IJavaProject subPrj = prj.getJavaModel().getJavaProject(prjName);

                    try {
                        add(data, prj.getProject().getWorkspace().getRoot()
                                .findMember(subPrj.getOutputLocation()));
                    } catch (JavaModelException e1) {
                        TomcatLauncherPlugin.log(e1);
                        continue;
                    }
                    if (!visitedProjects.contains(prjName)) {
                        visitedProjects.add(prjName);
                        collectMavenDependencies(subPrj, data, visitedProjects);
                    }
                    continue;
                } else {
                    TomcatLauncherPlugin.log(">>> " + tmpEntry[j]);
                    if (tmpEntry[j].getPath() != null) {
                        add(data, tmpEntry[j].getPath());
                    }
                }
            }
        }
    }
}

From source file:net.sf.eclipse.tomcat.TomcatLauncherPlugin.java

License:Open Source License

/**
 * Remove TOMCAT_HOME variable from Tomcat projects build path
 * (Eclipse 3 will not compile Tomcat projects without this fix)
 *//*w  w w .  java2s  .  c  om*/
private void fixTomcatHomeBug() {
    if (this.getPreferenceStore().getString("fixTomcatHomeBug").equals("")) {
        this.getPreferenceStore().setValue("fixTomcatHomeBug", "fixed");
        IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
        IProject[] projects = root.getProjects();

        try {
            for (IProject project : projects) {
                if (project.hasNature(NATURE_ID)) {
                    List cp = new ArrayList(projects.length - 1);
                    IJavaProject javaProject = JavaCore.create(project);
                    IClasspathEntry[] classpath = javaProject.getRawClasspath();
                    cp.addAll(Arrays.asList(classpath));
                    for (IClasspathEntry element : classpath) {
                        if (element.getEntryKind() == IClasspathEntry.CPE_VARIABLE) {
                            if (element.getPath().equals(TomcatLauncherPlugin.getDefault().getTomcatIPath())) {
                                cp.remove(element);
                            }
                        }
                    }
                    javaProject.setRawClasspath((IClasspathEntry[]) cp.toArray(new IClasspathEntry[cp.size()]),
                            null);
                }
            }
        } catch (Exception e) {
            log(e);
        }
    }
}

From source file:net.sf.eclipse.tomcat.TomcatProject.java

License:Open Source License

/**
 * Add servlet.jar and jasper.jar to project classpath
 */// w ww  .  j  av a  2 s.  c o m
public void addTomcatJarToProjectClasspath() throws CoreException {
    TomcatBootstrap tb = TomcatLauncherPlugin.getDefault().getTomcatBootstrap();

    IClasspathEntry[] entries = javaProject.getRawClasspath();
    List cp = new ArrayList(entries.length + 1);

    for (IClasspathEntry entrie : entries) {
        IClasspathEntry entry = entrie;
        if (!((entry.getEntryKind() == IClasspathEntry.CPE_VARIABLE) && (entry.getPath().toOSString()
                .startsWith(TomcatLauncherPlugin.getDefault().getTomcatIPath().toOSString())))) {
            cp.add(entry);
        }
    }

    cp.addAll(tb.getTomcatJars());

    javaProject.setRawClasspath((IClasspathEntry[]) cp.toArray(new IClasspathEntry[cp.size()]), null);
}

From source file:net.sf.eclipse.tomcat.TomcatProjectWebclasspathPropertyPage.java

License:Open Source License

private void getClassPathEntries(IClasspathEntry[] entries, IJavaProject prj, ArrayList data,
        IPath outputPath) {//from   ww w.j  av a  2  s .  c om
    for (int i = 0; i < entries.length; i++) {
        IClasspathEntry entry = entries[i];
        if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
            String prjName = entry.getPath().lastSegment();
            if (!visitedProjects.contains(prjName)) {
                visitedProjects.add(prjName);
                getClassPathEntries(getJavaProject().getJavaModel().getJavaProject(prjName), data);
            }
        } else if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
            add(data, entry.getPath());
        } else if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            IPath path = entry.getOutputLocation();
            if (path != null && !path.equals(outputPath)) {
                add(data, path);
            }
        } else if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
            if (!entry.getPath().toString().equals("org.eclipse.jdt.launching.JRE_CONTAINER")) {
                // Add container itself, as TomcatBootstrap can actually process them
                // at the moment
                // Basically, users will be able to choose b/w the whole container
                // or some artifacts enclosed by it
                add(data, entry.getPath());

                // Expand container and add its content as individual
                // elements
                IClasspathContainer container;
                try {
                    container = JavaCore.getClasspathContainer(entry.getPath(), prj);
                } catch (JavaModelException e) {
                    TomcatLauncherPlugin.log("failed to obtain classpath container '" + entry.getPath() + "'"
                            + " for project '" + prj.getProject().getName() + "'");
                    TomcatLauncherPlugin.log(e);
                    container = null;
                }

                if (container != null) {
                    getClassPathEntries(container.getClasspathEntries(), prj, data, outputPath);
                }
            }
        } else {
            add(data, entry.getPath());
        }
    }
}