Search in sources :

Example 26 with NameException

use of org.apache.jackrabbit.spi.commons.conversion.NameException in project jackrabbit by apache.

the class NodeTypeManagerImpl method registerNodeTypes.

/**
     * Registers the node types defined in the given input stream depending
     * on the content type specified for the stream. This will also register
     * any namespaces identified in the input stream if they have not already
     * been registered.
     *
     * @param in node type XML stream
     * @param contentType type of the input stream
     * @param reregisterExisting flag indicating whether node types should be
     *                           reregistered if they already exist
     * @return registered node types
     * @throws IOException if the input stream could not be read or parsed
     * @throws RepositoryException if the node types are invalid or another
     *                             repository error occurs
     */
public NodeType[] registerNodeTypes(InputStream in, String contentType, boolean reregisterExisting) throws IOException, RepositoryException {
    // make sure the editing session is allowed to register node types.
    context.getAccessManager().checkRepositoryPermission(Permission.NODE_TYPE_DEF_MNGMT);
    try {
        Map<String, String> namespaceMap = new HashMap<String, String>();
        List<QNodeTypeDefinition> nodeTypeDefs = new ArrayList<QNodeTypeDefinition>();
        if (contentType.equalsIgnoreCase(TEXT_XML) || contentType.equalsIgnoreCase(APPLICATION_XML)) {
            try {
                NodeTypeReader ntr = new NodeTypeReader(in);
                Properties namespaces = ntr.getNamespaces();
                if (namespaces != null) {
                    Enumeration<?> prefixes = namespaces.propertyNames();
                    while (prefixes.hasMoreElements()) {
                        String prefix = (String) prefixes.nextElement();
                        String uri = namespaces.getProperty(prefix);
                        namespaceMap.put(prefix, uri);
                    }
                }
                QNodeTypeDefinition[] defs = ntr.getNodeTypeDefs();
                nodeTypeDefs.addAll(Arrays.asList(defs));
            } catch (NameException e) {
                throw new RepositoryException("Illegal JCR name", e);
            }
        } else if (contentType.equalsIgnoreCase(TEXT_X_JCR_CND)) {
            try {
                NamespaceMapping mapping = new NamespaceMapping(context.getSessionImpl());
                CompactNodeTypeDefReader<QNodeTypeDefinition, NamespaceMapping> reader = new CompactNodeTypeDefReader<QNodeTypeDefinition, NamespaceMapping>(new InputStreamReader(in), "cnd input stream", mapping, new QDefinitionBuilderFactory());
                namespaceMap.putAll(mapping.getPrefixToURIMapping());
                for (QNodeTypeDefinition ntDef : reader.getNodeTypeDefinitions()) {
                    nodeTypeDefs.add(ntDef);
                }
            } catch (ParseException e) {
                IOException e2 = new IOException(e.getMessage());
                e2.initCause(e);
                throw e2;
            }
        } else {
            throw new UnsupportedRepositoryOperationException("Unsupported content type: " + contentType);
        }
        new NamespaceHelper(context.getSessionImpl()).registerNamespaces(namespaceMap);
        if (reregisterExisting) {
            NodeTypeRegistry registry = context.getNodeTypeRegistry();
            // split the node types into new and already registered node types.
            // this way we can register new node types together with already
            // registered node types which make circular dependencies possible
            List<QNodeTypeDefinition> newNodeTypeDefs = new ArrayList<QNodeTypeDefinition>();
            List<QNodeTypeDefinition> registeredNodeTypeDefs = new ArrayList<QNodeTypeDefinition>();
            for (QNodeTypeDefinition nodeTypeDef : nodeTypeDefs) {
                if (registry.isRegistered(nodeTypeDef.getName())) {
                    registeredNodeTypeDefs.add(nodeTypeDef);
                } else {
                    newNodeTypeDefs.add(nodeTypeDef);
                }
            }
            ArrayList<NodeType> nodeTypes = new ArrayList<NodeType>();
            // register new node types
            nodeTypes.addAll(registerNodeTypes(newNodeTypeDefs));
            // re-register already existing node types
            for (QNodeTypeDefinition nodeTypeDef : registeredNodeTypeDefs) {
                registry.reregisterNodeType(nodeTypeDef);
                nodeTypes.add(getNodeType(nodeTypeDef.getName()));
            }
            return nodeTypes.toArray(new NodeType[nodeTypes.size()]);
        } else {
            Collection<NodeType> types = registerNodeTypes(nodeTypeDefs);
            return types.toArray(new NodeType[types.size()]);
        }
    } catch (InvalidNodeTypeDefException e) {
        throw new RepositoryException("Invalid node type definition", e);
    }
}
Also used : UnsupportedRepositoryOperationException(javax.jcr.UnsupportedRepositoryOperationException) HashMap(java.util.HashMap) NamespaceMapping(org.apache.jackrabbit.spi.commons.namespace.NamespaceMapping) ArrayList(java.util.ArrayList) Properties(java.util.Properties) QNodeTypeDefinition(org.apache.jackrabbit.spi.QNodeTypeDefinition) NamespaceHelper(org.apache.jackrabbit.commons.NamespaceHelper) QDefinitionBuilderFactory(org.apache.jackrabbit.spi.commons.nodetype.QDefinitionBuilderFactory) CompactNodeTypeDefReader(org.apache.jackrabbit.commons.cnd.CompactNodeTypeDefReader) InputStreamReader(java.io.InputStreamReader) NodeTypeReader(org.apache.jackrabbit.core.nodetype.xml.NodeTypeReader) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) NameException(org.apache.jackrabbit.spi.commons.conversion.NameException) NodeType(javax.jcr.nodetype.NodeType) ParseException(org.apache.jackrabbit.commons.cnd.ParseException)

