Search in sources :

Example 1 with NodeTypeQueryNode

use of org.apache.jackrabbit.spi.commons.query.NodeTypeQueryNode in project jackrabbit by apache.

the class XPathQueryBuilder method visit.

//---------------------< XPathVisitor >-------------------------------------
/**
     * Implements the generic visit method for this <code>XPathVisitor</code>.
     *
     * @param node the current node as created by the XPath parser.
     * @param data the current <code>QueryNode</code> created by this
     *             <code>XPathVisitor</code>.
     * @return the current <code>QueryNode</code>. Can be different from
     *         <code>data</code>.
     */
public Object visit(SimpleNode node, Object data) {
    QueryNode queryNode = (QueryNode) data;
    switch(node.getId()) {
        case JJTXPATH2:
            queryNode = createPathQueryNode(node);
            break;
        case JJTROOT:
        case JJTROOTDESCENDANTS:
            if (queryNode instanceof PathQueryNode) {
                ((PathQueryNode) queryNode).setAbsolute(true);
            } else {
                exceptions.add(new InvalidQueryException("Unsupported root level query node: " + queryNode));
            }
            break;
        case JJTSTEPEXPR:
            if (isAttributeAxis(node)) {
                if (queryNode.getType() == QueryNode.TYPE_RELATION || (queryNode.getType() == QueryNode.TYPE_DEREF && ((DerefQueryNode) queryNode).getRefProperty() == null) || queryNode.getType() == QueryNode.TYPE_ORDER || queryNode.getType() == QueryNode.TYPE_PATH || queryNode.getType() == QueryNode.TYPE_TEXTSEARCH) {
                    // traverse
                    node.childrenAccept(this, queryNode);
                } else if (queryNode.getType() == QueryNode.TYPE_NOT) {
                    // is null expression
                    RelationQueryNode isNull = factory.createRelationQueryNode(queryNode, RelationQueryNode.OPERATION_NULL);
                    applyRelativePath(isNull);
                    node.childrenAccept(this, isNull);
                    NotQueryNode notNode = (NotQueryNode) queryNode;
                    NAryQueryNode parent = (NAryQueryNode) notNode.getParent();
                    parent.removeOperand(notNode);
                    parent.addOperand(isNull);
                } else {
                    // not null expression
                    RelationQueryNode notNull = factory.createRelationQueryNode(queryNode, RelationQueryNode.OPERATION_NOT_NULL);
                    applyRelativePath(notNull);
                    node.childrenAccept(this, notNull);
                    ((NAryQueryNode) queryNode).addOperand(notNull);
                }
            } else {
                if (queryNode.getType() == QueryNode.TYPE_PATH) {
                    createLocationStep(node, (NAryQueryNode) queryNode);
                } else if (queryNode.getType() == QueryNode.TYPE_TEXTSEARCH || queryNode.getType() == QueryNode.TYPE_RELATION) {
                    node.childrenAccept(this, queryNode);
                } else {
                    // step within a predicate
                    RelationQueryNode tmp = factory.createRelationQueryNode(null, RelationQueryNode.OPERATION_NOT_NULL);
                    node.childrenAccept(this, tmp);
                    if (tmpRelPath == null) {
                        tmpRelPath = new PathBuilder();
                    }
                    PathQueryNode relPath = tmp.getRelativePath();
                    LocationStepQueryNode[] steps = relPath.getPathSteps();
                    Name nameTest = steps[steps.length - 1].getNameTest();
                    if (nameTest == null) {
                        // see LocationStepQueryNode javadoc on when getNameTest()==null: when it was a star (asterisk)
                        nameTest = RelationQueryNode.STAR_NAME_TEST;
                    }
                    tmpRelPath.addLast(nameTest);
                }
            }
            break;
        case JJTNAMETEST:
            if (queryNode.getType() == QueryNode.TYPE_LOCATION || queryNode.getType() == QueryNode.TYPE_DEREF || queryNode.getType() == QueryNode.TYPE_RELATION || queryNode.getType() == QueryNode.TYPE_TEXTSEARCH || queryNode.getType() == QueryNode.TYPE_PATH) {
                createNodeTest(node, queryNode);
            } else if (queryNode.getType() == QueryNode.TYPE_ORDER) {
                setOrderSpecPath(node, (OrderQueryNode) queryNode);
            } else {
                // traverse
                node.childrenAccept(this, queryNode);
            }
            break;
        case JJTELEMENTNAMEORWILDCARD:
            if (queryNode.getType() == QueryNode.TYPE_LOCATION) {
                SimpleNode child = (SimpleNode) node.jjtGetChild(0);
                if (child.getId() != JJTANYNAME) {
                    createNodeTest(child, queryNode);
                }
            }
            break;
        case JJTTEXTTEST:
            if (queryNode.getType() == QueryNode.TYPE_LOCATION) {
                LocationStepQueryNode loc = (LocationStepQueryNode) queryNode;
                loc.setNameTest(JCR_XMLTEXT);
            }
            break;
        case JJTTYPENAME:
            if (queryNode.getType() == QueryNode.TYPE_LOCATION) {
                LocationStepQueryNode loc = (LocationStepQueryNode) queryNode;
                String ntName = ((SimpleNode) node.jjtGetChild(0)).getValue();
                try {
                    Name nt = resolver.getQName(ntName);
                    NodeTypeQueryNode nodeType = factory.createNodeTypeQueryNode(loc, nt);
                    loc.addPredicate(nodeType);
                } catch (NameException e) {
                    exceptions.add(new InvalidQueryException("Not a valid name: " + ntName));
                } catch (NamespaceException e) {
                    exceptions.add(new InvalidQueryException("Not a valid name: " + ntName));
                }
            }
            break;
        case JJTOREXPR:
            NAryQueryNode parent = (NAryQueryNode) queryNode;
            QueryNode orQueryNode = factory.createOrQueryNode(parent);
            parent.addOperand(orQueryNode);
            // traverse
            node.childrenAccept(this, orQueryNode);
            break;
        case JJTANDEXPR:
            parent = (NAryQueryNode) queryNode;
            QueryNode andQueryNode = factory.createAndQueryNode(parent);
            parent.addOperand(andQueryNode);
            // traverse
            node.childrenAccept(this, andQueryNode);
            break;
        case JJTCOMPARISONEXPR:
            createExpression(node, (NAryQueryNode) queryNode);
            break;
        case JJTSTRINGLITERAL:
        case JJTDECIMALLITERAL:
        case JJTDOUBLELITERAL:
        case JJTINTEGERLITERAL:
            if (queryNode.getType() == QueryNode.TYPE_RELATION) {
                assignValue(node, (RelationQueryNode) queryNode);
            } else if (queryNode.getType() == QueryNode.TYPE_LOCATION) {
                if (node.getId() == JJTINTEGERLITERAL) {
                    int index = Integer.parseInt(node.getValue());
                    ((LocationStepQueryNode) queryNode).setIndex(index);
                } else {
                    exceptions.add(new InvalidQueryException("LocationStep only allows integer literal as position index"));
                }
            } else {
                exceptions.add(new InvalidQueryException("Parse error: data is not a RelationQueryNode"));
            }
            break;
        case JJTUNARYMINUS:
            if (queryNode.getType() == QueryNode.TYPE_RELATION) {
                ((RelationQueryNode) queryNode).setUnaryMinus(true);
            } else {
                exceptions.add(new InvalidQueryException("Parse error: data is not a RelationQueryNode"));
            }
            break;
        case JJTFUNCTIONCALL:
            queryNode = createFunction(node, queryNode);
            break;
        case JJTORDERBYCLAUSE:
            root.setOrderNode(factory.createOrderQueryNode(root));
            queryNode = root.getOrderNode();
            node.childrenAccept(this, queryNode);
            break;
        case JJTORDERSPEC:
            OrderQueryNode orderQueryNode = (OrderQueryNode) queryNode;
            orderQueryNode.newOrderSpec();
            node.childrenAccept(this, queryNode);
            if (!orderQueryNode.isValid()) {
                exceptions.add(new InvalidQueryException("Invalid order specification. (Missing @?)"));
            }
            break;
        case JJTORDERMODIFIER:
            if (node.jjtGetNumChildren() > 0 && ((SimpleNode) node.jjtGetChild(0)).getId() == JJTDESCENDING) {
                ((OrderQueryNode) queryNode).setAscending(false);
            }
            break;
        case JJTPREDICATELIST:
            if (queryNode.getType() == QueryNode.TYPE_PATH) {
                // switch to last location
                QueryNode[] operands = ((PathQueryNode) queryNode).getOperands();
                queryNode = operands[operands.length - 1];
            }
            node.childrenAccept(this, queryNode);
            break;
        case JJTPREDICATE:
            if (queryNode.getType() == QueryNode.TYPE_LOCATION || queryNode.getType() == QueryNode.TYPE_DEREF) {
                node.childrenAccept(this, queryNode);
            } else {
                // predicate not allowed here
                exceptions.add(new InvalidQueryException("Unsupported location for predicate"));
            }
            break;
        case JJTDOTDOT:
            if (queryNode instanceof LocationStepQueryNode) {
                ((LocationStepQueryNode) queryNode).setNameTest(PATH_FACTORY.getParentElement().getName());
            } else {
                ((RelationQueryNode) queryNode).addPathElement(PATH_FACTORY.getParentElement());
            }
            break;
        default:
            // per default traverse
            node.childrenAccept(this, queryNode);
    }
    return queryNode;
}
Also used : NAryQueryNode(org.apache.jackrabbit.spi.commons.query.NAryQueryNode) OrderQueryNode(org.apache.jackrabbit.spi.commons.query.OrderQueryNode) LocationStepQueryNode(org.apache.jackrabbit.spi.commons.query.LocationStepQueryNode) RelationQueryNode(org.apache.jackrabbit.spi.commons.query.RelationQueryNode) Name(org.apache.jackrabbit.spi.Name) PathQueryNode(org.apache.jackrabbit.spi.commons.query.PathQueryNode) PathBuilder(org.apache.jackrabbit.spi.commons.name.PathBuilder) NameException(org.apache.jackrabbit.spi.commons.conversion.NameException) IllegalNameException(org.apache.jackrabbit.spi.commons.conversion.IllegalNameException) RelationQueryNode(org.apache.jackrabbit.spi.commons.query.RelationQueryNode) OrderQueryNode(org.apache.jackrabbit.spi.commons.query.OrderQueryNode) QueryNode(org.apache.jackrabbit.spi.commons.query.QueryNode) DerefQueryNode(org.apache.jackrabbit.spi.commons.query.DerefQueryNode) NAryQueryNode(org.apache.jackrabbit.spi.commons.query.NAryQueryNode) NotQueryNode(org.apache.jackrabbit.spi.commons.query.NotQueryNode) NodeTypeQueryNode(org.apache.jackrabbit.spi.commons.query.NodeTypeQueryNode) PathQueryNode(org.apache.jackrabbit.spi.commons.query.PathQueryNode) TextsearchQueryNode(org.apache.jackrabbit.spi.commons.query.TextsearchQueryNode) PropertyFunctionQueryNode(org.apache.jackrabbit.spi.commons.query.PropertyFunctionQueryNode) LocationStepQueryNode(org.apache.jackrabbit.spi.commons.query.LocationStepQueryNode) NamespaceException(javax.jcr.NamespaceException) NotQueryNode(org.apache.jackrabbit.spi.commons.query.NotQueryNode) InvalidQueryException(javax.jcr.query.InvalidQueryException) NodeTypeQueryNode(org.apache.jackrabbit.spi.commons.query.NodeTypeQueryNode)

