Author: davsclaus Date: Sat May 15 08:56:30 2010 New Revision: 944597 URL: http://svn.apache.org/viewvc?rev=944597&view=rev Log: CAMEL-2725: Fixed String to Source type converter.
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/DomConverter.java camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultManagementLifecycleStrategy.java camel/trunk/camel-core/src/test/java/org/apache/camel/converter/jaxp/DomConverterTest.java camel/trunk/camel-core/src/test/java/org/apache/camel/converter/jaxp/XmlConverterTest.java camel/trunk/camel-core/src/test/java/org/apache/camel/converter/stream/StreamCacheConverterTest.java camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ExceptionHandlerStreamCacheTest.java camel/trunk/components/camel-cache/src/main/java/org/apache/camel/processor/cache/CacheBasedXPathReplacer.java camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfPayload.java camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/util/CxfUtils.java camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/converter/MyCxfCustomerConverter.java Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/DomConverter.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/DomConverter.java?rev=944597&r1=944596&r2=944597&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/DomConverter.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/DomConverter.java Sat May 15 08:56:30 2010 @@ -23,6 +23,7 @@ import java.util.Iterator; import java.util.List; import javax.xml.transform.TransformerException; +import org.apache.camel.Exchange; import org.w3c.dom.Attr; import org.w3c.dom.Element; import org.w3c.dom.Node; @@ -46,7 +47,7 @@ public final class DomConverter { } @Converter - public String toString(NodeList nodeList) throws TransformerException { + public String toString(NodeList nodeList, Exchange exchange) throws TransformerException { // converting NodeList to String is more tricky // sometimes the NodeList is a Node which we can then leverage // the XML converter to turn into XML incl. tags @@ -57,7 +58,7 @@ public final class DomConverter { boolean found = false; if (nodeList instanceof Node) { Node node = (Node) nodeList; - String s = xml.toString(node); + String s = xml.toString(node, exchange); if (ObjectHelper.isNotEmpty(s)) { found = true; buffer.append(s); @@ -67,7 +68,7 @@ public final class DomConverter { int size = nodeList.getLength(); for (int i = 0; i < size; i++) { Node node = nodeList.item(i); - String s = xml.toString(node); + String s = xml.toString(node, exchange); if (ObjectHelper.isNotEmpty(s)) { found = true; buffer.append(s); @@ -112,13 +113,13 @@ public final class DomConverter { } @Converter - public InputStream toInputStream(NodeList nodeList) throws TransformerException { - return new ByteArrayInputStream(toByteArray(nodeList)); + public InputStream toInputStream(NodeList nodeList, Exchange exchange) throws TransformerException { + return new ByteArrayInputStream(toByteArray(nodeList, exchange)); } @Converter - public byte[] toByteArray(NodeList nodeList) throws TransformerException { - String data = toString(nodeList); + public byte[] toByteArray(NodeList nodeList, Exchange exchange) throws TransformerException { + String data = toString(nodeList, exchange); return data.getBytes(); } 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=944597&r1=944596&r2=944597&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 Sat May 15 08:56:30 2010 @@ -143,7 +143,7 @@ public class XmlConverter { * Converts the given byte[] to a Source */ @Converter - public BytesSource toSource(byte[] data) { + public BytesSource toBytesSource(byte[] data) { return new BytesSource(data); } @@ -152,15 +152,25 @@ public class XmlConverter { * Converts the given String to a Source */ @Converter - public StringSource toSource(String data) { + public StringSource toStringSource(String data) { return new StringSource(data); } /** * Converts the given Document to a Source + * @deprecated use toDOMSource instead */ @Converter + @Deprecated public DOMSource toSource(Document document) { + return toDOMSource(document); + } + + /** + * Converts the given Document to a Source + */ + @Converter + public DOMSource toDOMSource(Document document) { return new DOMSource(document); } @@ -173,6 +183,14 @@ public class XmlConverter { } /** + * Converts the given String to a Source + */ + @Converter + public Source toSource(String data) { + return new StringSource(data); + } + + /** * Converts the given input Source into text */ @Deprecated Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultManagementLifecycleStrategy.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultManagementLifecycleStrategy.java?rev=944597&r1=944596&r2=944597&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultManagementLifecycleStrategy.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultManagementLifecycleStrategy.java Sat May 15 08:56:30 2010 @@ -418,9 +418,6 @@ public class DefaultManagementLifecycleS } public void onRoutesRemove(Collection<Route> routes) { - // noop - keep the route in the mbean so its still there, it will still be unregistered - // when camel itself is shutting down - // the agent hasn't been started if (!initialized) { return; @@ -440,10 +437,8 @@ public class DefaultManagementLifecycleS try { getManagementStrategy().unmanageObject(mr); - } catch (JMException e) { - LOG.warn("Could not register Route MBean", e); } catch (Exception e) { - LOG.warn("Could not create Route MBean", e); + LOG.warn("Could not unregister Route MBean", e); } } } Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/converter/jaxp/DomConverterTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/jaxp/DomConverterTest.java?rev=944597&r1=944596&r2=944597&view=diff ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/converter/jaxp/DomConverterTest.java (original) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/converter/jaxp/DomConverterTest.java Sat May 15 08:56:30 2010 @@ -33,14 +33,14 @@ public class DomConverterTest extends Co public void testDomConverterToString() throws Exception { Document document = context.getTypeConverter().convertTo(Document.class, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><hello>world!</hello>"); - String s = new DomConverter().toString(document.getChildNodes()); + String s = new DomConverter().toString(document.getChildNodes(), null); assertEquals("<hello>world!</hello>", s); } public void testDomConverterToBytes() throws Exception { Document document = context.getTypeConverter().convertTo(Document.class, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><hello>world!</hello>"); - byte[] bytes = new DomConverter().toByteArray(document.getChildNodes()); + byte[] bytes = new DomConverter().toByteArray(document.getChildNodes(), null); assertTrue("Should be equal", ObjectHelper.equalByteArray("<hello>world!</hello>".getBytes(), bytes)); } @@ -69,14 +69,14 @@ public class DomConverterTest extends Co List sub = DomConverter.toList(nl); assertEquals(2, sub.size()); - assertEquals("<hello>Hello World</hello>", new DomConverter().toString((NodeList) sub.get(0))); - assertEquals("<bye>Bye Camel</bye>", new DomConverter().toString((NodeList) sub.get(1))); + assertEquals("<hello>Hello World</hello>", new DomConverter().toString((NodeList) sub.get(0), null)); + assertEquals("<bye>Bye Camel</bye>", new DomConverter().toString((NodeList) sub.get(1), null)); } public void testDomConverterToInputStream() throws Exception { Document document = context.getTypeConverter().convertTo(Document.class, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><hello>world!</hello>"); - InputStream is = new DomConverter().toInputStream(document.getChildNodes()); + InputStream is = new DomConverter().toInputStream(document.getChildNodes(), null); assertEquals("<hello>world!</hello>", context.getTypeConverter().convertTo(String.class, is)); } 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=944597&r1=944596&r2=944597&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 Sat May 15 08:56:30 2010 @@ -51,7 +51,7 @@ public class XmlConverterTest extends Co public void testToBytesSource() throws Exception { XmlConverter conv = new XmlConverter(); - BytesSource bs = conv.toSource("<foo>bar</foo>".getBytes()); + BytesSource bs = conv.toBytesSource("<foo>bar</foo>".getBytes()); assertNotNull(bs); assertEquals("<foo>bar</foo>", new String(bs.getData())); } @@ -67,16 +67,35 @@ public class XmlConverterTest extends Co public void testToStringWithBytesSource() throws Exception { XmlConverter conv = new XmlConverter(); - Source source = conv.toSource("<foo>bar</foo>".getBytes()); + Source source = conv.toBytesSource("<foo>bar</foo>".getBytes()); String out = conv.toString(source, null); assertEquals("<foo>bar</foo>", out); } + public void testToSource() throws Exception { + XmlConverter conv = new XmlConverter(); + + Source source = conv.toSource("<foo>bar</foo>"); + String out = conv.toString(source, null); + assertEquals("<foo>bar</foo>", out); + } + + public void testToSourceUsingTypeConverter() throws Exception { + Source source = context.getTypeConverter().convertTo(Source.class, "<foo>bar</foo>"); + String out = context.getTypeConverter().convertTo(String.class, source); + assertEquals("<foo>bar</foo>", out); + + // try again to ensure it works the 2nd time + source = context.getTypeConverter().convertTo(Source.class, "<foo>baz</foo>"); + out = context.getTypeConverter().convertTo(String.class, source); + assertEquals("<foo>baz</foo>", out); + } + public void testToByteArrayWithExchange() throws Exception { Exchange exchange = new DefaultExchange(context); XmlConverter conv = new XmlConverter(); - Source source = conv.toSource("<foo>bar</foo>".getBytes()); + Source source = conv.toBytesSource("<foo>bar</foo>".getBytes()); byte[] out = conv.toByteArray(source, exchange); assertEquals("<foo>bar</foo>", new String(out)); } @@ -84,7 +103,7 @@ public class XmlConverterTest extends Co public void testToByteArrayWithNoExchange() throws Exception { XmlConverter conv = new XmlConverter(); - Source source = conv.toSource("<foo>bar</foo>".getBytes()); + Source source = conv.toBytesSource("<foo>bar</foo>".getBytes()); byte[] out = conv.toByteArray(source, null); assertEquals("<foo>bar</foo>", new String(out)); } Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/converter/stream/StreamCacheConverterTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/stream/StreamCacheConverterTest.java?rev=944597&r1=944596&r2=944597&view=diff ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/converter/stream/StreamCacheConverterTest.java (original) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/converter/stream/StreamCacheConverterTest.java Sat May 15 08:56:30 2010 @@ -65,9 +65,9 @@ public class StreamCacheConverterTest ex StreamCache cache = converter.convertToStreamCache(source, exchange); //assert re-readability of the cached StreamSource XmlConverter converter = new XmlConverter(); - assertNotNull(converter.toString((Source)cache)); + assertNotNull(converter.toString((Source)cache, null)); cache.reset(); - assertNotNull(converter.toString((Source)cache)); + assertNotNull(converter.toString((Source)cache, null)); } public void testConvertToStreamCacheInputStream() throws Exception { Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ExceptionHandlerStreamCacheTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ExceptionHandlerStreamCacheTest.java?rev=944597&r1=944596&r2=944597&view=diff ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ExceptionHandlerStreamCacheTest.java (original) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/ExceptionHandlerStreamCacheTest.java Sat May 15 08:56:30 2010 @@ -81,7 +81,7 @@ public class ExceptionHandlerStreamCache exceptionEndpoint.assertIsSatisfied(); StreamSource body = (StreamSource) exceptionEndpoint.getExchanges().get(0).getIn().getBody(); - assertEquals("Ensure message re-readability in the exception handler", xml, new XmlConverter().toString(body)); + assertEquals("Ensure message re-readability in the exception handler", xml, new XmlConverter().toString(body, null)); } @Override Modified: camel/trunk/components/camel-cache/src/main/java/org/apache/camel/processor/cache/CacheBasedXPathReplacer.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cache/src/main/java/org/apache/camel/processor/cache/CacheBasedXPathReplacer.java?rev=944597&r1=944596&r2=944597&view=diff ============================================================================== --- camel/trunk/components/camel-cache/src/main/java/org/apache/camel/processor/cache/CacheBasedXPathReplacer.java (original) +++ camel/trunk/components/camel-cache/src/main/java/org/apache/camel/processor/cache/CacheBasedXPathReplacer.java Sat May 15 08:56:30 2010 @@ -96,7 +96,7 @@ public class CacheBasedXPathReplacer ext Source xslSource = xmlConverter.toStreamSource(new StringReader(xslString)); TransformerFactory transformerFactory = xmlConverter.createTransformerFactory(); Transformer transformer = transformerFactory.newTransformer(xslSource); - source = xmlConverter.toSource(document); + source = xmlConverter.toDOMSource(document); result = new DOMResult(); transformer.setParameter("cacheValue", cacheValueDocument); Modified: camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfPayload.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfPayload.java?rev=944597&r1=944596&r2=944597&view=diff ============================================================================== --- camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfPayload.java (original) +++ camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/CxfPayload.java Sat May 15 08:56:30 2010 @@ -61,7 +61,7 @@ public class CxfPayload<T> { for (Element element : body) { String elementString = ""; try { - elementString = converter.toString(element); + elementString = converter.toString(element, null); } catch (TransformerException e) { elementString = element.toString(); } Modified: camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/util/CxfUtils.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/util/CxfUtils.java?rev=944597&r1=944596&r2=944597&view=diff ============================================================================== --- camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/util/CxfUtils.java (original) +++ camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/util/CxfUtils.java Sat May 15 08:56:30 2010 @@ -56,8 +56,7 @@ public final class CxfUtils { W3CDOMStreamWriter writer = new W3CDOMStreamWriter(); writeElement(element, writer, namespaces); XmlConverter converter = new XmlConverter(); - return converter.toString(converter.toSource(writer.getDocument())); - + return converter.toString(converter.toSource(writer.getDocument()), null); } private static void writeElement(Element e, Modified: camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/converter/MyCxfCustomerConverter.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/converter/MyCxfCustomerConverter.java?rev=944597&r1=944596&r2=944597&view=diff ============================================================================== --- camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/converter/MyCxfCustomerConverter.java (original) +++ camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/converter/MyCxfCustomerConverter.java Sat May 15 08:56:30 2010 @@ -39,7 +39,7 @@ public final class MyCxfCustomerConverte for (Object element : payload.getBody()) { String elementString = ""; try { - elementString = converter.toString((Element)element); + elementString = converter.toString((Element)element, null); } catch (TransformerException e) { elementString = element.toString(); }