Search in sources :

Example 6 with XPathHelper

use of ddf.util.XPathHelper in project ddf by codice.

the class XPathHelperTest method testXPathHelperWithAnyNamespaceTextPath.

@Test
public void testXPathHelperWithAnyNamespaceTextPath() throws Exception {
    try {
        String xmlString = getFileContentsAsString(TEST_DATA_PATH + INPUT_FILE);
        XPathHelper xHelper = new XPathHelper(xmlString);
        NodeList nodeList = (NodeList) xHelper.evaluate("//xyz:fileTitle", XPathConstants.NODESET);
        LOGGER.debug("testXPathHelper_WithAnyNamespaceTextPath() - nodeList length = {}", nodeList.getLength());
        fail("Expected an XPathExpressionException");
    } catch (XPathExpressionException e1) {
        LOGGER.error("Exception thrown during testXPathHelper_WithAnyNamespaceTextPath", e1);
    }
}
Also used : XPathHelper(ddf.util.XPathHelper) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) Test(org.junit.Test)

Example 7 with XPathHelper

use of ddf.util.XPathHelper in project ddf by codice.

the class ContextualEvaluator method getIndexableText.

/**
     * Extract the text from the specified XML Document that is to be indexed using the specified
     * XPath selectors.
     *
     * @param document
     * @param xpathSelectors
     * @return
     */
private static String getIndexableText(String document, String[] xpathSelectors) {
    String methodName = "getIndexableText";
    List<String> indexedText = new ArrayList<String>();
    LOGGER.debug("xpathSelectors.size = {}", xpathSelectors.length);
    StringBuilder sbuilder = new StringBuilder();
    try {
        // TODO Is this safe for all cases? Can there be multiple default namespaces such that
        // this would screw up the metadata?
        // Treat the "default namespace" (i.e., xmlns="http://some.namespace") the same as the
        // "no namespace" (i.e., xmlns="")
        // so that user-specified XPath Selectors do not need to specify a namespace for
        // expressions in the default namespace
        // (For example, user can specify //fileTitle vs. //namespace:fileTitle, where a
        // NamespaceContext/NamespaceResolver
        // would try to resolve the namespace they specified)
        // The regex below, "xmlns=['\"].*?['\"]", looks for:
        // xmlns="any chars between single or double quotes"
        document = document.replaceAll("xmlns=['\"].*?['\"]", "");
        XPathHelper xHelper = new XPathHelper(document);
        for (String xpath : xpathSelectors) {
            LOGGER.debug("Processing xpath selector: {}", xpath);
            NodeList nodeList = (NodeList) xHelper.evaluate(xpath, XPathConstants.NODESET);
            LOGGER.debug("nodeList length = {}", nodeList.getLength());
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node node = nodeList.item(i);
                if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
                    Attr attribute = (Attr) node;
                    LOGGER.debug("Adding text [{}]", attribute.getNodeValue());
                    sbuilder.append(attribute.getNodeValue() + " ");
                // On each element node detected, traverse all of its children. Look for
                // any Text nodes it has, adding their text values to the list of indexable text
                } else if (node.getNodeType() == Node.ELEMENT_NODE) {
                    Element elem = (Element) node;
                    traverse(elem, indexedText);
                // getTextContent() concatenates *all* text from all descendant Text nodes
                // without
                // any white space between each Text node's value, e.g., JohnDoe vs. John
                // Doe
                // That's not good ...
                } else {
                    LOGGER.debug("Unsupported node type: " + node.getNodeType() + ",   node name = " + node.getNodeName());
                }
            }
        }
    } catch (XPathExpressionException e1) {
        LOGGER.debug("Unable to evaluate XPath", e1);
    }
    // Append all of the Text nodes' values to the single indexable text string
    for (String text : indexedText) {
        sbuilder.append(text);
    }
    return sbuilder.toString();
}
Also used : XPathHelper(ddf.util.XPathHelper) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Attr(org.w3c.dom.Attr)

Example 8 with XPathHelper

use of ddf.util.XPathHelper in project ddf by codice.

the class XsltResponseQueueTransformer method transform.

