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:org.antlr.eclipse.core.AntlrNature.java

License:Open Source License

/**
 * Remove the ANTLR nature from a project
 * @param aProject The project to remove the nature from
 * @param aMonitor A progress monitor//from  w  w w .jav  a2 s  .  c  om
 */
public static void removeNature(final IProject aProject, final IProgressMonitor aMonitor) {
    if (aProject != null) {
        if (DEBUG) {
            System.out.println("removing ANTLR nature from project '" + aProject.getName() + "'");
        }
        try {
            if (aProject.hasNature(ID)) {
                IProjectDescription desc = aProject.getDescription();
                ArrayList<String> natures = new ArrayList<String>(Arrays.asList(desc.getNatureIds()));
                natures.remove(ID);
                desc.setNatureIds(natures.toArray(new String[natures.size()]));
                aProject.setDescription(desc, aMonitor);

                // if it's a Java Project, remove the ANTLR_HOME class path variable
                if (aProject.hasNature(JavaCore.NATURE_ID)) {
                    IJavaProject javaProject = JavaCore.create(aProject);
                    ArrayList<IClasspathEntry> rawClasspath = new ArrayList<IClasspathEntry>(
                            Arrays.asList(javaProject.getRawClasspath()));
                    boolean hadAntlrJar = false;
                    for (Iterator<IClasspathEntry> i = rawClasspath.iterator(); i.hasNext();) {
                        IClasspathEntry entry = i.next();
                        if (entry.getEntryKind() == IClasspathEntry.CPE_VARIABLE) {
                            String segment0 = entry.getPath().segment(0);
                            if (AntlrCorePlugin.ANTLR_HOME.equals(segment0)) {
                                i.remove();
                                hadAntlrJar = true;
                            }
                            if ("PARSEVIEW_HOME".equals(segment0)) {
                                i.remove();
                                hadAntlrJar = true;
                            }
                        }
                    }
                    if (hadAntlrJar) {
                        javaProject.setRawClasspath(
                                rawClasspath.toArray(new IClasspathEntry[rawClasspath.size()]), null);
                    }
                }
            }
        } catch (CoreException e) {
            AntlrCorePlugin.log(e);
        }
    }
}

From source file:org.antlr.eclipse.core.builder.AntlrBuilder.java

License:Open Source License

/**
 * Compile a grammar/*from   w w  w.  j av  a 2  s  .  co m*/
 * @param aFile The grammar file to compile
 * @param aMonitor A progress monitor
 */
public void compileFile(IFile aFile, IProgressMonitor aMonitor) {
    String grammarFileName = aFile.getProjectRelativePath().toString();
    try {
        // delete the old generated files for this grammar
        aFile.getProject().accept(new CleaningVisitor(aMonitor, grammarFileName));

        // if it's in a java project, only build it if it's in a source dir
        if (aFile.getProject().hasNature(JavaCore.NATURE_ID)) {
            IProject project = aFile.getProject();
            IJavaProject javaProject = JavaCore.create(project);
            IPath path = aFile.getFullPath();
            boolean ok = false;
            IClasspathEntry[] resolvedClasspath = javaProject.getResolvedClasspath(true);
            for (int i = 0; i < resolvedClasspath.length; i++) {
                IClasspathEntry entry = resolvedClasspath[i];

                if (entry.getEntryKind() != IClasspathEntry.CPE_SOURCE)
                    continue;

                IPath entryPath = entry.getPath();
                if (entryPath.isPrefixOf(path)) {
                    ok = true;
                    break;
                }
            }
            if (!ok) {
                return;
            }
        }
    } catch (CoreException e1) {
        e1.printStackTrace();
    }

    fFile = aFile;

    aMonitor.beginTask(
            AntlrCorePlugin.getFormattedMessage("AntlrBuilder.compiling", aFile.getFullPath().toString()), 4);

    // Remove all markers from this file
    try {
        aFile.deleteMarkers(null, true, IResource.DEPTH_INFINITE);
    } catch (CoreException e) {
        AntlrCorePlugin.log(e);
    }

    // Prepare arguments for ANTLR compiler
    // read the settings for this grammar
    HashMap<String, HashMap<String, String>> map = SettingsPersister.readSettings(aFile.getProject());

    ArrayList<String> args = createArguments(map, aFile);
    if (DEBUG) {
        System.out.println("Compiling ANTLR grammar '" + aFile.getName() + "': arguments=" + args);
    }

    // Monitor system out and err
    fOriginalOut = System.out;
    fOriginalErr = System.err;
    System.setOut(new PrintStream(new MonitoredOutputStream(this)));
    System.setErr(new PrintStream(new MonitoredOutputStream(this)));

    try {
        // Compile ANTLR grammar file
        AntlrTool tool = new AntlrTool();
        if (!tool.preprocess(args.toArray(new String[args.size()]))) {
            try {
                aMonitor.worked(1);
                if (!tool.parse()) {
                    aMonitor.worked(1);
                    if (!tool.generate()) {
                        aMonitor.worked(1);
                        refreshFolder(map, tool, args, aFile, grammarFileName,
                                ResourcesPlugin.getWorkspace().getRoot().findMember(fOutput), aMonitor,
                                tool.getSourceMaps());
                    } else {

                        // If errors during generate then delete all
                        // generated files
                        deleteFiles(tool, aFile.getParent(), aMonitor);
                    }
                    aMonitor.worked(1);
                }
            } catch (CoreException e) {
                AntlrCorePlugin.log(e);
            }
        }

    } catch (Throwable e) {
        if (!(e instanceof SecurityException)) {
            AntlrCorePlugin.log(e);
        }
    } finally {
        System.setOut(fOriginalOut);
        System.setErr(fOriginalErr);
        aMonitor.done();
    }
}

