cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1158246 - in /cxf/trunk: common/common/src/main/java/org/apache/cxf/staxutils/transform/ common/common/src/test/java/org/apache/cxf/staxutils/transform/ rt/core/src/main/java/org/apache/cxf/interceptor/transform/ rt/frontend/jaxrs/src/main...
Date Tue, 16 Aug 2011 12:52:29 GMT
Author: sergeyb
Date: Tue Aug 16 12:52:29 2011
New Revision: 1158246

URL: http://svn.apache.org/viewvc?rev=1158246&view=rev
Log:
[CXF-3745] Support for default namespaces in OutTransformWriter

Added:
    cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/transform/OutTransformWriterTest.java
  (with props)
Modified:
    cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/transform/OutTransformWriter.java
    cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/transform/TransformUtils.java
    cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/transform/TransformOutInterceptor.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java

Modified: cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/transform/OutTransformWriter.java
URL: http://svn.apache.org/viewvc/cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/transform/OutTransformWriter.java?rev=1158246&r1=1158245&r2=1158246&view=diff
==============================================================================
--- cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/transform/OutTransformWriter.java
(original)
+++ cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/transform/OutTransformWriter.java
Tue Aug 16 12:52:29 2011
@@ -34,6 +34,7 @@ import org.apache.cxf.helpers.XMLUtils;
 import org.apache.cxf.staxutils.DelegatingXMLStreamWriter;
 
 public class OutTransformWriter extends DelegatingXMLStreamWriter {
+    private String defaultNamespace;
     private QNamesMap elementsMap;
     private Map<QName, QName> appendMap = new HashMap<QName, QName>(5);
     private Map<String, String> nsMap = new HashMap<String, String>(5);
@@ -51,7 +52,8 @@ public class OutTransformWriter extends 
                               Map<String, String> outMap,
                               Map<String, String> append,
                               List<String> dropEls,
-                              boolean attributesToElements) {
+                              boolean attributesToElements,
+                              String defaultNamespace) {
         super(writer);
         elementsMap = new QNamesMap(outMap == null ? 0 : outMap.size());
         TransformUtils.convertToQNamesMap(outMap, elementsMap, nsMap);
@@ -60,6 +62,7 @@ public class OutTransformWriter extends 
         this.attributesToElements = attributesToElements;
         namespaceContext = new DelegatingNamespaceContext(
             writer.getNamespaceContext(), nsMap);
+        this.defaultNamespace = defaultNamespace;
     }
 
     @Override
@@ -67,14 +70,27 @@ public class OutTransformWriter extends 
         if (matchesDropped()) {
             return;
         }
-        if (writtenUris.contains(uri)) {
-            return;
-        }
         String value = nsMap.get(uri);
         if (value != null && value.length() == 0) {
             return;
         }
-        super.writeNamespace(prefix, value != null ? value : uri);
+        
+        uri = value != null ? value : uri;
+        
+        if (writtenUris.contains(uri)) {
+            return;
+        }
+        
+        if (defaultNamespace != null && defaultNamespace.equals(uri)) {
+            super.writeDefaultNamespace(uri);
+        } else {
+            if (prefix.length() == 0) {
+                prefix = namespaceContext.findUniquePrefix(uri);
+            }
+            super.writeNamespace(prefix, uri);
+        }
+        writtenUris.add(uri);
+        
     }
     
     @Override
@@ -88,8 +104,7 @@ public class OutTransformWriter extends 
             String theprefix = uri.equals(appendQName.getNamespaceURI()) ? prefix : "";
             write(new QName(appendQName.getNamespaceURI(), appendQName.getLocalPart(), theprefix));
             if (theprefix.length() > 0) {
-                super.writeNamespace(theprefix, uri);
-                writtenUris.add(uri);
+                this.writeNamespace(theprefix, uri);
             }
             appendedElements.add(appendQName);
             appendedIndexes.add(currentDepth - 1);
@@ -139,6 +154,10 @@ public class OutTransformWriter extends 
             }
             
         }
