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 22:31:17 UTC
svn commit: r1185358 - in /camel/branches/camel-2.8.x: ./
camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java
camel-core/src/test/java/org/apache/camel/converter/jaxp/XmlConverterTest.java
Author: dkulp
Date: Mon Oct 17 20:31:17 2011
New Revision: 1185358
URL: http://svn.apache.org/viewvc?rev=1185358&view=rev
Log:
Merged revisions 1185336 via svnmerge from
https://svn.apache.org/repos/asf/camel/trunk
........
r1185336 | dkulp | 2011-10-17 15:17:47 -0400 (Mon, 17 Oct 2011) | 1 line
[CAMEL-4554] Add converters for StAXSource
........
Modified:
camel/branches/camel-2.8.x/ (props changed)
camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java
camel/branches/camel-2.8.x/camel-core/src/test/java/org/apache/camel/converter/jaxp/XmlConverterTest.java
Propchange: camel/branches/camel-2.8.x/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.
Modified: camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java?rev=1185358&r1=1185357&r2=1185358&view=diff
==============================================================================
--- camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java (original)
+++ camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java Mon Oct 17 20:31:17 2011
@@ -18,6 +18,8 @@ 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;
@@ -31,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;
@@ -41,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;
@@ -270,6 +275,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;
}
@@ -323,7 +330,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).
@@ -344,6 +363,41 @@ 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).
+ */
+ @Converter
+ public SAXSource toSAXSource(File file, Exchange exchange) throws IOException, SAXException, TransformerException {
+ FileInputStream fis = new FileInputStream(file);
+ return toSAXSource(fis, 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 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).
@@ -367,6 +421,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;
}
@@ -388,6 +444,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;
}
@@ -471,6 +529,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) {
@@ -513,6 +576,12 @@ public class XmlConverter {
}
@Converter
+ public DOMSource toDOMSource(File file) throws ParserConfigurationException, IOException, SAXException {
+ FileInputStream fis = new FileInputStream(file);
+ return toDOMSource(fis);
+ }
+
+ @Converter
public DOMSource toDOMSourceFromStream(StreamSource source) throws ParserConfigurationException, IOException, SAXException {
Document document;
String systemId = source.getSystemId();
@@ -560,9 +629,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 {
@@ -570,6 +650,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/branches/camel-2.8.x/camel-core/src/test/java/org/apache/camel/converter/jaxp/XmlConverterTest.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/camel-core/src/test/java/org/apache/camel/converter/jaxp/XmlConverterTest.java?rev=1185358&r1=1185357&r2=1185358&view=diff
==============================================================================
--- camel/branches/camel-2.8.x/camel-core/src/test/java/org/apache/camel/converter/jaxp/XmlConverterTest.java (original)
+++ camel/branches/camel-2.8.x/camel-core/src/test/java/org/apache/camel/converter/jaxp/XmlConverterTest.java Mon Oct 17 20:31:17 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;
@@ -127,7 +128,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);
@@ -136,6 +137,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();
@@ -162,6 +172,38 @@ 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();
+
+ deleteDirectory("target/xml");
+ template.sendBodyAndHeader("file:target/xml", "<foo>bar</foo>", Exchange.FILE_NAME, "myxml.xml");
+ File file = new File("target/xml/myxml.xml");
+
+ SAXSource out = conv.toSAXSource(file, null);
+ 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();
@@ -173,7 +215,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);
@@ -225,7 +267,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);
@@ -234,6 +276,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();
@@ -331,6 +382,18 @@ public class XmlConverterTest extends Co
assertEquals("<foo>bar</foo>", context.getTypeConverter().convertTo(String.class, out));
}
+ public void testToDomSourceFromFile() 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");
+
+ DOMSource out = conv.toDOMSource(file);
+ assertNotNull(out);
+ assertEquals("<foo>bar</foo>", context.getTypeConverter().convertTo(String.class, out));
+ }
+
public void testToDomElement() throws Exception {
XmlConverter conv = new XmlConverter();
SAXSource source = conv.toSAXSource("<foo>bar</foo>", null);