jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r328810 - in /incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/xml/nodetype: ./ NamespaceExtractor.java NodeTypeXML.java NodeTypeXMLReader.java NodeTypeXMLWriter.java SAXNamespaceResolver.java nodetypes.dtd
Date Thu, 27 Oct 2005 06:45:03 GMT
Author: jukka
Date: Wed Oct 26 23:44:56 2005
New Revision: 328810

URL: http://svn.apache.org/viewcvs?rev=328810&view=rev
Log:
JCR-EXT: XML input and output features for the simple node type state implementation.

Added:
    incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/xml/nodetype/
    incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/xml/nodetype/NamespaceExtractor.java
  (with props)
    incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/xml/nodetype/NodeTypeXML.java
  (with props)
    incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/xml/nodetype/NodeTypeXMLReader.java
  (with props)
    incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/xml/nodetype/NodeTypeXMLWriter.java
  (with props)
    incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/xml/nodetype/SAXNamespaceResolver.java
  (with props)
    incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/xml/nodetype/nodetypes.dtd
  (with props)

Added: incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/xml/nodetype/NamespaceExtractor.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/xml/nodetype/NamespaceExtractor.java?rev=328810&view=auto
==============================================================================
--- incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/xml/nodetype/NamespaceExtractor.java
(added)
+++ incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/xml/nodetype/NamespaceExtractor.java
Wed Oct 26 23:44:56 2005
@@ -0,0 +1,87 @@
+/*
+ * 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.xml.nodetype;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.jackrabbit.name.QName;
+import org.apache.jackrabbit.state.nodetype.NodeDefinitionState;
+import org.apache.jackrabbit.state.nodetype.NodeTypeManagerState;
+import org.apache.jackrabbit.state.nodetype.NodeTypeState;
+import org.apache.jackrabbit.state.nodetype.PropertyDefinitionState;
+
+class NamespaceExtractor {
+
+    public static Map extractNamespaces(
+            NodeTypeManagerState nodeTypeManagerState) {
+        NamespaceExtractor extractor = new NamespaceExtractor();
+        NodeTypeState[] nodeTypeStates = nodeTypeManagerState.getNodeTypeStates();
+        for (int i = 0; i < nodeTypeStates.length; i++) {
+            extractor.extractNamespaces(nodeTypeStates[i]);
+        }
+        return extractor.namespaces;
+    }
+
+    private final Map namespaces = new HashMap();
+
+    private void extractNamespaces(NodeTypeState nodeTypeState) {
+        extractNamespace(nodeTypeState.getName());
+        extractNamespace(nodeTypeState.getPrimaryItemName());
+        extractNamespaces(nodeTypeState.getSupertypeNames());
+        NodeDefinitionState[] childNodeDefinitionStates =
+            nodeTypeState.getChildNodeDefinitionStates();
+        for (int i = 0; i < childNodeDefinitionStates.length; i++) {
+            extractNamespaces(childNodeDefinitionStates[i]);
+        }
+        PropertyDefinitionState[] propertyDefinitionStates =
+            nodeTypeState.getPropertyDefinitionStates();
+        for (int i = 0; i < propertyDefinitionStates.length; i++) {
+            extractNamespace(propertyDefinitionStates[i].getName());
+        }
+    }
+
+    private void extractNamespaces(NodeDefinitionState nodeDefinitionState) {
+        extractNamespace(nodeDefinitionState.getName());
+        extractNamespace(nodeDefinitionState.getDefaultPrimaryTypeName());
+        extractNamespaces(nodeDefinitionState.getRequiredPrimaryTypeNames());
+    }
+
+    private void extractNamespaces(QName[] names) {
+        for (int i = 0; i < names.length; i++) {
+            extractNamespace(names[i]);
+        }
+    }
+
+    private void extractNamespace(QName name) {
+        if (name != null) {
+            String uri = name.getNamespaceURI();
+            if (!namespaces.containsKey(uri)) {
+                if (uri.equals(QName.NS_JCR_URI)) {
+                    namespaces.put(uri, QName.NS_JCR_PREFIX);
+                } else if (uri.equals(QName.NS_MIX_URI)) {
+                    namespaces.put(uri, QName.NS_MIX_PREFIX);
+                } else if (uri.equals(QName.NS_NT_URI)) {
+                    namespaces.put(uri, QName.NS_NT_PREFIX);
+                } else {
+                    namespaces.put(uri, "ns" + namespaces.size());
+                }
+            }
+        }
+    }
+    
+}

Propchange: incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/xml/nodetype/NamespaceExtractor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/xml/nodetype/NodeTypeXML.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/xml/nodetype/NodeTypeXML.java?rev=328810&view=auto
==============================================================================
--- incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/xml/nodetype/NodeTypeXML.java
(added)
+++ incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/xml/nodetype/NodeTypeXML.java
Wed Oct 26 23:44:56 2005
@@ -0,0 +1,125 @@
+/*
+ * 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.xml.nodetype;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.parsers.SAXParser;
+import javax.xml.parsers.SAXParserFactory;
+import javax.xml.transform.Result;
+import javax.xml.transform.Source;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.sax.SAXResult;
+import javax.xml.transform.sax.SAXTransformerFactory;
+import javax.xml.transform.sax.TransformerHandler;
+import javax.xml.transform.stream.StreamResult;
+
+import org.apache.jackrabbit.state.nodetype.NodeTypeManagerState;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+
+/**
+ * TODO
+ */
+public class NodeTypeXML {
+
+    public static NodeTypeManagerState read(Source source)
+            throws TransformerConfigurationException, TransformerException {
+        NodeTypeXMLReader reader = new NodeTypeXMLReader();
+        TransformerFactory factory = TransformerFactory.newInstance();
+        factory.setFeature("http://xml.org/sax/features/namespaces", true);
+        Transformer transformer = factory.newTransformer();
+        transformer.transform(source, new SAXResult(reader));
+        return reader.getNodeTypeManagerState();
+    }
+
+    public static NodeTypeManagerState read(InputSource source)
+            throws IOException, ParserConfigurationException, SAXException {
+        NodeTypeXMLReader reader = new NodeTypeXMLReader();
+        SAXParserFactory factory = SAXParserFactory.newInstance();
+        factory.setFeature("http://xml.org/sax/features/namespaces", true);
+        SAXParser parser = factory.newSAXParser();
+        parser.parse(source, reader);
+        return reader.getNodeTypeManagerState();
+    }
+
+    public static NodeTypeManagerState read(File file)
+            throws IOException, ParserConfigurationException, SAXException {
+        NodeTypeXMLReader reader = new NodeTypeXMLReader();
+        SAXParserFactory factory = SAXParserFactory.newInstance();
+        factory.setFeature("http://xml.org/sax/features/namespaces", true);
+        SAXParser parser = factory.newSAXParser();
+        parser.parse(file, reader);
+        return reader.getNodeTypeManagerState();
+    }
+
+    public static NodeTypeManagerState read(InputStream input)
+            throws IOException, ParserConfigurationException, SAXException {
+        NodeTypeXMLReader reader = new NodeTypeXMLReader();
+        SAXParserFactory factory = SAXParserFactory.newInstance();
+        factory.setFeature("http://xml.org/sax/features/namespaces", true);
+        SAXParser parser = factory.newSAXParser();
+        parser.parse(input, reader);
+        return reader.getNodeTypeManagerState();
+    }
+
+    public static void write(NodeTypeManagerState state, ContentHandler handler)
+            throws SAXException {
+        NodeTypeXMLWriter writer = new NodeTypeXMLWriter(state);
+        writer.write(handler);
+    }
+
+    public static void write(NodeTypeManagerState state, Result result)
+            throws TransformerConfigurationException, SAXException {
+        SAXTransformerFactory factory = (SAXTransformerFactory)
+            SAXTransformerFactory.newInstance();
+        TransformerHandler handler = factory.newTransformerHandler();
+        handler.setResult(result);
+        write(state, handler);
+    }
+
+    public static void write(NodeTypeManagerState state, OutputStream output)
+            throws TransformerConfigurationException, SAXException {
+        write(state, new StreamResult(output));
+    }
+
+    public static void write(NodeTypeManagerState state, File file)
+            throws IOException, TransformerConfigurationException, SAXException {
+        OutputStream output = new FileOutputStream(file);
+        try {
+            write(state, output);
+        } finally {
+            output.close();
+        }
+    }
+
+    public static void write(NodeTypeManagerState state, String filename)
+            throws IOException, TransformerConfigurationException, SAXException {
+        write(state, new File(filename));
+    }
+
+}

