private Map<ASTNode, String> findMatches(String xmlDoc, Pattern description,
XmlVisitor visitor, XdmNode sourceDoc, XPathCompiler xpath)
throws SaxonApiException {
Map<ASTNode, String> result = new HashMap<ASTNode, String>();
String expression = description.xpath;
XPathSelector selector = compileExpr(xpath, expression);
selector.setContextItem(sourceDoc);
XPathSelector outputBeforeSelector = null;
if (description.outputBefore != null) {
outputBeforeSelector = outputXPathCompiler.compile(description.outputBefore).load();
}
for (XdmItem item : selector) {
String valueToInsertBefore = null;
ASTNode node = Utils.getMappedAstNode(visitor, item);
if (node == null)
throw new RuntimeException("Found node is not in AST: " + item);
if (outputBeforeSelector != null) {
outputBeforeSelector.setContextItem(item);
outputBeforeSelector.setVariable(CONTEXT_VARIABLE, item);
valueToInsertBefore = outputBeforeSelector.evaluate().toString();
}
result.put(node, valueToInsertBefore);
}
return result;