Search in sources :

Example 36 with NamespaceException

use of javax.jcr.NamespaceException in project jackrabbit by apache.

the class LuceneQueryBuilder method visit.

public Object visit(RelationQueryNode node, Object data) throws RepositoryException {
    PathQueryNode relPath = node.getRelativePath();
    if (relPath == null && node.getOperation() != QueryConstants.OPERATION_SIMILAR && node.getOperation() != QueryConstants.OPERATION_SPELLCHECK) {
        exceptions.add(new InvalidQueryException("@* not supported in predicate"));
        return data;
    }
    LocationStepQueryNode[] steps = relPath.getPathSteps();
    Name propertyName;
    if (node.getOperation() == QueryConstants.OPERATION_SIMILAR) {
        // this is a bit ugly:
        // use the name of a dummy property because relPath actually
        // references a property. whereas the relPath of the similar
        // operation references a node
        propertyName = NameConstants.JCR_PRIMARYTYPE;
    } else {
        propertyName = steps[steps.length - 1].getNameTest();
    }
    Query query;
    String[] stringValues = new String[1];
    switch(node.getValueType()) {
        case 0:
            // not set: either IS NULL or IS NOT NULL
            break;
        case QueryConstants.TYPE_DATE:
            stringValues[0] = DateField.dateToString(node.getDateValue());
            break;
        case QueryConstants.TYPE_DOUBLE:
            stringValues[0] = DoubleField.doubleToString(node.getDoubleValue());
            break;
        case QueryConstants.TYPE_LONG:
            stringValues[0] = LongField.longToString(node.getLongValue());
            break;
        case QueryConstants.TYPE_STRING:
            if (node.getOperation() == QueryConstants.OPERATION_EQ_GENERAL || node.getOperation() == QueryConstants.OPERATION_EQ_VALUE || node.getOperation() == QueryConstants.OPERATION_NE_GENERAL || node.getOperation() == QueryConstants.OPERATION_NE_VALUE) {
                // only use coercing on non-range operations
                stringValues = getStringValues(propertyName, node.getStringValue());
            } else {
                stringValues[0] = node.getStringValue();
            }
            break;
        case QueryConstants.TYPE_POSITION:
            // ignore position. is handled in the location step
            return null;
        default:
            throw new IllegalArgumentException("Unknown relation type: " + node.getValueType());
    }
    // get property transformation
    final int[] transform = new int[] { TransformConstants.TRANSFORM_NONE };
    node.acceptOperands(new DefaultQueryNodeVisitor() {

        public Object visit(PropertyFunctionQueryNode node, Object data) {
            if (node.getFunctionName().equals(PropertyFunctionQueryNode.LOWER_CASE)) {
                transform[0] = TransformConstants.TRANSFORM_LOWER_CASE;
            } else if (node.getFunctionName().equals(PropertyFunctionQueryNode.UPPER_CASE)) {
                transform[0] = TransformConstants.TRANSFORM_UPPER_CASE;
            }
            return data;
        }
    }, null);
    String field = "";
    try {
        field = resolver.getJCRName(propertyName);
    } catch (NamespaceException e) {
        // should never happen
        exceptions.add(e);
    }
    // support for fn:name()
    if (propertyName.equals(FN_NAME)) {
        if (node.getValueType() != QueryConstants.TYPE_STRING) {
            exceptions.add(new InvalidQueryException("Name function can " + "only be used in conjunction with a string literal"));
            return data;
        }
        if (node.getOperation() == QueryConstants.OPERATION_EQ_VALUE || node.getOperation() == QueryConstants.OPERATION_EQ_GENERAL) {
            // check if string literal is a valid XML Name
            if (XMLChar.isValidName(node.getStringValue())) {
                // parse string literal as JCR Name
                try {
                    Name n = session.getQName(ISO9075.decode(node.getStringValue()));
                    query = new NameQuery(n, indexFormatVersion, nsMappings);
                } catch (NameException e) {
                    exceptions.add(e);
                    return data;
                } catch (NamespaceException e) {
                    exceptions.add(e);
                    return data;
                }
            } else {
                // will never match -> create dummy query
                query = new BooleanQuery();
            }
        } else if (node.getOperation() == QueryConstants.OPERATION_LIKE) {
            // no coercing, see above
            if (stringValues[0].equals("%")) {
                query = new org.apache.lucene.search.MatchAllDocsQuery();
            } else {
                query = new WildcardNameQuery(stringValues[0], transform[0], session, nsMappings, cache);
            }
        } else {
            exceptions.add(new InvalidQueryException("Name function can " + "only be used in conjunction with the following operators: equals, like"));
            return data;
        }
    } else {
        switch(node.getOperation()) {
            // =
            case QueryConstants.OPERATION_EQ_VALUE:
            case QueryConstants.OPERATION_EQ_GENERAL:
                BooleanQuery or = new BooleanQuery();
                for (String value : stringValues) {
                    Term t = new Term(FieldNames.PROPERTIES, FieldNames.createNamedValue(field, value));
                    Query q;
                    if (transform[0] == TransformConstants.TRANSFORM_UPPER_CASE) {
                        q = new CaseTermQuery.Upper(t);
                    } else if (transform[0] == TransformConstants.TRANSFORM_LOWER_CASE) {
                        q = new CaseTermQuery.Lower(t);
                    } else {
                        q = new JackrabbitTermQuery(t);
                    }
                    or.add(q, Occur.SHOULD);
                }
                query = or;
                if (node.getOperation() == QueryConstants.OPERATION_EQ_VALUE) {
                    query = createSingleValueConstraint(or, field);
                }
                break;
            // >=
            case QueryConstants.OPERATION_GE_VALUE:
            case QueryConstants.OPERATION_GE_GENERAL:
                or = new BooleanQuery();
                for (String value : stringValues) {
                    Term lower = new Term(FieldNames.PROPERTIES, FieldNames.createNamedValue(field, value));
                    Term upper = new Term(FieldNames.PROPERTIES, FieldNames.createNamedValue(field, "￿"));
                    or.add(new RangeQuery(lower, upper, true, transform[0], cache), Occur.SHOULD);
                }
                query = or;
                if (node.getOperation() == QueryConstants.OPERATION_GE_VALUE) {
                    query = createSingleValueConstraint(or, field);
                }
                break;
            // >
            case QueryConstants.OPERATION_GT_VALUE:
            case QueryConstants.OPERATION_GT_GENERAL:
                or = new BooleanQuery();
                for (String value : stringValues) {
                    Term lower = new Term(FieldNames.PROPERTIES, FieldNames.createNamedValue(field, value));
                    Term upper = new Term(FieldNames.PROPERTIES, FieldNames.createNamedValue(field, "￿"));
                    or.add(new RangeQuery(lower, upper, false, transform[0], cache), Occur.SHOULD);
                }
                query = or;
                if (node.getOperation() == QueryConstants.OPERATION_GT_VALUE) {
                    query = createSingleValueConstraint(or, field);
                }
                break;
            // <=
            case QueryConstants.OPERATION_LE_VALUE:
            case // <=
            QueryConstants.OPERATION_LE_GENERAL:
                or = new BooleanQuery();
                for (String value : stringValues) {
                    Term lower = new Term(FieldNames.PROPERTIES, FieldNames.createNamedValue(field, ""));
                    Term upper = new Term(FieldNames.PROPERTIES, FieldNames.createNamedValue(field, value));
                    or.add(new RangeQuery(lower, upper, true, transform[0], cache), Occur.SHOULD);
                }
                query = or;
                if (node.getOperation() == QueryConstants.OPERATION_LE_VALUE) {
                    query = createSingleValueConstraint(query, field);
                }
                break;
            case // LIKE
            QueryConstants.OPERATION_LIKE:
                // no coercing, see above
                if (stringValues[0].equals("%")) {
                    query = Util.createMatchAllQuery(field, indexFormatVersion, cache);
                } else {
                    query = new WildcardQuery(FieldNames.PROPERTIES, field, stringValues[0], transform[0], cache);
                }
                break;
            // <
            case QueryConstants.OPERATION_LT_VALUE:
            case QueryConstants.OPERATION_LT_GENERAL:
                or = new BooleanQuery();
                for (String value : stringValues) {
                    Term lower = new Term(FieldNames.PROPERTIES, FieldNames.createNamedValue(field, ""));
                    Term upper = new Term(FieldNames.PROPERTIES, FieldNames.createNamedValue(field, value));
                    or.add(new RangeQuery(lower, upper, false, transform[0], cache), Occur.SHOULD);
                }
                query = or;
                if (node.getOperation() == QueryConstants.OPERATION_LT_VALUE) {
                    query = createSingleValueConstraint(or, field);
                }
                break;
            case // !=
            QueryConstants.OPERATION_NE_VALUE:
                // match nodes with property 'field' that includes svp and mvp
                BooleanQuery notQuery = new BooleanQuery();
                notQuery.add(Util.createMatchAllQuery(field, indexFormatVersion, cache), Occur.SHOULD);
                // exclude all nodes where 'field' has the term in question
                for (String value : stringValues) {
                    Term t = new Term(FieldNames.PROPERTIES, FieldNames.createNamedValue(field, value));
                    Query q;
                    if (transform[0] == TransformConstants.TRANSFORM_UPPER_CASE) {
                        q = new CaseTermQuery.Upper(t);
                    } else if (transform[0] == TransformConstants.TRANSFORM_LOWER_CASE) {
                        q = new CaseTermQuery.Lower(t);
                    } else {
                        q = new JackrabbitTermQuery(t);
                    }
                    notQuery.add(q, Occur.MUST_NOT);
                }
                // and exclude all nodes where 'field' is multi valued
                notQuery.add(new JackrabbitTermQuery(new Term(FieldNames.MVP, field)), Occur.MUST_NOT);
                query = notQuery;
                break;
            case // !=
            QueryConstants.OPERATION_NE_GENERAL:
                // that's:
                // all nodes with property 'field'
                // minus the nodes that have a single property 'field' that is
                //    not equal to term in question
                // minus the nodes that have a multi-valued property 'field' and
                //    all values are equal to term in question
                notQuery = new BooleanQuery();
                notQuery.add(Util.createMatchAllQuery(field, indexFormatVersion, cache), Occur.SHOULD);
                for (String value : stringValues) {
                    // exclude the nodes that have the term and are single valued
                    Term t = new Term(FieldNames.PROPERTIES, FieldNames.createNamedValue(field, value));
                    Query svp = new NotQuery(new JackrabbitTermQuery(new Term(FieldNames.MVP, field)));
                    BooleanQuery and = new BooleanQuery();
                    Query q;
                    if (transform[0] == TransformConstants.TRANSFORM_UPPER_CASE) {
                        q = new CaseTermQuery.Upper(t);
                    } else if (transform[0] == TransformConstants.TRANSFORM_LOWER_CASE) {
                        q = new CaseTermQuery.Lower(t);
                    } else {
                        q = new JackrabbitTermQuery(t);
                    }
                    and.add(q, Occur.MUST);
                    and.add(svp, Occur.MUST);
                    notQuery.add(and, Occur.MUST_NOT);
                }
                // todo above also excludes multi-valued properties that contain
                //      multiple instances of only stringValues. e.g. text={foo, foo}
                query = notQuery;
                break;
            case QueryConstants.OPERATION_NULL:
                query = new NotQuery(Util.createMatchAllQuery(field, indexFormatVersion, cache));
                break;
            case QueryConstants.OPERATION_SIMILAR:
                try {
                    NodeId id = hmgr.resolveNodePath(session.getQPath(node.getStringValue()));
                    if (id != null) {
                        query = new SimilarityQuery(id.toString(), analyzer);
                    } else {
                        query = new BooleanQuery();
                    }
                } catch (Exception e) {
                    exceptions.add(e);
                    query = new BooleanQuery();
                }
                break;
            case QueryConstants.OPERATION_NOT_NULL:
                query = Util.createMatchAllQuery(field, indexFormatVersion, cache);
                break;
            case QueryConstants.OPERATION_SPELLCHECK:
                query = Util.createMatchAllQuery(field, indexFormatVersion, cache);
                break;
            default:
                throw new IllegalArgumentException("Unknown relation operation: " + node.getOperation());
        }
    }
    if (steps.length > 1) {
        // child axis in relation
        // elements.length - 1 = property name
        // elements.length - 2 = last child axis name test
        boolean selectParent = true;
        for (int i = steps.length - 2; i >= 0; i--) {
            LocationStepQueryNode step = steps[i];
            Name name = steps[i].getNameTest();
            if (i == steps.length - 2) {
                if (step instanceof DerefQueryNode) {
                    query = createPredicateDeref(query, (DerefQueryNode) step, data);
                    if (steps.length == 2) {
                        selectParent = false;
                    }
                } else if (step != null) {
                    // join name test with property query if there is one
                    if (name != null) {
                        if (!name.equals(PARENT_ELEMENT_NAME)) {
                            Query nameTest = new NameQuery(name, indexFormatVersion, nsMappings);
                            BooleanQuery and = new BooleanQuery();
                            and.add(query, Occur.MUST);
                            and.add(nameTest, Occur.MUST);
                            query = and;
                        } else {
                            // If we're searching the parent, we want to return the child axis,
                            // not the parent because this is part of the predicate. For instance,
                            // if the query is //child[../base], this part of the code is operating
                            // on the "../base" portion. So we want to return all the child nodes
                            // of "base", which will then be matched against the non predicate part.
                            query = new ChildAxisQuery(sharedItemMgr, query, null, indexFormatVersion, nsMappings);
                            selectParent = false;
                        }
                    } else {
                    // otherwise the query can be used as is
                    }
                }
            } else if (name != null && name.equals(PARENT_ELEMENT_NAME)) {
                // We need to select one of the properties if we haven't already.
                if (selectParent) {
                    query = new ParentAxisQuery(query, null, indexFormatVersion, nsMappings);
                    selectParent = false;
                }
                // See the note above on searching parents
                query = new ChildAxisQuery(sharedItemMgr, query, null, indexFormatVersion, nsMappings);
            } else {
                if (step != null) {
                    query = new ParentAxisQuery(query, name, indexFormatVersion, nsMappings);
                } else {
                    throw new UnsupportedOperationException();
                }
            }
        }
        // finally select the parent of the selected nodes
        if (selectParent) {
            query = new ParentAxisQuery(query, null, indexFormatVersion, nsMappings);
        }
    }
    return query;
}
Also used : PropertyFunctionQueryNode(org.apache.jackrabbit.spi.commons.query.PropertyFunctionQueryNode) DerefQueryNode(org.apache.jackrabbit.spi.commons.query.DerefQueryNode) Name(org.apache.jackrabbit.spi.Name) PathQueryNode(org.apache.jackrabbit.spi.commons.query.PathQueryNode) DefaultQueryNodeVisitor(org.apache.jackrabbit.spi.commons.query.DefaultQueryNodeVisitor) LocationStepQueryNode(org.apache.jackrabbit.spi.commons.query.LocationStepQueryNode) Term(org.apache.lucene.index.Term) NameException(org.apache.jackrabbit.spi.commons.conversion.NameException) ParseException(org.apache.lucene.queryParser.ParseException) RepositoryException(javax.jcr.RepositoryException) NamespaceException(javax.jcr.NamespaceException) InvalidQueryException(javax.jcr.query.InvalidQueryException) NameException(org.apache.jackrabbit.spi.commons.conversion.NameException) NodeId(org.apache.jackrabbit.core.id.NodeId) NamespaceException(javax.jcr.NamespaceException) InvalidQueryException(javax.jcr.query.InvalidQueryException)