Propchange: incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/xml/nodetype/NodeTypeXML.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/xml/nodetype/NodeTypeXMLReader.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/xml/nodetype/NodeTypeXMLReader.java?rev=328810&view=auto
==============================================================================
--- incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/xml/nodetype/NodeTypeXMLReader.java
(added)
+++ incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/xml/nodetype/NodeTypeXMLReader.java
Wed Oct 26 23:44:56 2005
@@ -0,0 +1,249 @@
+/*
+ * 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.xml.nodetype;
+
+import java.util.LinkedList;
+import java.util.List;
+
+import javax.jcr.PropertyType;
+import javax.jcr.version.OnParentVersionAction;
+
+import org.apache.jackrabbit.name.IllegalNameException;
+import org.apache.jackrabbit.name.QName;
+import org.apache.jackrabbit.name.UnknownPrefixException;
+import org.apache.jackrabbit.state.nodetype.NodeDefinitionState;
+import org.apache.jackrabbit.state.nodetype.NodeTypeManagerState;
+import org.apache.jackrabbit.state.nodetype.NodeTypeState;
+import org.apache.jackrabbit.state.nodetype.PropertyDefinitionState;
+import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.DefaultHandler;
+
+/**
+ * TODO
+ */
+class NodeTypeXMLReader extends DefaultHandler {
+
+    private StringBuffer buffer;
+
+    private SAXNamespaceResolver resolver;
+
+    private NodeTypeManagerState nodeTypeManagerState;
+
+    private List nodeTypeStates;
+
+    private NodeTypeState nodeTypeState;
+
+    private List supertypeNames;
+
+    private List propertyDefinitionStates;
+
+    private PropertyDefinitionState propertyDefinitionState;
+
+    private List defaultValues;
+
+    private List valueConstraints;
+
+    private List childNodeDefinitionStates;
+
+    private NodeDefinitionState childNodeDefinitionState;
+
+    private List requiredPrimaryTypeNames;
+
+    public NodeTypeManagerState getNodeTypeManagerState() {
+        return nodeTypeManagerState;
+    }
+
+    public void startDocument() {
+        buffer = new StringBuffer();
+        resolver = new SAXNamespaceResolver();
+        resolver.startDocument();
+        nodeTypeManagerState = new NodeTypeManagerState();
+    }
+
+    public void endDocument() {
+        resolver.endDocument();
+    }
+
+    public void startPrefixMapping(String prefix, String uri) {
+        resolver.startPrefixMapping(prefix, uri);
+    }
+
+    public void endPrefixMapping(String prefix) {
+        resolver.endPrefixMapping(prefix);
+    }
+
+    /** {@inheritDoc} */
+    public void startElement(
+            String uri, String localName, String qName, Attributes attributes)
+            throws SAXException {
+        buffer.setLength(0);
+
+        if (uri == null || uri.length() == 0) {
+            System.out.println("<" + localName + ">");
+            if (localName.equals("nodeTypes")) {
+                nodeTypeStates = new LinkedList();
+            } else if (localName.equals("nodeType")) {
+                nodeTypeState = getNodeTypeState(attributes);
+                supertypeNames = new LinkedList();
+                propertyDefinitionStates = new LinkedList();
+                childNodeDefinitionStates = new LinkedList();
+            } else if (localName.equals("supertypes")) {
+            } else if (localName.equals("supertype")) {
+            } else if (localName.equals("propertyDefinition")) {
+                propertyDefinitionState = getPropertyDefinitionState(attributes);
+            } else if (localName.equals("valueConstraints")) {
+            } else if (localName.equals("valueConstraint")) {
+            } else if (localName.equals("defaultValues")) {
+            } else if (localName.equals("defaultValue")) {
+            } else if (localName.equals("childNodeDefinition")) {
+                childNodeDefinitionState = getNodeDefinitionState(attributes);
+            } else if (localName.equals("requiredPrimaryTypes")) {
+                requiredPrimaryTypeNames = new LinkedList();
+            } else if (localName.equals("requiredPrimaryType")) {
+            } else {
+                throw new SAXException("Unknown element: " + localName);
+            }
+        }
+
+        resolver.startElement();
+    }
+
+    public void endElement(String uri, String localName, String qName)
+            throws SAXException {
+        resolver.endElement();
+
+        if (uri == null || uri.length() == 0) {
+            System.out.println("</" + localName + ">");
+            if (localName.equals("nodeTypes")) {
+                nodeTypeManagerState.setNodeTypeStates((NodeTypeState[])
+                        nodeTypeStates.toArray(new NodeTypeState[nodeTypeStates.size()]));
+            } else if (localName.equals("nodeType")) {
+                nodeTypeState.setSupertypeNames((QName[])
+                        supertypeNames.toArray(new QName[supertypeNames.size()]));
+                nodeTypeState.setPropertyDefinitionStates((PropertyDefinitionState[])
+                        propertyDefinitionStates.toArray(new PropertyDefinitionState[propertyDefinitionStates.size()]));
+                nodeTypeState.setChildNodeDefinitionStates((NodeDefinitionState[])
+                        childNodeDefinitionStates.toArray(new NodeDefinitionState[childNodeDefinitionStates.size()]));
+                nodeTypeStates.add(nodeTypeState);
+            } else if (localName.equals("supertypes")) {
+            } else if (localName.equals("supertype")) {
+                supertypeNames.add(getName());
+            } else if (localName.equals("propertyDefinition")) {
+                propertyDefinitionStates.add(propertyDefinitionState);
+            } else if (localName.equals("valueConstraints")) {
+            } else if (localName.equals("valueConstraint")) {
+            } else if (localName.equals("defaultValues")) {
+            } else if (localName.equals("defaultValue")) {
+            } else if (localName.equals("childNodeDefinition")) {
+                childNodeDefinitionStates.add(childNodeDefinitionState);
+            } else if (localName.equals("requiredPrimaryTypes")) {
+                childNodeDefinitionState.setRequiredPrimaryTypeName((QName[])
+                        requiredPrimaryTypeNames.toArray(new QName[requiredPrimaryTypeNames.size()]));
+            } else if (localName.equals("requiredPrimaryType")) {
+                requiredPrimaryTypeNames.add(getName());
+            } else {
+                throw new SAXException("Unknown element: " + localName);
+            }
+        }
+
+        buffer.setLength(0);
+    }
+
+    public void characters(char[] ch, int start, int length) {
+        buffer.append(ch, start, length);
+    }
+
+    public NodeTypeState getNodeTypeState(Attributes attributes) throws SAXException {
+        NodeTypeState state = new NodeTypeState();
+        state.setName(getName(attributes, "name"));
+        if (getValue(attributes, "primaryItemName").length() > 0) {
+            state.setPrimaryItemName(getName(attributes, "primaryItemName"));
+        }
+        state.setMixin(getBoolean(attributes, "isMixin"));
+        state.setHasOrderableChildNodes(getBoolean(attributes, "hasOrderableChildNodes"));
+        return state;
+    }
+    
+    public NodeDefinitionState getNodeDefinitionState(Attributes attributes) throws SAXException
{
+        NodeDefinitionState state = new NodeDefinitionState();
+        if (!getValue(attributes, "name").equals("*")) {
+            state.setName(getName(attributes, "name"));
+        }
+        if (getValue(attributes, "defaultPrimaryType").length() > 0) {
+            state.setDefaultPrimaryTypeName(getName(attributes, "defaultPrimaryType"));
+        }
+        state.setAutoCreated(getBoolean(attributes, "autoCreated"));
+        state.setMandatory(getBoolean(attributes, "mandatory"));
+        state.setOnParentVersion(OnParentVersionAction.valueFromName(getValue(attributes,
"onParentVersion")));
+        state.setProtected(getBoolean(attributes, "protected"));
+        state.setAllowsSameNameSiblings(getBoolean(attributes, "sameNameSiblings"));
+        return state;
+    }
+
+    private PropertyDefinitionState getPropertyDefinitionState(Attributes attributes) throws
SAXException {
+        PropertyDefinitionState state = new PropertyDefinitionState();
+        if (!getValue(attributes, "name").equals("*")) {
+            state.setName(getName(attributes, "name"));
+        }
+        state.setRequiredType(PropertyType.valueFromName(getValue(attributes, "requiredType")));
+        state.setAutoCreated(getBoolean(attributes, "autoCreated"));
+        state.setMandatory(getBoolean(attributes, "mandatory"));
+        state.setOnParentVersion(OnParentVersionAction.valueFromName(getValue(attributes,
"onParentVersion")));
+        state.setProtected(getBoolean(attributes, "protected"));
+        state.setMultiple(getBoolean(attributes, "multiple"));
+        return state;
+    }
+
+    private QName getName() throws SAXException {
+        String value = buffer.toString();
+        try {
+            return QName.fromJCRName(value, resolver);
+        } catch (UnknownPrefixException e) {
+            throw new SAXException("Unknown prefix: " + value, e);
+        } catch (IllegalNameException e) {
+            throw new SAXException("Illegal name: " + value, e);
+        }
+    }
+
+    private String getValue(Attributes attributes, String name) throws SAXException {
+        String value = attributes.getValue("", name);
+        if (value != null) {
+            return value;
+        } else {
+            throw new SAXException("Required attribute " + name + " not found");
+        }
+    }
+
+    private QName getName(Attributes attributes, String name) throws SAXException {
+        String value = getValue(attributes, name);
+        try {
+            return QName.fromJCRName(value, resolver);
+        } catch (UnknownPrefixException e) {
+            throw new SAXException("Unknown prefix: " + value, e);
+        } catch (IllegalNameException e) {
+            throw new SAXException("Illegal name: " + value, e);
+        }
+    }
+
+    private boolean getBoolean(Attributes attributes, String name)
+            throws SAXException {
+        String value = getValue(attributes, name);
+        return Boolean.valueOf(value).booleanValue();
+    }
+
+}

