ws-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From veit...@apache.org
Subject svn commit: r1729243 - in /webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/serializer/push: NamespaceContextPreservationFilterHandler.java NamespaceHelper.java SerializerImpl.java
Date Mon, 08 Feb 2016 20:16:17 GMT
Author: veithen
Date: Mon Feb  8 20:16:17 2016
New Revision: 1729243

URL: http://svn.apache.org/viewvc?rev=1729243&view=rev
Log:
Extract the namespace preservation logic from SerializerImpl.

Added:
    webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/serializer/push/NamespaceContextPreservationFilterHandler.java
  (with props)
Modified:
    webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/serializer/push/NamespaceHelper.java
    webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/serializer/push/SerializerImpl.java

Added: webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/serializer/push/NamespaceContextPreservationFilterHandler.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/serializer/push/NamespaceContextPreservationFilterHandler.java?rev=1729243&view=auto
==============================================================================
--- webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/serializer/push/NamespaceContextPreservationFilterHandler.java
(added)
+++ webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/serializer/push/NamespaceContextPreservationFilterHandler.java
Mon Feb  8 20:16:17 2016
@@ -0,0 +1,83 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.axiom.om.impl.common.serializer.push;
+
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Set;
+
+import org.apache.axiom.om.OMContainer;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMNamespace;
+import org.apache.axiom.om.impl.stream.StreamException;
+import org.apache.axiom.om.impl.stream.XmlHandler;
+import org.apache.axiom.om.impl.stream.XmlHandlerWrapper;
+
+public final class NamespaceContextPreservationFilterHandler extends XmlHandlerWrapper {
+    private final OMElement contextElement;
+    // Maintain a set of the prefixes we have already seen. This is required to take into
+    // account that a namespace mapping declared on an element can hide another one declared
+    // for the same prefix on an ancestor of the element.
+    private Set<String> prefixesAlreadyBound;
+    private boolean done = false;
+    
+    public NamespaceContextPreservationFilterHandler(XmlHandler parent, OMElement contextElement)
{
+        super(parent);
+        this.contextElement = contextElement;
+    }
+
+    @Override
+    public void startElement(String namespaceURI, String localName, String prefix) throws
StreamException {
+        super.startElement(namespaceURI, localName, prefix);
+        if (!done) {
+            prefixesAlreadyBound = new HashSet<String>();
+        }
+    }
+
+    @Override
+    public void processNamespaceDeclaration(String prefix, String namespaceURI) throws StreamException
{
+        super.processNamespaceDeclaration(prefix, namespaceURI);
+        if (!done) {
+            prefixesAlreadyBound.add(prefix);
+        }
+    }
+
+    @Override
+    public void attributesCompleted() throws StreamException {
+        if (!done) {
+            OMElement current = contextElement;
+            while (true) {
+                for (Iterator<OMNamespace> it = current.getAllDeclaredNamespaces();
it.hasNext(); ) {
+                    OMNamespace ns = it.next();
+                    if (prefixesAlreadyBound.add(ns.getPrefix())) {
+                        super.processNamespaceDeclaration(ns.getPrefix(), ns.getNamespaceURI());
+                    }
+                }
+                OMContainer parent = current.getParent();
+                if (!(parent instanceof OMElement)) {
+                    break;
+                }
+                current = (OMElement)parent;
+            }
+            prefixesAlreadyBound = null;
+            done = true;
+        }
+        super.attributesCompleted();
+    }
+}

Propchange: webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/serializer/push/NamespaceContextPreservationFilterHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/serializer/push/NamespaceHelper.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/serializer/push/NamespaceHelper.java?rev=1729243&r1=1729242&r2=1729243&view=diff
==============================================================================
--- webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/serializer/push/NamespaceHelper.java
(original)
+++ webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/serializer/push/NamespaceHelper.java
Mon Feb  8 20:16:17 2016
@@ -21,6 +21,7 @@ package org.apache.axiom.om.impl.common.
 import org.apache.axiom.om.OMElement;
 import org.apache.axiom.om.OMNamespace;
 import org.apache.axiom.om.impl.stream.StreamException;
