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);
}
}
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();
}
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);
}
}
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);
}
}
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;
}
Aggregations