org.limy.eclipse.qalab.common.LimyQalabUtils.java Source code

Java tutorial

Introduction

Here is the source code for org.limy.eclipse.qalab.common.LimyQalabUtils.java

Source

/*
 * Created 2006/08/19
 * Copyright (C) 2003-2009  Naoki Iwami (naoki@limy.org)
 *
 * This file is part of Limy Eclipse Plugin.
 *
 * Limy Eclipse Plugin is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Limy Eclipse Plugin is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Limy Eclipse Plugin.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.limy.eclipse.qalab.common;

import java.io.File;
import java.util.Collection;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
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.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.limy.eclipse.common.resource.LimyResourceUtils;

/**
 * Limy Qalabp?[eBeBNX?B
 * @author Naoki Iwami
 */
public final class LimyQalabUtils {

    /**
     * private constructor
     */
    private LimyQalabUtils() {
    }

    /**
     * IACe?eJavav?WFNg?B
     * @param selection Ie
     * @return ?eJavav?WFNg?i?null?j
     */
    public static IJavaProject getJavaProject(IStructuredSelection selection) {

        // I??ACe
        Object element = selection.getFirstElement();
        if (element == null) {
            return null;
        }

        // Javav?WFNg
        IJavaProject project = null;
        if (element instanceof IResource) {
            IResource resource = (IResource) element;
            IProject normalProject = resource.getProject();
            project = JavaCore.create(normalProject);
        }
        if (element instanceof IJavaElement) {
            IJavaElement javaEl = (IJavaElement) element;
            project = javaEl.getJavaProject();
        }
        return project;

    }

    /**
     * Javat@CQA?O?B
     * @param env 
     * @param path Javat@C
     * @param store v?WFNgStore
     * @return QA?O\?[XfBNg?^
     */
    public static boolean isIgnoreSource(LimyQalabEnvironment env, IPath path) {

        String packages = env.getStore().getString(LimyQalabConstants.IGNORE_PACKAGES);
        String[] ignorePackages = packages.split("\n");

        IResource resource = LimyResourceUtils.newFile(path);
        try {
            String className = getQualifiedClassName(env, resource);
            if (className == null) {
                return false;
            }
            for (String ignorePackage : ignorePackages) {
                if (className.startsWith(ignorePackage + ".")) {
                    return true;
                }
            }
        } catch (CoreException e) {
            // do nothing
        }
        return false;
    }

    /**
     * Javav?WFNgJDKo?[W?B
     * @param project Javav?WFNg
     * @return JDKo?[W
     */
    public static String getJdkVersion(IJavaProject project) {
        return project.getOption(JavaCore.COMPILER_SOURCE, true);
    }

    /**
     * IPath`pX?A?pX?B
     * @param javaProject Javav?WFNg?inull?j
     * @param path pX
     * @return ?pX
     * @throws CoreException RAO
     */
    public static String createFullPath(IJavaProject javaProject, IPath path) throws CoreException {

        if (javaProject != null && javaProject.getProject().getName().equals(path.segment(0))) {

            // v?WFNg??
            return javaProject.getProject().getLocation().append(path.removeFirstSegments(1)).toString();
        }

        IResource[] projects = ResourcesPlugin.getWorkspace().getRoot().members();
        for (IResource resource : projects) {
            IProject targetProject = (IProject) resource;
            if (targetProject.getName().equals(path.segment(0))) {
                return targetProject.getLocation().append(path.removeFirstSegments(1)).toString();
            }
        }
        return null;
    }

    /**
     * Java\?[XNX?iS?j?B
     * @param env 
     * @param resource \?[X
     * @return NX
     * @throws CoreException 
     */
    public static String getQualifiedClassName(LimyQalabEnvironment env, IResource resource) throws CoreException {

        // ex. /project/src/pack1/ClassA.java
        String pathStr = resource.getFullPath().toString();

        Collection<IPath> paths = env.getSourcePaths(true);
        for (IPath path : paths) {

            if (pathStr.equals(path.toString())) {
                return null;
            }
            // ex. "/project/src/pack1/ClassA.java".startsWith("/project/src")
            if (pathStr.startsWith(path.toString())) {

                // ex. pack1/ClassA.java
                String relativeName = pathStr.substring(path.toString().length() + 1);

                if (relativeName.lastIndexOf('.') <= 0) {
                    return null;
                }

                // ex. pack1.ClassA
                return relativeName.substring(0, relativeName.lastIndexOf('.')).replace('\\', '/').replace('/',
                        '.');
            }
        }
        return null;
    }

    /**
     * ".limy"?e|t@C???B
     * @param project v?WFNg
     * @param relativePath t@C
     * @return
     */
    public static File createTempFile(IProject project, String relativePath) {
        File baseDir = new File(project.getLocation().toFile(), ".limy");
        File file = new File(baseDir, relativePath);
        file.getParentFile().mkdirs();
        return file;
    }

    /**
     * v?WFNgS?o?pX results ?B
     * @param project 
     * @param results i[?
     */
    public static void appendProjectBinPaths(IJavaProject project, Collection<IPath> results) {

        results.add(project.readOutputLocation());
        // v?WFNg\?[XfBNg?[v
        for (IClasspathEntry entry : project.readRawClasspath()) {
            IPath location = entry.getOutputLocation();
            if (location != null) {
                // \?[XfBNgL?ofBNgw??
                results.add(location);
            }
        }
    }

}