Example usage for org.eclipse.jdt.internal.core JavaProject CLASSPATH_FILENAME

List of usage examples for org.eclipse.jdt.internal.core JavaProject CLASSPATH_FILENAME

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.core JavaProject CLASSPATH_FILENAME.

Prototype

String CLASSPATH_FILENAME

To view the source code for org.eclipse.jdt.internal.core JavaProject CLASSPATH_FILENAME.

Click Source Link

Document

Name of file containing project classpath

Usage

From source file:io.sarl.eclipse.util.JavaClasspathParser.java

License:Apache License

/**
 * Reads entry of a .classpath file./*ww w.j  a v a  2s.  c  o  m*/
 *
 * @param projectName
 *            - the name of project containing the .classpath file
 * @param projectRootAbsoluteFullPath
 *            - the path to project containing the .classpath file
 * @param unknownElements
 *            - map of unknow elements
 * @return the set of CLasspath Entries extracted from the .classpath
 * @throws CoreException
 *             - exception during parsing of .classpath
 * @throws IOException
 *             - exception during parsing of .classpath
 * @throws ClasspathEntry.AssertionFailedException
 *             - exception during parsing of .classpath
 * @throws URISyntaxException
 *             - exception during parsing of .classpath
 */
@SuppressWarnings("checkstyle:innerassignment")
public static IClasspathEntry[][] readFileEntriesWithException(String projectName,
        URL projectRootAbsoluteFullPath, Map<IPath, UnknownXmlElements> unknownElements)
        throws CoreException, IOException, ClasspathEntry.AssertionFailedException, URISyntaxException {

    final URL rscFile = new URL(
            projectRootAbsoluteFullPath.toExternalForm().concat(JavaProject.CLASSPATH_FILENAME));
    byte[] bytes;

    // when a project is imported, we get a first delta for the addition of the .project, but the .classpath is not accessible
    // so default to using java.io.File
    // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=96258
    final URI location;
    try {
        location = rscFile.toURI();
    } catch (URISyntaxException e) {
        throw e;
    }
    if (location == null) {
        throw new IOException("Cannot obtain a location URI for " + rscFile); //$NON-NLS-1$
    }
    final File file = Util.toLocalFile(location, null/* no progress monitor available */);
    if (file == null) {
        throw new IOException("Unable to fetch file from " + location); //$NON-NLS-1$
    }

    try {
        bytes = org.eclipse.jdt.internal.compiler.util.Util.getFileByteContent(file);
    } catch (IOException e) {
        throw e;
    }

    if (hasUTF8BOM(bytes)) {
        // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=240034
        final int length = bytes.length - IContentDescription.BOM_UTF_8.length;
        System.arraycopy(bytes, IContentDescription.BOM_UTF_8.length, bytes = new byte[length], 0, length);
    }
    String xmlClasspath;
    try {
        // .classpath always encoded with UTF-8
        xmlClasspath = new String(bytes, org.eclipse.jdt.internal.compiler.util.Util.UTF_8);
    } catch (UnsupportedEncodingException e) {
        Util.log(e, "Could not read .classpath with UTF-8 encoding"); //$NON-NLS-1$
        // fallback to default
        xmlClasspath = new String(bytes);
    }
    return decodeClasspath(projectName, Path.fromPortableString(projectRootAbsoluteFullPath.getPath()),
            xmlClasspath, unknownElements);
}

From source file:org.eclipse.jdt.internal.core.JavaProject.java

License:Open Source License

