Package com.googlecode.grinderstone.debug.ui.launching.jdt

Source Code of com.googlecode.grinderstone.debug.ui.launching.jdt.JavaProjectPyPathBuilder

package com.googlecode.grinderstone.debug.ui.launching.jdt;

import java.io.File;
import java.util.HashSet;
import java.util.Set;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectNature;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;

import com.googlecode.grinderstone.debug.ui.launching.PathUtils;

/**
* @author Andruschuk Borislav
*/
public class JavaProjectPyPathBuilder {

    private final IProject project;

    /**
     * Default constructor.
     */
    public JavaProjectPyPathBuilder(IProject project) {
        super();
        this.project = project;
    }

    public String buildPyPath() throws CoreException {
        String pyPath = "";
        IProject[] referencedProjects = project.getReferencedProjects();
        if (referencedProjects != null) {
            // for exclusion duplicate entries
            Set<String> classpath = new HashSet<String>();
            // for project cycles prevention
            Set<String> dependendedProjects = new HashSet<String>();
            for (IProject rProject : referencedProjects) {
                // We cannot have dependency to JDT to allow plugin usage in other Eclipse
                // builds (i.e. w/o JDT)
                extractClasspathFromJavaProjects(rProject, classpath, dependendedProjects);
            }

            for (String cpentry : classpath) {
                pyPath += (File.pathSeparatorChar + cpentry);
            }
        }
        return pyPath;
    }

    private void extractClasspathFromJavaProjects(IProject rProject, Set<String> classpath,
                    Set<String> dependendedProjects) throws CoreException {

        // prevent cycles
        String projectPath = rProject.getLocation().toOSString();
        if (dependendedProjects.contains(projectPath)) {
            return;
        } else {
            dependendedProjects.add(projectPath);
        }

        IProjectNature nature = rProject.getNature(JavaCore.NATURE_ID);
        if (nature != null) {
            IJavaProject javaProject = (IJavaProject) nature;
            IPath output = javaProject.getOutputLocation();
            String substitutedOutput = PathUtils.substituteProjectPath(rProject, output
                            .toOSString());
            classpath.add(substitutedOutput);

            IClasspathEntry[] classpathEntries = javaProject.getResolvedClasspath(true);
            if (classpathEntries != null && classpathEntries.length > 0) {
                for (IClasspathEntry e : classpathEntries) {
                    Integer kind = e.getEntryKind();
                    String additionalPath = null;
                    switch (kind.intValue()) {
                        case IClasspathEntry.CPE_PROJECT:
                            IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
                            IPath ePath = e.getPath();
                            IResource member = workspaceRoot.findMember(ePath);
                            if (member != null && member.getType() == IResource.PROJECT) {
                                IProject projRsc = (IProject) member;
                                extractClasspathFromJavaProjects(projRsc, classpath,
                                                dependendedProjects);
                            }
                            break;
                        case IClasspathEntry.CPE_SOURCE:
                            IPath specificPath = e.getOutputLocation();
                            if (specificPath != null) {
                                additionalPath = specificPath.makeAbsolute().toOSString();
                                classpath.add(additionalPath);
                            }
                            break;
                        case IClasspathEntry.CPE_LIBRARY:
                        case IClasspathEntry.CPE_VARIABLE:
                        case IClasspathEntry.CPE_CONTAINER:
                            IPath path = e.getPath();
                            additionalPath = path.toOSString();
                            if (!path.toFile().exists()
                                            && additionalPath.startsWith("/" + rProject.getName())) {
                                additionalPath = PathUtils.substituteProjectPath(rProject,
                                                additionalPath);
                            }
                            classpath.add(additionalPath);
                            break;
                    }

                }
            }
        }
    }

}
TOP

Related Classes of com.googlecode.grinderstone.debug.ui.launching.jdt.JavaProjectPyPathBuilder

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.