List of usage examples for org.eclipse.jdt.internal.core ClasspathEntry elementDecode
public static IClasspathEntry elementDecode(Element element, IJavaProject project, Map unknownElements)
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. * //from www .j a v a 2 s .c om */ 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 av a 2s . 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; } }