Example 37 with NamespaceException

use of javax.jcr.NamespaceException in project jackrabbit by apache.

the class SysViewImportHandler method endElement.

/**
     * {@inheritDoc}
     */
@Override
public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
    Name name = NameFactoryImpl.getInstance().create(namespaceURI, localName);
    // check element name
    ImportState state = stack.peek();
    if (name.equals(NameConstants.SV_NODE)) {
        // sv:node element
        if (!state.started) {
            // need to start & end current node
            processNode(state, true, true);
            state.started = true;
        } else {
            // need to end current node
            processNode(state, false, true);
        }
        // pop current state from stack
        stack.pop();
    } else if (name.equals(NameConstants.SV_PROPERTY)) {
        // have been collected and create node as necessary
        if (currentPropName.equals(NameConstants.JCR_PRIMARYTYPE)) {
            BufferedStringValue val = currentPropValues.get(0);
            String s = null;
            try {
                s = val.retrieve();
                state.nodeTypeName = resolver.getQName(s);
            } catch (IOException ioe) {
                throw new SAXException("error while retrieving value", ioe);
            } catch (NameException e) {
                throw new SAXException(new InvalidSerializedDataException("illegal node type name: " + s, e));
            } catch (NamespaceException e) {
                throw new SAXException(new InvalidSerializedDataException("illegal node type name: " + s, e));
            }
        } else if (currentPropName.equals(NameConstants.JCR_MIXINTYPES)) {
            if (state.mixinNames == null) {
                state.mixinNames = new ArrayList<Name>(currentPropValues.size());
            }
            for (BufferedStringValue val : currentPropValues) {
                String s = null;
                try {
                    s = val.retrieve();
                    Name mixin = resolver.getQName(s);
                    state.mixinNames.add(mixin);
                } catch (IOException ioe) {
                    throw new SAXException("error while retrieving value", ioe);
                } catch (NameException e) {
                    throw new SAXException(new InvalidSerializedDataException("illegal mixin type name: " + s, e));
                } catch (NamespaceException e) {
                    throw new SAXException(new InvalidSerializedDataException("illegal mixin type name: " + s, e));
                }
            }
        } else if (currentPropName.equals(NameConstants.JCR_UUID)) {
            BufferedStringValue val = currentPropValues.get(0);
            try {
                state.uuid = val.retrieve();
            } catch (IOException ioe) {
                throw new SAXException("error while retrieving value", ioe);
            }
        } else {
            if (currentPropMultipleStatus == MultipleStatus.UNKNOWN && currentPropValues.size() != 1) {
                currentPropMultipleStatus = MultipleStatus.MULTIPLE;
            }
            PropInfo prop = new PropInfo(currentPropName, currentPropType, currentPropValues.toArray(new TextValue[currentPropValues.size()]), currentPropMultipleStatus);
            state.props.add(prop);
        }
        // reset temp fields
        currentPropValues.clear();
    } else if (name.equals(NameConstants.SV_VALUE)) {
        // sv:value element
        currentPropValues.add(currentPropValue);
        // reset temp fields
        currentPropValue = null;
    } else {
        throw new SAXException(new InvalidSerializedDataException("invalid element in system view xml document: " + localName));
    }
}
Also used : NameException(org.apache.jackrabbit.spi.commons.conversion.NameException) InvalidSerializedDataException(javax.jcr.InvalidSerializedDataException) NamespaceException(javax.jcr.NamespaceException) IOException(java.io.IOException) Name(org.apache.jackrabbit.spi.Name) SAXException(org.xml.sax.SAXException)

