jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r160450 - in incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/nodetype: ./ xml/
Date Thu, 07 Apr 2005 20:05:45 GMT
Author: jukka
Date: Thu Apr  7 13:05:44 2005
New Revision: 160450

URL: http://svn.apache.org/viewcvs?view=rev&rev=160450
Log:
JCR-54: Refactoring^2, taking full advantage of the DOMBuilder and DOMWalker utility classes.

Added:
    incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/nodetype/xml/Constants.java
    incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/nodetype/xml/NodeTypeReader.java
    incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/nodetype/xml/NodeTypeWriter.java
Removed:
    incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/nodetype/xml/CommonFormat.java
    incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/nodetype/xml/ItemDefFormat.java
    incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/nodetype/xml/NodeDefFormat.java
    incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/nodetype/xml/NodeTypeFormat.java
    incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/nodetype/xml/NodeTypeFormatter.java
    incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/nodetype/xml/PropDefFormat.java
    incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/nodetype/xml/template.xml
Modified:
    incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/nodetype/NodeTypeDefStore.java
    incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/nodetype/xml/AdditionalNamespaceResolver.java

Modified: incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/nodetype/NodeTypeDefStore.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/nodetype/NodeTypeDefStore.java?view=diff&r1=160449&r2=160450
==============================================================================
--- incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/nodetype/NodeTypeDefStore.java
(original)
+++ incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/nodetype/NodeTypeDefStore.java
Thu Apr  7 13:05:44 2005
@@ -22,13 +22,13 @@
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
-import java.util.Iterator;
 
 import javax.jcr.NamespaceRegistry;
 import javax.jcr.RepositoryException;
 
 import org.apache.jackrabbit.core.QName;
-import org.apache.jackrabbit.core.nodetype.xml.NodeTypeFormatter;
+import org.apache.jackrabbit.core.nodetype.xml.NodeTypeReader;
+import org.apache.jackrabbit.core.nodetype.xml.NodeTypeWriter;
 
 /**
  * <code>NodeTypeDefStore</code> ...
@@ -38,15 +38,11 @@
     /** Map of node type names to node type definitions. */
     private final HashMap ntDefs;
 
-    /** The node type definition file formatter. */
-    private final NodeTypeFormatter formatter;
-
     /**
      * Empty default constructor.
      */
     NodeTypeDefStore() throws RepositoryException {
         ntDefs = new HashMap();
-        formatter = new NodeTypeFormatter();
     }
 
     /**
@@ -57,10 +53,9 @@
     void load(InputStream in)
             throws IOException, InvalidNodeTypeDefException,
             RepositoryException {
-        Collection types = formatter.read(in);
-        Iterator iterator = types.iterator();
-        while (iterator.hasNext()) {
-            add((NodeTypeDef) iterator.next());
+        NodeTypeDef[] types = NodeTypeReader.read(in);
+        for (int i = 0; i < types.length; i++) {
+            add(types[i]);
         }
     }
 
@@ -72,7 +67,9 @@
      */
     void store(OutputStream out, NamespaceRegistry registry)
             throws IOException, RepositoryException {
-        formatter.write(out, registry, ntDefs.values());
+        NodeTypeDef[] types = (NodeTypeDef[])
+            ntDefs.values().toArray(new NodeTypeDef[ntDefs.size()]);
+        NodeTypeWriter.write(out, types, registry);
     }
 
     /**

Modified: incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/nodetype/xml/AdditionalNamespaceResolver.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/nodetype/xml/AdditionalNamespaceResolver.java?view=diff&r1=160449&r2=160450
==============================================================================
--- incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/nodetype/xml/AdditionalNamespaceResolver.java
(original)
+++ incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/nodetype/xml/AdditionalNamespaceResolver.java
Thu Apr  7 13:05:44 2005
@@ -16,17 +16,14 @@
  */
 package org.apache.jackrabbit.core.nodetype.xml;
 
+import java.util.Enumeration;
 import java.util.Properties;
 
 import javax.jcr.NamespaceException;
 import javax.jcr.NamespaceRegistry;
 import javax.jcr.RepositoryException;
 
