Search in sources :

Example 96 with NodeList

use of org.w3c.dom.NodeList in project che by eclipse.

the class Launching method restoreLibraryInfo.

/**
     * Restores library information for VMs
     */
private static void restoreLibraryInfo() {
    fgLibraryInfoMap = new HashMap<String, LibraryInfo>(10);
    IPath libPath = getDefault().getStateLocation();
    //$NON-NLS-1$
    libPath = libPath.append("libraryInfos.xml");
    File file = libPath.toFile();
    if (file.exists()) {
        try {
            InputStream stream = new BufferedInputStream(new FileInputStream(file));
            DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            parser.setErrorHandler(new DefaultHandler());
            Element root = parser.parse(new InputSource(stream)).getDocumentElement();
            if (!root.getNodeName().equals("libraryInfos")) {
                //$NON-NLS-1$
                return;
            }
            NodeList list = root.getChildNodes();
            int length = list.getLength();
            for (int i = 0; i < length; ++i) {
                Node node = list.item(i);
                short type = node.getNodeType();
                if (type == Node.ELEMENT_NODE) {
                    Element element = (Element) node;
                    String nodeName = element.getNodeName();
                    if (nodeName.equalsIgnoreCase("libraryInfo")) {
                        //$NON-NLS-1$
                        //$NON-NLS-1$
                        String version = element.getAttribute("version");
                        //$NON-NLS-1$
                        String location = element.getAttribute("home");
                        //$NON-NLS-1$
                        String[] bootpath = getPathsFromXML(element, "bootpath");
                        //$NON-NLS-1$
                        String[] extDirs = getPathsFromXML(element, "extensionDirs");
                        //$NON-NLS-1$
                        String[] endDirs = getPathsFromXML(element, "endorsedDirs");
                        if (location != null) {
                            LibraryInfo info = new LibraryInfo(version, bootpath, extDirs, endDirs);
                            fgLibraryInfoMap.put(location, info);
                        }
                    }
                }
            }
        } catch (IOException | SAXException | ParserConfigurationException e) {
            log(e);
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) IPath(org.eclipse.core.runtime.IPath) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) DefaultHandler(org.xml.sax.helpers.DefaultHandler) SAXException(org.xml.sax.SAXException) BufferedInputStream(java.io.BufferedInputStream) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) File(java.io.File)

Example 97 with NodeList

use of org.w3c.dom.NodeList in project che by eclipse.

the class Launching method readInstallInfo.

/**
     * Reads the file of saved time stamps and populates the {@link #fgInstallTimeMap}.
     * See https://bugs.eclipse.org/bugs/show_bug.cgi?id=266651 for more information
     *
     * @since 3.7
     */
private static void readInstallInfo() {
    fgInstallTimeMap = new HashMap<String, Long>();
    IPath libPath = getDefault().getStateLocation();
    //$NON-NLS-1$
    libPath = libPath.append(".install.xml");
    File file = libPath.toFile();
    if (file.exists()) {
        try {
            InputStream stream = new BufferedInputStream(new FileInputStream(file));
            DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            parser.setErrorHandler(new DefaultHandler());
            Element root = parser.parse(new InputSource(stream)).getDocumentElement();
            if (root.getNodeName().equalsIgnoreCase("dirs")) {
                //$NON-NLS-1$
                NodeList nodes = root.getChildNodes();
                Node node = null;
                Element element = null;
                for (int i = 0; i < nodes.getLength(); i++) {
                    node = nodes.item(i);
                    if (node.getNodeType() == Node.ELEMENT_NODE) {
                        element = (Element) node;
                        if (element.getNodeName().equalsIgnoreCase("entry")) {
                            //$NON-NLS-1$
                            //$NON-NLS-1$
                            String loc = element.getAttribute("loc");
                            //$NON-NLS-1$
                            String stamp = element.getAttribute("stamp");
                            try {
                                Long l = new Long(stamp);
                                fgInstallTimeMap.put(loc, l);
                            } catch (NumberFormatException nfe) {
                            //do nothing
                            }
                        }
                    }
                }
            }
        } catch (IOException e) {
            log(e);
        } catch (ParserConfigurationException e) {
            log(e);
        } catch (SAXException e) {
            log(e);
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) IPath(org.eclipse.core.runtime.IPath) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) DefaultHandler(org.xml.sax.helpers.DefaultHandler) SAXException(org.xml.sax.SAXException) BufferedInputStream(java.io.BufferedInputStream) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) File(java.io.File)

Example 98 with NodeList

use of org.w3c.dom.NodeList in project buck by facebook.

the class CompileStringsStep method compileStringFiles.

private StringResources compileStringFiles(ProjectFilesystem filesystem, Collection<Path> filepaths) throws IOException, SAXException {
    TreeMap<Integer, EnumMap<Gender, String>> stringsMap = Maps.newTreeMap();
    TreeMap<Integer, EnumMap<Gender, ImmutableMap<String, String>>> pluralsMap = Maps.newTreeMap();
    TreeMap<Integer, EnumMap<Gender, ImmutableList<String>>> arraysMap = Maps.newTreeMap();
    for (Path stringFilePath : filepaths) {
        Document dom = XmlDomParser.parse(filesystem.getPathForRelativePath(stringFilePath));
        NodeList stringNodes = dom.getElementsByTagName("string");
        scrapeStringNodes(stringNodes, stringsMap);
        NodeList pluralNodes = dom.getElementsByTagName("plurals");
        scrapePluralsNodes(pluralNodes, pluralsMap);
        NodeList arrayNodes = dom.getElementsByTagName("string-array");
        scrapeStringArrayNodes(arrayNodes, arraysMap);
    }
    return new StringResources(stringsMap, pluralsMap, arraysMap);
}
Also used : Path(java.nio.file.Path) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) EnumMap(java.util.EnumMap)

