cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1087243 - in /cxf/trunk/rt/frontend/jaxrs/src: main/java/org/apache/cxf/jaxrs/ext/xml/XMLSource.java test/java/org/apache/cxf/jaxrs/ext/xml/XMLSourceTest.java
Date Thu, 31 Mar 2011 10:38:57 GMT
Author: sergeyb
Date: Thu Mar 31 10:38:57 2011
New Revision: 1087243

URL: http://svn.apache.org/viewvc?rev=1087243&view=rev
Log:
[JAX-RS] Minor improvements to XMLSource

Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/xml/XMLSource.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/ext/xml/XMLSourceTest.java

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/xml/XMLSource.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/xml/XMLSource.java?rev=1087243&r1=1087242&r2=1087243&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/xml/XMLSource.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/xml/XMLSource.java
Thu Mar 31 10:38:57 2011
@@ -120,7 +120,7 @@ public class XMLSource {
         if (cls.isPrimitive() || cls == String.class) {
             return readPrimitiveValue(node, cls);    
         } else {
-            return readFromSource(new DOMSource(node), cls);
+            return readNode(node, cls);
         }
     }
     
@@ -162,7 +162,7 @@ public class XMLSource {
             if (InjectionUtils.isPrimitive(cls)) {
                 values[i] = readPrimitiveValue(node, cls);
             } else {
-                values[i] = readFromSource(new DOMSource(node), cls);
+                values[i] = readNode(node, cls);
             }
         }
         return values;
@@ -344,7 +344,17 @@ public class XMLSource {
     }
     
     
-    private <T> T readFromSource(Source s, Class<T> cls) {
+    private <T> T readNode(Node node, Class<T> cls) {
+        
+        if (Node.class.isAssignableFrom(cls)) {
+            return cls.cast(node);
+        }
+        
+        DOMSource s = new DOMSource(node);
+        if (Source.class == cls || DOMSource.class == cls) {
+            return cls.cast(s);
+        }
+        
         try {
             
             JAXBElementProvider provider = new JAXBElementProvider();

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/ext/xml/XMLSourceTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/ext/xml/XMLSourceTest.java?rev=1087243&r1=1087242&r2=1087243&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/ext/xml/XMLSourceTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/ext/xml/XMLSourceTest.java
Thu Mar 31 10:38:57 2011
@@ -29,6 +29,10 @@ import java.util.Map;
 
 import javax.xml.bind.annotation.XmlAttribute;
 import javax.xml.bind.annotation.XmlRootElement;
+import javax.xml.transform.Source;
+
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
 
 import org.apache.cxf.common.xmlschema.XmlSchemaConstants;
 
@@ -57,6 +61,15 @@ public class XMLSourceTest extends Asser
     }
     
     @Test
+    public void testAttributeValueAsNode() {
+        InputStream is = new ByteArrayInputStream("<foo><bar attr=\"baz\">barValue</bar></foo>".getBytes());
+        XMLSource xp = new XMLSource(is);
+        Node node = xp.getNode("/foo/bar/@attr", Node.class);
+        assertNotNull(node);
+        assertEquals("baz", node.getTextContent());
+    }
+    
+    @Test
     public void testNodeTextValue() {
         InputStream is = new ByteArrayInputStream("<foo><bar attr=\"baz\">barValue</bar></foo>".getBytes());
         XMLSource xp = new XMLSource(is);
@@ -104,6 +117,29 @@ public class XMLSourceTest extends Asser
     }
     
     @Test
+    public void testGetNodeAsElement() {
+        InputStream is = new ByteArrayInputStream("<foo><bar/></foo>".getBytes());
+        XMLSource xp = new XMLSource(is);
+        Element element = xp.getNode("/foo/bar", Element.class);
+        assertNotNull(element);
+    }
+    
+    @Test
+    public void testGetNodeAsSource() {
+        InputStream is = new ByteArrayInputStream("<foo><bar/></foo>".getBytes());
+        XMLSource xp = new XMLSource(is);
+        Source element = xp.getNode("/foo/bar", Source.class);
+        assertNotNull(element);
+    }
+    
+    @Test
+    public void testGetNodeNull() {
+        InputStream is = new ByteArrayInputStream("<foo><bar/></foo>".getBytes());
+        XMLSource xp = new XMLSource(is);
+        assertNull(xp.getNode("/foo/bar1", Element.class));
+    }
+    
+    @Test
     public void testGetNodeAsJaxbElement() {
         InputStream is = new ByteArrayInputStream("<foo><bar name=\"foo\"/></foo>".getBytes());
         XMLSource xp = new XMLSource(is);



Mime
View raw message