-import org.apache.jackrabbit.core.Constants;
 import org.apache.jackrabbit.core.NamespaceResolver;
-import org.w3c.dom.Attr;
-import org.w3c.dom.Element;
-import org.w3c.dom.NamedNodeMap;
 
 /**
  * A simple namespace resolver implementation, that uses the additional
@@ -41,20 +38,18 @@
     private final Properties uriToPrefix = new Properties();
 
     /**
-     * Creates a namespace resolver using the namespaces declared
-     * in the given XML element.
+     * Creates a namespace resolver using the namespaces defined in
+     * the given prefix-to-URI property set.
      *
-     * @param element XML element
+     * @param namespaces namespace properties
      */
-    public AdditionalNamespaceResolver(Element element) {
-        NamedNodeMap attributes = element.getAttributes();
-        for (int i = 0; i < attributes.getLength(); i++) {
-            Attr attribute = (Attr) attributes.item(i);
-            if (Constants.NS_XMLNS_PREFIX.equals(attribute.getPrefix())) {
-                addNamespace(attribute.getLocalName(), attribute.getValue());
-            }
+    public AdditionalNamespaceResolver(Properties namespaces) {
+        Enumeration prefixes = namespaces.propertyNames();
+        while (prefixes.hasMoreElements()) {
+            String prefix = (String) prefixes.nextElement();
+            addNamespace(prefix, namespaces.getProperty(prefix));
         }
-        addNamespace(Constants.NS_EMPTY_PREFIX, Constants.NS_DEFAULT_URI);
+        addNamespace("", "");
     }
 
     /**

Added: incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/nodetype/xml/Constants.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/nodetype/xml/Constants.java?view=auto&rev=160450
==============================================================================
--- incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/nodetype/xml/Constants.java
(added)
+++ incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/nodetype/xml/Constants.java
Thu Apr  7 13:05:44 2005
@@ -0,0 +1,100 @@
+/*
+ * Copyright 2004-2005 The Apache Software Foundation or its licensors,
+ *                     as applicable.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.core.nodetype.xml;
+
+/**
+ * Name constants for the node type XML elements and attributes.
+ */
+final class Constants {
+
+    /** Name of the node type definition root element. */
+    public static final String NODETYPES_ELEMENT = "nodeTypes";
+
+    /** Name of the node type definition element. */
+    public static final String NODETYPE_ELEMENT = "nodeType";
+
+    /** Name of the child node definition element. */
+    public static final String CHILDNODEDEF_ELEMENT = "childNodeDef";
+
+    /** Name of the property definition element. */
+    public static final String PROPERTYDEF_ELEMENT = "propertyDef";
+
+    /** Name of the <code>isMixin</code> attribute. */
+    public static final String ISMIXIN_ATTRIBUTE = "isMixin";
+
+    /** Name of the <code>hasOrderableChildNodes</code> attribute. */
+    public static final String HASORDERABLECHILDNODES_ATTRIBUTE =
+        "hasOrderableChildNodes";
+
+    /** Name of the primary item name attribute. */
+    public static final String PRIMARYITEMNAME_ATTRIBUTE = "primaryItemName";
+
+    /** Name of the supertypes element. */
+    public static final String SUPERTYPES_ELEMENT = "supertypes";
+
+    /** Name of the supertype element. */
+    public static final String SUPERTYPE_ELEMENT = "supertype";
+
+    /** Name of the <code>name</code> attribute. */
+    public static final String NAME_ATTRIBUTE = "name";
+
+    /** Name of the <code>autoCreate</code> attribute. */
+    public static final String AUTOCREATE_ATTRIBUTE = "autoCreate";
+
+    /** Name of the <code>mandatory</code> attribute. */
+    public static final String MANDATORY_ATTRIBUTE = "mandatory";
+
+    /** Name of the <code>onParentVersion</code> attribute. */
+    public static final String ONPARENTVERSION_ATTRIBUTE = "onParentVersion";
+
+    /** Name of the <code>protected</code> attribute. */
+    public static final String PROTECTED_ATTRIBUTE = "protected";
+
+    /** Name of the required type attribute. */
+    public static final String REQUIREDTYPE_ATTRIBUTE = "requiredType";
+
+    /** Name of the value constraints element. */
+    public static final String VALUECONSTRAINTS_ELEMENT = "valueConstraints";
+
+    /** Name of the value constraint element. */
+    public static final String VALUECONSTRAINT_ELEMENT = "valueConstraint";
+
+    /** Name of the default values element. */
+    public static final String DEFAULTVALUES_ELEMENT = "defaultValues";
+
+    /** Name of the default value element. */
+    public static final String DEFAULTVALUE_ELEMENT = "defaultValue";
+
+    /** Name of the <code>multiple</code> attribute. */
+    public static final String MULTIPLE_ATTRIBUTE = "multiple";
+
+    /** Name of the required primary types element. */
+    public static final String REQUIREDPRIMARYTYPES_ELEMENT =
+        "requiredPrimaryTypes";
+
+    /** Name of the required primary type element. */
+    public static final String REQUIREDPRIMARYTYPE_ELEMENT =
+        "requiredPrimaryType";
+
+    /** Name of the default primary type attribute. */
+    public static final String DEFAULTPRIMARYTYPE_ATTRIBUTE =
+        "defaultPrimaryType";
+
+    /** Name of the <code>sameNameSibs</code> attribute. */
+    public static final String SAMENAMESIBS_ATTRIBUTE = "sameNameSibs";
+
+}

Added: incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/nodetype/xml/NodeTypeReader.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/nodetype/xml/NodeTypeReader.java?view=auto&rev=160450
==============================================================================
--- incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/nodetype/xml/NodeTypeReader.java
(added)
+++ incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/nodetype/xml/NodeTypeReader.java
Thu Apr  7 13:05:44 2005
@@ -0,0 +1,321 @@
+/*
+ * Copyright 2004-2005 The Apache Software Foundation or its licensors,
+ *                     as applicable.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.core.nodetype.xml;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Vector;
+
+import javax.jcr.PropertyType;
+import javax.jcr.RepositoryException;
+import javax.jcr.version.OnParentVersionAction;
+
+import org.apache.jackrabbit.core.IllegalNameException;
+import org.apache.jackrabbit.core.InternalValue;
+import org.apache.jackrabbit.core.NamespaceResolver;
+import org.apache.jackrabbit.core.QName;
+import org.apache.jackrabbit.core.UnknownPrefixException;
+import org.apache.jackrabbit.core.nodetype.ChildNodeDef;
+import org.apache.jackrabbit.core.nodetype.InvalidConstraintException;
+import org.apache.jackrabbit.core.nodetype.InvalidNodeTypeDefException;
+import org.apache.jackrabbit.core.nodetype.NodeTypeDef;
+import org.apache.jackrabbit.core.nodetype.PropDef;
+import org.apache.jackrabbit.core.nodetype.ValueConstraint;
+import org.apache.jackrabbit.core.util.DOMWalker;
+
+/**
+ * Node type definition reader. This class is used to read the
+ * persistent node type definition files used by Jackrabbit.
+ */
+public final class NodeTypeReader {
+
+    /**
+     * Reads a node type definition file. The file contents are read from
+     * the given input stream and the parsed node type definitions are
+     * returned.
+     *
+     * @param xml XML input stream
+     * @return node type definitions
+     * @throws IOException                 if the node type definitions
+     *                                     cannot be read
+     * @throws InvalidNodeTypeDefException if the node type definition
+     *                                     format is invalid
+     * @throws RepositoryException         on repository errors
+     */
+    public static NodeTypeDef[] read(InputStream xml)
+            throws IOException, InvalidNodeTypeDefException,
+            RepositoryException {
+        try {
+            NodeTypeReader reader = new NodeTypeReader(xml);
+            return reader.getNodeTypeDefs();
+        } catch (IllegalNameException e) {
+            throw new InvalidNodeTypeDefException(
+                    "Invalid namespace reference in a node type definition", e);
+        } catch (UnknownPrefixException e) {
+            throw new InvalidNodeTypeDefException(
+                    "Invalid namespace reference in a node type definition", e);
+        }
+    }
+
+    /** The node type document walker. */
+    private final DOMWalker walker;
+
+    /** The namespace resolver. */
+    private final NamespaceResolver resolver;
+
+    /**
+     * Creates a node type definition file reader.
+     *
+     * @param xml node type definition file
+     * @throws IOException if the node type definition file cannot be read
+     */
+    private NodeTypeReader(InputStream xml) throws IOException {
+        walker = new DOMWalker(xml);
+        resolver = new AdditionalNamespaceResolver(walker.getNamespaces());
+    }
+
+    /**
+     * Returns all node type definitions specified by node type elements
+     * under the current element.
+     *
+     * @return node type definitions
+     * @throws InvalidNodeTypeDefException if a definition is invalid
+     * @throws IllegalNameException        if a definition contains an
+     *                                     illegal name
+     * @throws UnknownPrefixException      if a definition contains an
+     *                                     unknown namespace prefix
+     */
+    private NodeTypeDef[] getNodeTypeDefs()
+            throws InvalidNodeTypeDefException, IllegalNameException,
+            UnknownPrefixException {
+        Vector defs = new Vector();
+        while (walker.iterateElements(Constants.NODETYPE_ELEMENT)) {
+            defs.add(getNodeTypeDef());
+        }
+        return (NodeTypeDef[]) defs.toArray(new NodeTypeDef[defs.size()]);
+    }
+
+    /**
+     * Returns the node type definition specified by the current element.
+     *
+     * @return node type definition
+     * @throws InvalidNodeTypeDefException if the definition is invalid
+     * @throws IllegalNameException        if the definition contains an
+     *                                     illegal name
+     * @throws UnknownPrefixException      if the definition contains an
+     *                                     unknown namespace prefix
+     */
+    private NodeTypeDef getNodeTypeDef()
+            throws InvalidNodeTypeDefException, IllegalNameException,
+            UnknownPrefixException {
+        NodeTypeDef type = new NodeTypeDef();
+
+        type.setName(QName.fromJCRName(
+                walker.getAttribute(Constants.NAME_ATTRIBUTE), resolver));
+        type.setMixin(Boolean.valueOf(
+                walker.getAttribute(Constants.ISMIXIN_ATTRIBUTE))
+                .booleanValue());
+        type.setOrderableChildNodes(Boolean.valueOf(
+                walker.getAttribute(Constants.HASORDERABLECHILDNODES_ATTRIBUTE))
+                .booleanValue());
+        String primaryItemName =
+            walker.getAttribute(Constants.PRIMARYITEMNAME_ATTRIBUTE);
+        if (primaryItemName != null && primaryItemName.length() > 0) {
+            type.setPrimaryItemName(
+                    QName.fromJCRName(primaryItemName, resolver));
+        }
+
+        // supertype declarations
+        if (walker.enterElement(Constants.SUPERTYPES_ELEMENT)) {
+            Vector supertypes = new Vector();
+            while (walker.iterateElements(Constants.SUPERTYPE_ELEMENT)) {
+                supertypes.add(
+                        QName.fromJCRName(walker.getContent(), resolver));
+            }
+            type.setSupertypes((QName[])
+                    supertypes.toArray(new QName[supertypes.size()]));
+            walker.leaveElement();
+        }
+
+        // property definitions
+        Vector properties = new Vector();
+        while (walker.iterateElements(Constants.PROPERTYDEF_ELEMENT)) {
+            PropDef def = getPropDef();
+            def.setDeclaringNodeType(type.getName());
+            properties.add(def);
+        }
+        type.setPropertyDefs((PropDef[])
+                properties.toArray(new PropDef[properties.size()]));
+
+        // child node definitions
+        Vector nodes = new Vector();
+        while (walker.iterateElements(Constants.CHILDNODEDEF_ELEMENT)) {
+            ChildNodeDef def = getChildNodeDef();
+            def.setDeclaringNodeType(type.getName());
+            nodes.add(def);
+        }
+        type.setChildNodeDefs((ChildNodeDef[])
+                nodes.toArray(new ChildNodeDef[nodes.size()]));
+
+        return type;
+    }
+
+    /**
+     * Returns the property definition specified by the current element.
+     *
+     * @return property definition
+     * @throws InvalidNodeTypeDefException if the definition is invalid
+     * @throws IllegalNameException        if the definition contains an
+     *                                     illegal name
+     * @throws UnknownPrefixException      if the definition contains an
+     *                                     unknown namespace prefix
+     */
+    private PropDef getPropDef()
+            throws InvalidNodeTypeDefException, IllegalNameException,
+            UnknownPrefixException {
+        PropDef def = new PropDef();
+        String name = walker.getAttribute(Constants.NAME_ATTRIBUTE);
+        if (name.equals("*")) {
+            def.setName(new QName("", "*"));
+        } else {
+            def.setName(QName.fromJCRName(name, resolver));
+        }
+
+        // simple attributes
+        def.setAutoCreate(Boolean.valueOf(
+                walker.getAttribute(Constants.AUTOCREATE_ATTRIBUTE))
+                .booleanValue());
+        def.setMandatory(Boolean.valueOf(
+                walker.getAttribute(Constants.MANDATORY_ATTRIBUTE))
+                .booleanValue());
+        def.setProtected(Boolean.valueOf(
+                walker.getAttribute(Constants.PROTECTED_ATTRIBUTE))
+                .booleanValue());
+        def.setOnParentVersion(OnParentVersionAction.valueFromName(
+                walker.getAttribute(Constants.ONPARENTVERSION_ATTRIBUTE)));
+        def.setMultiple(Boolean.valueOf(
+                walker.getAttribute(Constants.MULTIPLE_ATTRIBUTE))
+                .booleanValue());
+        def.setRequiredType(PropertyType.valueFromName(
+                walker.getAttribute(Constants.REQUIREDTYPE_ATTRIBUTE)));
+
+        // value constraints
+        if (walker.enterElement(Constants.VALUECONSTRAINTS_ELEMENT)) {
+            Vector constraints = new Vector();
+            int type = def.getRequiredType();
+            while (walker.iterateElements(Constants.VALUECONSTRAINT_ELEMENT)) {
+                String constraint = walker.getContent();
+                try {
+                    constraints.add(ValueConstraint.create(
+                            type, constraint.trim(), resolver));
+                } catch (InvalidConstraintException e) {
+                    throw new InvalidNodeTypeDefException(
+                            "Invalid value constraint " + constraint, e);
+                }
+            }
+            def.setValueConstraints((ValueConstraint[]) constraints.toArray(
+                    new ValueConstraint[constraints.size()]));
+            walker.leaveElement();
+        }
+
+        // default values
+        if (walker.enterElement(Constants.DEFAULTVALUES_ELEMENT)) {
+            Vector values = new Vector();
+            int type = def.getRequiredType();
+            if (type == PropertyType.UNDEFINED) {
+                type = PropertyType.STRING;
+            }
+            while (walker.iterateElements(Constants.DEFAULTVALUE_ELEMENT)) {
+                String value = walker.getContent();
+                try {
+                    values.add(InternalValue.create(value, type, resolver));
+                } catch (RepositoryException e) {
+                    throw new InvalidNodeTypeDefException(e);
+                }
+            }
+            def.setDefaultValues((InternalValue[])
+                    values.toArray(new InternalValue[values.size()]));
+            walker.leaveElement();
+        }
+
+        return def;
+    }
+
+    /**
+     * Returns the child node definition specified by the current element.
+     *
+     * @return child node definition
+     * @throws InvalidNodeTypeDefException if the definition is invalid
+     * @throws IllegalNameException        if the definition contains an
+     *                                     illegal name
+     * @throws UnknownPrefixException      if the definition contains an
+     *                                     unknown namespace prefix
+     */
+    private ChildNodeDef getChildNodeDef()
+            throws InvalidNodeTypeDefException, IllegalNameException,
+            UnknownPrefixException {
+        ChildNodeDef def = new ChildNodeDef();
+        String name = walker.getAttribute(Constants.NAME_ATTRIBUTE);
+        if (name.equals("*")) {
+            def.setName(new QName("", "*"));
+        } else {
+            def.setName(QName.fromJCRName(name, resolver));
+        }
+
+        // simple attributes
+        def.setAutoCreate(Boolean.valueOf(
+                walker.getAttribute(Constants.AUTOCREATE_ATTRIBUTE))
+                .booleanValue());
+        def.setMandatory(Boolean.valueOf(
+                walker.getAttribute(Constants.MANDATORY_ATTRIBUTE))
+                .booleanValue());
+        def.setProtected(Boolean.valueOf(
+                walker.getAttribute(Constants.PROTECTED_ATTRIBUTE))
+                .booleanValue());
+        def.setAllowSameNameSibs(Boolean.valueOf(
+                walker.getAttribute(Constants.SAMENAMESIBS_ATTRIBUTE))
+                .booleanValue());
+
+        // default primary type
+        String type =
+            walker.getAttribute(Constants.DEFAULTPRIMARYTYPE_ATTRIBUTE);
+        if (type != null && type.length() > 0) {
+            def.setDefaultPrimaryType(QName.fromJCRName(type, resolver));
+        }
+
+        // required primary types
+        if (walker.enterElement(Constants.REQUIREDPRIMARYTYPES_ELEMENT)) {
+            Vector types = new Vector();
+            while (walker.iterateElements(Constants.REQUIREDPRIMARYTYPE_ELEMENT)) {
+                types.add(QName.fromJCRName(walker.getContent(), resolver));
+            }
+            def.setRequiredPrimaryTypes(
+                    (QName[]) types.toArray(new QName[types.size()]));
+            walker.leaveElement();
+        } else {
+            /* Default to nt:base?
+            throw new InvalidNodeTypeDefException(
+                    "Required primary type(s) not defined for child node "
+                    + def.getName() + " of node type "
+                    + def.getDeclaringNodeType());
+            */
+        }
+
+        return def;
+    }
+
+}

Added: incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/nodetype/xml/NodeTypeWriter.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/nodetype/xml/NodeTypeWriter.java?view=auto&rev=160450
==============================================================================
--- incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/nodetype/xml/NodeTypeWriter.java
(added)
+++ incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/nodetype/xml/NodeTypeWriter.java
Thu Apr  7 13:05:44 2005
@@ -0,0 +1,281 @@
+/*
+ * Copyright 2004-2005 The Apache Software Foundation or its licensors,
+ *                     as applicable.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.core.nodetype.xml;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+import javax.jcr.NamespaceRegistry;
+import javax.jcr.PropertyType;
+import javax.jcr.RepositoryException;
+import javax.jcr.version.OnParentVersionAction;
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.apache.jackrabbit.core.InternalValue;
+import org.apache.jackrabbit.core.NamespaceResolver;
+import org.apache.jackrabbit.core.NoPrefixDeclaredException;
+import org.apache.jackrabbit.core.QName;
+import org.apache.jackrabbit.core.nodetype.ChildNodeDef;
+import org.apache.jackrabbit.core.nodetype.NodeTypeDef;
+import org.apache.jackrabbit.core.nodetype.PropDef;
+import org.apache.jackrabbit.core.nodetype.ValueConstraint;
+import org.apache.jackrabbit.core.util.DOMBuilder;
+
+/**
+ * Node type definition writer. This class is used to write the
+ * persistent node type definition files used by Jackrabbit.
+ */
+public final class NodeTypeWriter {
+
+    /**
+     * Writes a node type definition file. The file contents are written
+     * to the given output stream and will contain the given node type
+     * definitions. The given namespace registry is used for namespace
+     * mappings.
+     *
+     * @param xml XML output stream
+     * @param registry namespace registry
+     * @param types node types
+     * @throws IOException         if the node type definitions cannot
+     *                             be written
+     * @throws RepositoryException on repository errors
+     */
+    public static void write(
+            OutputStream xml, NodeTypeDef[] types, NamespaceRegistry registry)
+            throws IOException, RepositoryException {
+        try {
+            NodeTypeWriter writer = new NodeTypeWriter(registry);
+            for (int i = 0; i < types.length; i++) {
+                writer.addNodeTypeDef(types[i]);
+            }
+            writer.write(xml);
+        } catch (ParserConfigurationException e) {
+            throw new IOException(e.getMessage());
+        } catch (NoPrefixDeclaredException e) {
+            throw new RepositoryException(
+                    "Invalid namespace reference in a node type definition", e);
+        }
+    }
+
+    /** The node type document builder. */
+    private final DOMBuilder builder;
+
+    /** The namespace resolver. */
+    private final NamespaceResolver resolver;
+
+    /**
+     * Creates a node type definition file writer. The given namespace
+     * registry is used for the XML namespace bindings.
+     *
+     * @param registry namespace registry
+     * @throws ParserConfigurationException if the node type definition
+     *                                      document cannot be created
+     * @throws RepositoryException          if the namespace mappings cannot
+     *                                      be retrieved from the registry
+     */
+    private NodeTypeWriter(NamespaceRegistry registry)
+            throws ParserConfigurationException, RepositoryException {
+        builder = new DOMBuilder(Constants.NODETYPES_ELEMENT);
+
+        String[] prefixes = registry.getPrefixes();
+        for (int i = 0; i < prefixes.length; i++) {
+            if (!"".equals(prefixes[i])) {
+                String uri = registry.getURI(prefixes[i]);
+                builder.setAttribute("xmlns:" + prefixes[i], uri);
+            }
+        }
+
+        resolver = new AdditionalNamespaceResolver(registry);
+    }
+
+    /**
+     * Builds a node type definition element under the current element.
+     *
+     * @param def node type definition
+     * @throws RepositoryException       if the default property values
+     *                                   cannot be serialized
+     * @throws NoPrefixDeclaredException if the node type definition contains
+     *                                   invalid namespace references
+     */
+    private void addNodeTypeDef(NodeTypeDef def)
+            throws RepositoryException, NoPrefixDeclaredException {
+        builder.startElement(Constants.NODETYPE_ELEMENT);
+
+        // simple attributes
+        builder.setAttribute(
+                Constants.NAME_ATTRIBUTE, def.getName().toJCRName(resolver));
+        builder.setAttribute(
+                Constants.ISMIXIN_ATTRIBUTE, def.isMixin());
+        builder.setAttribute(
+                Constants.HASORDERABLECHILDNODES_ATTRIBUTE,
+                def.hasOrderableChildNodes());
+
+        // primary item name
+        QName item = def.getPrimaryItemName();
+        if (item != null) {
+            builder.setAttribute(
+                    Constants.PRIMARYITEMNAME_ATTRIBUTE,
+                    item.toJCRName(resolver));
+        } else {
+            builder.setAttribute(Constants.PRIMARYITEMNAME_ATTRIBUTE, "");
+        }
+
+        // supertype declarations
+        QName[] supertypes = def.getSupertypes();
+        if (supertypes != null && supertypes.length > 0) {
+            builder.startElement(Constants.SUPERTYPES_ELEMENT);
+            for (int i = 0; i < supertypes.length; i++) {
+                builder.addContentElement(
+                        Constants.SUPERTYPE_ELEMENT,
+                        supertypes[i].toJCRName(resolver));
+            }
+            builder.endElement();
+        }
+
+        // property definitions
+        PropDef[] properties = def.getPropertyDefs();
+        for (int i = 0; i < properties.length; i++) {
+            addPropDef(properties[i]);
+        }
+
+        // child node definitions
+        ChildNodeDef[] nodes = def.getChildNodeDefs();
+        for (int i = 0; i < nodes.length; i++) {
+            addChildNodeDef(nodes[i]);
+        }
+
+        builder.endElement();
+    }
+
+    /**
+     * Builds a property definition element under the current element.
+     *
+     * @param def property definition
+     * @throws RepositoryException       if the default values cannot
+     *                                   be serialized
+     * @throws NoPrefixDeclaredException if the property definition contains
+     *                                   invalid namespace references
+     */
+    private void addPropDef(PropDef def)
+            throws RepositoryException, NoPrefixDeclaredException {
+        builder.startElement(Constants.PROPERTYDEF_ELEMENT);
+
+        // simple attributes
+        builder.setAttribute(
+                Constants.NAME_ATTRIBUTE, def.getName().toJCRName(resolver));
+        builder.setAttribute(
+                Constants.AUTOCREATE_ATTRIBUTE, def.isAutoCreate());
+        builder.setAttribute(
+                Constants.MANDATORY_ATTRIBUTE, def.isMandatory());
+        builder.setAttribute(
+                Constants.PROTECTED_ATTRIBUTE, def.isProtected());
+        builder.setAttribute(
+                Constants.ONPARENTVERSION_ATTRIBUTE,
+                OnParentVersionAction.nameFromValue(def.getOnParentVersion()));
+        builder.setAttribute(
+                Constants.MULTIPLE_ATTRIBUTE, def.isMultiple());
+        builder.setAttribute(
+                Constants.REQUIREDTYPE_ATTRIBUTE,
+                PropertyType.nameFromValue(def.getRequiredType()));
+
+        // value constraints
+        ValueConstraint[] constraints = def.getValueConstraints();
+        if (constraints != null && constraints.length > 0) {
+            builder.startElement(Constants.VALUECONSTRAINTS_ELEMENT);
+            for (int i = 0; i < constraints.length; i++) {
+                builder.addContentElement(
+                        Constants.VALUECONSTRAINT_ELEMENT,
+                        constraints[i].getDefinition(resolver));
+            }
+            builder.endElement();
+        }
+
+        // default values
+        InternalValue[] defaults = def.getDefaultValues();
+        if (defaults != null && defaults.length > 0) {
+            builder.startElement(Constants.DEFAULTVALUES_ELEMENT);
+            for (int i = 0; i < defaults.length; i++) {
+                builder.addContentElement(
+                        Constants.DEFAULTVALUE_ELEMENT,
+                        defaults[i].toJCRValue(resolver).getString());
+            }
+            builder.endElement();
+        }
+
+        builder.endElement();
+    }
+
+    /**
+     * Builds a child node definition element under the current element.
+     *
+     * @param def child node definition
+     * @throws NoPrefixDeclaredException if the child node definition contains
+     *                                   invalid namespace references
+     */
+    private void addChildNodeDef(ChildNodeDef def)
+            throws NoPrefixDeclaredException {
+        builder.startElement(Constants.CHILDNODEDEF_ELEMENT);
+
+        // simple attributes
+        builder.setAttribute(
+                Constants.NAME_ATTRIBUTE, def.getName().toJCRName(resolver));
+        builder.setAttribute(
+                Constants.AUTOCREATE_ATTRIBUTE, def.isAutoCreate());
+        builder.setAttribute(
+                Constants.MANDATORY_ATTRIBUTE, def.isMandatory());
+        builder.setAttribute(
+                Constants.PROTECTED_ATTRIBUTE, def.isProtected());
+        builder.setAttribute(
+                Constants.ONPARENTVERSION_ATTRIBUTE,
+                OnParentVersionAction.nameFromValue(def.getOnParentVersion()));
+        builder.setAttribute(
+                Constants.SAMENAMESIBS_ATTRIBUTE, def.allowSameNameSibs());
+
+        // default primary type
+        QName type = def.getDefaultPrimaryType();
+        if (type != null) {
+            builder.setAttribute(
+                    Constants.DEFAULTPRIMARYTYPE_ATTRIBUTE,
+                    type.toJCRName(resolver));
+        } else {
+            builder.setAttribute(Constants.DEFAULTPRIMARYTYPE_ATTRIBUTE, "");
+        }
+
+        // required primary types
+        QName[] requiredTypes = def.getRequiredPrimaryTypes();
+        builder.startElement(Constants.REQUIREDPRIMARYTYPES_ELEMENT);
+        for (int i = 0; i < requiredTypes.length; i++) {
+            builder.addContentElement(
+                    Constants.REQUIREDPRIMARYTYPE_ELEMENT,
+                    requiredTypes[i].toJCRName(resolver));
+        }
+        builder.endElement();
+
+        builder.endElement();
+    }
+
+    /**
+     * Writes the node type definition document to the given output stream.
+     *
+     * @param xml XML output stream
+     * @throws IOException if the node type document could not be written
+     */
+    private void write(OutputStream xml) throws IOException {
+        builder.write(xml);
+    }
+
+}



Mime
View raw message