+        if (defaultNamespace != null && defaultNamespace.equals(name.getNamespaceURI()))
{
+            prefix = "";
+        }
+        
         super.writeStartElement(prefix, name.getLocalPart(), name.getNamespaceURI());
         if (writeNs) {
             this.writeNamespace(prefix, name.getNamespaceURI());

Modified: cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/transform/TransformUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/transform/TransformUtils.java?rev=1158246&r1=1158245&r2=1158246&view=diff
==============================================================================
--- cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/transform/TransformUtils.java
(original)
+++ cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/transform/TransformUtils.java
Tue Aug 16 12:52:29 2011
@@ -49,12 +49,13 @@ public final class TransformUtils {
                                                                 Map<String, String>
outElementsMap,
                                                                 List<String> outDropElements,
                                                                 Map<String, String>
outAppendMap,
-                                                                boolean attributesToElements)
{
+                                                                boolean attributesToElements,
+                                                                String defaultNamespace)
{
         if (outElementsMap != null || outDropElements != null 
             || outAppendMap != null || attributesToElements) {
             writer = createNewWriterIfNeeded(writer, os);
             writer = new OutTransformWriter(writer, outElementsMap, outAppendMap,
-                                            outDropElements, attributesToElements);
+                                            outDropElements, attributesToElements, defaultNamespace);
         }
         return writer;
     }

