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:com.jstar.eclipse.objects.JavaFile.java

License:BSD License

private String getAbsolutePath(IClasspathEntry entry) {
    IPath entryPath = entry.getPath();
    IPackageFragmentRoot lib = null;/*from  ww  w  .  j  a v  a2  s .  c o  m*/

    try {
        lib = getJavaProject().getProject().findPackageFragmentRoot(entryPath);
    } catch (JavaModelException jme) {
    }

    if (lib == null) {
        return entryPath.toOSString();
    }

    if (lib.getResource() == null) {
        return entryPath.toOSString();
    } else {
        return lib.getResource().getLocation().toOSString();
    }

}

From source file:com.laex.j2objc.preferences.ClasspathPropertyPage.java

License:Open Source License

/**
 * Load project referenced classpaths.//from w ww .  ja  va2  s .c o m
 */
private void loadProjectReferencedClasspaths() {
    try {
        IClasspathEntry[] refClasspath = ProjectUtil.getJavaProject(getElement()).getResolvedClasspath(true);
        for (IClasspathEntry o : refClasspath) {

            IClasspathEntry entry = JavaCore.getResolvedClasspathEntry((IClasspathEntry) o);
            String path = null;

            // We need to figure out the path for different types of
            // classpath entries.
            // the types of entries are: CPE_LIBRARY, CPE_PROJECT,
            // CPE_SOURCE, CPE_VARIABLE, CPE_CONTAINER
            switch (entry.getEntryKind()) {
            case IClasspathEntry.CPE_LIBRARY:

                // With CPE Library: some jar files could reside in system,
                // like JDK jar files
                // some jar files may reside in workspace, within other
                // project.
                // Check if the library resides in workspace project
                IResource file = ResourcesPlugin.getWorkspace().getRoot().findMember(entry.getPath());
                // null means, this library does not reside in workspace but
                // instead in the system
                if (file == null) {
                    // get the apropriate path
                    path = entry.getPath().makeAbsolute().toOSString();
                } else {
                    // if resdies in workspace, get the appropriate path
                    if (file.exists()) {
                        path = file.getLocation().makeAbsolute().toOSString();
                    }
                }

                break;

            case IClasspathEntry.CPE_PROJECT:
            case IClasspathEntry.CPE_SOURCE:
                // project and source
                IResource res = ResourcesPlugin.getWorkspace().getRoot().findMember(entry.getPath());
                if (res.exists()) {
                    path = res.getFullPath().makeAbsolute().toOSString();
                }
                break;
            }

            // some path may be folders. In that case, we have to make sure
            // we store the full absolute path to the folders
            boolean isArchive = path.endsWith("jar") || path.endsWith("zip");
            if (!isArchive) {
                IPath ipath = new Path(path);
                // We can only get a folder if the segment count is greater
                // 2 i.e. if the path has at least two segments
                if (ipath.segmentCount() >= 2) {
                    IFolder folder = ResourcesPlugin.getWorkspace().getRoot().getFolder(ipath);
                    if (folder.exists()) {
                        classpathRef.add(folder.getLocation().makeAbsolute().toOSString());
                    }
                }

                // If the segment count is 1, then it is probably a project
                if (ipath.segmentCount() == 1) {
                    IProject refPrj = ResourcesPlugin.getWorkspace().getRoot().getProject(path);
                    // if this is a project, then we assume it has a SRC
                    // folder; and therefore append SRC at the end
                    // this is required because j2objc compiler needs the
                    // path till the src folder to compile properly

                    classpathRef.add(refPrj.getLocation().append("src").makeAbsolute().toOSString());
                }

            } else {
                // add whatever archive path we get from the results
                classpathRef.add(path);
            }

        }

        checkboxTableViewer.setInput(classpathRef);
        checkboxTableViewer.refresh();

    } catch (JavaModelException e) {
        LogUtil.logException(e);
    } catch (CoreException e) {
        LogUtil.logException(e);
    }
}

From source file:com.legstar.eclipse.plugin.schemagen.wizards.JavaToXsdWizardPage.java

License:Open Source License

/**
 * Given classpath entries from a java project, populate a list of
 * collections.//from   ww  w  .  j a  v a  2s .  c o  m
 * 
 * @param selectedPathElementsLocations the output path locations
 * @param classPathEntries the java project class path entries
 * @param javaProject the java project
 * @throws JavaModelException if invalid classpath
 */
