Search in sources :

Example 16 with Element

use of org.w3c.dom.Element in project cucumber-jvm by cucumber.

the class JUnitFormatter method addDummyTestCase.

private void addDummyTestCase() {
    Element dummy = doc.createElement("testcase");
    dummy.setAttribute("classname", "dummy");
    dummy.setAttribute("name", "dummy");
    rootElement.appendChild(dummy);
    Element skipped = doc.createElement("skipped");
    skipped.setAttribute("message", "No features found");
    dummy.appendChild(skipped);
}
Also used : Element(org.w3c.dom.Element)

Example 17 with Element

use of org.w3c.dom.Element in project lucida by claritylab.

the class RuleBasedQuestionClassifier method loadRulesFile.

private void loadRulesFile(String fileName) {
    // PARSE XML RULES FILE 
    log.debug("Parsing xml rules file");
    Document rulesDocument;
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(false);
        factory.setIgnoringComments(true);
        factory.setIgnoringElementContentWhitespace(true);
        factory.setNamespaceAware(true);
        DocumentBuilder db = factory.newDocumentBuilder();
        rulesDocument = db.parse(fileName);
    } catch (Exception e) {
        throw new RuntimeException("Failed to parse XML patterns file", e);
    }
    // EXTRACT RULE DATA.
    log.debug("Loading rules");
    NodeList ruleList = rulesDocument.getElementsByTagName("RULE");
    for (int i = 0; i < ruleList.getLength(); i++) {
        Node rule = ruleList.item(i);
        if (!rule.getNodeName().equals("RULE") || rule.getNodeType() != Node.ELEMENT_NODE)
            continue;
        rules.add(new Rule((Element) rule));
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document)

Example 18 with Element

use of org.w3c.dom.Element in project checkstyle by checkstyle.

the class CheckUtil method getCheckStyleModulesReferencedInConfig.

/**
     * Gets a set of names of checkstyle's checks which are referenced in checkstyle_checks.xml.
     *
     * @param configFilePath
     *            file path of checkstyle_checks.xml.
     * @return names of checkstyle's checks which are referenced in checkstyle_checks.xml.
     */
private static Set<String> getCheckStyleModulesReferencedInConfig(String configFilePath) {
    try {
        final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        // Validations of XML file make parsing too slow, that is why we
        // disable all validations.
        factory.setNamespaceAware(false);
        factory.setValidating(false);
        factory.setFeature("http://xml.org/sax/features/namespaces", false);
        factory.setFeature("http://xml.org/sax/features/validation", false);
        factory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
        factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        final DocumentBuilder builder = factory.newDocumentBuilder();
        final Document document = builder.parse(new File(configFilePath));
        // optional, but recommended
        // FYI:
        // http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-
        // how-does-it-work
        document.getDocumentElement().normalize();
        final NodeList nodeList = document.getElementsByTagName("module");
        final Set<String> checksReferencedInCheckstyleChecksXml = new HashSet<>();
        for (int i = 0; i < nodeList.getLength(); i++) {
            final Node currentNode = nodeList.item(i);
            if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
                final Element module = (Element) currentNode;
                final String checkName = module.getAttribute("name");
                checksReferencedInCheckstyleChecksXml.add(checkName);
            }
        }
        return checksReferencedInCheckstyleChecksXml;
    } catch (Exception exception) {
        throw new IllegalStateException(exception);
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) IOException(java.io.IOException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) File(java.io.File) HashSet(java.util.HashSet)

Example 19 with Element

use of org.w3c.dom.Element in project cw-omnibus by commonsguy.

the class WeatherFragment method buildForecasts.

private ArrayList<Forecast> buildForecasts(String raw) throws Exception {
    ArrayList<Forecast> forecasts = new ArrayList<Forecast>();
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = builder.parse(new InputSource(new StringReader(raw)));
    NodeList times = doc.getElementsByTagName("start-valid-time");
    for (int i = 0; i < times.getLength(); i++) {
        Element time = (Element) times.item(i);
        Forecast forecast = new Forecast();
        forecasts.add(forecast);
        forecast.setTime(time.getFirstChild().getNodeValue());
    }
    NodeList temps = doc.getElementsByTagName("value");
    for (int i = 0; i < temps.getLength(); i++) {
        Element temp = (Element) temps.item(i);
        Forecast forecast = forecasts.get(i);
        forecast.setTemp(Integer.valueOf(temp.getFirstChild().getNodeValue()));
    }
    NodeList icons = doc.getElementsByTagName("icon-link");
    for (int i = 0; i < icons.getLength(); i++) {
        Element icon = (Element) icons.item(i);
        Forecast forecast = forecasts.get(i);
        forecast.setIcon(icon.getFirstChild().getNodeValue());
    }
    return (forecasts);
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilder(javax.xml.parsers.DocumentBuilder) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) StringReader(java.io.StringReader) Document(org.w3c.dom.Document)

Example 20 with Element

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

the class MiniAapt method processDrawables.

void processDrawables(ProjectFilesystem filesystem, Path resourceFile) throws IOException, ResourceParseException {
    String filename = resourceFile.getFileName().toString();
    int dotIndex = filename.indexOf('.');
    String resourceName = dotIndex != -1 ? filename.substring(0, dotIndex) : filename;
    // Look into the XML file.
    boolean isGrayscaleImage = false;
    boolean isCustomDrawable = false;
    if (filename.endsWith(".xml")) {
        try (InputStream stream = filesystem.newFileInputStream(resourceFile)) {
            Document dom = parseXml(resourceFile, stream);
            Element root = dom.getDocumentElement();
            isCustomDrawable = root.getNodeName().startsWith(CUSTOM_DRAWABLE_PREFIX);
        }
    } else if (isGrayscaleImageProcessingEnabled) {
        isGrayscaleImage = filename.endsWith(".g.png");
    }
    if (isCustomDrawable) {
        resourceCollector.addCustomDrawableResourceIfNotPresent(RType.DRAWABLE, resourceName);
    } else if (isGrayscaleImage) {
        resourceCollector.addGrayscaleImageResourceIfNotPresent(RType.DRAWABLE, resourceName);
    } else {
        resourceCollector.addIntResourceIfNotPresent(RType.DRAWABLE, resourceName);
    }
}
Also used : InputStream(java.io.InputStream) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document)

Aggregations

Element (org.w3c.dom.Element)9072 Document (org.w3c.dom.Document)2651 NodeList (org.w3c.dom.NodeList)2103 Node (org.w3c.dom.Node)1855 ArrayList (java.util.ArrayList)957 DocumentBuilder (javax.xml.parsers.DocumentBuilder)793 IOException (java.io.IOException)732 Test (org.junit.Test)693 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)591 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)489 HashMap (java.util.HashMap)434 SAXException (org.xml.sax.SAXException)406 File (java.io.File)358 Attr (org.w3c.dom.Attr)333 InputStream (java.io.InputStream)309 QName (javax.xml.namespace.QName)292 Map (java.util.Map)285 JAXBElement (javax.xml.bind.JAXBElement)285 NamedNodeMap (org.w3c.dom.NamedNodeMap)266 List (java.util.List)264