servicemix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject svn commit: r413968 - /incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/xslt/XsltComponent.java
Date Tue, 13 Jun 2006 19:53:31 GMT
Author: gnodet
Date: Tue Jun 13 12:53:30 2006
New Revision: 413968

URL: http://svn.apache.org/viewvc?rev=413968&view=rev
Log:
Fix problem with xslt component when receiving a DOMSource with an element instead of a Document

Modified:
    incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/xslt/XsltComponent.java

Modified: incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/xslt/XsltComponent.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/xslt/XsltComponent.java?rev=413968&r1=413967&r2=413968&view=diff
==============================================================================
--- incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/xslt/XsltComponent.java
(original)
+++ incubator/servicemix/trunk/servicemix-components/src/main/java/org/apache/servicemix/components/xslt/XsltComponent.java
Tue Jun 13 12:53:30 2006
@@ -20,10 +20,14 @@
 import org.apache.servicemix.jbi.jaxp.BytesSource;
 import org.apache.servicemix.jbi.jaxp.StringSource;
 import org.springframework.core.io.Resource;
+import org.w3c.dom.Document;
+import org.w3c.dom.Node;
 
 import javax.jbi.messaging.MessageExchange;
 import javax.jbi.messaging.MessagingException;
 import javax.jbi.messaging.NormalizedMessage;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.transform.Result;
 import javax.xml.transform.Source;
 import javax.xml.transform.Templates;
@@ -31,6 +35,7 @@
 import javax.xml.transform.TransformerConfigurationException;
 import javax.xml.transform.TransformerException;
 import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
 import javax.xml.transform.stream.StreamResult;
 import javax.xml.transform.stream.StreamSource;
 
@@ -55,6 +60,21 @@
     private Templates templates;
     private boolean disableOutput;
     private boolean useStringBuffer = true;
+    private boolean forceDocIfDom = true;
+
+    /**
+     * @return the forceDocIfDom
+     */
+    public boolean isForceDocIfDom() {
+        return forceDocIfDom;
+    }
+
+    /**
+     * @param forceDocIfDom the forceDocIfDom to set
+     */
+    public void setForceDocIfDom(boolean forceDocIfDom) {
+        this.forceDocIfDom = forceDocIfDom;
+    }
 
     // Properties
     // -------------------------------------------------------------------------
@@ -117,6 +137,10 @@
             transformContent(transformer, exchange, in, out);
             return shouldOutputResult(transformer);
         }
+        catch (ParserConfigurationException e) {
+            e.printStackTrace();
+            throw new MessagingException("Failed to transform: " + e, e);
+        }
         catch (TransformerException e) {
             e.printStackTrace();
             throw new MessagingException("Failed to transform: " + e, e);
@@ -126,17 +150,26 @@
         }
     }
 
-    protected void transformContent(Transformer transformer, MessageExchange exchange, NormalizedMessage
in, NormalizedMessage out) throws TransformerException, MessagingException {
+    protected void transformContent(Transformer transformer, MessageExchange exchange, NormalizedMessage
in, NormalizedMessage out) throws TransformerException, MessagingException, ParserConfigurationException
{
+        Source src = in.getContent();
+        if (forceDocIfDom && src instanceof DOMSource) {
+            Node n = ((DOMSource) src).getNode();
+            if (n instanceof Document == false) {
+                Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
+                doc.appendChild(doc.importNode(n, true));
+                src = new DOMSource(doc);
+            }
+        }
         if (isUseStringBuffer()) {
             StringWriter buffer = new StringWriter();
             Result result = new StreamResult(buffer);
-            transformer.transform(in.getContent(), result);
+            transformer.transform(src, result);
             out.setContent(new StringSource(buffer.toString()));
         }
         else {
             ByteArrayOutputStream buffer = new ByteArrayOutputStream();
             Result result = new StreamResult(buffer);
-            transformer.transform(in.getContent(), result);
+            transformer.transform(src, result);
             out.setContent(new BytesSource(buffer.toByteArray()));
         }
     }



Mime
View raw message