/**
 * Reads the classpath file entries of this project's .classpath file.
 * Returns a two-dimensional array, where the number of elements in the row is fixed to 2.
 * The first element is an array of raw classpath entries, which includes the output entry,
 * and the second element is an array of referenced entries that may have been stored 
 * by the client earlier. /*from  www.  j  av  a2  s.c  o  m*/
 * See {@link IJavaProject#getReferencedClasspathEntries()} for more details.
 * As a side effect, unknown elements are stored in the given map (if not null)
 * Throws exceptions if the file cannot be accessed or is malformed.
 */
public IClasspathEntry[][] readFileEntriesWithException(Map unknownElements)
        throws CoreException, IOException, ClasspathEntry.AssertionFailedException {
    IFile rscFile = this.project.getFile(JavaProject.CLASSPATH_FILENAME);
    byte[] bytes;
    if (rscFile.exists()) {
        bytes = Util.getResourceContentsAsByteArray(rscFile);
    } else {
        // when a project is imported, we get a first delta for the addition of the .project, but the .classpath is not accessible
        // so default to using java.io.File
        // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=96258
        URI location = rscFile.getLocationURI();
        if (location == null)
            throw new IOException("Cannot obtain a location URI for " + rscFile); //$NON-NLS-1$
        File file = Util.toLocalFile(location, null/*no progress monitor available*/);
        if (file == null)
            throw new IOException("Unable to fetch file from " + location); //$NON-NLS-1$
        try {
            bytes = org.eclipse.jdt.internal.compiler.util.Util.getFileByteContent(file);
        } catch (IOException e) {
            if (!file.exists())
                return new IClasspathEntry[][] { defaultClasspath(), ClasspathEntry.NO_ENTRIES };
            throw e;
        }
    }
    if (hasUTF8BOM(bytes)) { // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=240034
        int length = bytes.length - IContentDescription.BOM_UTF_8.length;
        System.arraycopy(bytes, IContentDescription.BOM_UTF_8.length, bytes = new byte[length], 0, length);
    }
    String xmlClasspath;
    try {
        xmlClasspath = new String(bytes, org.eclipse.jdt.internal.compiler.util.Util.UTF_8); // .classpath always encoded with UTF-8
    } catch (UnsupportedEncodingException e) {
        Util.log(e, "Could not read .classpath with UTF-8 encoding"); //$NON-NLS-1$
        // fallback to default
        xmlClasspath = new String(bytes);
    }
    return decodeClasspath(xmlClasspath, unknownElements);
}

From source file:org.eclipse.jdt.internal.core.JavaProject.java

License:Open Source License

private IClasspathEntry[][] readFileEntries(Map unkwownElements) {
    try {/*from   w ww  .  java 2 s .  co  m*/
        return readFileEntriesWithException(unkwownElements);
    } catch (CoreException e) {
        Util.log(e, "Exception while reading " + getPath().append(JavaProject.CLASSPATH_FILENAME)); //$NON-NLS-1$
        return new IClasspathEntry[][] { JavaProject.INVALID_CLASSPATH, ClasspathEntry.NO_ENTRIES };
    } catch (IOException e) {
        Util.log(e, "Exception while reading " + getPath().append(JavaProject.CLASSPATH_FILENAME)); //$NON-NLS-1$
        return new IClasspathEntry[][] { JavaProject.INVALID_CLASSPATH, ClasspathEntry.NO_ENTRIES };
    } catch (ClasspathEntry.AssertionFailedException e) {
        Util.log(e, "Exception while reading " + getPath().append(JavaProject.CLASSPATH_FILENAME)); //$NON-NLS-1$
        return new IClasspathEntry[][] { JavaProject.INVALID_CLASSPATH, ClasspathEntry.NO_ENTRIES };
    }
}

From source file:org.eclipse.jdt.internal.core.JavaProject.java

License:Open Source License

/**
 * Writes the classpath in a sharable format (VCM-wise) only when necessary, that is, if  it is semantically different
 * from the existing one in file. Will never write an identical one.
 *
 * @param newClasspath IClasspathEntry[]
 * @param newOutputLocation IPath//from  w  w w  .  j a  va 2s .c o m
 * @return boolean Return whether the .classpath file was modified.
 * @throws JavaModelException
 */
public boolean writeFileEntries(IClasspathEntry[] newClasspath, IClasspathEntry[] referencedEntries,
        IPath newOutputLocation) throws JavaModelException {

    if (!this.project.isAccessible())
        return false;

    Map unknownElements = new HashMap();
    IClasspathEntry[][] fileEntries = readFileEntries(unknownElements);
    if (fileEntries[0] != JavaProject.INVALID_CLASSPATH
            && areClasspathsEqual(newClasspath, newOutputLocation, fileEntries[0])
            && (referencedEntries == null || areClasspathsEqual(referencedEntries, fileEntries[1]))) {
        // no need to save it, it is the same
        return false;
    }

    // actual file saving
    try {
        setSharedProperty(JavaProject.CLASSPATH_FILENAME,
                encodeClasspath(newClasspath, referencedEntries, newOutputLocation, true, unknownElements));
        return true;
    } catch (CoreException e) {
        throw new JavaModelException(e);
    }
}

From source file:org.eclipse.jst.common.internal.modulecore.AddMappedOutputFoldersParticipant.java

License:Open Source License

public final static HashMap<IContainer, IPath> getMappedJavaOutputContainers(IProject project) {
    ComponentResourceProxy[] proxies = findAllMappingProxies(project);
    IJavaProjectLite javaProjectLite = JavaCoreLite.create(project);

    HashMap<IContainer, IPath> map = new HashMap<IContainer, IPath>();
    IFile classpathFile = project.getFile(JavaProject.CLASSPATH_FILENAME);
    if (javaProjectLite.exists() && classpathFile.exists()) {
        IClasspathEntry[] entries = javaProjectLite.readRawClasspath();
        for (IClasspathEntry entry : entries) {
            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                IPath cpePath = entry.getPath();
                for (int i = 0; i < proxies.length; i++) {
                    if (cpePath.equals(new Path(project.getName()).append(proxies[i].source).makeAbsolute())) {
                        IContainer outputContainer = JavaLiteUtilities.getJavaOutputContainer(javaProjectLite,
                                entry);/*from www  .j a  v  a  2  s  .co  m*/
                        if (!map.containsKey(outputContainer)) {
                            map.put(outputContainer, proxies[i].runtimePath);
                        }
                    }
                    // TODO 
                }
            }
        }
    }
    return map;
}