private void addPathElements(final List<String> selectedPathElementsLocations,
        final IClasspathEntry[] classPathEntries, final IJavaProject javaProject) throws JavaModelException {

    IClasspathEntry jreEntry = JavaRuntime.getDefaultJREContainerEntry();
    IPath projectPath = javaProject.getProject().getLocation();

    for (int i = 0; i < classPathEntries.length; i++) {
        IClasspathEntry classpathEntry = classPathEntries[i];
        String pathElementLocation = null;
        switch (classpathEntry.getEntryKind()) {
        case IClasspathEntry.CPE_LIBRARY:
            pathElementLocation = classpathEntry.getPath().toOSString();
            break;
        case IClasspathEntry.CPE_CONTAINER:
            /* No need for the default jre */
            if (classpathEntry.equals(jreEntry)) {
                break;
            }
            /* Resolve container into class path entries */
            IClasspathContainer classpathContainer = JavaCore.getClasspathContainer(classpathEntry.getPath(),
                    javaProject);
            addPathElements(selectedPathElementsLocations, classpathContainer.getClasspathEntries(),
                    javaProject);
            break;
        case IClasspathEntry.CPE_VARIABLE:
            pathElementLocation = JavaCore.getResolvedVariablePath(classpathEntry.getPath()).toOSString();
            break;
        case IClasspathEntry.CPE_SOURCE:
            /*
             * If source has no specific output, use the project default
             * one
             */
            IPath outputLocation = classpathEntry.getOutputLocation();
            if (outputLocation == null) {
                outputLocation = javaProject.getOutputLocation();
            }
            pathElementLocation = projectPath.append(outputLocation.removeFirstSegments(1)).toOSString();
            break;
        default:
            break;
        }

        if (pathElementLocation != null && !selectedPathElementsLocations.contains(pathElementLocation)) {
            selectedPathElementsLocations.add(pathElementLocation);
        }
    }
}

From source file:com.liferay.ide.core.model.internal.GenericResourceBundlePathService.java

License:Open Source License

/**
 * @param project/*www  .ja v  a  2 s  .  c o  m*/
 * @return
 */
final List<Path> computeRoots(IProject project) {
    List<Path> roots = new ArrayList<Path>();

    if (project != null) {
        IClasspathEntry[] cpEntries = CoreUtil.getClasspathEntries(project);

        for (IClasspathEntry iClasspathEntry : cpEntries) {
            if (IClasspathEntry.CPE_SOURCE == iClasspathEntry.getEntryKind()) {
                IPath entryPath = WORKSPACE_ROOT.getFolder(iClasspathEntry.getPath()).getLocation();
                String fullPath = entryPath.toOSString();
                Path sapphirePath = new Path(fullPath);
                roots.add(sapphirePath);
            }
        }
    }

    return roots;
}

From source file:com.liferay.ide.core.util.CoreUtil.java

License:Open Source License

public final static List<IFolder> getSourceFolders(final IJavaProject project) {
    final List<IFolder> folders = new ArrayList<IFolder>();

    if (project != null) {
        final IClasspathEntry[] entries = project.readRawClasspath();

        for (final IClasspathEntry entry : entries) {
            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE && entry.getPath().segmentCount() > 0) {
                IContainer container = null;

                if (entry.getPath().segmentCount() == 1) {
                    container = ResourcesPlugin.getWorkspace().getRoot().getProject(entry.getPath().segment(0));
                } else {
                    container = ResourcesPlugin.getWorkspace().getRoot().getFolder(entry.getPath());
                }/*from   www  . j  a v  a 2 s. c  om*/

                if (!folders.contains(container) && container instanceof IFolder) {
                    folders.add((IFolder) container);
                }
            }
        }
    }

    return folders;
}

From source file:com.liferay.ide.layouttpl.core.facet.LayoutTplPluginFacetInstall.java

License:Open Source License

protected void removeUnneededClasspathEntries() {
    IFacetedProjectWorkingCopy facetedProject = getFacetedProject();
    IJavaProject javaProject = JavaCore.create(facetedProject.getProject());

    try {// ww  w.  j  a va  2 s .  c  om
        IClasspathEntry[] existingClasspath = javaProject.getRawClasspath();
        List<IClasspathEntry> newClasspath = new ArrayList<IClasspathEntry>();

        for (IClasspathEntry entry : existingClasspath) {
            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                continue;
            } else if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                String path = entry.getPath().toPortableString();

                if (path.contains("org.eclipse.jdt.launching.JRE_CONTAINER") || //$NON-NLS-1$
                        path.contains("org.eclipse.jst.j2ee.internal.web.container") || //$NON-NLS-1$
                        path.contains("org.eclipse.jst.j2ee.internal.module.container")) //$NON-NLS-1$
                {
                    continue;
                }
            }

            newClasspath.add(entry);
        }

        javaProject.setRawClasspath(newClasspath.toArray(new IClasspathEntry[0]), null);

        IResource sourceFolder = javaProject.getProject()
                .findMember(IPluginFacetConstants.PORTLET_PLUGIN_SDK_SOURCE_FOLDER);

        if (sourceFolder.exists()) {
            sourceFolder.delete(true, null);
        }
    } catch (Exception e) {
        // no need to report errors
    }
}

From source file:com.liferay.ide.maven.core.LiferayMavenProject.java

License:Open Source License