Example 27 with NameException

use of org.apache.jackrabbit.spi.commons.conversion.NameException in project jackrabbit by apache.

the class NodeTypeManagerImpl method unregisterNodeTypes.

/**
     * Unregisters the specified set of node types. Used to unregister a set of node types with mutual dependencies.
     * <p>
     * Throws a <code>NoSuchNodeTypeException</code> if one of the names listed is not a registered node type.
     * <p>
     * Throws an <code>UnsupportedRepositoryOperationException</code>
     * if this implementation does not support node type registration.
     *
     * @param names a <code>String</code> array
     * @throws UnsupportedRepositoryOperationException if this implementation does not support node type registration.
     * @throws NoSuchNodeTypeException if one of the names listed is not a registered node type.
     * @throws RepositoryException if another error occurs.
     * @since JCR 2.0
     */
public void unregisterNodeTypes(String[] names) throws UnsupportedRepositoryOperationException, NoSuchNodeTypeException, RepositoryException {
    // make sure the editing session is allowed to un-register node types.
    context.getAccessManager().checkRepositoryPermission(Permission.NODE_TYPE_DEF_MNGMT);
    Set<Name> ntNames = new HashSet<Name>();
    for (String name : names) {
        try {
            ntNames.add(context.getQName(name));
        } catch (NamespaceException e) {
            throw new RepositoryException("Invalid name: " + name, e);
        } catch (NameException e) {
            throw new RepositoryException("Invalid name: " + name, e);
        }
    }
    getNodeTypeRegistry().unregisterNodeTypes(ntNames);
}
Also used : NameException(org.apache.jackrabbit.spi.commons.conversion.NameException) NamespaceException(javax.jcr.NamespaceException) RepositoryException(javax.jcr.RepositoryException) HashSet(java.util.HashSet) Name(org.apache.jackrabbit.spi.Name)

Example 28 with NameException

use of org.apache.jackrabbit.spi.commons.conversion.NameException in project jackrabbit by apache.

the class NodeTypeImpl method canSetProperty.

/**
     * {@inheritDoc}
     */
public boolean canSetProperty(String propertyName, Value[] values) {
    if (values == null) {
        // setting a property to null is equivalent of removing it
        return canRemoveItem(propertyName);
    }
    try {
        Name name = resolver.getQName(propertyName);
        // determine type of values
        int type = PropertyType.UNDEFINED;
        for (Value value : values) {
            if (value == null) {
                // skip null values as those would be purged
                continue;
            }
            if (type == PropertyType.UNDEFINED) {
                type = value.getType();
            } else if (type != value.getType()) {
                // inhomogeneous types
                return false;
            }
        }
        QPropertyDefinition def;
        try {
            // try to get definition that matches the given value type
            def = ent.getApplicablePropertyDef(name, type, true);
        } catch (ConstraintViolationException cve) {
            // fallback: ignore type
            def = ent.getApplicablePropertyDef(name, PropertyType.UNDEFINED, true);
        }
        if (def.isProtected()) {
            return false;
        }
        if (!def.isMultiple()) {
            return false;
        }
        // determine target type
        int targetType;
        if (def.getRequiredType() != PropertyType.UNDEFINED && def.getRequiredType() != type) {
            // type conversion required
            targetType = def.getRequiredType();
        } else {
            // no type conversion required
            targetType = type;
        }
        List<InternalValue> list = new ArrayList<InternalValue>();
        // convert values and compact array (purge null entries)
        for (Value value : values) {
            if (value != null) {
                // perform type conversion as necessary and create InternalValue
                // from (converted) Value
                InternalValue internalValue;
                if (targetType != type) {
                    // type conversion required
                    Value targetVal = ValueHelper.convert(value, targetType, valueFactory);
                    internalValue = InternalValue.create(targetVal, resolver, store);
                } else {
                    // no type conversion required
                    internalValue = InternalValue.create(value, resolver, store);
                }
                list.add(internalValue);
            }
        }
        InternalValue[] internalValues = list.toArray(new InternalValue[list.size()]);
        EffectiveNodeType.checkSetPropertyValueConstraints(def, internalValues);
        return true;
    } catch (NameException be) {
    // implementation specific exception, fall through
    } catch (RepositoryException re) {
    // fall through
    }
    return false;
}
Also used : NameException(org.apache.jackrabbit.spi.commons.conversion.NameException) QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) InternalValue(org.apache.jackrabbit.core.value.InternalValue) Value(javax.jcr.Value) ArrayList(java.util.ArrayList) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) RepositoryException(javax.jcr.RepositoryException) InternalValue(org.apache.jackrabbit.core.value.InternalValue) Name(org.apache.jackrabbit.spi.Name)

