You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by gn...@apache.org on 2007/01/11 16:00:34 UTC
svn commit: r495245 - in
/incubator/servicemix/trunk/core/servicemix-core/src:
main/java/org/apache/servicemix/jbi/jaxp/
test/java/org/apache/servicemix/jbi/jaxp/
Author: gnodet
Date: Thu Jan 11 07:00:30 2007
New Revision: 495245
URL: http://svn.apache.org/viewvc?view=rev&rev=495245
Log:
SM-808: Remove Xalan dependency in SourceTransformer
Thanks to Eric Dofonsu !
Modified:
incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/SourceTransformer.java
incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/StAXSourceTransformer.java
incubator/servicemix/trunk/core/servicemix-core/src/test/java/org/apache/servicemix/jbi/jaxp/StaxSourceTest.java
Modified: incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/SourceTransformer.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/SourceTransformer.java?view=diff&rev=495245&r1=495244&r2=495245
==============================================================================
--- incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/SourceTransformer.java (original)
+++ incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/SourceTransformer.java Thu Jan 11 07:00:30 2007
@@ -16,12 +16,13 @@
*/
package org.apache.servicemix.jbi.jaxp;
-import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
+import java.io.StringReader;
import java.io.StringWriter;
+import java.lang.reflect.Constructor;
import javax.jbi.messaging.MessagingException;
import javax.jbi.messaging.NormalizedMessage;
@@ -35,20 +36,18 @@
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
-import org.apache.xalan.xsltc.trax.DOM2SAX;
-import org.apache.xalan.xsltc.trax.SAX2DOM;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
-import org.xml.sax.helpers.XMLReaderFactory;
/**
* A helper class to transform from one type of {@link Source} to another
@@ -149,7 +148,7 @@
* 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).
*/
- public SAXSource toSAXSource(Source source) throws IOException, SAXException {
+ public SAXSource toSAXSource(Source source) throws IOException, SAXException, TransformerException {
if (source instanceof SAXSource) {
return (SAXSource) source;
}
@@ -179,24 +178,29 @@
public StreamSource toStreamSourceFromSAX(SAXSource source) throws TransformerException {
InputSource inputSource = source.getInputSource();
if (inputSource != null) {
- if (inputSource.getByteStream() != null) {
- return new StreamSource(inputSource.getByteStream());
- }
if (inputSource.getCharacterStream() != null) {
return new StreamSource(inputSource.getCharacterStream());
}
+ if (inputSource.getByteStream() != null) {
+ return new StreamSource(inputSource.getByteStream());
+ }
}
String result = toString(source);
- return new StreamSource(new ByteArrayInputStream(result.getBytes()));
+ return new StringSource(result);
}
public StreamSource toStreamSourceFromDOM(DOMSource source) throws TransformerException {
String result = toString(source);
- return new StreamSource(new ByteArrayInputStream(result.getBytes()));
+ return new StringSource(result);
}
public SAXSource toSAXSourceFromStream(StreamSource source) {
- InputSource inputSource = new InputSource(source.getInputStream());
+ InputSource inputSource;
+ if (source.getReader() != null) {
+ inputSource = new InputSource(source.getReader());
+ } else {
+ inputSource = new InputSource(source.getInputStream());
+ }
inputSource.setSystemId(source.getSystemId());
inputSource.setPublicId(source.getPublicId());
return new SAXSource(inputSource);
@@ -211,8 +215,7 @@
return r;
}
- public DOMSource toDOMSourceFromStream(StreamSource source) throws ParserConfigurationException, IOException,
- SAXException {
+ public DOMSource toDOMSourceFromStream(StreamSource source) throws ParserConfigurationException, IOException, SAXException {
DocumentBuilder builder = createDocumentBuilder();
String systemId = source.getSystemId();
Document document = null;
@@ -233,38 +236,48 @@
return new DOMSource(document, systemId);
}
- public SAXSource toSAXSourceFromDOM(DOMSource source) {
- DOM2SAX converter = new DOM2SAX(source.getNode());
- String systemId = converter.getSystemId();
- return new SAXSource(converter, new InputSource(systemId));
+ /*
+ * When converting a DOM tree to a SAXSource,
+ * we try to use Xalan internal DOM parser if
+ * available. Else, transform the DOM tree
+ * to a String and build a SAXSource on top of
+ * it.
+ */
+ private static final Class dom2SaxClass;
+
+ static {
+ Class cl = null;
+ try {
+ cl = Class.forName("org.apache.xalan.xsltc.trax.DOM2SAX");
+ } catch (Throwable t) {}
+ dom2SaxClass = cl;
+ }
+
+ public SAXSource toSAXSourceFromDOM(DOMSource source) throws TransformerException {
+ if (dom2SaxClass != null) {
+ try {
+ Constructor cns = dom2SaxClass.getConstructor(new Class[] { Node.class });
+ XMLReader converter = (XMLReader) cns.newInstance(new Object[] { source.getNode() });
+ return new SAXSource(converter, new InputSource());
+ } catch (Exception e) {
+ throw new TransformerException(e);
+ }
+ } else {
+ String str = toString(source);
+ StringReader reader = new StringReader(str);
+ return new SAXSource(new InputSource(reader));
+ }
}
- public DOMSource toDOMSourceFromSAX(SAXSource source) throws IOException, SAXException, ParserConfigurationException {
+ public DOMSource toDOMSourceFromSAX(SAXSource source) throws IOException, SAXException, ParserConfigurationException, TransformerException {
return new DOMSource(toDOMNodeFromSAX(source));
}
- public Node toDOMNodeFromSAX(SAXSource source) throws ParserConfigurationException, IOException, SAXException {
- SAX2DOM converter = new SAX2DOM(createDocument());
- XMLReader xmlReader = source.getXMLReader();
- if (xmlReader == null) {
- xmlReader = createXMLReader();
- }
- xmlReader.setContentHandler(converter);
- xmlReader.parse(source.getInputSource());
- return converter.getDOM();
+ public Node toDOMNodeFromSAX(SAXSource source) throws ParserConfigurationException, IOException, SAXException, TransformerException {
+ DOMResult result = new DOMResult();
+ toResult(source, result);
+ return result.getNode();
}
-
- private XMLReader createXMLReader() throws SAXException {
- // In JDK 1.4, the xml reader factory does not look for META-INF services
- // If the org.xml.sax.driver system property is not defined, and exception will be thrown.
- // In these cases, default to xerces parser
- try {
- return XMLReaderFactory.createXMLReader();
- } catch (Exception e) {
- return XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
- }
- }
-
/**
* Converts the given TRaX Source into a W3C DOM node
Modified: incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/StAXSourceTransformer.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/StAXSourceTransformer.java?view=diff&rev=495245&r1=495244&r2=495245
==============================================================================
--- incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/StAXSourceTransformer.java (original)
+++ incubator/servicemix/trunk/core/servicemix-core/src/main/java/org/apache/servicemix/jbi/jaxp/StAXSourceTransformer.java Thu Jan 11 07:00:30 2007
@@ -88,7 +88,7 @@
return answer;
}
- public SAXSource toSAXSource(Source source) throws IOException, SAXException {
+ public SAXSource toSAXSource(Source source) throws IOException, SAXException, TransformerException {
SAXSource answer = super.toSAXSource(source);
if (answer == null && source instanceof StaxSource) {
answer = toSAXSourceFromStax((StaxSource) source);
Modified: incubator/servicemix/trunk/core/servicemix-core/src/test/java/org/apache/servicemix/jbi/jaxp/StaxSourceTest.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/core/servicemix-core/src/test/java/org/apache/servicemix/jbi/jaxp/StaxSourceTest.java?view=diff&rev=495245&r1=495244&r2=495245
==============================================================================
--- incubator/servicemix/trunk/core/servicemix-core/src/test/java/org/apache/servicemix/jbi/jaxp/StaxSourceTest.java (original)
+++ incubator/servicemix/trunk/core/servicemix-core/src/test/java/org/apache/servicemix/jbi/jaxp/StaxSourceTest.java Thu Jan 11 07:00:30 2007
@@ -28,7 +28,9 @@
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.stream.StreamSource;
import junit.framework.TestCase;
@@ -38,6 +40,7 @@
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
+import org.xml.sax.InputSource;
public class StaxSourceTest extends TestCase {
@@ -93,7 +96,17 @@
assertNotNull(src.getNode());
checkDomResult((Document) src.getNode());
}
-
+
+ public void testEncoding() throws Exception {
+ final String msg = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><hello>åäö</hello>";
+ StringSource src = new StringSource(msg);
+ DOMSource dom = new SourceTransformer().toDOMSource(src);
+ StreamSource stream = new SourceTransformer().toStreamSource(dom);
+ System.err.println(new SourceTransformer().toString(stream));
+ SAXSource sax = new SourceTransformer().toSAXSource(dom);
+ System.err.println(new SourceTransformer().toString(sax));
+ }
+
protected void checkDomResult(Document doc) {
// Whitespace only elements must be preserved
NodeList l = doc.getElementsByTagName("child4");