Propchange: incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/xml/nodetype/NodeTypeXMLReader.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/xml/nodetype/NodeTypeXMLWriter.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/xml/nodetype/NodeTypeXMLWriter.java?rev=328810&view=auto
==============================================================================
--- incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/xml/nodetype/NodeTypeXMLWriter.java
(added)
+++ incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/xml/nodetype/NodeTypeXMLWriter.java
Wed Oct 26 23:44:56 2005
@@ -0,0 +1,226 @@
+/*
+ * 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.xml.nodetype;
+
+import java.util.Iterator;
+import java.util.Map;
+
+import javax.jcr.PropertyType;
+import javax.jcr.version.OnParentVersionAction;
+
+import org.apache.jackrabbit.name.QName;
+import org.apache.jackrabbit.state.nodetype.NodeDefinitionState;
+import org.apache.jackrabbit.state.nodetype.NodeTypeManagerState;
+import org.apache.jackrabbit.state.nodetype.NodeTypeState;
+import org.apache.jackrabbit.state.nodetype.PropertyDefinitionState;
+import org.apache.xerces.util.XMLChar;
+import org.xml.sax.Attributes;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.AttributesImpl;
+
+/**
+ * TODO
+ */
+class NodeTypeXMLWriter {
+
+    private final NodeTypeManagerState nodeTypeManagerState;
+
+    private Map namespaces;
+
+    private ContentHandler handler;
+
+    public NodeTypeXMLWriter(NodeTypeManagerState nodeTypeManagerState) {
+        this.nodeTypeManagerState = nodeTypeManagerState;
+    }
+
+    private void startElement(String name) throws SAXException {
+        handler.startElement("", name, name, null);
+    }
+
+    private void startElement(String name, Attributes attributes)
+            throws SAXException {
+        handler.startElement("", name, name, attributes);
+    }
+
+    private void endElement(String name) throws SAXException {
+        handler.endElement("", name, name);
+    }
+
+    public void write(ContentHandler handler) throws SAXException {
+        this.namespaces =
+            NamespaceExtractor.extractNamespaces(nodeTypeManagerState);
+        this.handler = handler;
+        handler.startDocument();
+
+        Iterator entries = namespaces.entrySet().iterator();
+        while (entries.hasNext()) {
+            Map.Entry entry = (Map.Entry) entries.next();
+            handler.startPrefixMapping(
+                    (String) entry.getValue(), (String) entry.getKey()); 
+        }
+
+        write(nodeTypeManagerState);
+
+        Iterator values = namespaces.values().iterator();
+        while (values.hasNext()) {
+            handler.endPrefixMapping((String) values.next()); 
+        }
+
+        handler.endDocument();
+    }
+
+    private void write(NodeTypeManagerState nodeTypeManagerState)
+            throws SAXException {
+        startElement("nodeTypes");
+        NodeTypeState[] nodeTypeStates = nodeTypeManagerState.getNodeTypeStates();
+        for (int i = 0; i < nodeTypeStates.length; i++) {
+            write(nodeTypeStates[i]);
+        }
+        endElement("nodeTypes");
+    }
+
+    private void write(NodeTypeState nodeTypeState) throws SAXException {
+        AttributesImpl attributes = new AttributesImpl();
+        addAttribute(attributes, "name", nodeTypeState.getName());
+        QName primaryItemName = nodeTypeState.getPrimaryItemName();
+        if (primaryItemName != null) {
+            addAttribute(attributes, "primaryItemName", primaryItemName);
+        } else {
+            addAttribute(attributes, "primaryItemName", "");
+        }
+        addAttribute(attributes, "isMixin", nodeTypeState.isMixin());
+        addAttribute(attributes, "hasOrderableChildNodes", nodeTypeState.hasOrderableChildNodes());
+        startElement("nodeType", attributes);
+
+        QName[] supertypeNames = nodeTypeState.getSupertypeNames();
+        if (supertypeNames != null && supertypeNames.length > 0) {
+            startElement("supertypes");
+            for (int i = 0; i < supertypeNames.length; i++) {
+                startElement("supertype");
+                char[] characters = filterXML(toJCRName(supertypeNames[i]));
+                handler.characters(characters, 0, characters.length);
+                endElement("supertype");
+            }
+            endElement("supertypes");
+        }
+
+        PropertyDefinitionState[] propertyDefinitionStates =
+            nodeTypeState.getPropertyDefinitionStates();
+        for (int i = 0; i < propertyDefinitionStates.length; i++) {
+            write(propertyDefinitionStates[i]);
+        }
+
+        NodeDefinitionState[] childNodeDefinitionStates =
+            nodeTypeState.getChildNodeDefinitionStates();
+        for (int i = 0; i < childNodeDefinitionStates.length; i++) {
+            write(childNodeDefinitionStates[i]);
+        }
+
+        endElement("nodeType");
+    }
+
+    private void write(PropertyDefinitionState propertyDefinitionState)
+            throws SAXException {
+        AttributesImpl attributes = new AttributesImpl();
+        if (propertyDefinitionState.getName() != null) {
+            addAttribute(attributes, "name", propertyDefinitionState.getName());
+        } else {
+            addAttribute(attributes, "name", "*");
+        }
+        addAttribute(
+                attributes, "requiredType",
+                PropertyType.nameFromValue(propertyDefinitionState.getRequiredType()));
+        addAttribute(attributes, "autoCreated", propertyDefinitionState.isAutoCreated());
+        addAttribute(attributes, "mandatory", propertyDefinitionState.isMandatory());
+        addAttribute(
+                attributes, "onParentVersion",
+                OnParentVersionAction.nameFromValue(propertyDefinitionState.getOnParentVersion()));
+        addAttribute(attributes, "protected", propertyDefinitionState.isProtected());
+        addAttribute(attributes, "multiple", propertyDefinitionState.isMultiple());
+        startElement("propertyDefinition", attributes);
+        // TODO: default values
+        // TODO: value constraints
+        endElement("propertyDefinition");
+    }
+
+    private void write(NodeDefinitionState childNodeDefinitionState)
+            throws SAXException {
+        AttributesImpl attributes = new AttributesImpl();
+        if (childNodeDefinitionState.getName() != null) {
+            addAttribute(attributes, "name", childNodeDefinitionState.getName());
+        } else {
+            addAttribute(attributes, "name", "*");
+        }
+        if (childNodeDefinitionState.getDefaultPrimaryTypeName() != null) {
+            addAttribute(attributes, "defaultPrimaryType", childNodeDefinitionState.getDefaultPrimaryTypeName());
+        } else {
+            addAttribute(attributes, "defaultPrimaryType", "");
+        }
+        addAttribute(attributes, "autoCreated", childNodeDefinitionState.isAutoCreated());
+        addAttribute(attributes, "mandatory", childNodeDefinitionState.isMandatory());
+        addAttribute(
+                attributes, "onParentVersion",
+                OnParentVersionAction.nameFromValue(childNodeDefinitionState.getOnParentVersion()));
+        addAttribute(attributes, "protected", childNodeDefinitionState.isProtected());
+        addAttribute(attributes, "sameNameSiblings", childNodeDefinitionState.allowsSameNameSiblings());
+        startElement("childNodeDefinition", attributes);
+
+        QName[] requiredPrimaryTypeNames = childNodeDefinitionState.getRequiredPrimaryTypeNames();
+        if (requiredPrimaryTypeNames != null && requiredPrimaryTypeNames.length >
0) {
+            startElement("requiredPrimaryTypes");
+            for (int i = 0; i < requiredPrimaryTypeNames.length; i++) {
+                startElement("requiredPrimaryType");
+                char[] characters = filterXML(toJCRName(requiredPrimaryTypeNames[i]));
+                handler.characters(characters, 0, characters.length);
+                endElement("requiredPrimaryType");
+            }
+            endElement("requiredPrimaryTypes");
+        }
+
+        endElement("childNodeDefinition");
+    }
+
+    private void addAttribute(AttributesImpl attributes, String name, boolean value) {
+        addAttribute(attributes, name, Boolean.toString(value));
+    }
+
+    private void addAttribute(AttributesImpl attributes, String name, QName value) {
+        addAttribute(attributes, name, toJCRName(value));
+    }
+
+    private void addAttribute(AttributesImpl attributes, String name, String value) {
+        attributes.addAttribute(
+                "", name, name, "CDATA", new String(filterXML(value)));
+    }
+
+    private String toJCRName(QName name) {
+        String prefix = (String) namespaces.get(name.getNamespaceURI());
+        return prefix + ":" + name.getLocalName();
+    }
+
+    private char[] filterXML(String value) {
+        char[] characters = value.toCharArray();
+        for (int i = 0; i < characters.length; i++) {
+            if (XMLChar.isInvalid(characters[i])) {
+                characters[i] = ' '; // TODO: better escape?
+            }
+        }
+        return characters;
+    }
+
+}

