servicemix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jbono...@apache.org
Subject svn commit: r1144368 - in /servicemix/utils/trunk/src: main/java/org/apache/servicemix/jbi/helper/ main/java/org/apache/servicemix/jbi/jaxp/ test/java/org/apache/servicemix/jbi/jaxp/
Date Fri, 08 Jul 2011 15:55:04 GMT
Author: jbonofre
Date: Fri Jul  8 15:55:04 2011
New Revision: 1144368

URL: http://svn.apache.org/viewvc?rev=1144368&view=rev
Log:
[SM-1831] Gathering all methods in SourceTransformer in place of the StaxSourceTransformer.

Removed:
    servicemix/utils/trunk/src/main/java/org/apache/servicemix/jbi/jaxp/StAXSourceTransformer.java
Modified:
    servicemix/utils/trunk/src/main/java/org/apache/servicemix/jbi/helper/MessageUtil.java
    servicemix/utils/trunk/src/main/java/org/apache/servicemix/jbi/jaxp/SourceTransformer.java
    servicemix/utils/trunk/src/test/java/org/apache/servicemix/jbi/jaxp/StaxSourceTest.java
    servicemix/utils/trunk/src/test/java/org/apache/servicemix/jbi/jaxp/WsRfRpTest.java

Modified: servicemix/utils/trunk/src/main/java/org/apache/servicemix/jbi/helper/MessageUtil.java
URL: http://svn.apache.org/viewvc/servicemix/utils/trunk/src/main/java/org/apache/servicemix/jbi/helper/MessageUtil.java?rev=1144368&r1=1144367&r2=1144368&view=diff
==============================================================================
--- servicemix/utils/trunk/src/main/java/org/apache/servicemix/jbi/helper/MessageUtil.java
(original)
+++ servicemix/utils/trunk/src/main/java/org/apache/servicemix/jbi/helper/MessageUtil.java
Fri Jul  8 15:55:04 2011
@@ -37,7 +37,6 @@ import javax.xml.transform.TransformerEx
 import javax.xml.transform.sax.SAXSource;
 import javax.xml.transform.stream.StreamSource;
 
-import org.apache.servicemix.jbi.jaxp.StAXSourceTransformer;
 import org.xml.sax.SAXException;
 
 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
