Search in sources :

Example 86 with IClasspathEntry

use of org.eclipse.jdt.core.IClasspathEntry in project che by eclipse.

the class JavaProject method encodeClasspathEntry.

public String encodeClasspathEntry(IClasspathEntry classpathEntry) {
    try {
        ByteArrayOutputStream s = new ByteArrayOutputStream();
        //$NON-NLS-1$
        OutputStreamWriter writer = new OutputStreamWriter(s, "UTF8");
        XMLWriter xmlWriter = new XMLWriter(writer, this, false);
        ((ClasspathEntry) classpathEntry).elementEncode(xmlWriter, this.project.getFullPath(), true, /*indent*/
        true, /*insert new line*/
        null, /*not interested in unknown elements*/
        (classpathEntry.getReferencingEntry() != null));
        writer.flush();
        writer.close();
        //$NON-NLS-1$
        return s.toString("UTF8");
    } catch (IOException e) {
        // never happens since all is done in memory
        return null;
    }
}
Also used : OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry)

Example 87 with IClasspathEntry

use of org.eclipse.jdt.core.IClasspathEntry in project che by eclipse.

the class JavaProject method readFileEntriesWithException.

/**
    * 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.
            * 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(org.eclipse.jdt.internal.core.JavaProject.CLASSPATH_FILENAME);
    byte[] bytes;
    if (rscFile.exists()) {
        bytes = Util.getResourceContentsAsByteArray(rscFile);
    } else {
        //                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 {
        // .classpath always encoded with UTF-8
        xmlClasspath = new String(bytes, org.eclipse.jdt.internal.compiler.util.Util.UTF_8);
    } catch (UnsupportedEncodingException e) {
        //$NON-NLS-1$
        Util.log(e, "Could not read .classpath with UTF-8 encoding");
        // fallback to default
        xmlClasspath = new String(bytes);
    }
    return decodeClasspath(xmlClasspath, unknownElements);
}
Also used : IFile(org.eclipse.core.resources.IFile) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 88 with IClasspathEntry

use of org.eclipse.jdt.core.IClasspathEntry in project che by eclipse.

the class JavaProject method decodeClasspath.

/**
     * Reads and decode an XML classpath string. 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 and the second element is an array of referenced entries that may have been stored
     * by the client earlier. See {@link IJavaProject#getReferencedClasspathEntries()} for more details.
     *
     */
