You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by dk...@apache.org on 2011/10/17 21:17:48 UTC

svn commit: r1185336 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/converter/jaxp/XmlConverter.java test/java/org/apache/camel/converter/jaxp/XmlConverterTest.java

Author: dkulp
Date: Mon Oct 17 19:17:47 2011
New Revision: 1185336

URL: http://svn.apache.org/viewvc?rev=1185336&view=rev
Log:
[CAMEL-4554] Add converters for StAXSource

Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/converter/jaxp/XmlConverterTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java?rev=1185336&r1=1185335&r2=1185336&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java Mon Oct 17 19:17:47 2011
@@ -19,6 +19,7 @@ package org.apache.camel.converter.jaxp;
 import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
@@ -32,6 +33,8 @@ import java.util.Properties;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
 import javax.xml.transform.OutputKeys;
 import javax.xml.transform.Result;
 import javax.xml.transform.Source;
@@ -42,6 +45,7 @@ import javax.xml.transform.TransformerFa
 import javax.xml.transform.dom.DOMResult;
 import javax.xml.transform.dom.DOMSource;
 import javax.xml.transform.sax.SAXSource;
+import javax.xml.transform.stax.StAXSource;
 import javax.xml.transform.stream.StreamResult;
 import javax.xml.transform.stream.StreamSource;
 
@@ -273,6 +277,8 @@ public class XmlConverter {
             return toDOMSourceFromSAX((SAXSource) source);
         } else if (source instanceof StreamSource) {
             return toDOMSourceFromStream((StreamSource) source);
+        } else if (source instanceof StAXSource) {
+            return toDOMSourceFromStAX((StAXSource)source);
         } else {
             return null;
         }