@@ -143,7 +142,7 @@ public final class MessageUtil {
         if (message.getContent() instanceof StreamSource
                 || message.getContent() instanceof SAXSource) {
             try {
-                String content = new StAXSourceTransformer().contentToString(message);
+                String content = new SourceTransformer().contentToString(message);
                 if (content != null) {
                     message.setContent(new StringSource(content));
                 }

Modified: servicemix/utils/trunk/src/main/java/org/apache/servicemix/jbi/jaxp/SourceTransformer.java
URL: http://svn.apache.org/viewvc/servicemix/utils/trunk/src/main/java/org/apache/servicemix/jbi/jaxp/SourceTransformer.java?rev=1144368&r1=1144367&r2=1144368&view=diff
==============================================================================
--- servicemix/utils/trunk/src/main/java/org/apache/servicemix/jbi/jaxp/SourceTransformer.java
(original)
+++ servicemix/utils/trunk/src/main/java/org/apache/servicemix/jbi/jaxp/SourceTransformer.java
Fri Jul  8 15:55:04 2011
@@ -27,9 +27,14 @@ import java.lang.reflect.Constructor;
 
 import javax.jbi.messaging.MessagingException;
 import javax.jbi.messaging.NormalizedMessage;
+import javax.xml.crypto.dsig.Transform;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLOutputFactory;
+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;
@@ -43,6 +48,7 @@ import javax.xml.transform.sax.SAXSource
 import javax.xml.transform.stream.StreamResult;
 import javax.xml.transform.stream.StreamSource;
 
+import org.jaxen.function.ext.EndsWithFunction;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
@@ -62,6 +68,8 @@ public class SourceTransformer {
     public static final String DEFAULT_VALIDATING_DTD_PROPERTY = "org.apache.servicemix.default.validating-dtd";
     private static ThreadLocal<WeakReference<DocumentBuilder>> docBuilderCache
= new ThreadLocal<WeakReference<DocumentBuilder>>();
     private static ThreadLocal<WeakReference<TransformerFactory>> transformerFactoryCache
= new ThreadLocal<WeakReference<TransformerFactory>>();
+    private XMLInputFactory inputFactory;
+    private XMLOutputFactory outputFactory;
 
     /*
      * When converting a DOM tree to a SAXSource, we try to use Xalan internal
@@ -185,6 +193,8 @@ public class SourceTransformer {
             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;
         }
@@ -196,6 +206,13 @@ public class SourceTransformer {
         return new DOMSource(node);
     }
 
+    public Source toDOMSource(StaxSource source) throws ParserConfigurationException, IOException,
SAXException, TransformerException {
+        Transformer transformer = createTransfomer();
+        DOMResult result = new DOMResult();
+        transformer.transform(source, result);
+        return new DOMSource(result.getNode(), result.getSystemId());
+    }
+
     /**
      * 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
@@ -208,6 +225,8 @@ public class SourceTransformer {
             return toSAXSourceFromDOM((DOMSource) source);
         } else if (source instanceof StreamSource) {
             return toSAXSourceFromStream((StreamSource) source);
+        } else if (source instanceof StaxSource) {
+            return toSAXSourceFromStax((StaxSource) source);
         } else {
             return null;
         }
@@ -256,6 +275,10 @@ public class SourceTransformer {
         return new SAXSource(inputSource);
     }
 
+    public SAXSource toSAXSourceFromStax(StaxSource source) {
+        return (SAXSource) source;
+    }
+
     public Reader toReaderFromSource(Source src) throws TransformerException {
         StreamSource stSrc = toStreamSource(src);
         Reader r = stSrc.getReader();
@@ -285,6 +308,13 @@ public class SourceTransformer {
         return new DOMSource(document, systemId);
     }
 
+    public DOMSource toDOMSourceFromStax(StaxSource source) throws TransformerException {
+        Transformer transformer = createTransfomer();
+        DOMResult result = new DOMResult();
+        transformer.transform(source, result);
+        return new DOMSource(result.getNode(), result.getSystemId());
+    }
+
     public SAXSource toSAXSourceFromDOM(DOMSource source) throws TransformerException {
         if (DOM_2_SAX_CLASS != null) {
             try {
@@ -461,6 +491,46 @@ public class SourceTransformer {
         }
     }
 
+    /**
+     * Converts the source instance to a StaxSource or returns null if the
+     * conversion is not supported (making it easy to derive from this class
+     * to add new kinds of conversion).
+     *
+     * @param source the source
+     * @return the converted StaxSource
+     * @throws XMLStreamException
+     */
+    public StaxSource toStaxSource(Source source) throws XMLStreamException {
+        if (source instanceof StaxSource) {
+            return (StaxSource) source;
+        } else {
+            XMLInputFactory factory = getInputFactory();
+            XMLStreamReader reader = factory.createXMLStreamReader(source);
+            return new StaxSource(reader);
+        }
+    }
+
+    public XMLStreamReader toXMLStreamReader(Source source) throws XMLStreamException, TransformerException
{
+        if (source instanceof StaxSource) {
+            return ((StaxSource) source).getXMLStreamReader();
+        }
+        // It seems that woodstox 2.9.3 throws some NPE in the servicemix-soap
+        // when using DOM, so use our own dom / stax parser
+        if (source instanceof DOMSource) {
+            Node n = ((DOMSource) source).getNode();
+            Element el = n instanceof Document ? ((Document) n).getDocumentElement() : n
instanceof Element ? (Element) n : null;
+            if (el != null) {
+                return new W3CDOMStreamReader(el);
+            }
+        }
+        XMLInputFactory factory = getInputFactory();
+        try {
+            return factory.createXMLStreamReader(source);
+        } catch (XMLStreamException e) {
+            return factory.createXMLStreamReader(toReaderFromSource(source));
+        }
+    }
+
     // Properties
     // -------------------------------------------------------------------------
     public DocumentBuilderFactory getDocumentBuilderFactory() {
@@ -534,5 +604,34 @@ public class SourceTransformer {
     	transformerFactoryCache.set(new WeakReference<TransformerFactory>(transformerFactory));
     }
 
-    
+    public XMLInputFactory getInputFactory() {
+        if (inputFactory == null) {
+            inputFactory = createInputFactory();
+        }
+        return inputFactory;
+    }
+
+    public void setInputFactory(XMLInputFactory inputFactory) {
+        this.inputFactory = inputFactory;
+    }
+
+    public XMLOutputFactory getOutputFactory() {
+        if (outputFactory == null) {
+            outputFactory = createOutputFactory();
+        }
+        return outputFactory;
+    }
+
+    public void setOutputFactory(XMLOutputFactory outputFactory) {
+        this.outputFactory = outputFactory;
+    }
+
+    protected XMLInputFactory createInputFactory() {
+        return XMLInputFactory.newInstance();
+    }
+
+    protected XMLOutputFactory createOutputFactory() {
+        return XMLOutputFactory.newInstance();
+    }
+
 }

Modified: servicemix/utils/trunk/src/test/java/org/apache/servicemix/jbi/jaxp/StaxSourceTest.java
URL: http://svn.apache.org/viewvc/servicemix/utils/trunk/src/test/java/org/apache/servicemix/jbi/jaxp/StaxSourceTest.java?rev=1144368&r1=1144367&r2=1144368&view=diff
==============================================================================
--- servicemix/utils/trunk/src/test/java/org/apache/servicemix/jbi/jaxp/StaxSourceTest.java
(original)
+++ servicemix/utils/trunk/src/test/java/org/apache/servicemix/jbi/jaxp/StaxSourceTest.java
Fri Jul  8 15:55:04 2011
@@ -90,7 +90,7 @@ public class StaxSourceTest extends Test
         InputStream is = getClass().getResourceAsStream("test.xml");
         XMLStreamReader xsr = XMLInputFactory.newInstance().createXMLStreamReader(is);
         StaxSource ss = new StaxSource(xsr);
-        DOMSource src = new SourceTransformer().toDOMSource(ss);
+        DOMSource src = (DOMSource) new SourceTransformer().toDOMSource(ss);
         assertNotNull(src);
         assertNotNull(src.getNode());
         checkDomResult((Document) src.getNode());

Modified: servicemix/utils/trunk/src/test/java/org/apache/servicemix/jbi/jaxp/WsRfRpTest.java
URL: http://svn.apache.org/viewvc/servicemix/utils/trunk/src/test/java/org/apache/servicemix/jbi/jaxp/WsRfRpTest.java?rev=1144368&r1=1144367&r2=1144368&view=diff
==============================================================================
--- servicemix/utils/trunk/src/test/java/org/apache/servicemix/jbi/jaxp/WsRfRpTest.java (original)
+++ servicemix/utils/trunk/src/test/java/org/apache/servicemix/jbi/jaxp/WsRfRpTest.java Fri
Jul  8 15:55:04 2011
@@ -32,7 +32,7 @@ public class WsRfRpTest extends TestCase
     public void test() throws Exception {
         JAXBContext context = JAXBContext.newInstance(ObjectFactory.class);
         Source source = new JAXBSource(context, new ObjectFactory().createGetResourceProperty(new
QName("urn", "local")));
-        XMLStreamReader reader = new StAXSourceTransformer().toXMLStreamReader(source);
+        XMLStreamReader reader = new SourceTransformer().toXMLStreamReader(source);
         reader.nextTag();
         source = new StaxSource(new FragmentStreamReader(reader));
 



Mime
View raw message