org.antlr.eclipse.smapinstaller.SMapInstallerBuilder.java Source code

Java tutorial

Introduction

Here is the source code for org.antlr.eclipse.smapinstaller.SMapInstallerBuilder.java

Source

/**
 * <small>
 * <p><i>Copyright (C) 2005 Torsten Juergeleit, 
 * All rights reserved. </i></p>
 * 
 * <p>USE OF THIS CONTENT IS GOVERNED BY THE TERMS AND CONDITIONS OF THIS
 * AGREEMENT AND/OR THE TERMS AND CONDITIONS OF LICENSE AGREEMENTS OR NOTICES
 * INDICATED OR REFERENCED BELOW. BY USING THE CONTENT, YOU AGREE THAT YOUR USE
 * OF THE CONTENT IS GOVERNED BY THIS AGREEMENT AND/OR THE TERMS AND CONDITIONS
 * OF ANY APPLICABLE LICENSE AGREEMENTS OR NOTICES INDICATED OR REFERENCED
 * BELOW. IF YOU DO NOT AGREE TO THE TERMS AND CONDITIONS OF THIS AGREEMENT AND
 * THE TERMS AND CONDITIONS OF ANY APPLICABLE LICENSE AGREEMENTS OR NOTICES
 * INDICATED OR REFERENCED BELOW, THEN YOU MAY NOT USE THE CONTENT.</p>
 * 
 * <p>This Content is Copyright (C) 2005 Torsten Juergeleit, 
 * and is provided to you under the terms and conditions of the Common Public 
 * License Version 1.0 ("CPL"). A copy of the CPL is provided with this Content 
 * and is also available at 
 *     <a href="http://www.eclipse.org/legal/cpl-v10.html">
 *         http://www.eclipse.org/legal/cpl-v10.html </a>.
 * 
 * For purposes of the CPL, "Program" will mean the Content.</p>
 * 
 * <p>Content includes, but is not limited to, source code, object code,
 * documentation and any other files in this distribution.</p>
 * 
 * </small>
 * 
 * NOTE:This class originally appeared as part of the Apache Tomcat 4.1 
 * distribution. It has been modified for use in the ANTLR Eclipse plugin by
 * Scott Stanchfield (scott@javadude.com) and is re-distributed under the
 * Common Public License. In accordance with the Apache license, the Apache
 * license text appears as smap-installer-apache-license.txt in the
 * root directory of this project.
 */

package org.antlr.eclipse.smapinstaller;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import org.antlr.eclipse.core.builder.AntlrBuilder;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IResourceDeltaVisitor;
import org.eclipse.core.resources.IResourceVisitor;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;

/**
 * A custom builder for Java files in Eclipse that removes trailing spaces and
 *   converts leading tabs into spaces. This builder is intended to be
 *   used whenever a java file is saved, and only acts upon files that are
 *   open in the editor.
 */
public class SMapInstallerBuilder extends IncrementalProjectBuilder {
    /** the builder id for the smap installer */
    public static final String BUILDER_ID = "org.antlr.eclipse.core.smapbuilder";

    /**
     * A delta visitor that will execute against a change made on some
     *   resources. This visitor simply calls spaceConvert when a resource
     *   has changed.
     * Note that delta visitors are called for incremental compilations,
     *   not full builds of the system.
     */
    private IResourceDeltaVisitor deltaVisitor = new IResourceDeltaVisitor() {
        @Override
        public boolean visit(IResourceDelta delta) throws CoreException {
            if (delta.getKind() == IResourceDelta.CHANGED) {
                installSmap(delta.getResource());
            }
            return true;
        }
    };

    /**
     * A visitor that will execute when the project is rebuilt. This visitor 
     *   simply calls spaceConvert against each resource.
     */
    private IResourceVisitor visitor = new IResourceVisitor() {
        @Override
        public boolean visit(IResource resource) {
            try {
                installSmap(resource);
            } catch (JavaModelException e) {
                e.printStackTrace();
            }
            return true;
        }
    };

    /**
      * Installs the modified smap into a generated classfile
      * @param resource
     * @throws JavaModelException 
      */
    protected void installSmap(final IResource resource) throws JavaModelException {
        // We only work on smap files -- skip everything else
        if (!(resource instanceof IFile))
            return;
        IFile smapIFile = (IFile) resource;
        if (!"smap".equalsIgnoreCase(smapIFile.getFileExtension()))
            return;
        IJavaProject javaProject = JavaCore.create(smapIFile.getProject());

        // get the name of the corresponding java source file
        IPath smapPath = smapIFile.getFullPath();

        IClasspathEntry[] classpathEntries = javaProject.getResolvedClasspath(true);
        for (int i = 0, l = classpathEntries.length; i < l; i++) {
            IClasspathEntry entry = classpathEntries[i];
            if (entry.getEntryKind() != IClasspathEntry.CPE_SOURCE)
                continue;
            if (!entry.getPath().isPrefixOf(smapPath))
                continue;

            // found the right source container
            IPath outputLocation = entry.getOutputLocation();
            if (outputLocation == null)
                outputLocation = javaProject.getOutputLocation();
            // strip the source dir and .smap suffix
            String sourceDir = entry.getPath().toString();
            String smapName = smapPath.toString();
            String javaSourceName = smapName.substring(0, smapName.length() - 5) + ".java";
            String className = smapName.substring(sourceDir.length(), smapName.length() - 5) + ".class";
            IPath path = outputLocation.append(className);
            IPath workspaceLoc = ResourcesPlugin.getWorkspace().getRoot().getLocation();
            IPath classFileLocation = workspaceLoc.append(path);
            IResource classResource = ResourcesPlugin.getWorkspace().getRoot().findMember(javaSourceName);

            File classFile = classFileLocation.toFile();
            File smapFile = smapIFile.getLocation().toFile();
            try {
                String installSmap = classResource.getPersistentProperty(AntlrBuilder.INSTALL_SMAP);
                if ("true".equals(installSmap))
                    SDEInstaller.install(classFile, smapFile);
            } catch (CoreException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /** {@inheritDoc} */
    @Override
    protected IProject[] build(final int kind, final Map<String, String> args, final IProgressMonitor monitor)
            throws CoreException {
        // split the build via incremental or full, and pass the
        //   right visitor to the delta
        if (kind == IncrementalProjectBuilder.FULL_BUILD) {
            getProject().accept(visitor);
        } else {
            IResourceDelta delta = getDelta(getProject());
            if (delta == null) {
                getProject().accept(visitor);
            } else {
                delta.accept(deltaVisitor);
            }
        }
        return null;
    }
}