Search in sources :

Example 1 with Predicate

use of org.jaxen.expr.Predicate in project pmd by pmd.

the class JaxenXPathRuleQuery method initializeXPathExpression.

private void initializeXPathExpression(final Navigator navigator) throws JaxenException {
    /*
        Attempt to use the RuleChain with this XPath query.

        To do so, the queries should generally look like //TypeA or //TypeA | //TypeB. We will look at the parsed XPath
        AST using the Jaxen APIs to make this determination.

        If the query is not exactly what we are looking for, do not use the
        RuleChain.
        */
    nodeNameToXPaths = new HashMap<>();
    final BaseXPath originalXPath = createXPath(xpath, navigator);
    addQueryToNode(originalXPath, AST_ROOT);
    boolean useRuleChain = true;
    final Deque<Expr> pending = new ArrayDeque<>();
    pending.push(originalXPath.getRootExpr());
    while (!pending.isEmpty()) {
        final Expr node = pending.pop();
        // Need to prove we can handle this part of the query
        boolean valid = false;
        // Must be a LocationPath... that is something like //Type
        if (node instanceof LocationPath) {
            final LocationPath locationPath = (LocationPath) node;
            if (locationPath.isAbsolute()) {
                // Should be at least two steps
                @SuppressWarnings("unchecked") final List<Step> steps = locationPath.getSteps();
                if (steps.size() >= 2) {
                    final Step step1 = steps.get(0);
                    final Step step2 = steps.get(1);
                    // descendant or self axis
                    if (step1 instanceof AllNodeStep && step1.getAxis() == Axis.DESCENDANT_OR_SELF) {
                        // axis.
                        if (step2 instanceof NameStep && step2.getAxis() == Axis.CHILD) {
                            // Construct a new expression that is
                            // appropriate for RuleChain use
                            final XPathFactory xpathFactory = new DefaultXPathFactory();
                            // Instead of an absolute location path, we'll
                            // be using a relative path
                            final LocationPath relativeLocationPath = xpathFactory.createRelativeLocationPath();
                            // The first step will be along the self axis
                            final Step allNodeStep = xpathFactory.createAllNodeStep(Axis.SELF);
                            // Retain all predicates from the original name
                            // step
                            @SuppressWarnings("unchecked") final List<Predicate> predicates = step2.getPredicates();
                            for (Predicate predicate : predicates) {
                                allNodeStep.addPredicate(predicate);
                            }
                            relativeLocationPath.addStep(allNodeStep);
                            // location path
                            for (int i = 2; i < steps.size(); i++) {
                                relativeLocationPath.addStep(steps.get(i));
                            }
                            final BaseXPath xpath = createXPath(relativeLocationPath.getText(), navigator);
                            addQueryToNode(xpath, ((NameStep) step2).getLocalName());
                            valid = true;
                        }
                    }
                }
            }
        } else if (node instanceof UnionExpr) {
            // Or a UnionExpr, that is
            // something like //TypeA |
            // //TypeB
            UnionExpr unionExpr = (UnionExpr) node;
            pending.push(unionExpr.getLHS());
            pending.push(unionExpr.getRHS());
            valid = true;
        }
        if (!valid) {
            useRuleChain = false;
            break;
        }
    }
    if (useRuleChain) {
        // Use the RuleChain for all the nodes extracted from the xpath
        // queries
        super.ruleChainVisits.addAll(nodeNameToXPaths.keySet());
    } else {
        // Use original XPath if we cannot use the RuleChain
        nodeNameToXPaths.clear();
        addQueryToNode(originalXPath, AST_ROOT);
        if (LOG.isLoggable(Level.FINE)) {
            LOG.log(Level.FINE, "Unable to use RuleChain for XPath: " + xpath);
        }
    }
    if (navigator == null) {
        this.initializationStatus = InitializationStatus.PARTIAL;
        // Clear the node data, because we did not have a Navigator
        nodeNameToXPaths = null;
    } else {
        this.initializationStatus = InitializationStatus.FULL;
    }
}
Also used : UnionExpr(org.jaxen.expr.UnionExpr) AllNodeStep(org.jaxen.expr.AllNodeStep) Step(org.jaxen.expr.Step) NameStep(org.jaxen.expr.NameStep) ArrayDeque(java.util.ArrayDeque) Predicate(org.jaxen.expr.Predicate) XPathFactory(org.jaxen.expr.XPathFactory) DefaultXPathFactory(org.jaxen.expr.DefaultXPathFactory) UnionExpr(org.jaxen.expr.UnionExpr) Expr(org.jaxen.expr.Expr) LocationPath(org.jaxen.expr.LocationPath) BaseXPath(org.jaxen.BaseXPath) DefaultXPathFactory(org.jaxen.expr.DefaultXPathFactory) NameStep(org.jaxen.expr.NameStep) AllNodeStep(org.jaxen.expr.AllNodeStep)