Example 38 with NamespaceException

use of javax.jcr.NamespaceException in project jackrabbit by apache.

the class DocViewImportHandler method parseNames.

/**
     * Parses the given string as a list of JCR names. Any whitespace sequence
     * is supported as a names separator instead of just a single space to
     * be more liberal in what we accept. The current namespace context is
     * used to convert the prefixed name strings to QNames.
     *
     * @param value string value
     * @return the parsed names
     * @throws SAXException if an invalid name was encountered
     */
private Name[] parseNames(String value) throws SAXException {
    String[] names = value.split("\\p{Space}+");
    Name[] qnames = new Name[names.length];
    for (int i = 0; i < names.length; i++) {
        try {
            qnames[i] = resolver.getQName(names[i]);
        } catch (NameException ne) {
            throw new SAXException("Invalid name: " + names[i], ne);
        } catch (NamespaceException e) {
            throw new SAXException("Invalid name: " + names[i], e);
        }
    }
    return qnames;
}
Also used : NameException(org.apache.jackrabbit.spi.commons.conversion.NameException) IllegalNameException(org.apache.jackrabbit.spi.commons.conversion.IllegalNameException) NamespaceException(javax.jcr.NamespaceException) Name(org.apache.jackrabbit.spi.Name) SAXException(org.xml.sax.SAXException)