Propchange: incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/xml/nodetype/NodeTypeXMLWriter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/xml/nodetype/SAXNamespaceResolver.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/xml/nodetype/SAXNamespaceResolver.java?rev=328810&view=auto
==============================================================================
--- incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/xml/nodetype/SAXNamespaceResolver.java
(added)
+++ incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/xml/nodetype/SAXNamespaceResolver.java
Wed Oct 26 23:44:56 2005
@@ -0,0 +1,88 @@
+/*
+ * 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.xml.nodetype;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.Map;
+
+import javax.jcr.NamespaceException;
+
+import org.apache.jackrabbit.name.NamespaceResolver;
+
+/**
+ * TODO
+ */
+public class SAXNamespaceResolver implements NamespaceResolver {
+
+    private final LinkedList stack = new LinkedList();
+
+    public void startDocument() {
+        stack.addFirst(new HashMap());
+    }
+
+    public void endDocument() {
+        stack.removeFirst();
+    }
+
+    public void startElement() {
+        stack.addFirst(new HashMap());
+    }
+
+    public void endElement() {
+        stack.removeFirst();
+    }
+    
+    public void startPrefixMapping(String prefix, String uri) {
+        Map context = (Map) stack.getFirst();
+        context.put(prefix, uri);
+    }
+
+    public void endPrefixMapping(String prefix) {
+        Map context = (Map) stack.getFirst();
+        context.remove(prefix);
+    }
+
+    public String getURI(String prefix) throws NamespaceException {
+        Iterator iterator = stack.iterator();
+        while (iterator.hasNext()) {
+            Map context = (Map) iterator.next();
+            String uri = (String) context.get(prefix);
+            if (uri != null) {
+                return uri;
+            }
+        }
+        throw new NamespaceException("Prefix " + prefix + " not found");
+    }
+
+    public String getPrefix(String uri) throws NamespaceException {
+        Iterator iterator = stack.iterator();
+        while (iterator.hasNext()) {
+            Map context = (Map) iterator.next();
+            Iterator entries = context.entrySet().iterator();
+            while (entries.hasNext()) {
+                Map.Entry entry = (Map.Entry) entries.next();
+                if (uri.equals(entry.getValue())) {
+                    return (String) entry.getKey();
+                }
+            }
+        }
+        throw new NamespaceException("Namespace URI " + uri + " not found");
+    }
+
+}