Example 2 with Predicate

use of org.jaxen.expr.Predicate in project webservices-axiom by apache.

the class ExprComparator method compare.

public int compare(Object o1, Object o2) {
    int type1 = getType(o1);
    int type2 = getType(o2);
    int cmp;
    if (type1 == type2) {
        switch(type1) {
            case TYPE_ADDITIVE_EXPR:
                AdditiveExpr additiveExpr1 = (AdditiveExpr) o1;
                AdditiveExpr additiveExpr2 = (AdditiveExpr) o2;
                cmp = additiveExpr1.getOperator().compareTo(additiveExpr2.getOperator());
                if (cmp == 0) {
                    cmp = compare(additiveExpr1.getLHS(), additiveExpr2.getLHS());
                    if (cmp == 0) {
                        cmp = compare(additiveExpr1.getRHS(), additiveExpr2.getRHS());
                    }
                }
                break;
            case TYPE_ALL_NODE_STEP:
                AllNodeStep allNodeStep1 = (AllNodeStep) o1;
                AllNodeStep allNodeStep2 = (AllNodeStep) o2;
                cmp = allNodeStep1.getAxis() - allNodeStep2.getAxis();
                if (cmp == 0) {
                    cmp = compareLists(allNodeStep1.getPredicates(), allNodeStep2.getPredicates());
                }
                break;
            case TYPE_COMMENT_NODE_STEP:
                CommentNodeStep commentNodeStep1 = (CommentNodeStep) o1;
                CommentNodeStep commentNodeStep2 = (CommentNodeStep) o2;
                cmp = commentNodeStep1.getAxis() - commentNodeStep2.getAxis();
                if (cmp == 0) {
                    cmp = compareLists(commentNodeStep1.getPredicates(), commentNodeStep2.getPredicates());
                }
                break;
            case TYPE_EQUALITY_EXPR:
                EqualityExpr equalityExpr1 = (EqualityExpr) o1;
                EqualityExpr equalityExpr2 = (EqualityExpr) o2;
                cmp = equalityExpr1.getOperator().compareTo(equalityExpr2.getOperator());
                if (cmp == 0) {
                    cmp = compare(equalityExpr1.getLHS(), equalityExpr1.getLHS());
                    if (cmp == 0) {
                        cmp = compare(equalityExpr1.getRHS(), equalityExpr1.getRHS());
                    }
                }
                break;
            case TYPE_FILTER_EXPR:
                if (true)
                    throw new RuntimeException("Not yet implemented!");
                break;
            case TYPE_FUNCTION_CALL_EXPR:
                FunctionCallExpr functionCallExpr1 = (FunctionCallExpr) o1;
                FunctionCallExpr functionCallExpr2 = (FunctionCallExpr) o2;
                cmp = compareStrings(functionCallExpr1.getPrefix(), functionCallExpr2.getPrefix());
                if (cmp == 0) {
                    cmp = functionCallExpr1.getFunctionName().compareTo(functionCallExpr2.getFunctionName());
                    if (cmp == 0) {
                        cmp = compareLists(functionCallExpr1.getParameters(), functionCallExpr2.getParameters());
                    }
                }
                break;
            case TYPE_LITERAL_EXPR:
                LiteralExpr literalExpr1 = (LiteralExpr) o1;
                LiteralExpr literalExpr2 = (LiteralExpr) o2;
                cmp = literalExpr1.getLiteral().compareTo(literalExpr2.getLiteral());
                break;
            case TYPE_LOCATION_PATH:
                LocationPath locationPath1 = (LocationPath) o1;
                LocationPath locationPath2 = (LocationPath) o2;
                if (locationPath1.isAbsolute() == locationPath2.isAbsolute()) {
                    cmp = compareLists(locationPath1.getSteps(), locationPath2.getSteps());
                } else if (locationPath1.isAbsolute()) {
                    cmp = 1;
                } else {
                    cmp = -1;
                }
                break;
            case TYPE_LOGICAL_EXP:
                LogicalExpr logicalExpr1 = (LogicalExpr) o1;
                LogicalExpr logicalExpr2 = (LogicalExpr) o2;
                cmp = logicalExpr1.getOperator().compareTo(logicalExpr2.getOperator());
                if (cmp == 0) {
                    cmp = compare(logicalExpr1.getLHS(), logicalExpr2.getLHS());
                    if (cmp == 0) {
                        cmp = compare(logicalExpr1.getRHS(), logicalExpr2.getRHS());
                    }
                }
                break;
            case TYPE_MULTIPLICATIVE_EXPR:
                MultiplicativeExpr multiplicativeExpr1 = (MultiplicativeExpr) o1;
                MultiplicativeExpr multiplicativeExpr2 = (MultiplicativeExpr) o2;
                cmp = multiplicativeExpr1.getOperator().compareTo(multiplicativeExpr2.getOperator());
                if (cmp == 0) {
                    cmp = compare(multiplicativeExpr1.getLHS(), multiplicativeExpr2.getLHS());
                    if (cmp == 0) {
                        cmp = compare(multiplicativeExpr1.getRHS(), multiplicativeExpr2.getRHS());
                    }
                }
                break;
            case TYPE_NAME_STEP:
                NameStep nameStep1 = (NameStep) o1;
                NameStep nameStep2 = (NameStep) o2;
                cmp = nameStep1.getAxis() - nameStep2.getAxis();
                if (cmp == 0) {
                    cmp = compareStrings(nameStep1.getPrefix(), nameStep2.getPrefix());
                    if (cmp == 0) {
                        cmp = nameStep1.getLocalName().compareTo(nameStep2.getLocalName());
                        if (cmp == 0) {
                            cmp = compareLists(nameStep1.getPredicates(), nameStep2.getPredicates());
                        }
                    }
                }
                break;
            case TYPE_NUMBER_EXPR:
                NumberExpr numberExpr1 = (NumberExpr) o1;
                NumberExpr numberExpr2 = (NumberExpr) o2;
                cmp = new Double(numberExpr1.getNumber().doubleValue()).compareTo(new Double(numberExpr2.getNumber().doubleValue()));
                break;
            case TYPE_PATH_EXPR:
                PathExpr pathExpr1 = (PathExpr) o1;
                PathExpr pathExpr2 = (PathExpr) o2;
                cmp = compare(pathExpr1.getLocationPath(), pathExpr2.getLocationPath());
                if (cmp == 0) {
                    cmp = compare(pathExpr1.getFilterExpr(), pathExpr2.getFilterExpr());
                }
                break;
            case TYPE_PREDICATE:
                Predicate predicate1 = (Predicate) o1;
                Predicate predicate2 = (Predicate) o2;
                cmp = compare(predicate1.getExpr(), predicate2.getExpr());
                break;
            case TYPE_PROCESSING_INSTRUCTION_NODE_STEP:
                ProcessingInstructionNodeStep processingInstructionNodeStep1 = (ProcessingInstructionNodeStep) o1;
                ProcessingInstructionNodeStep processingInstructionNodeStep2 = (ProcessingInstructionNodeStep) o2;
                cmp = processingInstructionNodeStep1.getAxis() - processingInstructionNodeStep2.getAxis();
                if (cmp == 0) {
                    cmp = compareStrings(processingInstructionNodeStep1.getName(), processingInstructionNodeStep2.getName());
                    if (cmp == 0) {
                        cmp = compareLists(processingInstructionNodeStep1.getPredicates(), processingInstructionNodeStep2.getPredicates());
                    }
                }
                break;
            case TYPE_RELATIONAL_EXPR:
                RelationalExpr relationalExpr1 = (RelationalExpr) o1;
                RelationalExpr relationalExpr2 = (RelationalExpr) o2;
                cmp = relationalExpr1.getOperator().compareTo(relationalExpr2.getOperator());
                if (cmp == 0) {
                    cmp = compare(relationalExpr1.getLHS(), relationalExpr2.getLHS());
                    if (cmp == 0) {
                        cmp = compare(relationalExpr1.getRHS(), relationalExpr2.getRHS());
                    }
                }
                break;
            case TYPE_TEXT_NODE_STEP:
                TextNodeStep textNodeStep1 = (TextNodeStep) o1;
                TextNodeStep textNodeStep2 = (TextNodeStep) o2;
                cmp = textNodeStep1.getAxis() - textNodeStep2.getAxis();
                if (cmp == 0) {
                    cmp = compareLists(textNodeStep1.getPredicates(), textNodeStep2.getPredicates());
                }
                break;
            case TYPE_UNARY_EXPR:
                UnaryExpr unaryExpr1 = (UnaryExpr) o1;
                UnaryExpr unaryExpr2 = (UnaryExpr) o2;
                cmp = compare(unaryExpr1.getExpr(), unaryExpr2.getExpr());
                break;
            case TYPE_UNION_EXPR:
                if (true)
                    throw new RuntimeException("Not yet implemented!");
                break;
            case TYPE_VARIABLE_REFERENCE_EXPR:
                VariableReferenceExpr variableReferenceExpr1 = (VariableReferenceExpr) o1;
                VariableReferenceExpr variableReferenceExpr2 = (VariableReferenceExpr) o2;
                cmp = compareStrings(variableReferenceExpr1.getPrefix(), variableReferenceExpr2.getPrefix());
                if (cmp == 0) {
                    cmp = variableReferenceExpr1.getVariableName().compareTo(variableReferenceExpr2.getVariableName());
                }
                break;
            default:
                throw new IllegalArgumentException("Unhandled type: " + type1);
        }
    } else {
        cmp = type1 - type2;
    }
    return cmp;
}
Also used : TextNodeStep(org.jaxen.expr.TextNodeStep) RelationalExpr(org.jaxen.expr.RelationalExpr) UnaryExpr(org.jaxen.expr.UnaryExpr) Predicate(org.jaxen.expr.Predicate) VariableReferenceExpr(org.jaxen.expr.VariableReferenceExpr) MultiplicativeExpr(org.jaxen.expr.MultiplicativeExpr) ProcessingInstructionNodeStep(org.jaxen.expr.ProcessingInstructionNodeStep) EqualityExpr(org.jaxen.expr.EqualityExpr) FunctionCallExpr(org.jaxen.expr.FunctionCallExpr) AdditiveExpr(org.jaxen.expr.AdditiveExpr) LocationPath(org.jaxen.expr.LocationPath) NumberExpr(org.jaxen.expr.NumberExpr) CommentNodeStep(org.jaxen.expr.CommentNodeStep) NameStep(org.jaxen.expr.NameStep) AllNodeStep(org.jaxen.expr.AllNodeStep) LiteralExpr(org.jaxen.expr.LiteralExpr) LogicalExpr(org.jaxen.expr.LogicalExpr) PathExpr(org.jaxen.expr.PathExpr)