Example 39 with NamespaceException

use of javax.jcr.NamespaceException in project jackrabbit by apache.

the class EventState method getCommonPath.

/**
     * Get the longest common path of all event state paths.
     *
     * @param events The list of EventState
     * @param session The associated session; it can be null
     * @return the longest common path
     */
public static String getCommonPath(List<EventState> events, SessionImpl session) {
    String common = null;
    try {
        for (int i = 0; i < events.size(); i++) {
            EventState state = events.get(i);
            Path parentPath = state.getParentPath();
            String s;
            if (session == null) {
                s = getJCRPath(parentPath);
            } else {
                s = session.getJCRPath(parentPath);
            }
            if (common == null) {
                common = s;
            } else if (!common.equals(s)) {
                // Assign the shorter path to common.
                if (s.length() < common.length()) {
                    String temp = common;
                    common = s;
                    s = temp;
                }
                // Find the real common.
                while (!s.startsWith(common)) {
                    int idx = s.lastIndexOf('/');
                    if (idx < 0) {
                        break;
                    }
                    common = s.substring(0, idx + 1);
                }
            }
        }
    } catch (NamespaceException e) {
        log.debug("Problem in retrieving JCR path", e);
    }
    return common;
}
Also used : Path(org.apache.jackrabbit.spi.Path) NamespaceException(javax.jcr.NamespaceException)