From source file:org.antlr.eclipse.smapinstaller.SMapInstallerBuilder.java

License:Open Source License

/**
  * Installs the modified smap into a generated classfile
  * @param resource/* www .  ja  va 2s.co m*/
 * @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();
        }
    }
}

From source file:org.apache.buildr.eclipse.BuildrNature.java

License:Apache License

@Override
public void configure() throws CoreException {
    if (project.hasNature(JavaCore.NATURE_ID)) {
        IJavaProject targetProject = JavaCore.create(project);
        IClasspathEntry[] entries = targetProject.getRawClasspath();
        Path buildrContainer = new Path("org.apache.buildr.eclipse.BUILDR_CONTAINER/dependencies");
        for (IClasspathEntry entry : entries) {
            if (entry.getPath().equals(buildrContainer)) {
                return;
            }/*from  w ww .  j  a  v  a2s . c o m*/
        }

        IClasspathEntry entry = JavaCore.newContainerEntry(buildrContainer);
        IClasspathEntry[] newEntries = new IClasspathEntry[entries.length + 1];
        System.arraycopy(entries, 0, newEntries, 0, entries.length);
        newEntries[entries.length] = entry;
        targetProject.setRawClasspath(newEntries, true, new NullProgressMonitor());
    }
}

From source file:org.apache.buildr.eclipse.BuildrNature.java

License:Apache License

@Override
public void deconfigure() throws CoreException {
    if (project.hasNature(JavaCore.NATURE_ID)) {
        IJavaProject targetProject = JavaCore.create(project);
        IClasspathEntry[] entries = targetProject.getRawClasspath();
        Path buildrContainer = new Path("org.apache.buildr.eclipse.BUILDR_CONTAINER/dependencies");
        List<IClasspathEntry> newEntries = new ArrayList<>();
        for (IClasspathEntry entry : entries) {
            if (!entry.getPath().equals(buildrContainer)) {
                newEntries.add(entry);//  www .  ja v  a2 s .co  m
            }
        }

        targetProject.setRawClasspath(newEntries.toArray(new IClasspathEntry[newEntries.size()]), true,
                new NullProgressMonitor());
    }
}

From source file:org.apache.cactus.eclipse.runner.ui.CactifyActionDelegate.java

License:Apache License

/**
 * @param theFirstArray an array of classpath entries
 * @param theSecondArray an array of classpath entries
 * @return the fusion of the two given arrays 
 *///from   ww w  .  j  a  v a 2s. c o  m
private static IClasspathEntry[] merge(IClasspathEntry[] theFirstArray, IClasspathEntry[] theSecondArray) {

    Vector result = new Vector();
    for (int i = 0; i < theFirstArray.length; i++) {
        result.add(theFirstArray[i]);
    }
    for (int i = 0; i < theSecondArray.length; i++) {
        IClasspathEntry currentEntry = theSecondArray[i];
        String currentEntryFileName = currentEntry.getPath().toFile().getName();
        boolean entryAlreadyExists = false;
        boolean isFile = false;
        for (int j = 0; j < theFirstArray.length; j++) {
            IClasspathEntry comparedEntry = theFirstArray[j];
            isFile = comparedEntry.getPath().toFile().getAbsolutePath().endsWith(".jar");
            String comparedFileName = comparedEntry.getPath().toFile().getName();
            if (comparedEntry.getPath().equals(currentEntry.getPath())
                    || (comparedFileName.equals(currentEntryFileName) && isFile)) {
                entryAlreadyExists = true;
                break;
            }
        }
        if (!entryAlreadyExists) {
            result.add(currentEntry);
        }
    }
    return (IClasspathEntry[]) result.toArray(new IClasspathEntry[result.size()]);
}

From source file:org.apache.cactus.eclipse.webapp.internal.WarBuilder.java

License:Apache License

/**
 * For each IClasspathEntry transform the path in an absolute path.
 * @param theEntries array of IClasspathEntry to render asbolute
 * @return an array of absolute IClasspathEntries
 *///from  w  w w . j  ava 2s . co  m
