new CachedXPathAPI(input.getCachedXPathAPI().getCachedXPathAPI());
if (inputSet.size() == 0) {
Object exArgs[] = { "input node set contains no nodes" };
throw new TransformationException("empty", exArgs);
}
Element xpathElement =XMLUtils.selectNode(
this._transformObject.getElement().getFirstChild(),
XPath2FilterContainer04.XPathFilter2NS,
XPath2FilterContainer04._TAG_XPATH2,0);
if (xpathElement == null) {
Object exArgs[] = { "dsig-xpath:XPath", "Transform" };
throw new TransformationException("xml.WrongContent", exArgs);
}
XPath2FilterContainer04 xpathContainer =
XPath2FilterContainer04.newInstance(xpathElement,
input.getSourceURI());
Document doc = this._transformObject.getElement().getOwnerDocument();
NodeList subtreeRoots = xPathFuncHereAPI.selectNodeList(doc,
xpathContainer.getXPathFilterTextNode(),
CachedXPathFuncHereAPI.getStrFromNode(xpathContainer.getXPathFilterTextNode()),
xpathContainer.getElement());
if (log.isDebugEnabled())
log.debug("subtreeRoots contains " + subtreeRoots.getLength()
+ " nodes");
/*
* foreach subtree
*/
Set selectedNodes = new HashSet();
for (int i = 0; i < subtreeRoots.getLength(); i++) {
Node currentRootNode = subtreeRoots.item(i);
int currentRootNodeType = currentRootNode.getNodeType();
if ((currentRootNodeType == Node.ELEMENT_NODE)
|| (currentRootNodeType == Node.DOCUMENT_NODE)) {
NodeList nodesInSubtree =
myXPathAPI
.selectNodeList(currentRootNode, Canonicalizer
.XPATH_C14N_WITH_COMMENTS_SINGLE_NODE);
int jMax = nodesInSubtree.getLength();
for (int j = 0; j < jMax; j++) {
selectedNodes.add(nodesInSubtree.item(j));
}
} else if ((currentRootNodeType == Node.ATTRIBUTE_NODE)
|| (currentRootNodeType == Node.TEXT_NODE)
|| (currentRootNodeType == Node.CDATA_SECTION_NODE)
|| (currentRootNodeType
== Node.PROCESSING_INSTRUCTION_NODE)) {
selectedNodes.add(currentRootNode);
} else {
throw new RuntimeException("unknown node type: " + currentRootNodeType + " " + currentRootNode);
}
}
if (log.isDebugEnabled())
log.debug("selection process identified " + selectedNodes.size()
+ " nodes");
Set resultNodes = new HashSet();
if (xpathContainer.isIntersect()) {
Iterator inputSetIterator = inputSet.iterator();
while (inputSetIterator.hasNext()) {
Node currentInputNode = (Node) inputSetIterator.next();
// if the input node is selected, include it in the output
if (selectedNodes.contains(currentInputNode)) {
resultNodes.add(currentInputNode);
}
}
} else if (xpathContainer.isSubtract()) {
Iterator inputSetIterator = inputSet.iterator();
while (inputSetIterator.hasNext()) {
Node currentInputNode = (Node) inputSetIterator.next();
// if the input node is selected, do not include it
// otherwise, include it
if (!selectedNodes.contains(currentInputNode)) {
resultNodes.add(currentInputNode);
}
}
} else if (xpathContainer.isUnion()) {
Iterator inputSetIterator = inputSet.iterator();
while (inputSetIterator.hasNext()) {
Node currentInputNode = (Node) inputSetIterator.next();
// add all input nodes to the result
resultNodes.add(currentInputNode);
}
Iterator selectedSetIterator = selectedNodes.iterator();
while (selectedSetIterator.hasNext()) {
Node currentSelectedNode = (Node) selectedSetIterator.next();
// add all selected nodes to the result
resultNodes.add(currentSelectedNode);
}
} else {
throw new TransformationException("empty");
}
XMLSignatureInput result = new XMLSignatureInput(resultNodes,
input.getCachedXPathAPI());
result.setSourceURI(input.getSourceURI());
return result;
} catch (TransformerException ex) {
throw new TransformationException("empty", ex);
} catch (DOMException ex) {
throw new TransformationException("empty", ex);
} catch (IOException ex) {
throw new TransformationException("empty", ex);
} catch (CanonicalizationException ex) {
throw new TransformationException("empty", ex);
} catch (InvalidCanonicalizerException ex) {
throw new TransformationException("empty", ex);
} catch (ParserConfigurationException ex) {
throw new TransformationException("empty", ex);
} catch (XMLSecurityException ex) {
throw new TransformationException("empty", ex);
} catch (SAXException ex) {
throw new TransformationException("empty", ex);
}
}