Example 2 with NodeTypeQueryNode

use of org.apache.jackrabbit.spi.commons.query.NodeTypeQueryNode in project jackrabbit by apache.

the class QueryFormat method visit.

public Object visit(LocationStepQueryNode node, Object data) throws RepositoryException {
    StringBuffer sb = (StringBuffer) data;
    if (node.getIncludeDescendants()) {
        sb.append('/');
    }
    final Name[] nodeType = new Name[1];
    node.acceptOperands(new DefaultQueryNodeVisitor() {

        public Object visit(NodeTypeQueryNode node, Object data) {
            nodeType[0] = node.getValue();
            return data;
        }
    }, null);
    if (nodeType[0] != null) {
        sb.append("element(");
    }
    if (node.getNameTest() == null) {
        sb.append("*");
    } else {
        try {
            if (node.getNameTest().getLocalName().length() == 0) {
                sb.append(resolver.getJCRName(XPathQueryBuilder.JCR_ROOT));
            } else {
                sb.append(resolver.getJCRName(encode(node.getNameTest())));
            }
        } catch (NamespaceException e) {
            exceptions.add(e);
        }
    }
    if (nodeType[0] != null) {
        sb.append(", ");
        try {
            sb.append(resolver.getJCRName(encode(nodeType[0])));
        } catch (NamespaceException e) {
            exceptions.add(e);
        }
        sb.append(")");
    }
    if (node.getIndex() != LocationStepQueryNode.NONE) {
        sb.append('[').append(node.getIndex()).append(']');
    }
    QueryNode[] predicates = node.getPredicates();
    for (int i = 0; i < predicates.length; i++) {
        // ignore node type query nodes
        if (predicates[i].getType() == QueryNode.TYPE_NODETYPE) {
            continue;
        }
        sb.append('[');
        predicates[i].accept(this, sb);
        sb.append(']');
    }
    return sb;
}
Also used : ExactQueryNode(org.apache.jackrabbit.spi.commons.query.ExactQueryNode) RelationQueryNode(org.apache.jackrabbit.spi.commons.query.RelationQueryNode) OrQueryNode(org.apache.jackrabbit.spi.commons.query.OrQueryNode) OrderQueryNode(org.apache.jackrabbit.spi.commons.query.OrderQueryNode) QueryNode(org.apache.jackrabbit.spi.commons.query.QueryNode) DerefQueryNode(org.apache.jackrabbit.spi.commons.query.DerefQueryNode) AndQueryNode(org.apache.jackrabbit.spi.commons.query.AndQueryNode) NotQueryNode(org.apache.jackrabbit.spi.commons.query.NotQueryNode) NodeTypeQueryNode(org.apache.jackrabbit.spi.commons.query.NodeTypeQueryNode) PathQueryNode(org.apache.jackrabbit.spi.commons.query.PathQueryNode) TextsearchQueryNode(org.apache.jackrabbit.spi.commons.query.TextsearchQueryNode) PropertyFunctionQueryNode(org.apache.jackrabbit.spi.commons.query.PropertyFunctionQueryNode) LocationStepQueryNode(org.apache.jackrabbit.spi.commons.query.LocationStepQueryNode) NamespaceException(javax.jcr.NamespaceException) DefaultQueryNodeVisitor(org.apache.jackrabbit.spi.commons.query.DefaultQueryNodeVisitor) NodeTypeQueryNode(org.apache.jackrabbit.spi.commons.query.NodeTypeQueryNode) Name(org.apache.jackrabbit.spi.Name)