@Override
public ddf.catalog.data.BinaryContent transform(SourceResponse upstreamResponse, Map<String, Serializable> arguments) throws CatalogTransformerException {
    LOGGER.debug("Transforming ResponseQueue with XSLT tranformer");
    long grandTotal = -1;
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        try {
            factory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
            factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        } catch (ParserConfigurationException e) {
            LOGGER.debug("Unable to configure features on document builder.", e);
        }
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.newDocument();
        Node resultsElement = doc.appendChild(createElement(doc, XML_RESULTS_NAMESPACE, "results", null));
        // TODO use streaming XSLT, not DOM
        List<Result> results = upstreamResponse.getResults();
        grandTotal = upstreamResponse.getHits();
        for (Result result : results) {
            Metacard metacard = result.getMetacard();
            if (metacard != null) {
                String metadata = metacard.getMetadata();
                if (metadata != null) {
                    Element metacardElement = createElement(doc, XML_RESULTS_NAMESPACE, "metacard", null);
                    if (metacard.getId() != null) {
                        metacardElement.appendChild(createElement(doc, XML_RESULTS_NAMESPACE, "id", metacard.getId()));
                    }
                    if (metacard.getMetacardType().toString() != null) {
                        metacardElement.appendChild(createElement(doc, XML_RESULTS_NAMESPACE, "type", metacard.getMetacardType().getName()));
                    }
                    if (metacard.getTitle() != null) {
                        metacardElement.appendChild(createElement(doc, XML_RESULTS_NAMESPACE, "title", metacard.getTitle()));
                    }
                    if (result.getRelevanceScore() != null) {
                        metacardElement.appendChild(createElement(doc, XML_RESULTS_NAMESPACE, "score", result.getRelevanceScore().toString()));
                    }
                    if (result.getDistanceInMeters() != null) {
                        metacardElement.appendChild(createElement(doc, XML_RESULTS_NAMESPACE, "distance", result.getDistanceInMeters().toString()));
                    }
                    if (metacard.getSourceId() != null) {
                        metacardElement.appendChild(createElement(doc, XML_RESULTS_NAMESPACE, "site", metacard.getSourceId()));
                    }
                    if (metacard.getContentTypeName() != null) {
                        String contentType = metacard.getContentTypeName();
                        Element typeElement = createElement(doc, XML_RESULTS_NAMESPACE, "content-type", contentType);
                        // TODO revisit what to put in the qualifier
                        typeElement.setAttribute("qualifier", "content-type");
                        metacardElement.appendChild(typeElement);
                    }
                    if (metacard.getResourceURI() != null) {
                        try {
                            metacardElement.appendChild(createElement(doc, XML_RESULTS_NAMESPACE, "product", metacard.getResourceURI().toString()));
                        } catch (DOMException e) {
                            LOGGER.debug(" Unable to create resource uri element", e);
                        }
                    }
                    if (metacard.getThumbnail() != null) {
                        metacardElement.appendChild(createElement(doc, XML_RESULTS_NAMESPACE, "thumbnail", Base64.getEncoder().encodeToString(metacard.getThumbnail())));
                        try {
                            String mimeType = URLConnection.guessContentTypeFromStream(new ByteArrayInputStream(metacard.getThumbnail()));
                            metacardElement.appendChild(createElement(doc, XML_RESULTS_NAMESPACE, "t_mimetype", mimeType));
                        } catch (IOException e) {
                            metacardElement.appendChild(createElement(doc, XML_RESULTS_NAMESPACE, "t_mimetype", "image/png"));
                        }
                    }
                    DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
                    if (metacard.getCreatedDate() != null) {
                        metacardElement.appendChild(createElement(doc, XML_RESULTS_NAMESPACE, "created", fmt.print(metacard.getCreatedDate().getTime())));
                    }
                    // looking at the date last modified
                    if (metacard.getModifiedDate() != null) {
                        metacardElement.appendChild(createElement(doc, XML_RESULTS_NAMESPACE, "updated", fmt.print(metacard.getModifiedDate().getTime())));
                    }
                    if (metacard.getEffectiveDate() != null) {
                        metacardElement.appendChild(createElement(doc, XML_RESULTS_NAMESPACE, "effective", fmt.print(metacard.getEffectiveDate().getTime())));
                    }
                    if (metacard.getLocation() != null) {
                        metacardElement.appendChild(createElement(doc, XML_RESULTS_NAMESPACE, "location", metacard.getLocation()));
                    }
                    Element documentElement = doc.createElementNS(XML_RESULTS_NAMESPACE, "document");
                    metacardElement.appendChild(documentElement);
                    resultsElement.appendChild(metacardElement);
                    Node importedNode = doc.importNode(new XPathHelper(metadata).getDocument().getFirstChild(), true);
                    documentElement.appendChild(importedNode);
                } else {
                    LOGGER.debug("Null content/document returned to XSLT ResponseQueueTransformer");
                }
            }
        }
        if (LOGGER.isDebugEnabled()) {
            DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation();
            LSSerializer lsSerializer = domImplementation.createLSSerializer();
            LOGGER.debug("Generated XML input for transform: " + lsSerializer.writeToString(doc));
        }
        LOGGER.debug("Starting responsequeue xslt transform.");
        Transformer transformer;
        Map<String, Object> mergedMap = new HashMap<String, Object>();
        mergedMap.put(GRAND_TOTAL, grandTotal);
        if (arguments != null) {
            mergedMap.putAll(arguments);
        }
        BinaryContent resultContent;
        StreamResult resultOutput = null;
        Source source = new DOMSource(doc);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        resultOutput = new StreamResult(baos);
        try {
            transformer = templates.newTransformer();
        } catch (TransformerConfigurationException tce) {
            throw new CatalogTransformerException("Could not perform Xslt transform: ", tce);
        }
        for (Map.Entry<String, Object> entry : mergedMap.entrySet()) {
            LOGGER.trace("Adding parameter to transform {{}:{}}", entry.getKey(), entry.getValue());
            transformer.setParameter(entry.getKey(), entry.getValue());
        }
        try {
            transformer.transform(source, resultOutput);
            byte[] bytes = baos.toByteArray();
            LOGGER.debug("Transform complete.");
            resultContent = new XsltTransformedContent(bytes, mimeType);
        } catch (TransformerException te) {
            LOGGER.debug("Could not perform Xslt transform: ", te);
            throw new CatalogTransformerException("Could not perform Xslt transform: ", te);
        }
        return resultContent;
    } catch (ParserConfigurationException e) {
        LOGGER.debug("Error creating new document: ", e);
        throw new CatalogTransformerException("Error merging entries to xml feed.", e);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Transformer(javax.xml.transform.Transformer) QueryResponseTransformer(ddf.catalog.transform.QueryResponseTransformer) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) XPathHelper(ddf.util.XPathHelper) HashMap(java.util.HashMap) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) DOMImplementationLS(org.w3c.dom.ls.DOMImplementationLS) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) Document(org.w3c.dom.Document) BinaryContent(ddf.catalog.data.BinaryContent) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) StreamResult(javax.xml.transform.stream.StreamResult) Result(ddf.catalog.data.Result) DOMException(org.w3c.dom.DOMException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) TransformerException(javax.xml.transform.TransformerException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) StreamResult(javax.xml.transform.stream.StreamResult) LSSerializer(org.w3c.dom.ls.LSSerializer) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Metacard(ddf.catalog.data.Metacard) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) HashMap(java.util.HashMap) Map(java.util.Map)

