diff --git a/xml/src/main/java/org/springframework/xml/transform/ResultHandler.java b/xml/src/main/java/org/springframework/xml/transform/ResultHandler.java
deleted file mode 100644
index 77571b05..00000000
--- a/xml/src/main/java/org/springframework/xml/transform/ResultHandler.java
+++ /dev/null
@@ -1,69 +0,0 @@
-/*
- * Copyright 2007 the original author or authors.
- *
- * Licensed 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.springframework.xml.transform;
-
-import java.io.OutputStream;
-import java.io.Writer;
-import javax.xml.stream.XMLEventWriter;
-import javax.xml.stream.XMLStreamWriter;
-import javax.xml.transform.Result;
-import javax.xml.transform.dom.DOMSource;
-import javax.xml.transform.sax.SAXResult;
-import javax.xml.transform.stax.StAXResult;
-import javax.xml.transform.stream.StreamResult;
-
-import org.w3c.dom.Document;
-import org.w3c.dom.DocumentFragment;
-import org.w3c.dom.Element;
-import org.w3c.dom.Node;
-import org.xml.sax.ContentHandler;
-import org.xml.sax.ext.LexicalHandler;
-
-/**
- * Strategy interface for dealing with various {@link Result} implementations. By using this interface, in combination
- * with {@link TraxUtils#handleResult(Result, ResultHandler)} , implementations are freed from instanceof
- * checks typically seen in TrAX code.
- *
- * @author Arjen Poutsma
- * @see TraxUtils#handleResult(Result, ResultHandler)
- * @since 1.5.0
- */
-public interface ResultHandler {
-
- /**
- * Handles a {@link DOMSource} with a {@link Node}.
- *
instanceof
- * checks typically seen in TrAX code.
- *
- * @author Arjen Poutsma
- * @see TraxUtils#handleSource(Source, SourceHandler)
- * @since 1.5.0
- */
-public interface SourceHandler {
-
- /** Handles a {@link DOMSource} with a {@link Node}. */
- void domSource(Node node);
-
- /** Handles a {@link SAXSource} with an {@link XMLReader} and an {@link InputSource}. */
- void saxSource(XMLReader xmlReader, InputSource inputSource);
-
- /** Handles a StAX Source with an {@link XMLEventReader}, either in a {@link StaxSource} or a {@link StAXSource}. */
- void staxSource(XMLEventReader eventReader);
-
- /** Handles a StAX Source with an {@link XMLStreamReader}, either in a {@link StaxSource} or a {@link StAXSource}. */
- void staxSource(XMLStreamReader streamReader);
-
- /** Handles a {@link StreamSource} with an {@link InputStream}. */
- void streamSource(InputStream inputStream);
-
- /** Handles a {@link StreamSource} with an {@link Reader}. */
- void streamSource(Reader reader);
-
-}
diff --git a/xml/src/main/java/org/springframework/xml/transform/TraxUtils.java b/xml/src/main/java/org/springframework/xml/transform/TraxUtils.java
index f7875342..eba44b47 100644
--- a/xml/src/main/java/org/springframework/xml/transform/TraxUtils.java
+++ b/xml/src/main/java/org/springframework/xml/transform/TraxUtils.java
@@ -16,9 +16,7 @@
package org.springframework.xml.transform;
-import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLStreamException;
@@ -26,21 +24,8 @@ import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
-import javax.xml.transform.dom.DOMResult;
-import javax.xml.transform.dom.DOMSource;
-import javax.xml.transform.sax.SAXResult;
-import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stax.StAXResult;
import javax.xml.transform.stax.StAXSource;
-import javax.xml.transform.stream.StreamResult;
-import javax.xml.transform.stream.StreamSource;
-
-import org.xml.sax.ContentHandler;
-import org.xml.sax.InputSource;
-import org.xml.sax.SAXException;
-import org.xml.sax.ext.LexicalHandler;
-import org.xml.sax.helpers.DefaultHandler;
-import org.xml.sax.helpers.XMLReaderFactory;
import org.springframework.util.Assert;
import org.springframework.xml.JaxpVersion;
@@ -59,6 +44,122 @@ public abstract class TraxUtils {
documentBuilderFactory.setNamespaceAware(true);
}
+ /**
+ * Indicates whether the given {@link Source} is a StAX Source.
+ *
+ * @return true if source is a Spring-WS {@link StaxSource} or JAXP 1.4 {@link
+ * StAXSource}; false otherwise.
+ */
+ public static boolean isStaxSource(Source source) {
+ if (source instanceof StaxSource) {
+ return true;
+ }
+ else if (JaxpVersion.isAtLeastJaxp14()) {
+ return Jaxp14StaxHandler.isStaxSource(source);
+ }
+ else {
+ return false;
+ }
+ }
+
+ /**
+ * Indicates whether the given {@link Result} is a StAX Result.
+ *
+ * @return true if result is a Spring-WS {@link StaxResult} or JAXP 1.4 {@link
+ * StAXResult}; false otherwise.
+ */
+ public static boolean isStaxResult(Result result) {
+ if (result instanceof StaxResult) {
+ return true;
+ }
+ else if (JaxpVersion.isAtLeastJaxp14()) {
+ return Jaxp14StaxHandler.isStaxResult(result);
+ }
+ else {
+ return false;
+ }
+ }
+
+ /**
+ * Returns the {@link XMLStreamReader} for the given StAX Source.
+ *
+ * @param source a Spring-WS {@link StaxSource} or {@link StAXSource}
+ * @return the {@link XMLStreamReader}
+ * @throws IllegalArgumentException if source is neither a Spring-WS {@link StaxSource} or {@link
+ * StAXSource}
+ */
+ public static XMLStreamReader getXMLStreamReader(Source source) {
+ if (source instanceof StaxSource) {
+ return ((StaxSource) source).getXMLStreamReader();
+ }
+ else if (JaxpVersion.isAtLeastJaxp14()) {
+ return Jaxp14StaxHandler.getXMLStreamReader(source);
+ }
+ else {
+ throw new IllegalArgumentException("Source '" + source + "' is neither StaxSource nor StAXSource");
+ }
+ }
+
+ /**
+ * Returns the {@link XMLEventReader} for the given StAX Source.
+ *
+ * @param source a Spring-WS {@link StaxSource} or {@link StAXSource}
+ * @return the {@link XMLEventReader}
+ * @throws IllegalArgumentException if source is neither a Spring-WS {@link StaxSource} or {@link
+ * StAXSource}
+ */
+ public static XMLEventReader getXMLEventReader(Source source) {
+ if (source instanceof StaxSource) {
+ return ((StaxSource) source).getXMLEventReader();
+ }
+ else if (JaxpVersion.isAtLeastJaxp14()) {
+ return Jaxp14StaxHandler.getXMLEventReader(source);
+ }
+ else {
+ throw new IllegalArgumentException("Source '" + source + "' is neither StaxSource nor StAXSource");
+ }
+ }
+
+ /**
+ * Returns the {@link XMLStreamWriter} for the given StAX Result.
+ *
+ * @param result a Spring-WS {@link StaxResult} or {@link StAXResult}
+ * @return the {@link XMLStreamReader}
+ * @throws IllegalArgumentException if source is neither a Spring-WS {@link StaxResult} or {@link
+ * StAXResult}
+ */
+ public static XMLStreamWriter getXMLStreamWriter(Result result) {
+ if (result instanceof StaxResult) {
+ return ((StaxResult) result).getXMLStreamWriter();
+ }
+ else if (JaxpVersion.isAtLeastJaxp14()) {
+ return Jaxp14StaxHandler.getXMLStreamWriter(result);
+ }
+ else {
+ throw new IllegalArgumentException("Result '" + result + "' is neither StaxResult nor StAXResult");
+ }
+ }
+
+ /**
+ * Returns the {@link XMLEventWriter} for the given StAX Result.
+ *
+ * @param result a Spring-WS {@link StaxResult} or {@link StAXResult}
+ * @return the {@link XMLStreamReader}
+ * @throws IllegalArgumentException if source is neither a Spring-WS {@link StaxResult} or {@link
+ * StAXResult}
+ */
+ public static XMLEventWriter getXMLEventWriter(Result result) {
+ if (result instanceof StaxResult) {
+ return ((StaxResult) result).getXMLEventWriter();
+ }
+ else if (JaxpVersion.isAtLeastJaxp14()) {
+ return Jaxp14StaxHandler.getXMLEventWriter(result);
+ }
+ else {
+ throw new IllegalArgumentException("Result '" + result + "' is neither StaxResult nor StAXResult");
+ }
+ }
+
/**
* Creates a StAX {@link Source} for the given {@link XMLStreamReader}. Returns a {@link StAXSource} under JAXP 1.4
* or higher, or a {@link StaxSource} otherwise.
@@ -92,198 +193,6 @@ public abstract class TraxUtils {
}
}
- /**
- * Handles the given {@link Source} by calling one of the methods on the given {@link SourceHandler}.
- *
- * @param source the source to handle
- * @param handler the handler
- */
- public static void handleSource(Source source, SourceHandler handler) {
- if (source instanceof DOMSource) {
- handleDomSource((DOMSource) source, handler);
- }
- else if (isStaxSource(source)) {
- handleStaxSource(source, handler);
- }
- else if (source instanceof SAXSource) {
- handleSaxSource((SAXSource) source, handler);
- }
- else if (source instanceof StreamSource) {
- handleStreamSource((StreamSource) source, handler);
- }
- else {
- throw new IllegalArgumentException("Unknown Source type: " + source.getClass());
- }
- }
-
- private static boolean isStaxSource(Source source) {
- if (source instanceof StaxSource) {
- return true;
- }
- else if (JaxpVersion.isAtLeastJaxp14()) {
- return Jaxp14StaxHandler.isStaxSource(source);
- }
- else {
- return false;
- }
- }
-
- private static void handleDomSource(DOMSource domSource, SourceHandler handler) {
- if (domSource.getNode() == null) {
- try {
- DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
- domSource.setNode(documentBuilder.newDocument());
- }
- catch (ParserConfigurationException ex) {
- throw new IllegalStateException("Could not create document for DOMSource: " + ex.getMessage(), ex);
- }
- }
- handler.domSource(domSource.getNode());
- }
-
- private static void handleStaxSource(Source source, SourceHandler handler) {
- XMLStreamReader streamReader = null;
- XMLEventReader eventReader = null;
- if (source instanceof StaxSource) {
- StaxSource staxSource = (StaxSource) source;
- streamReader = staxSource.getXMLStreamReader();
- eventReader = staxSource.getXMLEventReader();
- }
- else if (JaxpVersion.isAtLeastJaxp14()) {
- streamReader = Jaxp14StaxHandler.getXMLStreamReader(source);
- eventReader = Jaxp14StaxHandler.getXMLEventReader(source);
- }
- if (streamReader != null) {
- handler.staxSource(streamReader);
- }
- else if (eventReader != null) {
- handler.staxSource(eventReader);
- }
- else {
- throw new IllegalArgumentException("StAX Source contains neither XMLStreamReader nor XMLEventReader");
- }
- }
-
- private static void handleSaxSource(SAXSource saxSource, SourceHandler handler) {
- if (saxSource.getXMLReader() == null) {
- try {
- saxSource.setXMLReader(XMLReaderFactory.createXMLReader());
- }
- catch (SAXException ex) {
- throw new IllegalStateException("Could not create XMLReader for SAXSource: " + ex.getMessage(), ex);
- }
- }
- if (saxSource.getInputSource() == null) {
- saxSource.setInputSource(new InputSource());
- }
- handler.saxSource(saxSource.getXMLReader(), saxSource.getInputSource());
- }
-
- private static void handleStreamSource(StreamSource streamSource, SourceHandler handler) {
- if (streamSource.getInputStream() != null) {
- handler.streamSource(streamSource.getInputStream());
- }
- else if (streamSource.getReader() != null) {
- handler.streamSource(streamSource.getReader());
- }
- else {
- throw new IllegalArgumentException("StreamSource contains neither InputStream nor Reader");
- }
- }
-
- /**
- * Handles the given {@link Result} by calling one of the methods on the given {@link ResultHandler}.
- *
- * @param result the result to handle
- * @param handler the handler
- */
- public static void handleResult(Result result, ResultHandler handler) {
- if (result instanceof DOMResult) {
- handleDomResult((DOMResult) result, handler);
- }
- else if (isStaxResult(result)) {
- handleStaxResult(result, handler);
- }
- else if (result instanceof SAXResult) {
- handleSaxResult((SAXResult) result, handler);
- }
- else if (result instanceof StreamResult) {
- handleStreamResult((StreamResult) result, handler);
- }
- else {
- throw new IllegalArgumentException("Unknown Result type: " + result.getClass());
- }
- }
-
- private static boolean isStaxResult(Result result) {
- if (result instanceof StaxResult) {
- return true;
- }
- else if (JaxpVersion.isAtLeastJaxp14()) {
- return Jaxp14StaxHandler.isStaxResult(result);
- }
- else {
- return false;
- }
- }
-
- private static void handleDomResult(DOMResult domResult, ResultHandler handler) {
- if (domResult.getNode() == null) {
- try {
- DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
- domResult.setNode(documentBuilder.newDocument());
- }
- catch (ParserConfigurationException ex) {
- throw new IllegalStateException("Could not create document for DOMResult: " + ex.getMessage(), ex);
- }
- }
- handler.domResult(domResult.getNode());
- }
-
- private static void handleStaxResult(Result result, ResultHandler handler) {
- XMLStreamWriter streamWriter = null;
- XMLEventWriter eventWriter = null;
- if (result instanceof StaxResult) {
- StaxResult staxResult = (StaxResult) result;
- streamWriter = staxResult.getXMLStreamWriter();
- eventWriter = staxResult.getXMLEventWriter();
- }
- else if (JaxpVersion.isAtLeastJaxp14()) {
- streamWriter = Jaxp14StaxHandler.getXMLStreamWriter(result);
- eventWriter = Jaxp14StaxHandler.getXMLEventWriter(result);
- }
- if (streamWriter != null) {
- handler.staxResult(streamWriter);
- }
- else if (eventWriter != null) {
- handler.staxResult(eventWriter);
- }
- else {
- throw new IllegalArgumentException("StAX Result contains neither XMLStreamWriter nor XMLEventWriter");
- }
- }
-
- private static void handleSaxResult(SAXResult saxResult, ResultHandler handler) {
- ContentHandler contentHandler = saxResult.getHandler();
- if (contentHandler == null) {
- contentHandler = new DefaultHandler();
- }
- LexicalHandler lexicalHandler = saxResult.getLexicalHandler();
- handler.saxResult(contentHandler, lexicalHandler);
- }
-
- private static void handleStreamResult(StreamResult streamResult, ResultHandler handler) {
- if (streamResult.getOutputStream() != null) {
- handler.streamResult(streamResult.getOutputStream());
- }
- else if (streamResult.getWriter() != null) {
- handler.streamResult(streamResult.getWriter());
- }
- else {
- throw new IllegalArgumentException("StreamResult contains neither OutputStream nor Writer");
- }
- }
-
/** Inner class to avoid a static JAXP 1.4 dependency. */
private static class Jaxp14StaxHandler {
diff --git a/xml/src/test/java/org/springframework/xml/transform/TraxUtilsTest.java b/xml/src/test/java/org/springframework/xml/transform/TraxUtilsTest.java
index 9384d7d8..8bbf91b4 100644
--- a/xml/src/test/java/org/springframework/xml/transform/TraxUtilsTest.java
+++ b/xml/src/test/java/org/springframework/xml/transform/TraxUtilsTest.java
@@ -16,16 +16,8 @@
package org.springframework.xml.transform;
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
-import java.io.Writer;
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLInputFactory;
@@ -45,31 +37,53 @@ import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.custommonkey.xmlunit.XMLTestCase;
-import org.easymock.MockControl;
-import org.w3c.dom.Document;
-import org.xml.sax.ContentHandler;
-import org.xml.sax.InputSource;
-import org.xml.sax.XMLReader;
-import org.xml.sax.ext.DefaultHandler2;
-import org.xml.sax.ext.LexicalHandler;
-import org.xml.sax.helpers.DefaultHandler;
-import org.xml.sax.helpers.XMLReaderFactory;
public class TraxUtilsTest extends XMLTestCase {
- private SourceHandler sourceHandlerMock;
+ public void testIsStaxSourceInvalid() throws Exception {
+ assertFalse("A StAX Source", TraxUtils.isStaxSource(new DOMSource()));
+ assertFalse("A StAX Source", TraxUtils.isStaxSource(new SAXSource()));
+ assertFalse("A StAX Source", TraxUtils.isStaxSource(new StreamSource()));
+ }
- private MockControl sourceHandlerControl;
+ public void testIsStaxSource() throws Exception {
+ XMLInputFactory inputFactory = XMLInputFactory.newInstance();
+ String expected = "