Example 3 with NodeTypeQueryNode

use of org.apache.jackrabbit.spi.commons.query.NodeTypeQueryNode in project jackrabbit by apache.

the class QueryImpl method getColumns.

/**
     * Returns the columns for this query.
     *
     * @return array of columns.
     * @throws RepositoryException if an error occurs.
     */
protected ColumnImpl[] getColumns() throws RepositoryException {
    SessionImpl session = sessionContext.getSessionImpl();
    QueryObjectModelFactory qomFactory = session.getWorkspace().getQueryManager().getQOMFactory();
    // get columns
    Map<Name, ColumnImpl> columns = new LinkedHashMap<Name, ColumnImpl>();
    for (Name name : root.getSelectProperties()) {
        String pn = sessionContext.getJCRName(name);
        ColumnImpl col = (ColumnImpl) qomFactory.column(sessionContext.getJCRName(DEFAULT_SELECTOR_NAME), pn, pn);
        columns.put(name, col);
    }
    if (columns.size() == 0) {
        // use node type constraint
        LocationStepQueryNode[] steps = root.getLocationNode().getPathSteps();
        final Name[] ntName = new Name[1];
        steps[steps.length - 1].acceptOperands(new DefaultQueryNodeVisitor() {

            public Object visit(AndQueryNode node, Object data) throws RepositoryException {
                return node.acceptOperands(this, data);
            }

            public Object visit(NodeTypeQueryNode node, Object data) {
                ntName[0] = node.getValue();
                return data;
            }
        }, null);
        if (ntName[0] == null) {
            ntName[0] = NameConstants.NT_BASE;
        }
        NodeTypeImpl nt = session.getNodeTypeManager().getNodeType(ntName[0]);
        PropertyDefinition[] propDefs = nt.getPropertyDefinitions();
        for (PropertyDefinition pd : propDefs) {
            QPropertyDefinition propDef = ((PropertyDefinitionImpl) pd).unwrap();
            if (!propDef.definesResidual() && !propDef.isMultiple()) {
                columns.put(propDef.getName(), columnForName(propDef.getName()));
            }
        }
    }
    // add jcr:path and jcr:score if not selected already
    if (!columns.containsKey(NameConstants.JCR_PATH)) {
        columns.put(NameConstants.JCR_PATH, columnForName(NameConstants.JCR_PATH));
    }
    if (!columns.containsKey(NameConstants.JCR_SCORE)) {
        columns.put(NameConstants.JCR_SCORE, columnForName(NameConstants.JCR_SCORE));
    }
    return columns.values().toArray(new ColumnImpl[columns.size()]);
}
Also used : NodeTypeImpl(org.apache.jackrabbit.core.nodetype.NodeTypeImpl) LocationStepQueryNode(org.apache.jackrabbit.spi.commons.query.LocationStepQueryNode) PropertyDefinitionImpl(org.apache.jackrabbit.spi.commons.nodetype.PropertyDefinitionImpl) RepositoryException(javax.jcr.RepositoryException) QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition) LinkedHashMap(java.util.LinkedHashMap) Name(org.apache.jackrabbit.spi.Name) QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) SessionImpl(org.apache.jackrabbit.core.SessionImpl) QueryObjectModelFactory(javax.jcr.query.qom.QueryObjectModelFactory) ColumnImpl(org.apache.jackrabbit.spi.commons.query.qom.ColumnImpl) DefaultQueryNodeVisitor(org.apache.jackrabbit.spi.commons.query.DefaultQueryNodeVisitor) NodeTypeQueryNode(org.apache.jackrabbit.spi.commons.query.NodeTypeQueryNode) AndQueryNode(org.apache.jackrabbit.spi.commons.query.AndQueryNode)