Added: cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/transform/OutTransformWriterTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/transform/OutTransformWriterTest.java?rev=1158246&view=auto
==============================================================================
--- cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/transform/OutTransformWriterTest.java
(added)
+++ cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/transform/OutTransformWriterTest.java
Tue Aug 16 12:52:29 2011
@@ -0,0 +1,126 @@
+/**
+ * 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.cxf.staxutils.transform;
+
+import java.io.ByteArrayOutputStream;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.Marshaller;
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+import javax.xml.stream.XMLStreamWriter;
+
+import org.apache.cxf.staxutils.StaxUtils;
+import org.junit.Assert;
+import org.junit.Test;
+
+
+public class OutTransformWriterTest extends Assert {
+
+    @Test
+    public void testDefaultNamespace() throws Exception {
+        ByteArrayOutputStream os = new ByteArrayOutputStream();
+        XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(os, "UTF-8");
+        
+        Map<String, String> outMap = new HashMap<String, String>();
+        outMap.put("{http://testbeans.com}*", "{http://testbeans.com/v2}*");
+        OutTransformWriter transformWriter = new OutTransformWriter(writer,
+                                                                    outMap,
+                                                                    Collections.<String,
String>emptyMap(),
+                                                                    Collections.<String>emptyList(),
+                                                                    false,
+                                                                    "http://testbeans.com/v2");
+        JAXBContext context = JAXBContext.newInstance(TestBean.class);
+        Marshaller m = context.createMarshaller();
+        m.marshal(new TestBean(), transformWriter);
+        
+        String expected = "<?xml version='1.0' encoding='UTF-8'?>"
+            + "<testBean xmlns=\"http://testbeans.com/v2\"><bean/></testBean>";
+        assertEquals(expected, os.toString());
+    }
+    
+    @Test
+    public void testNamespaceConversion() throws Exception {
+        ByteArrayOutputStream os = new ByteArrayOutputStream();
+        XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(os, "UTF-8");
+        
+        Map<String, String> outMap = new HashMap<String, String>();
+        outMap.put("{http://testbeans.com}testBean", "{http://testbeans.com/v2}testBean");
+        outMap.put("{http://testbeans.com}bean", "{http://testbeans.com/v3}bean");
+        OutTransformWriter transformWriter = new OutTransformWriter(writer,
+                                                                    outMap,
+                                                                    Collections.<String,
String>emptyMap(),
+                                                                    Collections.<String>emptyList(),
+                                                                    false,
+                                                                    null);
+        JAXBContext context = JAXBContext.newInstance(TestBean.class);
+        Marshaller m = context.createMarshaller();
+        m.marshal(new TestBean(), transformWriter);
+        
+        String expected = "<?xml version='1.0' encoding='UTF-8'?>"
+            + "<ps1:testBean xmlns:ps1=\"http://testbeans.com/v2\""
+            + " xmlns:ps2=\"http://testbeans.com/v3\"><ps2:bean/></ps1:testBean>";
+        assertEquals(expected, os.toString());
+        
+    }
+    
+    @Test
+    public void testNamespaceConversionAndDefaultNS() throws Exception {
+        ByteArrayOutputStream os = new ByteArrayOutputStream();
+        XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(os, "UTF-8");
+        
+        Map<String, String> outMap = new HashMap<String, String>();
+        outMap.put("{http://testbeans.com}testBean", "{http://testbeans.com/v2}testBean");
+        outMap.put("{http://testbeans.com}bean", "{http://testbeans.com/v3}bean");
+        OutTransformWriter transformWriter = new OutTransformWriter(writer,
+                                                                    outMap,
+                                                                    Collections.<String,
String>emptyMap(),
+                                                                    Collections.<String>emptyList(),
+                                                                    false,
+                                                                    "http://testbeans.com/v2");
+        JAXBContext context = JAXBContext.newInstance(TestBean.class);
+        Marshaller m = context.createMarshaller();
+        m.marshal(new TestBean(), transformWriter);
+        
+        String expected = "<?xml version='1.0' encoding='UTF-8'?>"
+            + "<testBean xmlns=\"http://testbeans.com/v2\""
+            + " xmlns:ps2=\"http://testbeans.com/v3\"><ps2:bean/></testBean>";
+        assertEquals(expected, os.toString());
+    }
+ 
+    @XmlRootElement(name = "testBean", namespace = "http://testbeans.com")
+    public static class TestBean {
+        private TestBean2 bean = new TestBean2();
+
+        @XmlElement(name = "bean", namespace = "http://testbeans.com")
+        public void setBean(TestBean2 bean) {
+            this.bean = bean;
+        }
+
+        public TestBean2 getBean() {
+            return bean;
+        }
+    }
+    
+    private static class TestBean2 {
+    }
+}

Propchange: cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/transform/OutTransformWriterTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/transform/OutTransformWriterTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/transform/TransformOutInterceptor.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/transform/TransformOutInterceptor.java?rev=1158246&r1=1158245&r2=1158246&view=diff
==============================================================================
--- cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/transform/TransformOutInterceptor.java
(original)
+++ cxf/trunk/rt/core/src/main/java/org/apache/cxf/interceptor/transform/TransformOutInterceptor.java
Tue Aug 16 12:52:29 2011
@@ -50,6 +50,7 @@ public class TransformOutInterceptor ext
     private List<String> outDropElements;
     private boolean attributesToElements;
     private String contextPropertyName;
+    private String defaultNamespace;
     
     public TransformOutInterceptor() {
         this(Phase.PRE_STREAM);
@@ -106,7 +107,8 @@ public class TransformOutInterceptor ext
                                                       outElementsMap,
                                                       outDropElements,
                                                       outAppendMap,
-                                                      attributesToElements);
+                                                      attributesToElements,
+                                                      defaultNamespace);
     }
     
     public void setOutTransformElements(Map<String, String> outElements) {
@@ -132,5 +134,9 @@ public class TransformOutInterceptor ext
     public void setContextPropertyName(String propertyName) {
         contextPropertyName = propertyName;
     }
+
+    public void setDefaultNamespace(String defaultNamespace) {
+        this.defaultNamespace = defaultNamespace;
+    }
     
 }

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java?rev=1158246&r1=1158245&r2=1158246&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/AbstractJAXBProvider.java
Tue Aug 16 12:52:29 2011
@@ -589,7 +589,8 @@ public abstract class AbstractJAXBProvid
                                                       outElementsMap,
                                                       outDropElements,
                                                       outAppendMap,
-                                                      attributesToElements);
+                                                      attributesToElements,
+                                                      null);
     }
     
     protected XMLStreamReader createTransformReaderIfNeeded(XMLStreamReader reader, InputStream
is) {



Mime
View raw message