Example 9 with XPathHelper

use of ddf.util.XPathHelper in project ddf by codice.

the class XPathHelperTest method testXPathHelperWithNoNamespaceTextPath.

@Test
public void testXPathHelperWithNoNamespaceTextPath() throws Exception {
    try {
        String xmlString = getFileContentsAsString(TEST_DATA_PATH + INPUT_FILE);
        XPathHelper xHelper = new XPathHelper(xmlString);
        NodeList nodeList = (NodeList) xHelper.evaluate("//fileTitle", XPathConstants.NODESET, new MockNamespaceResolver());
        LOGGER.debug("testXPathHelper_WithNoNamespaceTextPath() - nodeList length = {}", nodeList.getLength());
        assertEquals(0, nodeList.getLength());
    } catch (Exception e1) {
        LOGGER.error("Exception thrown during testXPathHelper_WithNoNamespaceTextPath", e1);
    }
}
Also used : XPathHelper(ddf.util.XPathHelper) NodeList(org.w3c.dom.NodeList) XPathExpressionException(javax.xml.xpath.XPathExpressionException) Test(org.junit.Test)

Example 10 with XPathHelper

use of ddf.util.XPathHelper in project ddf by codice.

the class XPathEvaluator method evaluate.

public static boolean evaluate(XPathEvaluationCriteria xpathCriteria) {
    Document document = xpathCriteria.getDocument();
    String xpath = xpathCriteria.getXPath();
    XPathHelper evaluator = new XPathHelper(document);
    try {
        return (Boolean) evaluator.evaluate(xpath, XPathConstants.BOOLEAN);
    } catch (XPathExpressionException e) {
        LOGGER.debug("Unable to evaluate xpath", e);
    }
    return false;
}
Also used : XPathHelper(ddf.util.XPathHelper) XPathExpressionException(javax.xml.xpath.XPathExpressionException) Document(org.w3c.dom.Document)

Aggregations

XPathHelper (ddf.util.XPathHelper)10 XPathExpressionException (javax.xml.xpath.XPathExpressionException)8 Test (org.junit.Test)6 NodeList (org.w3c.dom.NodeList)6 Document (org.w3c.dom.Document)3 Metacard (ddf.catalog.data.Metacard)2 Result (ddf.catalog.data.Result)2 Element (org.w3c.dom.Element)2 Node (org.w3c.dom.Node)2 BinaryContent (ddf.catalog.data.BinaryContent)1 SourceResponse (ddf.catalog.operation.SourceResponse)1 QueryImpl (ddf.catalog.operation.impl.QueryImpl)1 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)1 CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)1 QueryResponseTransformer (ddf.catalog.transform.QueryResponseTransformer)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1