+import org.apache.axiom.om.impl.stream.XmlHandler;
 import org.apache.axiom.om.impl.stream.XmlHandlerWrapper;
 
 final class NamespaceHelper extends XmlHandlerWrapper {
@@ -30,8 +31,8 @@ final class NamespaceHelper extends XmlH
     private final SerializerImpl serializer;
     private final OMElement contextElement;
 
-    NamespaceHelper(SerializerImpl serializer, OMElement contextElement) {
-        super(serializer);
+    NamespaceHelper(SerializerImpl serializer, XmlHandler handler, OMElement contextElement)
{
+        super(handler);
         this.serializer = serializer;
         this.contextElement = contextElement;
     }

Modified: webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/serializer/push/SerializerImpl.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/serializer/push/SerializerImpl.java?rev=1729243&r1=1729242&r2=1729243&view=diff
==============================================================================
--- webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/serializer/push/SerializerImpl.java
(original)
+++ webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/common/serializer/push/SerializerImpl.java
Mon Feb  8 20:16:17 2016
@@ -18,9 +18,7 @@
  */
 package org.apache.axiom.om.impl.common.serializer.push;
 
-import java.util.HashSet;
 import java.util.Iterator;
-import java.util.Set;
 
 import javax.activation.DataHandler;
 import javax.xml.stream.XMLStreamException;
@@ -45,9 +43,7 @@ import org.apache.axiom.om.impl.stream.S
 import org.apache.axiom.om.impl.stream.XmlHandler;
 
 public abstract class SerializerImpl implements Serializer {
-    private final OMSerializable root;
     private final XmlHandler handler;
-    private final boolean preserveNamespaceContext;
     
     /**
      * Constructor.
@@ -63,7 +59,6 @@ public abstract class SerializerImpl imp
      *            should be strictly preserved in the output
      */
     public SerializerImpl(OMSerializable root, boolean namespaceRepairing, boolean preserveNamespaceContext)
{
-        this.root = root;
         OMElement contextElement;
         if (root instanceof OMNode) {
             OMContainer parent = ((OMNode)root).getParent();
@@ -75,8 +70,14 @@ public abstract class SerializerImpl imp
         } else {
             contextElement = null;
         }
-        handler = namespaceRepairing ? new NamespaceHelper(this, contextElement) : this;
-        this.preserveNamespaceContext = preserveNamespaceContext;
+        XmlHandler handler = this;
+        if (preserveNamespaceContext && contextElement != null) {
+            handler = new NamespaceContextPreservationFilterHandler(handler, contextElement);
+        }
+        if (namespaceRepairing) {
+            handler = new NamespaceHelper(this, handler, contextElement);
+        }
+        this.handler = handler;
     }
 
     public final void serializeStartpart(OMElement element) throws StreamException {
@@ -86,30 +87,9 @@ public abstract class SerializerImpl imp
         } else {
             handler.startElement(ns.getNamespaceURI(), element.getLocalName(), ns.getPrefix());
         }
-        if (preserveNamespaceContext && element == root) {
-            // Maintain a set of the prefixes we have already seen. This is required to take
into
-            // account that a namespace mapping declared on an element can hide another one
declared
-            // for the same prefix on an ancestor of the element.
-            Set<String> seenPrefixes = new HashSet<String>();
-            OMElement current = element;
-            while (true) {
-                for (Iterator<OMNamespace> it = current.getAllDeclaredNamespaces();
it.hasNext(); ) {
-                    ns = it.next();
-                    if (seenPrefixes.add(ns.getPrefix())) {
-                        handler.processNamespaceDeclaration(ns.getPrefix(), ns.getNamespaceURI());
-                    }
-                }
-                OMContainer parent = current.getParent();
-                if (!(parent instanceof OMElement)) {
-                    break;
-                }
-                current = (OMElement)parent;
-            }
-        } else {
-            for (Iterator<OMNamespace> it = element.getAllDeclaredNamespaces(); it.hasNext();
) {
-                ns = it.next();
-                handler.processNamespaceDeclaration(ns.getPrefix(), ns.getNamespaceURI());
-            }
+        for (Iterator<OMNamespace> it = element.getAllDeclaredNamespaces(); it.hasNext();
) {
+            ns = it.next();
+            handler.processNamespaceDeclaration(ns.getPrefix(), ns.getNamespaceURI());
         }
         for (Iterator<OMAttribute> it = element.getAllAttributes(); it.hasNext(); )
{
             OMAttribute attr = it.next();
@@ -120,7 +100,7 @@ public abstract class SerializerImpl imp
                 handler.processAttribute(ns.getNamespaceURI(), attr.getLocalName(), ns.getPrefix(),
attr.getAttributeValue(), attr.getAttributeType(), ((CoreAttribute)attr).coreGetSpecified());
             }
         }
-        attributesCompleted();
+        handler.attributesCompleted();
     }
     
     /**



Mime
View raw message