@@ -326,7 +332,19 @@ public class XmlConverter {
     public SAXSource toSAXSource(String source, Exchange exchange) throws IOException, SAXException, TransformerException {
         return toSAXSource(toSource(source), exchange);
     }
-   
+
+    
+    /**
+     * Converts the source instance to a {@link StAXSource} or returns null if the conversion is not
+     * supported (making it easy to derive from this class to add new kinds of conversion).
+     * @throws XMLStreamException 
+     */
+    @Converter
+    public StAXSource toStAXSource(String source, Exchange exchange) throws XMLStreamException {
+        XMLStreamReader r = new StaxConverter().createXMLStreamReader(new StringReader(source));
+        return new StAXSource(r);
+    }    
+    
     /**
      * Converts the source instance to a {@link SAXSource} or returns null if the conversion is not
      * supported (making it easy to derive from this class to add new kinds of conversion).
@@ -347,6 +365,18 @@ public class XmlConverter {
         return toSAXSource(toStreamSource(source), exchange);
     }
     
+    
+    /**
+     * Converts the source instance to a {@link StAXSource} or returns null if the conversion is not
+     * supported (making it easy to derive from this class to add new kinds of conversion).
+     * @throws XMLStreamException 
+     */
+    @Converter
+    public StAXSource toStAXSource(InputStream source, Exchange exchange) throws XMLStreamException {
+        XMLStreamReader r = new StaxConverter().createXMLStreamReader(source, exchange);
+        return new StAXSource(r);
+    }
+    
     /**
      * Converts the source instance to a {@link SAXSource} or returns null if the conversion is not
      * supported (making it easy to derive from this class to add new kinds of conversion).
@@ -358,6 +388,19 @@ public class XmlConverter {
     }
 
     /**
+     * Converts the source instance to a {@link StAXSource} or returns null if the conversion is not
+     * supported (making it easy to derive from this class to add new kinds of conversion).
+     * @throws FileNotFoundException 
+     * @throws XMLStreamException 
+     */
+    @Converter
+    public StAXSource toStAXSource(File file, Exchange exchange) throws FileNotFoundException, XMLStreamException {
+        FileInputStream fis = new FileInputStream(file);
+        XMLStreamReader r = new StaxConverter().createXMLStreamReader(fis, exchange);
+        return new StAXSource(r);
+    }
+
+    /**
      * Converts the source instance to a {@link SAXSource} or returns null if the conversion is not
      * supported (making it easy to derive from this class to add new kinds of conversion).
      *
@@ -380,6 +423,8 @@ public class XmlConverter {
             return toSAXSourceFromDOM((DOMSource) source, exchange);
         } else if (source instanceof StreamSource) {
             return toSAXSourceFromStream((StreamSource) source);
+        } else if (source instanceof StAXSource) {
+            return toSAXSourceFromStAX((StAXSource) source, exchange);
         } else {
             return null;
         }
@@ -401,6 +446,8 @@ public class XmlConverter {
             return toStreamSourceFromDOM((DOMSource) source, exchange);
         } else if (source instanceof SAXSource) {
             return toStreamSourceFromSAX((SAXSource) source, exchange);
+        } else if (source instanceof StAXSource) {
+            return toStreamSourceFromStAX((StAXSource) source, exchange);
         } else {
             return null;
         }
@@ -484,6 +531,11 @@ public class XmlConverter {
         String result = toString(source, exchange);
         return new StringSource(result);
     }
+    @Converter
+    public StreamSource toStreamSourceFromStAX(StAXSource source, Exchange exchange) throws TransformerException {
+        String result = toString(source, exchange);
+        return new StringSource(result);
+    }
 
     @Converter
     public SAXSource toSAXSourceFromStream(StreamSource source) {
@@ -579,9 +631,20 @@ public class XmlConverter {
     }
 
     @Converter
+    public SAXSource toSAXSourceFromStAX(StAXSource source, Exchange exchange) throws TransformerException {
+        String str = toString(source, exchange);
+        StringReader reader = new StringReader(str);
+        return new SAXSource(new InputSource(reader));
+    }
+
+    @Converter
     public DOMSource toDOMSourceFromSAX(SAXSource source) throws IOException, SAXException, ParserConfigurationException, TransformerException {
         return new DOMSource(toDOMNodeFromSAX(source));
     }
+    @Converter
+    public DOMSource toDOMSourceFromStAX(StAXSource source) throws IOException, SAXException, ParserConfigurationException, TransformerException {
+        return new DOMSource(toDOMNodeFromStAX(source));
+    }
 
     @Converter
     public Node toDOMNodeFromSAX(SAXSource source) throws ParserConfigurationException, IOException, SAXException, TransformerException {
@@ -589,6 +652,12 @@ public class XmlConverter {
         toResult(source, result);
         return result.getNode();
     }
+    @Converter
+    public Node toDOMNodeFromStAX(StAXSource source) throws ParserConfigurationException, IOException, SAXException, TransformerException {
+        DOMResult result = new DOMResult();
+        toResult(source, result);
+        return result.getNode();
+    }
 
     /**
      * Converts the given TRaX Source into a W3C DOM node

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/converter/jaxp/XmlConverterTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/jaxp/XmlConverterTest.java?rev=1185336&r1=1185335&r2=1185336&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/converter/jaxp/XmlConverterTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/converter/jaxp/XmlConverterTest.java Mon Oct 17 19:17:47 2011
@@ -26,6 +26,7 @@ import javax.xml.transform.Source;
 import javax.xml.transform.TransformerException;
 import javax.xml.transform.dom.DOMSource;
 import javax.xml.transform.sax.SAXSource;
+import javax.xml.transform.stax.StAXSource;
 import javax.xml.transform.stream.StreamSource;
 
 import org.w3c.dom.Document;
@@ -128,7 +129,7 @@ public class XmlConverterTest extends Co
         assertEquals(new String(bytes), new String(out));
     }
 
-    public void testToDomSourceByStaxSource() throws Exception {
+    public void testToDomSourceBySaxSource() throws Exception {
         XmlConverter conv = new XmlConverter();
 
         SAXSource source = conv.toSAXSource("<foo>bar</foo>", null);
@@ -137,6 +138,15 @@ public class XmlConverterTest extends Co
 
         assertEquals("<foo>bar</foo>", conv.toString(out, null));
     }
+    public void testToDomSourceByStAXSource() throws Exception {
+        XmlConverter conv = new XmlConverter();
+
+        StAXSource source = conv.toStAXSource("<foo>bar</foo>", null);
+        DOMSource out = conv.toDOMSource(source);
+        assertNotSame(source, out);
+
+        assertEquals("<foo>bar</foo>", conv.toString(out, null));
+    }
 
     public void testToDomSourceByCustomSource() throws Exception {
         XmlConverter conv = new XmlConverter();
@@ -163,6 +173,15 @@ public class XmlConverterTest extends Co
         assertNotNull(out);
         assertEquals("<foo>bar</foo>", conv.toString(out, null));
     }
+    public void testToStAXSourceByInputStream() throws Exception {
+        XmlConverter conv = new XmlConverter();
+
+        InputStream is = context.getTypeConverter().convertTo(InputStream.class, "<foo>bar</foo>");
+        StAXSource out = conv.toStAXSource(is, null);
+
+        assertNotNull(out);
+        assertEquals("<foo>bar</foo>", conv.toString(out, null));
+    }
 
     public void testToSaxSourceFromFile() throws Exception {
         XmlConverter conv = new XmlConverter();
@@ -175,6 +194,17 @@ public class XmlConverterTest extends Co
         assertNotNull(out);
         assertEquals("<foo>bar</foo>", context.getTypeConverter().convertTo(String.class, out));
     }
+    public void testToStAXSourceFromFile() throws Exception {
+        XmlConverter conv = new XmlConverter();
+
+        deleteDirectory("target/xml");
+        template.sendBodyAndHeader("file:target/xml", "<foo>bar</foo>", Exchange.FILE_NAME, "myxml.xml");
+        File file = new File("target/xml/myxml.xml");
+
+        StAXSource out = conv.toStAXSource(file, null);
+        assertNotNull(out);
+        assertEquals("<foo>bar</foo>", context.getTypeConverter().convertTo(String.class, out));
+    }
 
     public void testToSaxSourceByDomSource() throws Exception {
         XmlConverter conv = new XmlConverter();
@@ -186,7 +216,7 @@ public class XmlConverterTest extends Co
         assertEquals("<foo>bar</foo>", conv.toString(out, null));
     }
 
-    public void testToSaxSourceByStaxSource() throws Exception {
+    public void testToSaxSourceBySaxSource() throws Exception {
         XmlConverter conv = new XmlConverter();
 
         SAXSource source = conv.toSAXSource("<foo>bar</foo>", null);
@@ -238,7 +268,7 @@ public class XmlConverterTest extends Co
         assertEquals("<foo>bar</foo>", conv.toString(out, null));
     }
 
-    public void testToStreamSourceByStaxSource() throws Exception {
+    public void testToStreamSourceBySaxSource() throws Exception {
         XmlConverter conv = new XmlConverter();
 
         SAXSource source = conv.toSAXSource("<foo>bar</foo>", null);
@@ -247,6 +277,15 @@ public class XmlConverterTest extends Co
 
         assertEquals("<foo>bar</foo>", conv.toString(out, null));
     }
+    public void testToStreamSourceByStAXSource() throws Exception {
+        XmlConverter conv = new XmlConverter();
+
+        StAXSource source = conv.toStAXSource("<foo>bar</foo>", null);
+        StreamSource out = conv.toStreamSource(source, null);
+        assertNotSame(source, out);
+
+        assertEquals("<foo>bar</foo>", conv.toString(out, null));
+    }
 
     public void testToStreamSourceByCustomSource() throws Exception {
         XmlConverter conv = new XmlConverter();