Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[SM-1831] Gathering all methods in SourceTransformer in place of the …

…StaxSourceTransformer.

git-svn-id: https://svn.apache.org/repos/asf/servicemix/utils/trunk@1144368 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
commit 5dd9901e89d7eee18cf546b1f4e3287cf67d6a4d 1 parent 27b4776
@jbonofre jbonofre authored
View
3  src/main/java/org/apache/servicemix/jbi/helper/MessageUtil.java
@@ -37,7 +37,6 @@
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 static void enableContentRereadability(NormalizedMessage message) throws
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));
}
View
101 src/main/java/org/apache/servicemix/jbi/jaxp/SourceTransformer.java
@@ -27,9 +27,14 @@
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.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 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 DOMSource toDOMSource(Source source) throws ParserConfigurationException,
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 Source toDOMSource(NormalizedMessage message) throws MessagingException,
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 SAXSource toSAXSource(Source source) throws IOException, SAXException, Tr
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 SAXSource toSAXSourceFromStream(StreamSource source) {
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 DOMSource toDOMSourceFromStream(StreamSource source) throws ParserConfigu
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 Document toDOMDocument(Node node) throws ParserConfigurationException, Tr
}
}
+ /**
+ * 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 void setTransformerFactory(TransformerFactory transformerFactory) {
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();
+ }
+
}
View
151 src/main/java/org/apache/servicemix/jbi/jaxp/StAXSourceTransformer.java
@@ -1,151 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.servicemix.jbi.jaxp;
-
-import java.io.IOException;
-
-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.Source;
-import javax.xml.transform.Transformer;
-import javax.xml.transform.TransformerException;
-import javax.xml.transform.dom.DOMResult;
-import javax.xml.transform.dom.DOMSource;
-import javax.xml.transform.sax.SAXSource;
-
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-import org.w3c.dom.Node;
-
-import org.xml.sax.SAXException;
-
-/**
- * An enhanced {@link org.apache.servicemix.jbi.jaxp.SourceTransformer} which
- * adds support for converting from and to {@link StaxSource} instances. Since
- * this class introduces a runtime dependency on StAX which some users may not
- * use/require, this class is separated out from the core JAXP transformer.
- *
- * @version $Revision: 564900 $
- */
-public class StAXSourceTransformer extends SourceTransformer {
-
- private XMLInputFactory inputFactory;
-
- private XMLOutputFactory outputFactory;
-
- /**
- * Converts the source instance to a
- * {@link javax.xml.transform.dom.DOMSource} or returns null if the
- * conversion is not supported (making it easy to derive from this class to
- * add new kinds of conversion).
- */
- 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));
- }
- }
-
- public DOMSource toDOMSource(Source source) throws ParserConfigurationException, IOException, SAXException, TransformerException {
- DOMSource answer = super.toDOMSource(source);
- if (answer == null && source instanceof StaxSource) {
- answer = toDOMSourceFromStax((StaxSource) source);
- }
- return answer;
- }
-
- public SAXSource toSAXSource(Source source) throws IOException, SAXException, TransformerException {
- SAXSource answer = super.toSAXSource(source);
- if (answer == null && source instanceof StaxSource) {
- answer = toSAXSourceFromStax((StaxSource) source);
- }
- return answer;
- }
-
- 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 toSAXSourceFromStax(StaxSource source) {
- return (SAXSource) source;
- }
-
- // Properties
- // -------------------------------------------------------------------------
- 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;
- }
-
- // Implementation methods
- // -------------------------------------------------------------------------
- protected XMLInputFactory createInputFactory() {
- return XMLInputFactory.newInstance();
- }
-
- protected XMLOutputFactory createOutputFactory() {
- return XMLOutputFactory.newInstance();
- }
-
-}
View
2  src/test/java/org/apache/servicemix/jbi/jaxp/StaxSourceTest.java
@@ -90,7 +90,7 @@ public void testStaxToDOM() throws Exception {
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());
View
2  src/test/java/org/apache/servicemix/jbi/jaxp/WsRfRpTest.java
@@ -32,7 +32,7 @@
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));
Please sign in to comment.
Something went wrong with that request. Please try again.