Search in sources :

Example 1 with ParseException

use of org.apache.jackrabbit.commons.cnd.ParseException in project jackrabbit by apache.

the class AbstractRecord method readNodeTypeDef.

/**
     * {@inheritDoc}
     */
public QNodeTypeDefinition readNodeTypeDef() throws JournalException {
    try {
        StringReader sr = new StringReader(readString());
        CompactNodeTypeDefReader<QNodeTypeDefinition, NamespaceMapping> reader = new CompactNodeTypeDefReader<QNodeTypeDefinition, NamespaceMapping>(sr, "(internal)", new NamespaceMapping(nsResolver), new QDefinitionBuilderFactory());
        Collection<QNodeTypeDefinition> ntds = reader.getNodeTypeDefinitions();
        if (ntds.size() != 1) {
            throw new JournalException("Expected one node type definition: got " + ntds.size());
        }
        return ntds.iterator().next();
    } catch (ParseException e) {
        String msg = "Parse error while reading node type definition.";
        throw new JournalException(msg, e);
    }
}
Also used : QNodeTypeDefinition(org.apache.jackrabbit.spi.QNodeTypeDefinition) NamespaceMapping(org.apache.jackrabbit.spi.commons.namespace.NamespaceMapping) QDefinitionBuilderFactory(org.apache.jackrabbit.spi.commons.nodetype.QDefinitionBuilderFactory) StringReader(java.io.StringReader) ParseException(org.apache.jackrabbit.commons.cnd.ParseException) CompactNodeTypeDefReader(org.apache.jackrabbit.commons.cnd.CompactNodeTypeDefReader)

Example 2 with ParseException

use of org.apache.jackrabbit.commons.cnd.ParseException 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 3 with ParseException

use of org.apache.jackrabbit.commons.cnd.ParseException in project sling by apache.

the class NodeTypeLoader method registerNodeType.

public static boolean registerNodeType(Session session, String systemId, Reader reader, boolean reregisterExisting) throws IOException, RepositoryException {
    try {
        Workspace wsp = session.getWorkspace();
        CndImporter.registerNodeTypes(reader, systemId, wsp.getNodeTypeManager(), wsp.getNamespaceRegistry(), session.getValueFactory(), reregisterExisting);
    } catch (RepositoryException re) {
        if (isReRegisterBuiltinNodeType(re)) {
            log.debug("Attempt to re-register built-in node type, RepositoryException ignored", re);
        } else {
            throw re;
        }
    } catch (ParseException e) {
        throw new IOException("Unable to parse CND Input: " + e.getMessage());
    }
    return true;
}
Also used : RepositoryException(javax.jcr.RepositoryException) ParseException(org.apache.jackrabbit.commons.cnd.ParseException) IOException(java.io.IOException) Workspace(javax.jcr.Workspace)

Example 4 with ParseException

use of org.apache.jackrabbit.commons.cnd.ParseException in project jackrabbit by apache.

the class WorkspaceResourceImpl method setProperty.

/**
     * Allows to alter the registered namespaces ({@link ItemResourceConstants#JCR_NAMESPACES})
     * or register node types {@link ItemResourceConstants#JCR_NODETYPES_CND}
     * where the passed value is a cnd string containing the definition
     * and forwards any other property to the super class.
     * <p>
     * Note that again no property status is set. Any failure while setting
     * a property results in an exception (violating RFC 2518).
     *
     * @param property
     * @throws DavException
     * @see DavResource#setProperty(org.apache.jackrabbit.webdav.property.DavProperty)
     */