Aggregations

AllNodeStep (org.jaxen.expr.AllNodeStep)2 LocationPath (org.jaxen.expr.LocationPath)2 NameStep (org.jaxen.expr.NameStep)2 Predicate (org.jaxen.expr.Predicate)2 ArrayDeque (java.util.ArrayDeque)1 BaseXPath (org.jaxen.BaseXPath)1 AdditiveExpr (org.jaxen.expr.AdditiveExpr)1 CommentNodeStep (org.jaxen.expr.CommentNodeStep)1 DefaultXPathFactory (org.jaxen.expr.DefaultXPathFactory)1 EqualityExpr (org.jaxen.expr.EqualityExpr)1 Expr (org.jaxen.expr.Expr)1 FunctionCallExpr (org.jaxen.expr.FunctionCallExpr)1 LiteralExpr (org.jaxen.expr.LiteralExpr)1 LogicalExpr (org.jaxen.expr.LogicalExpr)1 MultiplicativeExpr (org.jaxen.expr.MultiplicativeExpr)1 NumberExpr (org.jaxen.expr.NumberExpr)1 PathExpr (org.jaxen.expr.PathExpr)1 ProcessingInstructionNodeStep (org.jaxen.expr.ProcessingInstructionNodeStep)1 RelationalExpr (org.jaxen.expr.RelationalExpr)1 Step (org.jaxen.expr.Step)1