public void visitAndExpr(OpAnd and) { if (predicates > 0) { Expression parent = and.getParent(); if (!(parent instanceof PathExpr)) { LOG.warn("Parent expression of boolean operator is not a PathExpr: " + parent); return; } PathExpr path; Predicate predicate; if (parent instanceof Predicate) { predicate = (Predicate) parent; path = predicate; } else { path = (PathExpr) parent; parent = path.getParent(); if (!(parent instanceof Predicate) || path.getLength() > 1) { LOG.warn( "Boolean operator is not a top-level expression in the predicate: " + parent.getClass().getName()); return; } predicate = (Predicate) parent; } if (LOG.isTraceEnabled()) LOG.trace("Rewriting boolean expression: " + ExpressionDumper.dump(and)); hasOptimized = true; LocationStep step = (LocationStep) predicate.getParent(); Predicate newPred = new Predicate(context); newPred.add(and.getRight()); step.insertPredicate(predicate, newPred); path.replaceExpression(and, and.getLeft()); } }
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 Expression staticCheck(ModuleContext context, int flags) { super.staticCheck(context, flags); for (int e = 0, E = attributes.size(); e < E; e++) { Expression attr = context.staticCheck(getAttribute(e), 0); attributes.set(e, attr); if (attr instanceof PathExpr) ((PathExpr) attr).tryToTrim(); } for (int c = 0; c < contents.length; c++) if (contents[c] instanceof PathExpr) ((PathExpr) contents[c]).tryToTrim(); type = XQType.ELEMENT; return this; }
public void visitPathExpr(PathExpr expression) { for (int i = 0; i < expression.getLength(); i++) { Expression next = expression.getExpression(i); next.accept(this); } }