Search in sources :

Example 46 with NamespaceException

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

the class QueryFormat method visit.

//-------------< QueryNodeVisitor interface >-------------------------------
public Object visit(QueryRootNode node, Object data) throws RepositoryException {
    StringBuffer sb = (StringBuffer) data;
    try {
        sb.append("SELECT");
        Name[] selectProps = node.getSelectProperties();
        if (selectProps.length == 0) {
            sb.append(" *");
        } else {
            String comma = "";
            for (int i = 0; i < selectProps.length; i++) {
                sb.append(comma).append(" ");
                appendName(selectProps[i], resolver, sb);
                comma = ",";
            }
        }
        sb.append(" FROM");
        // node type restrictions are within predicates of location nodes
        // therefore we write the where clause first to a temp string to
        // collect the node types.
        StringBuffer tmp = new StringBuffer();
        LocationStepQueryNode[] steps = node.getLocationNode().getPathSteps();
        QueryNode[] predicates = steps[steps.length - 1].getPredicates();
        // are there any relevant predicates?
        for (int i = 0; i < predicates.length; i++) {
            if (predicates[i].getType() != QueryNode.TYPE_NODETYPE) {
                tmp.append(" WHERE ");
            }
        }
        String and = "";
        for (int i = 0; i < predicates.length; i++) {
            if (predicates[i].getType() != QueryNode.TYPE_NODETYPE) {
                tmp.append(and);
                and = " AND ";
            }
            predicates[i].accept(this, tmp);
        }
        // node types have been collected by now
        String comma = "";
        int ntCount = 0;
        for (Iterator it = nodeTypes.iterator(); it.hasNext(); ntCount++) {
            Name nt = (Name) it.next();
            sb.append(comma).append(" ");
            appendName(nt, resolver, sb);
            comma = ",";
        }
        if (ntCount == 0) {
            sb.append(" ");
            sb.append(resolver.getJCRName(NameConstants.NT_BASE));
        }
        // append WHERE clause
        sb.append(tmp.toString());
        if (steps.length == 2 && steps[1].getIncludeDescendants() && steps[1].getNameTest() == null) {
        // then this query selects all paths
        } else if (steps.length == 1 && steps[0].getIncludeDescendants() && steps[0].getNameTest() == null) {
        // then this query selects all paths
        } else {
            if (predicates.length > 0) {
                sb.append(" AND ");
            } else {
                sb.append(" WHERE ");
            }
            node.getLocationNode().accept(this, sb);
        }
    } catch (NamespaceException e) {
        exceptions.add(e);
    }
    if (node.getOrderNode() != null) {
        node.getOrderNode().accept(this, sb);
    }
    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) LocationStepQueryNode(org.apache.jackrabbit.spi.commons.query.LocationStepQueryNode) Iterator(java.util.Iterator) NamespaceException(javax.jcr.NamespaceException) Name(org.apache.jackrabbit.spi.Name)

Example 47 with NamespaceException

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

the class QueryFormat method visit.

public Object visit(TextsearchQueryNode node, Object data) {
    StringBuffer sb = (StringBuffer) data;
    // escape quote
    String query = node.getQuery().replaceAll("'", "''");
    sb.append("CONTAINS(");
    if (node.getRelativePath() == null) {
        sb.append("*");
    } else {
        if (node.getRelativePath().getLength() > 1 || !node.getReferencesProperty()) {
            exceptions.add(new InvalidQueryException("Child axis not supported in SQL"));
        } else {
            try {
                appendName(node.getRelativePath().getName(), resolver, sb);
            } catch (NamespaceException e) {
                exceptions.add(e);
            }
        }
    }
    sb.append(", '");
    sb.append(query).append("')");
    return sb;
}
Also used : NamespaceException(javax.jcr.NamespaceException) InvalidQueryException(javax.jcr.query.InvalidQueryException)

Example 48 with NamespaceException

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

the class QueryFormat method visit.

public Object visit(ExactQueryNode node, Object data) {
    StringBuffer sb = (StringBuffer) data;
    sb.append("@");
    try {
        Name name = encode(node.getPropertyName());
        sb.append(resolver.getJCRName(name));
        sb.append("='");
        sb.append(resolver.getJCRName(node.getValue()));
    } catch (NamespaceException e) {
        exceptions.add(e);
    }
    sb.append("'");
    return sb;
}
Also used : NamespaceException(javax.jcr.NamespaceException) Name(org.apache.jackrabbit.spi.Name)

Example 49 with NamespaceException

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

the class ReadWriteNamespaceRegistry method unregisterNamespace.

@Override
public void unregisterNamespace(String prefix) throws RepositoryException {
    if (prefix.isEmpty()) {
        throw new NamespaceException("Cannot unregister the default empty namespace");
    }
    Root root = getWriteRoot();
    Tree namespaces = root.getTree(NAMESPACES_PATH);
    if (!namespaces.exists() || !namespaces.hasProperty(prefix)) {
        throw new NamespaceException("Namespace mapping from " + prefix + " to " + getURI(prefix) + " can not be unregistered");
    }
    try {
        namespaces.removeProperty(prefix);
        root.commit();
        refresh();
    } catch (CommitFailedException e) {
        String message = "Failed to unregister namespace mapping for prefix " + prefix;
        throw e.asRepositoryException(message);
    }
}
Also used : Root(org.apache.jackrabbit.oak.api.Root) NamespaceException(javax.jcr.NamespaceException) Tree(org.apache.jackrabbit.oak.api.Tree) CommitFailedException(org.apache.jackrabbit.oak.api.CommitFailedException)

Example 50 with NamespaceException

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

the class ReadWriteNamespaceRegistry method registerNamespace.

//--------------------------------------------------< NamespaceRegistry >---
@Override
public void registerNamespace(String prefix, String uri) throws RepositoryException {
    if (prefix.isEmpty() && uri.isEmpty()) {
        // the default empty namespace is always registered
        return;
    } else if (prefix.isEmpty() || uri.isEmpty()) {
        throw new NamespaceException("Cannot remap the default empty namespace");
    }
    PropertyState property = namespaces.getProperty(prefix);
    if (property != null && property.getType() == STRING && uri.equals(property.getValue(STRING))) {
        // common case: namespace already registered -> do nothing
        return;
    }
    try {
        Root root = getWriteRoot();
        Tree namespaces = root.getTree(NAMESPACES_PATH);
        // remove existing mapping to given URI
        for (PropertyState mapping : namespaces.getProperties()) {
            if (mapping.getType() == STRING && uri.equals(mapping.getValue(STRING))) {
                namespaces.removeProperty(mapping.getName());
            }
        }
        // add this mapping (overrides existing mapping with same prefix)
        namespaces.setProperty(prefix, uri);
        root.commit();
        refresh();
    } catch (CommitFailedException e) {
        throw e.asRepositoryException("Failed to register namespace mapping " + prefix + " -> " + uri);
    }
}
Also used : Root(org.apache.jackrabbit.oak.api.Root) NamespaceException(javax.jcr.NamespaceException) Tree(org.apache.jackrabbit.oak.api.Tree) CommitFailedException(org.apache.jackrabbit.oak.api.CommitFailedException) PropertyState(org.apache.jackrabbit.oak.api.PropertyState)

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