Example 4 with NodeTypeQueryNode

use of org.apache.jackrabbit.spi.commons.query.NodeTypeQueryNode in project jackrabbit by apache.

the class JCRSQLQueryBuilder method visit.

public Object visit(ASTQuery node, Object data) {
    root = factory.createQueryRootNode();
    root.setLocationNode(factory.createPathQueryNode(root));
    // pass to select, from, where, ...
    node.childrenAccept(this, root);
    // use //* if no path has been set
    PathQueryNode pathNode = root.getLocationNode();
    pathNode.setAbsolute(true);
    if (pathConstraints.size() == 0) {
        LocationStepQueryNode step = factory.createLocationStepQueryNode(pathNode);
        step.setNameTest(null);
        step.setIncludeDescendants(true);
        pathNode.addPathStep(step);
    } else {
        try {
            while (pathConstraints.size() > 1) {
                // merge path nodes
                MergingPathQueryNode path = null;
                for (Iterator it = pathConstraints.iterator(); it.hasNext(); ) {
                    path = (MergingPathQueryNode) it.next();
                    if (path.needsMerge()) {
                        break;
                    } else {
                        path = null;
                    }
                }
                if (path == null) {
                    throw new IllegalArgumentException("Invalid combination of jcr:path clauses");
                } else {
                    pathConstraints.remove(path);
                    MergingPathQueryNode[] paths = (MergingPathQueryNode[]) pathConstraints.toArray(new MergingPathQueryNode[pathConstraints.size()]);
                    paths = path.doMerge(paths);
                    pathConstraints.clear();
                    pathConstraints.addAll(Arrays.asList(paths));
                }
            }
        } catch (NoSuchElementException e) {
            throw new IllegalArgumentException("Invalid combination of jcr:path clauses");
        }
        MergingPathQueryNode path = (MergingPathQueryNode) pathConstraints.get(0);
        LocationStepQueryNode[] steps = path.getPathSteps();
        for (int i = 0; i < steps.length; i++) {
            LocationStepQueryNode step = factory.createLocationStepQueryNode(pathNode);
            step.setNameTest(steps[i].getNameTest());
            step.setIncludeDescendants(steps[i].getIncludeDescendants());
            step.setIndex(steps[i].getIndex());
            pathNode.addPathStep(step);
        }
    }
    if (constraintNode.getNumOperands() == 1) {
        // attach operand to last path step
        LocationStepQueryNode[] steps = pathNode.getPathSteps();
        steps[steps.length - 1].addPredicate(constraintNode.getOperands()[0]);
    } else if (constraintNode.getNumOperands() > 1) {
        // attach constraint to last path step
        LocationStepQueryNode[] steps = pathNode.getPathSteps();
        steps[steps.length - 1].addPredicate(constraintNode);
    }
    if (nodeTypeName != null) {
        // add node type constraint
        LocationStepQueryNode[] steps = pathNode.getPathSteps();
        NodeTypeQueryNode nodeType = factory.createNodeTypeQueryNode(steps[steps.length - 1], nodeTypeName);
        steps[steps.length - 1].addPredicate(nodeType);
    }
    return root;
}
Also used : PathQueryNode(org.apache.jackrabbit.spi.commons.query.PathQueryNode) LocationStepQueryNode(org.apache.jackrabbit.spi.commons.query.LocationStepQueryNode) Iterator(java.util.Iterator) NoSuchElementException(java.util.NoSuchElementException) NodeTypeQueryNode(org.apache.jackrabbit.spi.commons.query.NodeTypeQueryNode)