public IPath[] getUserLibs() {
    final List<IPath> libs = new ArrayList<IPath>();

    final IClasspathManager buildPathManager = MavenJdtPlugin.getDefault().getBuildpathManager();

    try {//from  ww  w  .  java2 s  .  c o m
        final IClasspathEntry[] classpath = buildPathManager.getClasspath(getProject(),
                IClasspathManager.CLASSPATH_RUNTIME, true, new NullProgressMonitor());

        for (IClasspathEntry entry : classpath) {
            libs.add(entry.getPath());
        }
    } catch (CoreException e) {
        LiferayMavenCore.logError("Unable to get maven classpath.", e); //$NON-NLS-1$
    }

    return libs.toArray(new IPath[0]);
}

From source file:com.liferay.ide.portlet.core.model.internal.LocaleBundleValidationService.java

License:Open Source License

public Status compute() {
    final Element modelElement = context(Element.class);

    if (!modelElement.disposed() && modelElement instanceof SupportedLocales) {
        final IProject project = modelElement.adapt(IProject.class);
        final Portlet portlet = modelElement.nearest(Portlet.class);
        final IWorkspaceRoot wroot = ResourcesPlugin.getWorkspace().getRoot();
        final IClasspathEntry[] cpEntries = CoreUtil.getClasspathEntries(project);

        if (cpEntries != null) {
            final String locale = modelElement.property(context(ValueProperty.class)).text(false);
            final Value<Path> resourceBundle = portlet.getResourceBundle();

            if (locale == null) {
                return Status.createErrorStatus(Resources.localeMustNotEmpty);
            } else {
                final String bundleName = resourceBundle.text();

                if (resourceBundle != null && bundleName != null) {
                    final String localeString = PortletUtil.localeString(locale);
                    final String ioFileName = PortletUtil.convertJavaToIoFileName(bundleName, RB_FILE_EXTENSION,
                            localeString);

                    for (IClasspathEntry iClasspathEntry : cpEntries) {
                        if (IClasspathEntry.CPE_SOURCE == iClasspathEntry.getEntryKind()) {
                            IPath entryPath = wroot.getFolder(iClasspathEntry.getPath()).getLocation();
                            entryPath = entryPath.append(ioFileName);
                            IFile resourceBundleFile = wroot.getFileForLocation(entryPath);
                            if (resourceBundleFile != null && resourceBundleFile.exists()) {
                                return Status.createOkStatus();
                            } else {
                                return Status.createWarningStatus(Resources.bind(
                                        StringEscapeUtils.unescapeJava(Resources.noResourceBundle),
                                        new Object[] { locale, bundleName, localeString }));
                            }//  w w  w  .  ja va  2s .c o m

                        }
                    }
                }
            }
        }
    }

    return Status.createOkStatus();
}

From source file:com.liferay.ide.portlet.core.util.PortletUtil.java

License:Open Source License

/**
 * this will convert the IO file name of the resource bundle to java package name
 * //from  w w w.  j a va  2s  .c o  m
 * @param project
 * @param value
 *            - the io file name
 * @return - the java package name of the resource bundle
 */
public static String convertIOToJavaFileName(IProject project, String value) {
    String rbIOFile = value.substring(value.lastIndexOf("/") + 1); //$NON-NLS-1$
    IFile resourceBundleFile = null;
    IWorkspace workspace = ResourcesPlugin.getWorkspace();
    IWorkspaceRoot wroot = workspace.getRoot();
    IClasspathEntry[] cpEntries = CoreUtil.getClasspathEntries(project);
    for (IClasspathEntry iClasspathEntry : cpEntries) {
        if (IClasspathEntry.CPE_SOURCE == iClasspathEntry.getEntryKind()) {
            IPath entryPath = wroot.getFolder(iClasspathEntry.getPath()).getLocation();
            entryPath = entryPath.append(rbIOFile);
            resourceBundleFile = wroot.getFileForLocation(entryPath);
            // System.out.println( "ResourceBundleValidationService.validate():" + resourceBundleFile );
            if (resourceBundleFile != null && resourceBundleFile.exists()) {
                break;
            }
        }
    }

    String javaName = resourceBundleFile.getProjectRelativePath().toPortableString();
    if (javaName.indexOf('.') != -1) {
        // Strip the extension
        javaName = value.substring(0, value.lastIndexOf('.'));
        // Replace all "/" by "."
        javaName = javaName.replace('/', '.');
    }
    return javaName;
}

From source file:com.liferay.ide.portlet.ui.editor.internal.AbstractResourceBundleActionHandler.java

License:Open Source License

/**
 * @param project//from  w  ww  . j  a va  2 s .co  m
 * @param ioFileName
 * @return
 */
protected final boolean getFileFromClasspath(IProject project, String ioFileName) {

    IClasspathEntry[] cpEntries = CoreUtil.getClasspathEntries(project);
    for (IClasspathEntry iClasspathEntry : cpEntries) {
        if (IClasspathEntry.CPE_SOURCE == iClasspathEntry.getEntryKind()) {
            IPath entryPath = wroot.getFolder(iClasspathEntry.getPath()).getLocation();
            entryPath = entryPath.append(ioFileName);
            IFile resourceBundleFile = wroot.getFileForLocation(entryPath);
            if (resourceBundleFile != null && resourceBundleFile.exists()) {
                return true;
            } else {
                return false;
            }
        }
    }
    return false;
}