public void visitLocationStep(LocationStep locationStep) { super.visitLocationStep(locationStep); boolean optimize = false; // only location steps with predicates can be optimized: if (locationStep.hasPredicates()) { List preds = locationStep.getPredicates(); // walk through the predicates attached to the current location step. // try to find a predicate containing an expression which is an instance // of Optimizable. for (Iterator i = preds.iterator(); i.hasNext(); ) { Predicate pred = (Predicate) i.next(); FindOptimizable find = new FindOptimizable(); pred.accept(find); List list = find.getOptimizables(); if (list.size() > 0 && canOptimize(list)) { optimize = true; break; } } } if (optimize) { // we found at least one Optimizable. Rewrite the whole expression and // enclose it in an (#exist:optimize#) pragma. Expression parent = locationStep.getParent(); if (!(parent instanceof PathExpr)) { LOG.warn("Parent expression of step is not a PathExpr: " + parent); return; } if (LOG.isTraceEnabled()) LOG.trace("Rewriting expression: " + ExpressionDumper.dump(locationStep)); hasOptimized = true; PathExpr path = (PathExpr) parent; try { // Create the pragma ExtensionExpression extension = new ExtensionExpression(context); extension.addPragma(new Optimize(context, Optimize.OPTIMIZE_PRAGMA, null, false)); extension.setExpression(locationStep); // Replace the old expression with the pragma path.replaceExpression(locationStep, extension); } catch (XPathException e) { LOG.warn("Failed to optimize expression: " + locationStep + ": " + e.getMessage(), e); } } }
public void visitPredicate(Predicate predicate) { ++predicates; super.visitPredicate(predicate); --predicates; }