Example usage for org.eclipse.jdt.internal.core ClasspathEntry TAG_CLASSPATHENTRY

List of usage examples for org.eclipse.jdt.internal.core ClasspathEntry TAG_CLASSPATHENTRY

Introduction

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

Prototype

String TAG_CLASSPATHENTRY

To view the source code for org.eclipse.jdt.internal.core ClasspathEntry TAG_CLASSPATHENTRY.

Click Source Link

Usage

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

License:Apache License

/**
 * 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.
 *
 * @param projectName/*from   ww  w . ja  v  a  2 s  .c om*/
 *            - the name of project containing the .classpath file
 * @param projectRootAbsoluteFullPath
 *            - the path to project containing the .classpath file
 * @param xmlClasspath
 *            - path to the XML
 * @param unknownElements
 *            - map of unknow elements
 * @return the set of CLasspath ENtries extracted from the .classpath
 * @throws IOException
 *             - exception during parsing of .classpath
 * @throws ClasspathEntry.AssertionFailedException
 *             - exception during parsing of .classpath
 */
@SuppressWarnings("checkstyle:npathcomplexity")
public static IClasspathEntry[][] decodeClasspath(String projectName, IPath projectRootAbsoluteFullPath,
        String xmlClasspath, Map<IPath, UnknownXmlElements> unknownElements)
        throws IOException, ClasspathEntry.AssertionFailedException {

    final List<IClasspathEntry> paths = new ArrayList<>();
    IClasspathEntry defaultOutput = null;
    final Element cpElement;

    try (StringReader reader = new StringReader(xmlClasspath);) {
        final 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);
    }

    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) {
        final Node node = list.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            final IClasspathEntry entry = elementDecode((Element) node, projectName,
                    projectRootAbsoluteFullPath, unknownElements);
            if (entry != null) {
                if (entry.getContentKind() == ClasspathEntry.K_OUTPUT) {
                    // separate output
                    defaultOutput = entry;
                } else {
                    paths.add(entry);
                }
            }
        }
    }
    final int pathSize = paths.size();
    final IClasspathEntry[][] entries = new IClasspathEntry[2][];
    entries[0] = new IClasspathEntry[pathSize + (defaultOutput == null ? 0 : 1)];
    paths.toArray(entries[0]);
    if (defaultOutput != null) {
        // ensure output is last item
        entries[0][pathSize] = defaultOutput;
    }
    paths.clear();
    list = cpElement.getElementsByTagName(ClasspathEntry.TAG_REFERENCED_ENTRY);
    length = list.getLength();

    for (int i = 0; i < length; ++i) {
        final Node node = list.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            final IClasspathEntry entry = elementDecode((Element) node, projectName,
                    projectRootAbsoluteFullPath, unknownElements);
            if (entry != null) {
                paths.add(entry);
            }
        }
    }
    entries[1] = new IClasspathEntry[paths.size()];
    paths.toArray(entries[1]);

    return entries;
}

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

License:Open Source License

/**
 * 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. 
 * //w ww  .j  a va  2 s  .co m
 */
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) {
                    defaultOutput = entry; // separate output
                } 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]);
    if (defaultOutput != null)
        entries[0][pathSize] = defaultOutput; // ensure output is last item

    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;
}

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

License:Open Source License

public IClasspathEntry decodeClasspathEntry(String encodedEntry) {

    try {//w ww  .  j a v a  2 s.  c  o m
        if (encodedEntry == null)
            return null;
        StringReader reader = new StringReader(encodedEntry);
        Element node;

        try {
            DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            node = parser.parse(new InputSource(reader)).getDocumentElement();
        } catch (SAXException e) {
            return null;
        } catch (ParserConfigurationException e) {
            return null;
        } finally {
            reader.close();
        }

        if (!node.getNodeName().equalsIgnoreCase(ClasspathEntry.TAG_CLASSPATHENTRY)
                || node.getNodeType() != Node.ELEMENT_NODE) {
            return null;
        }
        return ClasspathEntry.elementDecode(node, this, null/*not interested in unknown elements*/);
    } catch (IOException e) {
        // bad format
        return null;
    }
}

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

License:Open Source License

/**
 * Returns the XML String encoding of the class path.
 */// www . j  av a2  s.  c o m
protected String encodeClasspath(IClasspathEntry[] classpath, IClasspathEntry[] referencedEntries,
        IPath outputLocation, boolean indent, Map unknownElements) throws JavaModelException {
    try {
        ByteArrayOutputStream s = new ByteArrayOutputStream();
        OutputStreamWriter writer = new OutputStreamWriter(s, "UTF8"); //$NON-NLS-1$
        XMLWriter xmlWriter = new XMLWriter(writer, this, true/*print XML version*/);

        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/*insert new line*/);
        writer.flush();
        writer.close();
        return s.toString("UTF8");//$NON-NLS-1$
    } catch (IOException e) {
        throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
    }
}