private static IClasspathEntry[] getAbsoluteEntries(final IClasspathEntry[] theEntries) {
    if (theEntries == null) {
        return new IClasspathEntry[0];
    }
    Vector result = new Vector();
    for (int i = 0; i < theEntries.length; i++) {
        IClasspathEntry currentEntry = theEntries[i];
        if (currentEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
            IPath path = currentEntry.getPath();
            Object target = JavaModel.getTarget(ResourcesPlugin.getWorkspace().getRoot(), path, true);
            if (target instanceof IFile) {
                IFile file = (IFile) target;
                IPath absolutePath = file.getLocation();
                result.add(JavaCore.newLibraryEntry(absolutePath, null, null));
            } else if (target instanceof File) {
                File file = (File) target;
                result.add(JavaCore.newLibraryEntry(new Path(file.getAbsolutePath()), null, null));
            }
        }
    }
    return (IClasspathEntry[]) result.toArray(new IClasspathEntry[result.size()]);
}

From source file:org.apache.cactus.eclipse.webapp.internal.WarBuilder.java

License:Apache License

/**
 * @param theJarEntries the jars to build ZipFileSets from
 * @return an array of ZipFileSet corresponding to the given jars
 *///from   www.  j a va  2  s.  c  o  m
private static ZipFileSet[] getZipFileSets(final IClasspathEntry[] theJarEntries) {
    Vector result = new Vector();
    for (int i = 0; i < theJarEntries.length; i++) {

        IClasspathEntry currentEntry = theJarEntries[i];
        if (currentEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
            File currentJar = currentEntry.getPath().toFile();
            ZipFileSet zipFS = new ZipFileSet();
            zipFS.setFile(currentJar);
            result.add(zipFS);
        }
    }
    return (ZipFileSet[]) result.toArray(new ZipFileSet[result.size()]);
}

From source file:org.apache.cactus.eclipse.webapp.internal.Webapp.java

License:Apache License

/**
 * Converts an array of library classpath entries to a String 
 * @param theClasspathEntries an array of library entries
 * @return String string of delimiter-separated classpaths
 *//*from  ww  w . j  a  v  a 2 s .  c  om*/
private String toString(final IClasspathEntry[] theClasspathEntries) {
    StringBuffer result = new StringBuffer();
    for (int i = 0; i < theClasspathEntries.length; i++) {
        IClasspathEntry current = theClasspathEntries[i];
        result.append(current.getPath());
        result.append(CLASSPATH_DELIMITER);
    }
    return result.toString();
}

From source file:org.apache.derby.ui.popup.actions.RemoveDerbyNature.java

License:Apache License

public void run(IAction action) {
    IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    try {//from   w ww .j ava  2  s  . c  om
        ((ApplicationWindow) window).setStatus(Messages.REMOVING_NATURE);

        if (currentJavaProject == null) {
            currentJavaProject = JavaCore.create(currentProject);
        }
        //Shutdown server if running for the current project
        if (DerbyServerUtils.getDefault().getRunning(currentJavaProject.getProject())) {
            DerbyServerUtils.getDefault().stopDerbyServer(currentJavaProject.getProject());
        }
        IClasspathEntry[] rawClasspath = currentJavaProject.getRawClasspath();

        List<IClasspathEntry> newEntries = new ArrayList<IClasspathEntry>();
        for (IClasspathEntry e : rawClasspath) {
            if (e.getEntryKind() != IClasspathEntry.CPE_CONTAINER) {
                newEntries.add(e);
            } else if (!e.getPath().equals(DerbyClasspathContainer.CONTAINER_ID)) {
                newEntries.add(e);
            }
        }

        IClasspathEntry[] newEntriesArray = new IClasspathEntry[newEntries.size()];
        newEntriesArray = (IClasspathEntry[]) newEntries.toArray(newEntriesArray);
        currentJavaProject.setRawClasspath(newEntriesArray, null);

        IProjectDescription description = currentJavaProject.getProject().getDescription();
        String[] natures = description.getNatureIds();

        description.setNatureIds(removeDerbyNature(natures));
        currentJavaProject.getProject().setDescription(description, null);
        // refresh project so user sees changes
        currentJavaProject.getProject().refreshLocal(IResource.DEPTH_INFINITE, null);
        ((ApplicationWindow) window).setStatus(Messages.DERBY_NATURE_REMOVED);
    } catch (Exception e) {
        Logger.log(
                Messages.ERROR_REMOVING_NATURE + " '" + currentJavaProject.getProject().getName() + "': " + e,
                IStatus.ERROR);

        Shell shell = new Shell();
        MessageDialog.openInformation(shell, CommonNames.PLUGIN_NAME,
                Messages.ERROR_REMOVING_NATURE + ":\n" + SelectionUtil.getStatusMessages(e));
    }

}