Example 99 with NodeList

use of org.w3c.dom.NodeList in project buck by facebook.

the class MiniAapt method processXmlFile.

@VisibleForTesting
void processXmlFile(ProjectFilesystem filesystem, Path xmlFile, ImmutableSet.Builder<RDotTxtEntry> references) throws IOException, XPathExpressionException, ResourceParseException {
    try (InputStream stream = filesystem.newFileInputStream(xmlFile)) {
        Document dom = parseXml(xmlFile, stream);
        NodeList nodesWithIds = (NodeList) ANDROID_ID_DEFINITION.evaluate(dom, XPathConstants.NODESET);
        for (int i = 0; i < nodesWithIds.getLength(); i++) {
            String resourceName = nodesWithIds.item(i).getNodeValue();
            if (!resourceName.startsWith(ID_DEFINITION_PREFIX)) {
                throw new ResourceParseException("Invalid definition of a resource: '%s'", resourceName);
            }
            Preconditions.checkState(resourceName.startsWith(ID_DEFINITION_PREFIX));
            resourceCollector.addIntResourceIfNotPresent(RType.ID, resourceName.substring(ID_DEFINITION_PREFIX.length()));
        }
        NodeList nodesUsingIds = (NodeList) ANDROID_ID_USAGE.evaluate(dom, XPathConstants.NODESET);
        for (int i = 0; i < nodesUsingIds.getLength(); i++) {
            String resourceName = nodesUsingIds.item(i).getNodeValue();
            int slashPosition = resourceName.indexOf('/');
            if (resourceName.charAt(0) != '@' || slashPosition == -1) {
                throw new ResourceParseException("Invalid definition of a resource: '%s'", resourceName);
            }
            String rawRType = resourceName.substring(1, slashPosition);
            String name = resourceName.substring(slashPosition + 1);
            String nodeName = nodesUsingIds.item(i).getNodeName();
            if (name.startsWith("android:") || nodeName.startsWith("tools:")) {
                continue;
            }
            if (!RESOURCE_TYPES.containsKey(rawRType)) {
                throw new ResourceParseException("Invalid reference '%s' in '%s'", resourceName, xmlFile);
            }
            RType rType = Preconditions.checkNotNull(RESOURCE_TYPES.get(rawRType));
            references.add(new FakeRDotTxtEntry(IdType.INT, rType, sanitizeName(name)));
        }
    }
}
Also used : InputStream(java.io.InputStream) NodeList(org.w3c.dom.NodeList) RType(com.facebook.buck.android.aapt.RDotTxtEntry.RType) Document(org.w3c.dom.Document) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 100 with NodeList

use of org.w3c.dom.NodeList in project buck by facebook.

the class XmlTestResultParser method doParse.

private static TestCaseSummary doParse(String xml) throws IOException, SAXException {
    Document doc = XmlDomParser.parse(new InputSource(new StringReader(xml)), /* namespaceAware */
    true);
    Element root = doc.getDocumentElement();
    Preconditions.checkState("testcase".equals(root.getTagName()));
    String testCaseName = root.getAttribute("name");
    NodeList testElements = doc.getElementsByTagName("test");
    List<TestResultSummary> testResults = Lists.newArrayListWithCapacity(testElements.getLength());
    for (int i = 0; i < testElements.getLength(); i++) {
        Element node = (Element) testElements.item(i);
        String testName = node.getAttribute("name");
        long time = Long.parseLong(node.getAttribute("time"));
        String typeString = node.getAttribute("type");
        ResultType type = ResultType.valueOf(typeString);
        String message;
        String stacktrace;
        if (type == ResultType.SUCCESS) {
            message = null;
            stacktrace = null;
        } else {
            message = node.getAttribute("message");
            stacktrace = node.getAttribute("stacktrace");
        }
        NodeList stdoutElements = node.getElementsByTagName("stdout");
        String stdOut;
        if (stdoutElements.getLength() == 1) {
            stdOut = stdoutElements.item(0).getTextContent();
        } else {
            stdOut = null;
        }
        NodeList stderrElements = node.getElementsByTagName("stderr");
        String stdErr;
        if (stderrElements.getLength() == 1) {
            stdErr = stderrElements.item(0).getTextContent();
        } else {
            stdErr = null;
        }
        TestResultSummary testResult = new TestResultSummary(testCaseName, testName, type, time, message, stacktrace, stdOut, stdErr);
        testResults.add(testResult);
    }
    return new TestCaseSummary(testCaseName, testResults);
}
Also used : InputSource(org.xml.sax.InputSource) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) StringReader(java.io.StringReader) ResultType(com.facebook.buck.test.result.type.ResultType) Document(org.w3c.dom.Document)

Aggregations

NodeList (org.w3c.dom.NodeList)1806 Node (org.w3c.dom.Node)1059 Element (org.w3c.dom.Element)902 Document (org.w3c.dom.Document)636 ArrayList (java.util.ArrayList)314 DocumentBuilder (javax.xml.parsers.DocumentBuilder)268 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)208 IOException (java.io.IOException)183 NamedNodeMap (org.w3c.dom.NamedNodeMap)144 InputSource (org.xml.sax.InputSource)131 HashMap (java.util.HashMap)121 Test (org.junit.Test)117 SAXException (org.xml.sax.SAXException)117 StringReader (java.io.StringReader)106 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)100 XPath (javax.xml.xpath.XPath)99 Attr (org.w3c.dom.Attr)80 XPathExpressionException (javax.xml.xpath.XPathExpressionException)76 File (java.io.File)64 HashSet (java.util.HashSet)59