}
protected Node getOutputNodeViaXPath(Input input) throws Exception { //NOPMD
checkSearchValueNotNull(input);
checkSearchValueOfType(XPathFilterParameterSpec.class, input);
XPathFilterParameterSpec xpathFilter = (XPathFilterParameterSpec) input.getOutputNodeSearch();
XPathExpression expr = XmlSignatureHelper.getXPathExpression(xpathFilter);
NodeList nodes = (NodeList) expr.evaluate(input.getMessageBodyDocument(), XPathConstants.NODESET);
if (nodes == null || nodes.getLength() == 0) {
throw new XmlSignatureException(
String.format(
"Cannot extract root node for the output document from the XML signature document. No node found for XPATH %s as specified in the output node search.",
xpathFilter.getXPath()));
}
if (nodes.getLength() > 1) {
throw new XmlSignatureException(
String.format(
"Cannot extract root node for the output document from the XML signature document. XPATH %s as specified in the output node search results into more than one child.",
xpathFilter.getXPath()));
}
Node result = nodes.item(0);
if (Node.ELEMENT_NODE == result.getNodeType() || Node.TEXT_NODE == result.getNodeType()
|| Node.DOCUMENT_NODE == result.getNodeType()) {
return result;
}
throw new XmlSignatureException(
String.format(
"Cannot extract root node for the output document from the XML signature document. "
+ "XPATH %s as specified in the output node search results into a node which has the wrong type.",
xpathFilter.getXPath()));
}