Search in sources :

Example 81 with XPath

use of javax.xml.xpath.XPath in project stanbol by apache.

the class HtmlExtractionRegistry method initialize.

public void initialize(InputStream configFileStream) throws InitializationException {
    try {
        XPathFactory factory = XPathFactory.newInstance();
        XPath xPath = factory.newXPath();
        DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document document = parser.parse(new InputSource(configFileStream));
        Node node;
        NodeList nodes = (NodeList) xPath.evaluate("/htmlextractors/extractor", document, XPathConstants.NODESET);
        if (nodes != null) {
            TransformerFactory transFac = TransformerFactory.newInstance();
            transFac.setURIResolver(new BundleURIResolver());
            for (int j = 0, iCnt = nodes.getLength(); j < iCnt; j++) {
                Node nd = nodes.item(j);
                node = (Node) xPath.evaluate("@id", nd, XPathConstants.NODE);
                String id = node.getNodeValue();
                Node srcNode = (Node) xPath.evaluate("source", nd, XPathConstants.NODE);
                if (srcNode != null) {
                    node = (Node) xPath.evaluate("@type", srcNode, XPathConstants.NODE);
                    String srcType = node.getNodeValue();
                    if (srcType.equals("xslt")) {
                        String rdfFormat = "rdfxml";
                        Syntax rdfSyntax = Syntax.RdfXml;
                        node = (Node) xPath.evaluate("@syntax", srcNode, XPathConstants.NODE);
                        if (node != null) {
                            rdfFormat = node.getNodeValue();
                            if (rdfFormat.equalsIgnoreCase("turtle")) {
                                rdfSyntax = Syntax.Turtle;
                            } else if (rdfFormat.equalsIgnoreCase("ntriple")) {
                                rdfSyntax = Syntax.Ntriples;
                            } else if (rdfFormat.equalsIgnoreCase("n3")) {
                                rdfSyntax = XsltExtractor.N3;
                            } else if (!rdfFormat.equalsIgnoreCase("rdfxml")) {
                                throw new InitializationException("Unknown RDF Syntax: " + rdfFormat + " for " + id + " extractor");
                            }
                        }
                        // TODO: do something about disjunctions of
                        // Extractors? Assume, only RDFa or Microformats are
                        // used?
                        String fileName = DOMUtils.getText(srcNode);
                        XsltExtractor xsltExtractor = new XsltExtractor(id, fileName, transFac);
                        xsltExtractor.setSyntax(rdfSyntax);
                        // name of URI/URL parameter of the script (default
                        // "uri")
                        node = (Node) xPath.evaluate("@uri", srcNode, XPathConstants.NODE);
                        if (node != null) {
                            xsltExtractor.setUriParameter(node.getNodeValue());
                        }
                        registry.put(id, xsltExtractor);
                        activeExtractors.add(id);
                    } else if (srcType.equals("java")) {
                        String clsName = srcNode.getNodeValue();
                        Object extractor = Class.forName(clsName).newInstance();
                        if (extractor instanceof HtmlExtractionComponent) {
                            registry.put(id, (HtmlExtractionComponent) extractor);
                            activeExtractors.add(id);
                        } else {
                            throw new InitializationException("clsName is not an HtmlExtractionComponent");
                        }
                    } else {
                        LOG.warn("No valid type for extractor found: " + id);
                    }
                    LOG.info("Extractor for: " + id);
                }
            }
        }
    } catch (FileNotFoundException e) {
        throw new InitializationException(e.getMessage(), e);
    } catch (XPathExpressionException e) {
        throw new InitializationException(e.getMessage(), e);
    } catch (DOMException e) {
        throw new InitializationException(e.getMessage(), e);
    } catch (ParserConfigurationException e) {
        throw new InitializationException(e.getMessage(), e);
    } catch (SAXException e) {
        throw new InitializationException(e.getMessage(), e);
    } catch (IOException e) {
        throw new InitializationException(e.getMessage(), e);
    } catch (ClassNotFoundException e) {
        throw new InitializationException(e.getMessage(), e);
    } catch (InstantiationException e) {
        throw new InitializationException(e.getMessage(), e);
    } catch (IllegalAccessException e) {
        throw new InitializationException(e.getMessage(), e);
    }
}
Also used : InputSource(org.xml.sax.InputSource) XPathExpressionException(javax.xml.xpath.XPathExpressionException) Node(org.w3c.dom.Node) FileNotFoundException(java.io.FileNotFoundException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) XPathFactory(javax.xml.xpath.XPathFactory) DOMException(org.w3c.dom.DOMException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) XPath(javax.xml.xpath.XPath) TransformerFactory(javax.xml.transform.TransformerFactory) NodeList(org.w3c.dom.NodeList) IOException(java.io.IOException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Syntax(org.ontoware.rdf2go.model.Syntax)

Example 82 with XPath

use of javax.xml.xpath.XPath in project ddf by codice.

the class GmdTransformer method setMetacardDates.

private void setMetacardDates(Metacard metacard, final XstreamPathValueTracker pathValueTracker) {
    try (InputStream inputStream = getSourceInputStream()) {
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        try {
            domFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
            domFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        } catch (ParserConfigurationException e) {
            LOGGER.debug("Unable to configure features on document builder.", e);
        }
        domFactory.setNamespaceAware(false);
        DocumentBuilder builder = domFactory.newDocumentBuilder();
        Document document = builder.parse(inputStream);
        XPath xPath = XPathFactory.newInstance().newXPath();
        XPathExpression dataExpression = xPath.compile(GmdConstants.RESOURCE_DATE_PATH);
        NodeList dataList = (NodeList) dataExpression.evaluate(document, XPathConstants.NODESET);
        if (dataList != null && dataList.getLength() > 0) {
            List<String> dateList = new ArrayList<>();
            List<String> dateTypes = pathValueTracker.getAllValues(toPath(GmdConstants.CITATION_DATE_TYPE_PATH));
            for (int i = 0; i < dataList.getLength(); i++) {
                Node node = dataList.item(i);
                String datestring = node.getTextContent().trim().replaceAll(WHITE_SPACE_PATTER.pattern(), " ").replaceAll(NEW_LINE_PATTERN.pattern(), " ");
                String[] stringArray = datestring.split(" ");
                dateList.add(stringArray[0]);
            }
            if (CollectionUtils.isNotEmpty(dateList) && CollectionUtils.isNotEmpty(dateTypes) && dateList.size() == dateTypes.size()) {
                setDates(dateList, dateTypes, metacard);
            }
        }
    } catch (ParserConfigurationException | IOException | SAXException | XPathExpressionException e) {
        LOGGER.debug("Unable to parse dates in XML document.  Metacard Created / Effective / Modified dates will not be set.", e);
    }
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) InputStream(java.io.InputStream) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 83 with XPath

use of javax.xml.xpath.XPath in project bnd by bndtools.

the class PomParser method toBsn.

private Object toBsn(NodeList set) throws XPathExpressionException {
    XPath xpath = xpathf.newXPath();
    StringBuilder sb = new StringBuilder();
    String del = "";
    for (int i = 0; i < set.getLength(); i++) {
        Node child = set.item(i);
        String version = xpath.evaluate("version", child);
        sb.append(del);
        sb.append(xpath.evaluate("groupId", child));
        sb.append(".");
        sb.append(xpath.evaluate("artifactId", child));
        if (version != null && version.trim().length() != 0) {
            sb.append(";version=");
            sb.append(Analyzer.cleanupVersion(version));
        }
        del = ", ";
    }
    return sb.toString();
}
Also used : XPath(javax.xml.xpath.XPath) Node(org.w3c.dom.Node)

Example 84 with XPath

use of javax.xml.xpath.XPath in project opennms by OpenNMS.

the class XmlTest method xpathGetNodesMatching.

protected static NodeList xpathGetNodesMatching(final String xml, final String expression) throws XPathExpressionException {
    final XPath query = XPathFactory.newInstance().newXPath();
    StringReader sr = null;
    InputSource is = null;
    NodeList nodes = null;
    try {
        sr = new StringReader(xml);
        is = new InputSource(sr);
        nodes = (NodeList) query.evaluate(expression, is, XPathConstants.NODESET);
    } finally {
        sr.close();
        IOUtils.closeQuietly(sr);
    }
    return nodes;
}
Also used : XPath(javax.xml.xpath.XPath) InputSource(org.xml.sax.InputSource) NodeList(org.w3c.dom.NodeList) StringReader(java.io.StringReader)

Example 85 with XPath

use of javax.xml.xpath.XPath in project android by JetBrains.

the class ThemePreviewBuilderTest method testSearchFilter.

public void testSearchFilter() throws ParserConfigurationException, XPathExpressionException {
    List<ThemePreviewBuilder.ComponentDefinition> componentDefinitionList = ImmutableList.of(new ThemePreviewBuilder.ComponentDefinition("Spinner", ThemePreviewBuilder.ComponentGroup.TEXT, "Spinner"), new ThemePreviewBuilder.ComponentDefinition("Custom_Component", ThemePreviewBuilder.ComponentGroup.CUSTOM, "CustomComponent").addAlias("Spinner").addAlias("ABC").addAlias("DEF"));
    XPath xPath = XPathFactory.newInstance().newXPath();
    ThemePreviewBuilder customComponentBuilder = new ThemePreviewBuilder().addAllComponents(componentDefinitionList);
    // Check the search "spinner" returns both the actual spinner control and the custom component with the alias
    Document document = customComponentBuilder.addComponentFilter(new ThemePreviewBuilder.SearchFilter("SPINNER")).build();
    NodeList nodeList = (NodeList) xPath.evaluate(COMPONENTS_XPATH, document.getDocumentElement(), XPathConstants.NODESET);
    assertEquals(2, nodeList.getLength());
    // Test matching the name
    document = customComponentBuilder.addComponentFilter(new ThemePreviewBuilder.SearchFilter("CustomCOMPONENT")).build();
    nodeList = (NodeList) xPath.evaluate(COMPONENTS_XPATH, document.getDocumentElement(), XPathConstants.NODESET);
    assertEquals(1, nodeList.getLength());
    // Test matching the description
    document = customComponentBuilder.addComponentFilter(new ThemePreviewBuilder.SearchFilter("Custom_COMPONENT")).build();
    nodeList = (NodeList) xPath.evaluate(COMPONENTS_XPATH, document.getDocumentElement(), XPathConstants.NODESET);
    assertEquals(1, nodeList.getLength());
    // Test searching for an alias that only matches our custom component
    document = customComponentBuilder.addComponentFilter(new ThemePreviewBuilder.SearchFilter("AbC")).build();
    nodeList = (NodeList) xPath.evaluate(COMPONENTS_XPATH, document.getDocumentElement(), XPathConstants.NODESET);
    assertEquals(1, nodeList.getLength());
    // Test partial match
    document = customComponentBuilder.addComponentFilter(new ThemePreviewBuilder.SearchFilter("EF")).build();
    nodeList = (NodeList) xPath.evaluate(COMPONENTS_XPATH, document.getDocumentElement(), XPathConstants.NODESET);
    assertEquals(1, nodeList.getLength());
    // Test case sensitive search
    document = customComponentBuilder.addComponentFilter(new ThemePreviewBuilder.SearchFilter("AbC", true)).build();
    nodeList = (NodeList) xPath.evaluate(COMPONENTS_XPATH, document.getDocumentElement(), XPathConstants.NODESET);
    assertEquals(0, nodeList.getLength());
}
Also used : XPath(javax.xml.xpath.XPath) ThemePreviewBuilder(com.android.tools.idea.editors.theme.preview.ThemePreviewBuilder) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document)

Aggregations

XPath (javax.xml.xpath.XPath)526 Document (org.w3c.dom.Document)254 NodeList (org.w3c.dom.NodeList)230 XPathFactory (javax.xml.xpath.XPathFactory)215 Node (org.w3c.dom.Node)171 XPathExpressionException (javax.xml.xpath.XPathExpressionException)159 XPathExpression (javax.xml.xpath.XPathExpression)142 DocumentBuilder (javax.xml.parsers.DocumentBuilder)125 Element (org.w3c.dom.Element)118 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)97 IOException (java.io.IOException)89 Test (org.junit.Test)80 InputSource (org.xml.sax.InputSource)59 File (java.io.File)57 SAXException (org.xml.sax.SAXException)57 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)51 ByteArrayInputStream (java.io.ByteArrayInputStream)44 ArrayList (java.util.ArrayList)44 InputStream (java.io.InputStream)39 DSNamespaceContext (org.apache.xml.security.test.dom.DSNamespaceContext)37