Propchange: incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/xml/nodetype/SAXNamespaceResolver.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/xml/nodetype/nodetypes.dtd
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/xml/nodetype/nodetypes.dtd?rev=328810&view=auto
==============================================================================
--- incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/xml/nodetype/nodetypes.dtd
(added)
+++ incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/xml/nodetype/nodetypes.dtd
Wed Oct 26 23:44:56 2005
@@ -0,0 +1,42 @@
+<!ELEMENT nodeTypes (nodeType)*>
+<!ELEMENT nodeType (supertypes?|propertyDefinition*|childNodeDefinition*)>
+
+<!ATTLIST nodeType
+        name CDATA #REQUIRED
+	isMixin (true|false) #REQUIRED
+ 	hasOrderableChildNodes (true|false) #REQUIRED
+	primaryItemName CDATA #REQUIRED
+>
+
+<!ELEMENT supertypes (supertype+)>
+<!ELEMENT supertype (CDATA)>
+
+<!ELEMENT propertyDefinition (valueConstraints?|defaultValues?)>
+<!ATTLIST propertyDefinition
+	name CDATA #REQUIRED
+	requiredType (String|Date|Path|Name|Reference|Binary|Double|Long|Boolean|undefined) #REQUIRED
+	autoCreated (true|false) #REQUIRED
+	mandatory (true|false) #REQUIRED
+	onParentVersion (COPY|VERSION|INITIALIZE|COMPUTE|IGNORE|ABORT) #REQUIRED
+	protected (true|false) #REQUIRED
+	multiple  (true|false) #REQUIRED
+>
+
+<!ELEMENT valueConstraints (valueConstraint+)>
+<!ELEMENT valueConstraint (CDATA)>
+<!ELEMENT defaultValues (defaultValue+)>
+<!ELEMENT defaultValue (CDATA)>
+
+<!ELEMENT childNodeDefinition (requiredPrimaryTypes)>
+<!ATTLIST childNodeDefinition
+	name CDATA #REQUIRED
+	defaultPrimaryType  CDATA #REQUIRED
+	autoCreated (true|false) #REQUIRED
+	mandatory (true|false) #REQUIRED
+	onParentVersion (COPY|VERSION|INITIALIZE|COMPUTE|IGNORE|ABORT) #REQUIRED
+	protected (true|false) #REQUIRED
+	sameNameSiblings (true|false) #REQUIRED
+>
+
+<!ELEMENT requiredPrimaryTypes (requiredPrimaryType+)>
+<!ELEMENT requiredPrimaryType (CDATA)>

Propchange: incubator/jackrabbit/trunk/contrib/jcr-ext/src/java/org/apache/jackrabbit/xml/nodetype/nodetypes.dtd
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message