public IClasspathEntry[][] decodeClasspath(String xmlClasspath, Map unknownElements) throws IOException, ClasspathEntry.AssertionFailedException {
    ArrayList paths = new ArrayList();
    IClasspathEntry defaultOutput = null;
    StringReader reader = new StringReader(xmlClasspath);
    Element cpElement;
    try {
        DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        cpElement = parser.parse(new InputSource(reader)).getDocumentElement();
    } catch (SAXException e) {
        throw new IOException(Messages.file_badFormat);
    } catch (ParserConfigurationException e) {
        throw new IOException(Messages.file_badFormat);
    } finally {
        reader.close();
    }
    if (!cpElement.getNodeName().equalsIgnoreCase("classpath")) {
        //$NON-NLS-1$
        throw new IOException(Messages.file_badFormat);
    }
    NodeList list = cpElement.getElementsByTagName(ClasspathEntry.TAG_CLASSPATHENTRY);
    int length = list.getLength();
    for (int i = 0; i < length; ++i) {
        Node node = list.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            IClasspathEntry entry = ClasspathEntry.elementDecode((Element) node, this, unknownElements);
            if (entry != null) {
                if (entry.getContentKind() == ClasspathEntry.K_OUTPUT) {
                    // separate output
                    defaultOutput = entry;
                } else {
                    paths.add(entry);
                }
            }
        }
    }
    int pathSize = paths.size();
    IClasspathEntry[][] entries = new IClasspathEntry[2][];
    entries[0] = new IClasspathEntry[pathSize + (defaultOutput == null ? 0 : 1)];
    paths.toArray(entries[0]);
    // ensure output is last item
    if (defaultOutput != null)
        entries[0][pathSize] = defaultOutput;
    paths.clear();
    list = cpElement.getElementsByTagName(ClasspathEntry.TAG_REFERENCED_ENTRY);
    length = list.getLength();
    for (int i = 0; i < length; ++i) {
        Node node = list.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            IClasspathEntry entry = ClasspathEntry.elementDecode((Element) node, this, unknownElements);
            if (entry != null) {
                paths.add(entry);
            }
        }
    }
    entries[1] = new IClasspathEntry[paths.size()];
    paths.toArray(entries[1]);
    return entries;
}
Also used : InputSource(org.xml.sax.InputSource) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) Element(org.w3c.dom.Element) IJavaElement(org.eclipse.jdt.core.IJavaElement) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) StringReader(java.io.StringReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 89 with IClasspathEntry

use of org.eclipse.jdt.core.IClasspathEntry in project che by eclipse.

the class JavaProject method encodeClasspath.

/**
     * Returns the XML String encoding of the class path.
     */
protected String encodeClasspath(IClasspathEntry[] classpath, IClasspathEntry[] referencedEntries, IPath outputLocation, boolean indent, Map unknownElements) throws JavaModelException {
    try {
        ByteArrayOutputStream s = new ByteArrayOutputStream();
        //$NON-NLS-1$
        OutputStreamWriter writer = new OutputStreamWriter(s, "UTF8");
        XMLWriter xmlWriter = new XMLWriter(writer, this, true);
        xmlWriter.startTag(ClasspathEntry.TAG_CLASSPATH, indent);
        for (int i = 0; i < classpath.length; ++i) {
            ((ClasspathEntry) classpath[i]).elementEncode(xmlWriter, this.project.getFullPath(), indent, true, unknownElements, false);
        }
        if (outputLocation != null) {
            outputLocation = outputLocation.removeFirstSegments(1);
            outputLocation = outputLocation.makeRelative();
            HashMap parameters = new HashMap();
            parameters.put(ClasspathEntry.TAG_KIND, ClasspathEntry.kindToString(ClasspathEntry.K_OUTPUT));
            parameters.put(ClasspathEntry.TAG_PATH, String.valueOf(outputLocation));
            xmlWriter.printTag(ClasspathEntry.TAG_CLASSPATHENTRY, parameters, indent, true, true);
        }
        if (referencedEntries != null) {
            for (int i = 0; i < referencedEntries.length; ++i) {
                ((ClasspathEntry) referencedEntries[i]).elementEncode(xmlWriter, this.project.getFullPath(), indent, true, unknownElements, true);
            }
        }
        xmlWriter.endTag(ClasspathEntry.TAG_CLASSPATH, indent, true);
        writer.flush();
        writer.close();
        //$NON-NLS-1$
        return s.toString("UTF8");
    } catch (IOException e) {
        throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
    }
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) HashMap(java.util.HashMap) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry)

Example 90 with IClasspathEntry

use of org.eclipse.jdt.core.IClasspathEntry in project che by eclipse.

the class JavaProject method projectPrerequisites.

public String[] projectPrerequisites(IClasspathEntry[] resolvedClasspath) throws JavaModelException {
    ArrayList prerequisites = new ArrayList();
    for (int i = 0, length = resolvedClasspath.length; i < length; i++) {
        IClasspathEntry entry = resolvedClasspath[i];
        if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
            prerequisites.add(entry.getPath().lastSegment());
        }
    }
    int size = prerequisites.size();
    if (size == 0) {
        return NO_PREREQUISITES;
    } else {
        String[] result = new String[size];
        prerequisites.toArray(result);
        return result;
    }
}
Also used : IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) ArrayList(java.util.ArrayList)

Aggregations

IClasspathEntry (org.eclipse.jdt.core.IClasspathEntry)123 IPath (org.eclipse.core.runtime.IPath)55 IJavaProject (org.eclipse.jdt.core.IJavaProject)44 ArrayList (java.util.ArrayList)35 Path (org.eclipse.core.runtime.Path)27 JavaModelException (org.eclipse.jdt.core.JavaModelException)23 IProject (org.eclipse.core.resources.IProject)20 IClasspathAttribute (org.eclipse.jdt.core.IClasspathAttribute)16 File (java.io.File)15 IFolder (org.eclipse.core.resources.IFolder)13 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)13 CoreException (org.eclipse.core.runtime.CoreException)12 IJavaElement (org.eclipse.jdt.core.IJavaElement)12 JavaProject (org.eclipse.jdt.internal.core.JavaProject)11 IFile (org.eclipse.core.resources.IFile)10 IResource (org.eclipse.core.resources.IResource)10 HashMap (java.util.HashMap)9 Test (org.testng.annotations.Test)8 IOException (java.io.IOException)7 URL (java.net.URL)7