Example 29 with NameException

use of org.apache.jackrabbit.spi.commons.conversion.NameException in project jackrabbit by apache.

the class JCRSQLQueryBuilder method createPathQuery.

/**
     * Creates <code>LocationStepQueryNode</code>s from a <code>path</code>.
     *
     * @param path      the path pattern
     * @param operation the type of the parent node
     */
private void createPathQuery(String path, int operation) {
    MergingPathQueryNode pathNode = new MergingPathQueryNode(operation, factory.createPathQueryNode(null).getValidJcrSystemNodeTypeNames());
    pathNode.setAbsolute(true);
    if (path.equals("/")) {
        pathNode.addPathStep(factory.createLocationStepQueryNode(pathNode));
        pathConstraints.add(pathNode);
        return;
    }
    String[] names = path.split("/");
    for (int i = 0; i < names.length; i++) {
        if (names[i].length() == 0) {
            if (i == 0) {
                // root
                pathNode.addPathStep(factory.createLocationStepQueryNode(pathNode));
            } else {
                // descendant '//' -> invalid path
                // todo throw or ignore?
                // we currently do not throw and add location step for an
                // empty name (which is basically the root node)
                pathNode.addPathStep(factory.createLocationStepQueryNode(pathNode));
            }
        } else {
            int idx = names[i].indexOf('[');
            String name;
            int index = LocationStepQueryNode.NONE;
            if (idx > -1) {
                // contains index
                name = names[i].substring(0, idx);
                String suffix = names[i].substring(idx);
                String indexStr = suffix.substring(1, suffix.length() - 1);
                if (indexStr.equals("%")) {
                    // select all same name siblings
                    index = LocationStepQueryNode.NONE;
                } else {
                    try {
                        index = Integer.parseInt(indexStr);
                    } catch (NumberFormatException e) {
                        log.warn("Unable to parse index for path element: " + names[i]);
                    }
                }
                if (name.equals("%")) {
                    name = null;
                }
            } else {
                // no index specified
                // - index defaults to 1 if there is an explicit name test
                // - index defaults to NONE if name test is %
                name = names[i];
                if (name.equals("%")) {
                    name = null;
                } else {
                    index = 1;
                }
            }
            Name qName = null;
            if (name != null) {
                try {
                    qName = resolver.getQName(name);
                } catch (NamespaceException e) {
                    throw new IllegalArgumentException("Illegal name: " + name);
                } catch (NameException e) {
                    throw new IllegalArgumentException("Illegal name: " + name);
                }
            }
            // if name test is % this means also search descendants
            boolean descendant = name == null;
            LocationStepQueryNode step = factory.createLocationStepQueryNode(pathNode);
            step.setNameTest(qName);
            step.setIncludeDescendants(descendant);
            if (index > 0) {
                step.setIndex(index);
            }
            pathNode.addPathStep(step);
        }
    }
    pathConstraints.add(pathNode);
}
Also used : NameException(org.apache.jackrabbit.spi.commons.conversion.NameException) LocationStepQueryNode(org.apache.jackrabbit.spi.commons.query.LocationStepQueryNode) NamespaceException(javax.jcr.NamespaceException) Name(org.apache.jackrabbit.spi.Name)

Example 30 with NameException

use of org.apache.jackrabbit.spi.commons.conversion.NameException in project jackrabbit by apache.

the class JCRSQLParser method Identifier.

