jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ang...@apache.org
Subject svn commit: r447377 - in /jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/xml: AbstractSAXEventGenerator.java DocViewSAXEventGenerator.java SysViewSAXEventGenerator.java
Date Mon, 18 Sep 2006 11:48:02 GMT
Author: angela
Date: Mon Sep 18 04:48:01 2006
New Revision: 447377

URL: http://svn.apache.org/viewvc?view=rev&rev=447377
Log:
work in progress

- sync (JCR-482)

Modified:
    jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/xml/AbstractSAXEventGenerator.java
    jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/xml/DocViewSAXEventGenerator.java
    jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/xml/SysViewSAXEventGenerator.java

Modified: jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/xml/AbstractSAXEventGenerator.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/xml/AbstractSAXEventGenerator.java?view=diff&rev=447377&r1=447376&r2=447377
==============================================================================
--- jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/xml/AbstractSAXEventGenerator.java
(original)
+++ jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/xml/AbstractSAXEventGenerator.java
Mon Sep 18 04:48:01 2006
@@ -16,13 +16,15 @@
  */
 package org.apache.jackrabbit.jcr2spi.xml;
 
-import org.apache.jackrabbit.name.SessionNamespaceResolver;
-import org.apache.jackrabbit.name.NamespaceResolver;
 import org.apache.jackrabbit.name.NameException;
-import org.apache.jackrabbit.name.QName;
 import org.apache.jackrabbit.name.NameFormat;
+import org.apache.jackrabbit.name.NamespaceResolver;
+import org.apache.jackrabbit.name.QName;
+import org.apache.jackrabbit.name.SessionNamespaceResolver;
+
 import org.xml.sax.ContentHandler;
 import org.xml.sax.SAXException;
+import org.xml.sax.helpers.AttributesImpl;
 import org.slf4j.LoggerFactory;
 import org.slf4j.Logger;
 
@@ -33,6 +35,9 @@
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 
+import java.util.HashMap;
+import java.util.Map;
+
 /**
  * <code>AbstractSAXEventGenerator</code> serves as the base class for
  * <code>SysViewSAXEventGenerator</code> and <code>DocViewSAXEventGenerator</code>
@@ -72,6 +77,11 @@
     protected final boolean noRecurse;
 
     /**
+     * the set of namespace declarations that have already been serialized
+     */
+    protected NamespaceStack namespaces;
+
+    /**
      * The jcr:primaryType property name (allowed for session-local prefix mappings)
      */
     protected final String jcrPrimaryType;
@@ -118,6 +128,8 @@
         this.contentHandler = contentHandler;
         this.skipBinary = skipBinary;
         this.noRecurse = noRecurse;
+        // start with an empty set of known prefixes
+        this.namespaces = new NamespaceStack(null);
 
         // resolve the names of some wellknown properties
         // allowing for session-local prefix mappings
@@ -194,6 +206,54 @@
     }
 
     /**
+     * Adds explicit <code>xmlns:prefix="uri"</code> attributes to the
+     * XML element as required (e.g., normally just on the root
+     * element). The effect is the same as setting the
+     * "<code>http://xml.org/sax/features/namespace-prefixes</code>"
+     * property on an SAX parser.
+     *
+     * @param level level of the current XML element
+     * @param attributes attributes of the current XML element
+     * @throws RepositoryException on a repository error
+     */
+    protected void addNamespacePrefixes(int level, AttributesImpl attributes)
+            throws RepositoryException {
+        String[] prefixes = session.getNamespacePrefixes();
+        NamespaceStack newNamespaces = null;
+
+        for (int i = 0; i < prefixes.length; i++) {
+            String prefix = prefixes[i];
+
+            if (prefix.length() > 0
+                    && !QName.NS_XML_PREFIX.equals(prefix)) {
+                String uri = session.getNamespaceURI(prefix);
+
+                // get the matching namespace from previous declarations
+                String mappedToNs = this.namespaces.getNamespaceURI(prefix);
+
+                if (!uri.equals(mappedToNs)) {
+                    // when not the same, add a declaration
+                    attributes.addAttribute(
+                        QName.NS_XMLNS_URI,
+                        prefix,
+                        QName.NS_XMLNS_PREFIX + ":" + prefix,
+                        "CDATA",
+                        uri);
+
+                    if (newNamespaces == null) {
+                        // replace current namespace stack when needed
+                        newNamespaces = new NamespaceStack(this.namespaces);
+                        this.namespaces = newNamespaces;
+                    }
+
+                    // remember the new declaration
+                    newNamespaces.setNamespacePrefix(prefix, uri);
+                }
+            }
+        }
+    }
+
+    /**
      * @param node
      * @param level
      * @throws RepositoryException
@@ -247,9 +307,16 @@
             // child nodes
             NodeIterator nodeIter = node.getNodes();
             while (nodeIter.hasNext()) {
-                Node childNode = nodeIter.nextNode();
                 // recurse
+                Node childNode = nodeIter.nextNode();
+                // remember the current namespace declarations
+                NamespaceStack previousNamespaces = this.namespaces;
+
                 process(childNode, level + 1);
+
+                // restore the effective namespace declarations
+                // (from before visiting the child node)
+                this.namespaces = previousNamespaces;
             }
         }
 
@@ -323,5 +390,61 @@
      */
     protected abstract void leaving(Property prop, int level)
             throws RepositoryException, SAXException;