Example 40 with NamespaceException

use of javax.jcr.NamespaceException in project jackrabbit by apache.

the class NodeIndexer method addPropertyName.

/**
     * Adds the property name to the lucene _:PROPERTIES_SET field.
     *
     * @param doc  the document.
     * @param name the name of the property.
     */
protected void addPropertyName(Document doc, Name name) {
    String fieldName = name.getLocalName();
    try {
        fieldName = resolver.getJCRName(name);
    } catch (NamespaceException e) {
    // will never happen
    }
    doc.add(new Field(FieldNames.PROPERTIES_SET, false, fieldName, Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS, Field.TermVector.NO));
}
Also used : Field(org.apache.lucene.document.Field) NamespaceException(javax.jcr.NamespaceException)

Aggregations

NamespaceException (javax.jcr.NamespaceException)56 Name (org.apache.jackrabbit.spi.Name)26 NameException (org.apache.jackrabbit.spi.commons.conversion.NameException)17 Path (org.apache.jackrabbit.spi.Path)13 InvalidQueryException (javax.jcr.query.InvalidQueryException)10 RepositoryException (javax.jcr.RepositoryException)7 PathQueryNode (org.apache.jackrabbit.spi.commons.query.PathQueryNode)7 NamespaceRegistry (javax.jcr.NamespaceRegistry)6 IllegalNameException (org.apache.jackrabbit.spi.commons.conversion.IllegalNameException)6 LocationStepQueryNode (org.apache.jackrabbit.spi.commons.query.LocationStepQueryNode)6 IOException (java.io.IOException)5 Session (javax.jcr.Session)5 DerefQueryNode (org.apache.jackrabbit.spi.commons.query.DerefQueryNode)5 PropertyFunctionQueryNode (org.apache.jackrabbit.spi.commons.query.PropertyFunctionQueryNode)5 SAXException (org.xml.sax.SAXException)5 InvalidSerializedDataException (javax.jcr.InvalidSerializedDataException)4 NodeTypeQueryNode (org.apache.jackrabbit.spi.commons.query.NodeTypeQueryNode)4 NotQueryNode (org.apache.jackrabbit.spi.commons.query.NotQueryNode)4 OrderQueryNode (org.apache.jackrabbit.spi.commons.query.OrderQueryNode)4 QueryNode (org.apache.jackrabbit.spi.commons.query.QueryNode)4