@Override
public void setProperty(DavProperty<?> property) throws DavException {
    if (ItemResourceConstants.JCR_NAMESPACES.equals(property.getName())) {
        NamespacesProperty nsp = new NamespacesProperty(property);
        try {
            Map<String, String> changes = new HashMap<String, String>(nsp.getNamespaces());
            NamespaceRegistry nsReg = getRepositorySession().getWorkspace().getNamespaceRegistry();
            for (String prefix : nsReg.getPrefixes()) {
                if (!changes.containsKey(prefix)) {
                    // prefix not present amongst the new values any more > unregister
                    nsReg.unregisterNamespace(prefix);
                } else if (changes.get(prefix).equals(nsReg.getURI(prefix))) {
                    // present with same uri-value >> no action required
                    changes.remove(prefix);
                }
            }
            // it has not been present before.
            for (String prefix : changes.keySet()) {
                String uri = changes.get(prefix);
                nsReg.registerNamespace(prefix, uri);
            }
        } catch (RepositoryException e) {
            throw new JcrDavException(e);
        }
    } else if (ItemResourceConstants.JCR_NODETYPES_CND.equals(property.getName())) {
        try {
            Object value = property.getValue();
            List<?> cmds;
            if (value instanceof List) {
                cmds = (List) value;
            } else if (value instanceof Element) {
                cmds = Collections.singletonList(value);
            } else {
                log.warn("Unexpected structure of dcr:nodetypes-cnd property.");
                throw new DavException(DavServletResponse.SC_INTERNAL_SERVER_ERROR);
            }
            String registerCnd = null;
            boolean allowUpdate = false;
            List<String> unregisterNames = new ArrayList<String>();
            for (Object listEntry : cmds) {
                if (listEntry instanceof Element) {
                    Element e = (Element) listEntry;
                    String localName = e.getLocalName();
                    if (ItemResourceConstants.XML_CND.equals(localName)) {
                        registerCnd = DomUtil.getText(e);
                    } else if (ItemResourceConstants.XML_ALLOWUPDATE.equals(localName)) {
                        String allow = DomUtil.getTextTrim(e);
                        allowUpdate = Boolean.parseBoolean(allow);
                    } else if (ItemResourceConstants.XML_NODETYPENAME.equals(localName)) {
                        unregisterNames.add(DomUtil.getTextTrim(e));
                    }
                }
            }
            // TODO: for simplicity it's currently either registration or unregistration as nt-modifications are immediately persisted.
            Session s = getRepositorySession();
            NodeTypeManager ntMgr = s.getWorkspace().getNodeTypeManager();
            if (registerCnd != null) {
                StringReader reader = new StringReader(registerCnd);
                DefinitionBuilderFactory<NodeTypeTemplate, NamespaceRegistry> factory = new TemplateBuilderFactory(ntMgr, s.getValueFactory(), s.getWorkspace().getNamespaceRegistry());
                CompactNodeTypeDefReader<NodeTypeTemplate, NamespaceRegistry> cndReader = new CompactNodeTypeDefReader<NodeTypeTemplate, NamespaceRegistry>(reader, "davex", factory);
                List<NodeTypeTemplate> ntts = cndReader.getNodeTypeDefinitions();
                ntMgr.registerNodeTypes(ntts.toArray(new NodeTypeTemplate[ntts.size()]), allowUpdate);
            } else if (!unregisterNames.isEmpty()) {
                ntMgr.unregisterNodeTypes(unregisterNames.toArray(new String[unregisterNames.size()]));
            }
        } catch (ParseException e) {
            throw new DavException(DavServletResponse.SC_BAD_REQUEST, e);
        } catch (RepositoryException e) {
            throw new JcrDavException(e);
        }
    } else {
        // only jcr:namespace or node types can be modified
        throw new DavException(DavServletResponse.SC_CONFLICT);
    }
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) HashMap(java.util.HashMap) DavException(org.apache.jackrabbit.webdav.DavException) Element(org.w3c.dom.Element) TemplateBuilderFactory(org.apache.jackrabbit.commons.cnd.TemplateBuilderFactory) RepositoryException(javax.jcr.RepositoryException) NamespaceRegistry(javax.jcr.NamespaceRegistry) NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) StringReader(java.io.StringReader) DefinitionBuilderFactory(org.apache.jackrabbit.commons.cnd.DefinitionBuilderFactory) List(java.util.List) ArrayList(java.util.ArrayList) ParseException(org.apache.jackrabbit.commons.cnd.ParseException) NamespacesProperty(org.apache.jackrabbit.webdav.jcr.property.NamespacesProperty) CompactNodeTypeDefReader(org.apache.jackrabbit.commons.cnd.CompactNodeTypeDefReader) Session(javax.jcr.Session)

Aggregations

ParseException (org.apache.jackrabbit.commons.cnd.ParseException)4 RepositoryException (javax.jcr.RepositoryException)3 CompactNodeTypeDefReader (org.apache.jackrabbit.commons.cnd.CompactNodeTypeDefReader)3 IOException (java.io.IOException)2 StringReader (java.io.StringReader)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 QNodeTypeDefinition (org.apache.jackrabbit.spi.QNodeTypeDefinition)2 NamespaceMapping (org.apache.jackrabbit.spi.commons.namespace.NamespaceMapping)2 QDefinitionBuilderFactory (org.apache.jackrabbit.spi.commons.nodetype.QDefinitionBuilderFactory)2 InputStreamReader (java.io.InputStreamReader)1 List (java.util.List)1 Properties (java.util.Properties)1 NamespaceRegistry (javax.jcr.NamespaceRegistry)1 Session (javax.jcr.Session)1 UnsupportedRepositoryOperationException (javax.jcr.UnsupportedRepositoryOperationException)1 Workspace (javax.jcr.Workspace)1 NodeType (javax.jcr.nodetype.NodeType)1 NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)1 NodeTypeTemplate (javax.jcr.nodetype.NodeTypeTemplate)1