+
+    /**
+     * Implements a simple stack of namespace declarations.
+     */
+    private static class NamespaceStack {
+
+        /**
+         * Parent stack (may be <code>null</code>)
+         */
+        private final NamespaceStack parent;
+
+        /**
+         * Local namespace declarations.
+         */
+        private final Map namespaces;
+
+        /**
+         * Instantiate a new stack
+         *
+         * @param parent parent stack (may be <code>null</code> for the initial
stack)
+         */
+        public NamespaceStack(NamespaceStack parent) {
+            this.parent = parent;
+            this.namespaces = new HashMap();
+        }
+
+        /**
+         * Obtain namespace URI for a prefix
+         *
+         * @param prefix prefix
+         * @return namespace URI (or <code>null</code> when unknown)
+         */
+        public String getNamespaceURI(String prefix) {
+            String namespace = (String) namespaces.get(prefix);
+            if (namespace != null) {
+                // found in this element, return right away
+                return namespace;
+            } else if (parent != null) {
+                // ask parent, when present
+                return parent.getNamespaceURI(prefix);
+            } else {
+                return null;
+            }
+        }
+
+        /**
+         * Add a new prefix mapping
+         *
+         * @param prefix namespace prefix
+         * @param uri namespace URI
+         */
+        public void setNamespacePrefix(String prefix, String uri) {
+            namespaces.put(prefix, uri);
+        }
+
+    }
 
 }

Modified: jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/xml/DocViewSAXEventGenerator.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/xml/DocViewSAXEventGenerator.java?view=diff&rev=447377&r1=447376&r2=447377
==============================================================================
--- jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/xml/DocViewSAXEventGenerator.java
(original)
+++ jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/xml/DocViewSAXEventGenerator.java
Mon Sep 18 04:48:01 2006
@@ -133,6 +133,7 @@
 
             // attributes (properties)
             AttributesImpl attrs = new AttributesImpl();
+            addNamespacePrefixes(level, attrs);
             Iterator iter = props.iterator();
             while (iter.hasNext()) {
                 Property prop = (Property) iter.next();
@@ -169,6 +170,7 @@
                             attrValue.toString());
                 }
             }
+
             // start element (node)
             QName qName = getQName(elemName);
             contentHandler.startElement(qName.getNamespaceURI(),
@@ -187,8 +189,6 @@
             // (already processed in leavingProperties(NodeImpl, int)
             return;
         }
-        // encode node name to make sure it's a valid xml name
-        name = ISO9075.encode(name);
         // element name
         String elemName;
         if (node.getDepth() == 0) {

Modified: jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/xml/SysViewSAXEventGenerator.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/xml/SysViewSAXEventGenerator.java?view=diff&rev=447377&r1=447376&r2=447377
==============================================================================
--- jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/xml/SysViewSAXEventGenerator.java
(original)
+++ jackrabbit/trunk/contrib/spi/jcr2spi/src/main/java/org/apache/jackrabbit/jcr2spi/xml/SysViewSAXEventGenerator.java
Mon Sep 18 04:48:01 2006
@@ -16,8 +16,8 @@
  */
 package org.apache.jackrabbit.jcr2spi.xml;
 
-import org.apache.jackrabbit.value.ValueHelper;
 import org.apache.jackrabbit.name.QName;
+import org.apache.jackrabbit.value.ValueHelper;
 import org.xml.sax.ContentHandler;
 import org.xml.sax.SAXException;
 import org.xml.sax.helpers.AttributesImpl;
@@ -86,6 +86,7 @@
     protected void entering(Node node, int level)
             throws RepositoryException, SAXException {
         AttributesImpl attrs = new AttributesImpl();
+        addNamespacePrefixes(level, attrs);
         // name attribute
         String nodeName;
         if (node.getDepth() == 0) {



Mime
View raw message