From a7d3ed24bc439a484d56f1a1f101500b254269a5 Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Wed, 19 Dec 2007 18:11:02 +0000 Subject: [PATCH] Working on JAXP 1.4 support. --- xml/pom.xml | 29 +- .../org/springframework/xml/JaxpVersion.java | 12 + .../xml/transform/ResultHandler.java | 69 ++++ .../xml/transform/SourceHandler.java | 62 +++ .../xml/transform/TraxUtils.java | 328 ++++++++++++++++ .../xml/transform/TraxUtilsTest.java | 354 ++++++++++++++++++ 6 files changed, 830 insertions(+), 24 deletions(-) create mode 100644 xml/src/main/java/org/springframework/xml/transform/ResultHandler.java create mode 100644 xml/src/main/java/org/springframework/xml/transform/SourceHandler.java create mode 100644 xml/src/main/java/org/springframework/xml/transform/TraxUtils.java create mode 100644 xml/src/test/java/org/springframework/xml/transform/TraxUtilsTest.java diff --git a/xml/pom.xml b/xml/pom.xml index a24d3a9a..f4b3f770 100644 --- a/xml/pom.xml +++ b/xml/pom.xml @@ -1,4 +1,5 @@ - + spring-ws-parent org.springframework.ws @@ -19,7 +20,7 @@ ${artifactId} - org.springframework.xml* + org.springframework.xml*;version=${pom.version} org.apache.commons.logging*, javax.xml.namespace*, @@ -27,7 +28,7 @@ javax.xml.transform*, org.xml.sax*, org.w3c.dom*, - org.springframework.beans*, + org.springframework.beans*;version="[2.0,2.5)", org.springframework.core*, org.springframework.util*, *;resolution:=optional @@ -42,26 +43,6 @@ - - xml-apis - xml-apis - provided - - - stax - stax-api - true - - - xalan - xalan - provided - - - xerces - xercesImpl - provided - jaxen jaxen @@ -70,7 +51,7 @@ org.codehaus.woodstox wstx-asl - provided + test diff --git a/xml/src/main/java/org/springframework/xml/JaxpVersion.java b/xml/src/main/java/org/springframework/xml/JaxpVersion.java index 6dcd0c53..39d5d44d 100644 --- a/xml/src/main/java/org/springframework/xml/JaxpVersion.java +++ b/xml/src/main/java/org/springframework/xml/JaxpVersion.java @@ -80,4 +80,16 @@ public abstract class JaxpVersion { public static int getJaxpVersion() { return jaxpVersion; } + + /** + * Convenience method to determine if the current JAXP version is at least 1.4 (packaged with JDK 1.6). + * + * @return true if the current JAXP version is at least JAXP 1.4 + * @see #getJaxpVersion() + * @see #JAXP_14 + */ + public static boolean isAtLeastJaxp14() { + return getJaxpVersion() >= JAXP_14; + } + } \ No newline at end of file diff --git a/xml/src/main/java/org/springframework/xml/transform/ResultHandler.java b/xml/src/main/java/org/springframework/xml/transform/ResultHandler.java new file mode 100644 index 00000000..77571b05 --- /dev/null +++ b/xml/src/main/java/org/springframework/xml/transform/ResultHandler.java @@ -0,0 +1,69 @@ +/* + * 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}. + *

+ * In practice, node is typically a {@link Document}, a {@link DocumentFragment}, or a {@link Element}. In other + * words, a node that accepts children. + */ + void domResult(Node node); + + /** Handles a {@link SAXResult} with a {@link ContentHandler} and possibly a {@link LexicalHandler}. */ + void saxResult(ContentHandler contentHandler, LexicalHandler lexicalHandler); + + /** Handles a StAX Result with an {@link XMLEventWriter}, either in a {@link StaxResult} or a {@link StAXResult}. */ + void staxResult(XMLEventWriter eventWriter); + + /** Handles a StAX Result with an {@link XMLStreamWriter}, either in a {@link StaxResult} or a {@link StAXResult}. */ + void staxResult(XMLStreamWriter streamWriter); + + /** Handles a {@link StreamResult} with an {@link OutputStream}. */ + void streamResult(OutputStream outputStream); + + /** Handles a {@link StreamResult} with an {@link Writer}. */ + void streamResult(Writer writer); +} diff --git a/xml/src/main/java/org/springframework/xml/transform/SourceHandler.java b/xml/src/main/java/org/springframework/xml/transform/SourceHandler.java new file mode 100644 index 00000000..bd5b531b --- /dev/null +++ b/xml/src/main/java/org/springframework/xml/transform/SourceHandler.java @@ -0,0 +1,62 @@ +/* + * 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.InputStream; +import java.io.Reader; +import javax.xml.stream.XMLEventReader; +import javax.xml.stream.XMLStreamReader; +import javax.xml.transform.Source; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.sax.SAXSource; +import javax.xml.transform.stax.StAXSource; +import javax.xml.transform.stream.StreamSource; + +import org.w3c.dom.Node; +import org.xml.sax.InputSource; +import org.xml.sax.XMLReader; + +/** + * Strategy interface for dealing with various {@link Source} implementations. By using this interface, in combination + * with {@link TraxUtils#handleSource(Source, SourceHandler)} , implementations are freed from 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 new file mode 100644 index 00000000..f7875342 --- /dev/null +++ b/xml/src/main/java/org/springframework/xml/transform/TraxUtils.java @@ -0,0 +1,328 @@ +/* + * 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 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; +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; + +/** + * Convenient utility methods for dealing with TrAX. + * + * @author Arjen Poutsma + * @since 1.5.0 + */ +public abstract class TraxUtils { + + private static DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); + + static { + documentBuilderFactory.setNamespaceAware(true); + } + + /** + * 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. + * + * @param streamReader the StAX stream reader + * @return a source wrapping streamReader + */ + public static Source createStaxSource(XMLStreamReader streamReader) { + if (JaxpVersion.isAtLeastJaxp14()) { + return Jaxp14StaxHandler.createStaxSource(streamReader); + } + else { + return new StaxSource(streamReader); + } + } + + /** + * Creates a StAX {@link Source} for the given {@link XMLEventReader}. Returns a {@link StAXSource} under JAXP 1.4 + * or higher, or a {@link StaxSource} otherwise. + * + * @param eventReader the StAX event reader + * @return a source wrapping streamReader + * @throws XMLStreamException in case of StAX errors + */ + public static Source createStaxSource(XMLEventReader eventReader) throws XMLStreamException { + if (JaxpVersion.isAtLeastJaxp14()) { + return Jaxp14StaxHandler.createStaxSource(eventReader); + } + else { + return new StaxSource(eventReader); + } + } + + /** + * 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 { + + private static Source createStaxSource(XMLStreamReader streamReader) { + return new StAXSource(streamReader); + } + + private static Source createStaxSource(XMLEventReader eventReader) throws XMLStreamException { + return new StAXSource(eventReader); + } + + private static boolean isStaxSource(Source source) { + return source instanceof StAXSource; + } + + private static boolean isStaxResult(Result result) { + return result instanceof StAXResult; + } + + private static XMLStreamReader getXMLStreamReader(Source source) { + Assert.isInstanceOf(StAXSource.class, source); + return ((StAXSource) source).getXMLStreamReader(); + } + + private static XMLEventReader getXMLEventReader(Source source) { + Assert.isInstanceOf(StAXSource.class, source); + return ((StAXSource) source).getXMLEventReader(); + } + + private static XMLStreamWriter getXMLStreamWriter(Result result) { + Assert.isInstanceOf(StAXResult.class, result); + return ((StAXResult) result).getXMLStreamWriter(); + } + + private static XMLEventWriter getXMLEventWriter(Result result) { + Assert.isInstanceOf(StAXResult.class, result); + return ((StAXResult) result).getXMLEventWriter(); + } + } + + +} diff --git a/xml/src/test/java/org/springframework/xml/transform/TraxUtilsTest.java b/xml/src/test/java/org/springframework/xml/transform/TraxUtilsTest.java new file mode 100644 index 00000000..9384d7d8 --- /dev/null +++ b/xml/src/test/java/org/springframework/xml/transform/TraxUtilsTest.java @@ -0,0 +1,354 @@ +/* + * 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.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; +import javax.xml.stream.XMLOutputFactory; +import javax.xml.stream.XMLStreamReader; +import javax.xml.stream.XMLStreamWriter; +import javax.xml.transform.Source; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +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.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; + + private MockControl sourceHandlerControl; + + private MockControl resultHandlerControl; + + private ResultHandler resultHandlerMock; + + protected void setUp() throws Exception { + sourceHandlerControl = MockControl.createControl(SourceHandler.class); + sourceHandlerMock = (SourceHandler) sourceHandlerControl.getMock(); + resultHandlerControl = MockControl.createControl(ResultHandler.class); + resultHandlerMock = (ResultHandler) resultHandlerControl.getMock(); + } + + public void testCreateStaxSourceStreamReader() throws Exception { + XMLInputFactory inputFactory = XMLInputFactory.newInstance(); + String expected = ""; + XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(expected)); + + Source source = TraxUtils.createStaxSource(streamReader); + + Transformer transformer = TransformerFactory.newInstance().newTransformer(); + StringResult result = new StringResult(); + transformer.transform(source, result); + + assertXMLEqual(expected, result.toString()); + } + + public void testCreateStaxSourceEventReader() throws Exception { + XMLInputFactory inputFactory = XMLInputFactory.newInstance(); + String expected = ""; + XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(expected)); + + Source source = TraxUtils.createStaxSource(eventReader); + + Transformer transformer = TransformerFactory.newInstance().newTransformer(); + StringResult result = new StringResult(); + transformer.transform(source, result); + + assertXMLEqual(expected, result.toString()); + } + + public void testHandleDomSource() throws Exception { + DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); + Document document = documentBuilder.newDocument(); + + DOMSource domSource = new DOMSource(document); + sourceHandlerMock.domSource(domSource.getNode()); + sourceHandlerControl.replay(); + + TraxUtils.handleSource(domSource, sourceHandlerMock); + + sourceHandlerControl.verify(); + } + + public void testHandleEmptyDomSource() throws Exception { + DOMSource domSource = new DOMSource(); + sourceHandlerMock.domSource(domSource.getNode()); + sourceHandlerControl.setMatcher(MockControl.ALWAYS_MATCHER); + sourceHandlerControl.replay(); + + TraxUtils.handleSource(domSource, sourceHandlerMock); + + sourceHandlerControl.verify(); + } + + public void testHandlerJaxp14StaxSourceStreamReader() throws Exception { + XMLInputFactory inputFactory = XMLInputFactory.newInstance(); + XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader("")); + + StAXSource staxSource = new StAXSource(streamReader); + sourceHandlerMock.staxSource(streamReader); + sourceHandlerControl.replay(); + + TraxUtils.handleSource(staxSource, sourceHandlerMock); + + sourceHandlerControl.verify(); + } + + public void testHandlerJaxp14StaxSourceEventReader() throws Exception { + XMLInputFactory inputFactory = XMLInputFactory.newInstance(); + XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader("")); + + StAXSource staxSource = new StAXSource(eventReader); + sourceHandlerMock.staxSource(eventReader); + sourceHandlerControl.replay(); + + TraxUtils.handleSource(staxSource, sourceHandlerMock); + + sourceHandlerControl.verify(); + } + + public void testHandlerStaxSourceStreamReader() throws Exception { + XMLInputFactory inputFactory = XMLInputFactory.newInstance(); + XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader("")); + + StaxSource staxSource = new StaxSource(streamReader); + sourceHandlerMock.staxSource(streamReader); + sourceHandlerControl.replay(); + + TraxUtils.handleSource(staxSource, sourceHandlerMock); + + sourceHandlerControl.verify(); + } + + public void testHandlerStaxSourceEventReader() throws Exception { + XMLInputFactory inputFactory = XMLInputFactory.newInstance(); + XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader("")); + + StaxSource staxSource = new StaxSource(eventReader); + sourceHandlerMock.staxSource(eventReader); + sourceHandlerControl.replay(); + + TraxUtils.handleSource(staxSource, sourceHandlerMock); + + sourceHandlerControl.verify(); + } + + public void testHandlerSaxSource() throws Exception { + XMLReader xmlReader = XMLReaderFactory.createXMLReader(); + InputSource inputSource = new InputSource(new StringReader("")); + + SAXSource saxSource = new SAXSource(xmlReader, inputSource); + sourceHandlerMock.saxSource(xmlReader, inputSource); + sourceHandlerControl.replay(); + + TraxUtils.handleSource(saxSource, sourceHandlerMock); + + sourceHandlerControl.verify(); + } + + public void testHandlerEmptySaxSource() throws Exception { + SAXSource saxSource = new SAXSource(); + sourceHandlerMock.saxSource(null, null); + sourceHandlerControl.setMatcher(MockControl.ALWAYS_MATCHER); + sourceHandlerControl.replay(); + + TraxUtils.handleSource(saxSource, sourceHandlerMock); + + sourceHandlerControl.verify(); + } + + public void testHandleStreamSourceInputStream() throws Exception { + InputStream inputStream = new ByteArrayInputStream("".getBytes("UTF-8")); + + StreamSource streamSource = new StreamSource(inputStream); + sourceHandlerMock.streamSource(inputStream); + sourceHandlerControl.replay(); + + TraxUtils.handleSource(streamSource, sourceHandlerMock); + + sourceHandlerControl.verify(); + } + + public void testHandleStreamSourceReader() throws Exception { + Reader reader = new StringReader(""); + + StreamSource streamSource = new StreamSource(reader); + sourceHandlerMock.streamSource(reader); + sourceHandlerControl.replay(); + + TraxUtils.handleSource(streamSource, sourceHandlerMock); + + sourceHandlerControl.verify(); + } + + public void testHandleDomResult() throws Exception { + DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); + Document document = documentBuilder.newDocument(); + + DOMResult domResult = new DOMResult(document); + resultHandlerMock.domResult(domResult.getNode()); + resultHandlerControl.replay(); + + TraxUtils.handleResult(domResult, resultHandlerMock); + + resultHandlerControl.verify(); + } + + public void testHandleEmptyDomResult() throws Exception { + DOMResult domResult = new DOMResult(); + resultHandlerMock.domResult(domResult.getNode()); + resultHandlerControl.setMatcher(MockControl.ALWAYS_MATCHER); + resultHandlerControl.replay(); + + TraxUtils.handleResult(domResult, resultHandlerMock); + + resultHandlerControl.verify(); + } + + public void testHandlerJaxp14StaxResultStreamReader() throws Exception { + XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); + XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(new StringWriter()); + + StAXResult stAXResult = new StAXResult(streamWriter); + resultHandlerMock.staxResult(streamWriter); + resultHandlerControl.replay(); + + TraxUtils.handleResult(stAXResult, resultHandlerMock); + + resultHandlerControl.verify(); + } + + public void testHandlerJaxp14StaxResultEventReader() throws Exception { + XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); + XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(new StringWriter()); + + StAXResult stAXResult = new StAXResult(eventWriter); + resultHandlerMock.staxResult(eventWriter); + resultHandlerControl.replay(); + + TraxUtils.handleResult(stAXResult, resultHandlerMock); + + resultHandlerControl.verify(); + } + + public void testHandlerStaxResultStreamReader() throws Exception { + XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); + XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(new StringWriter()); + + StaxResult staxResult = new StaxResult(streamWriter); + resultHandlerMock.staxResult(streamWriter); + resultHandlerControl.replay(); + + TraxUtils.handleResult(staxResult, resultHandlerMock); + + resultHandlerControl.verify(); + } + + public void testHandlerStaxResultEventReader() throws Exception { + XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); + XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(new StringWriter()); + + StaxResult staxResult = new StaxResult(eventWriter); + resultHandlerMock.staxResult(eventWriter); + resultHandlerControl.replay(); + + TraxUtils.handleResult(staxResult, resultHandlerMock); + + resultHandlerControl.verify(); + } + + public void testHandlerSaxResult() throws Exception { + ContentHandler contentHandler = new DefaultHandler(); + LexicalHandler lexicalHandler = new DefaultHandler2(); + + SAXResult saxResult = new SAXResult(contentHandler); + saxResult.setLexicalHandler(lexicalHandler); + resultHandlerMock.saxResult(contentHandler, lexicalHandler); + resultHandlerControl.replay(); + + TraxUtils.handleResult(saxResult, resultHandlerMock); + + resultHandlerControl.verify(); + } + + public void testHandlerEmptySaxResult() throws Exception { + SAXResult saxResult = new SAXResult(); + resultHandlerMock.saxResult(null, null); + resultHandlerControl.setMatcher(MockControl.ALWAYS_MATCHER); + resultHandlerControl.replay(); + + TraxUtils.handleResult(saxResult, resultHandlerMock); + + resultHandlerControl.verify(); + } + + public void testHandleStreamResultOutputStream() throws Exception { + OutputStream outputStream = new ByteArrayOutputStream(); + + StreamResult streamResult = new StreamResult(outputStream); + resultHandlerMock.streamResult(outputStream); + resultHandlerControl.replay(); + + TraxUtils.handleResult(streamResult, resultHandlerMock); + + resultHandlerControl.verify(); + } + + public void testHandleStreamResultWriter() throws Exception { + Writer writer = new StringWriter(); + + StreamResult streamResult = new StreamResult(writer); + resultHandlerMock.streamResult(writer); + resultHandlerControl.replay(); + + TraxUtils.handleResult(streamResult, resultHandlerMock); + + resultHandlerControl.verify(); + } + +} \ No newline at end of file