Aggregations

LocationStepQueryNode (org.apache.jackrabbit.spi.commons.query.LocationStepQueryNode)4 NodeTypeQueryNode (org.apache.jackrabbit.spi.commons.query.NodeTypeQueryNode)4 Name (org.apache.jackrabbit.spi.Name)3 PathQueryNode (org.apache.jackrabbit.spi.commons.query.PathQueryNode)3 NamespaceException (javax.jcr.NamespaceException)2 AndQueryNode (org.apache.jackrabbit.spi.commons.query.AndQueryNode)2 DefaultQueryNodeVisitor (org.apache.jackrabbit.spi.commons.query.DefaultQueryNodeVisitor)2 DerefQueryNode (org.apache.jackrabbit.spi.commons.query.DerefQueryNode)2 NotQueryNode (org.apache.jackrabbit.spi.commons.query.NotQueryNode)2 OrderQueryNode (org.apache.jackrabbit.spi.commons.query.OrderQueryNode)2 PropertyFunctionQueryNode (org.apache.jackrabbit.spi.commons.query.PropertyFunctionQueryNode)2 QueryNode (org.apache.jackrabbit.spi.commons.query.QueryNode)2 RelationQueryNode (org.apache.jackrabbit.spi.commons.query.RelationQueryNode)2 TextsearchQueryNode (org.apache.jackrabbit.spi.commons.query.TextsearchQueryNode)2 Iterator (java.util.Iterator)1 LinkedHashMap (java.util.LinkedHashMap)1 NoSuchElementException (java.util.NoSuchElementException)1 RepositoryException (javax.jcr.RepositoryException)1 PropertyDefinition (javax.jcr.nodetype.PropertyDefinition)1 InvalidQueryException (javax.jcr.query.InvalidQueryException)1