public final Name Identifier() throws ParseException {
    /*@bgen(jjtree) Identifier */
    ASTIdentifier jjtn000 = new ASTIdentifier(JJTIDENTIFIER);
    boolean jjtc000 = true;
    jjtree.openNodeScope(jjtn000);
    Token t = null;
    Name name = null;
    boolean pseudoProperty = false;
    try {
        switch((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
            case REGULAR_IDENTIFIER:
                t = jj_consume_token(REGULAR_IDENTIFIER);
                switch((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
                    case LEFT_PAREN:
                        jj_consume_token(LEFT_PAREN);
                        jj_consume_token(RIGHT_PAREN);
                        pseudoProperty = true;
                        break;
                    default:
                        jj_la1[29] = jj_gen;
                        ;
                }
                try {
                    String jcrName = t.image;
                    if (pseudoProperty) {
                        jcrName += "()";
                    }
                    jjtn000.setName(resolver.getQName(jcrName));
                } catch (NameException e) {
                    {
                        if (true)
                            throw new ParseException(e.getMessage());
                    }
                } catch (NamespaceException e) {
                    {
                        if (true)
                            throw new ParseException(e.getMessage());
                    }
                }
                break;
            case DELIMITED_IDENTIFIER:
                t = jj_consume_token(DELIMITED_IDENTIFIER);
                try {
                    jjtn000.setName(resolver.getQName(t.image.substring(1, t.image.length() - 1)));
                } catch (NameException e) {
                    {
                        if (true)
                            throw new ParseException(e.getMessage());
                    }
                } catch (NamespaceException e) {
                    {
                        if (true)
                            throw new ParseException(e.getMessage());
                    }
                }
                break;
            case BY:
            case IN:
            case OR:
            case IS:
            case AND:
            case LIKE:
            case NULL:
            case FROM:
            case ORDER:
            case WHERE:
            case SELECT:
            case BETWEEN:
                switch((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
                    case BY:
                        // or any keyword
                        t = jj_consume_token(BY);
                        break;
                    case IN:
                        t = jj_consume_token(IN);
                        break;
                    case OR:
                        t = jj_consume_token(OR);
                        break;
                    case IS:
                        t = jj_consume_token(IS);
                        break;
                    case AND:
                        t = jj_consume_token(AND);
                        break;
                    case LIKE:
                        t = jj_consume_token(LIKE);
                        break;
                    case NULL:
                        t = jj_consume_token(NULL);
                        break;
                    case FROM:
                        t = jj_consume_token(FROM);
                        break;
                    case ORDER:
                        t = jj_consume_token(ORDER);
                        break;
                    case WHERE:
                        t = jj_consume_token(WHERE);
                        break;
                    case SELECT:
                        t = jj_consume_token(SELECT);
                        break;
                    case BETWEEN:
                        t = jj_consume_token(BETWEEN);
                        break;
                    default:
                        jj_la1[30] = jj_gen;
                        jj_consume_token(-1);
                        throw new ParseException();
                }
                try {
                    jjtn000.setName(resolver.getQName(t.image));
                } catch (NameException e) {
                    {
                        if (true)
                            throw new ParseException(e.getMessage());
                    }
                } catch (NamespaceException e) {
                    {
                        if (true)
                            throw new ParseException(e.getMessage());
                    }
                }
                break;
            default:
                jj_la1[31] = jj_gen;
                jj_consume_token(-1);
                throw new ParseException();
        }
        jjtree.closeNodeScope(jjtn000, true);
        jjtc000 = false;
        {
            if (true)
                return jjtn000.getName();
        }
    } finally {
        if (jjtc000) {
            jjtree.closeNodeScope(jjtn000, true);
        }
    }
    throw new Error("Missing return statement in function");
}
Also used : NameException(org.apache.jackrabbit.spi.commons.conversion.NameException) NamespaceException(javax.jcr.NamespaceException) Name(org.apache.jackrabbit.spi.Name)

Aggregations

NameException (org.apache.jackrabbit.spi.commons.conversion.NameException)65 RepositoryException (javax.jcr.RepositoryException)44 Name (org.apache.jackrabbit.spi.Name)38 Path (org.apache.jackrabbit.spi.Path)20 NamespaceException (javax.jcr.NamespaceException)17 ArrayList (java.util.ArrayList)16 IllegalNameException (org.apache.jackrabbit.spi.commons.conversion.IllegalNameException)9 SAXException (org.xml.sax.SAXException)8 Node (javax.jcr.Node)6 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)6 InvalidQueryException (javax.jcr.query.InvalidQueryException)6 NodeId (org.apache.jackrabbit.core.id.NodeId)6 IOException (java.io.IOException)5 ItemNotFoundException (javax.jcr.ItemNotFoundException)5 PathNotFoundException (javax.jcr.PathNotFoundException)5 Value (javax.jcr.Value)5 LocationStepQueryNode (org.apache.jackrabbit.spi.commons.query.LocationStepQueryNode)5 AccessDeniedException (javax.jcr.AccessDeniedException)4 NodeType (javax.jcr.nodetype.NodeType)4 VersionException (javax.jcr.version.VersionException)4