for (XPathFilterParameterSpec xp : xpathsToIdAttributes) {
XPathExpression exp;
try {
exp = XmlSignatureHelper.getXPathExpression(xp);
} catch (XPathExpressionException e) {
throw new XmlSignatureException("The configured xpath expression " + xp.getXPath() + " is invalid.", e);
}
NodeList list = (NodeList) exp.evaluate(messageBodyNode, XPathConstants.NODESET);
if (list == null) {
//assume optional element, XSD validation has been done before
LOG.warn("No ID attribute found for xpath expression {}. Therfore this xpath expression will be ignored.", xp.getXPath());
continue;
}
int length = list.getLength();
for (int i = 0; i < length; i++) {
Node node = list.item(i);
if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
Attr attr = (Attr) node;
String value = attr.getValue();
// check that attribute is ID attribute
Element element = messageBodyNode.getOwnerDocument().getElementById(value);
if (element == null) {
throw new XmlSignatureException(
"Wrong configured xpath expression for ID attributes: The evaluation of the xpath expression "
+ xp.getXPath() + " resulted in an attribute which is not of type ID. The attribute value is "
+ value + ".");
}
result.add(new ComparableNode(element, "#" + value));
LOG.debug("ID attribute with value {} found for xpath {}", value, xp.getXPath());
} else {
throw new XmlSignatureException(
"Wrong configured xpath expression for ID attributes: The evaluation of the xpath expression " + xp.getXPath()
+ " returned a node which was not of type Attribute.");
}
}
}
if (result.size() == 0) {
throw new XmlSignatureException(
"No element to sign found in the detached case. No node found for the configured xpath expressions "
+ toString(xpathsToIdAttributes)
+ ". Either the configuration of the XML signature component is wrong or the incoming message has not the correct structure.");
}
// sort so that elements with deeper hierarchy level are treated first