From 962caeab1e22f23cb8a9e1e75165d180a62005cc Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Fri, 12 Jan 2007 09:19:35 +0000 Subject: [PATCH] Added ResourceSource --- .../xml/transform/ResourceSource.java | 42 +++++++++++++++++++ .../xml/transform/ResourceSourceTest.java | 40 ++++++++++++++++++ .../xml/transform/StringResultTest.java | 8 +--- .../xml/transform/resourceSource.xml | 2 + 4 files changed, 85 insertions(+), 7 deletions(-) create mode 100644 xml/src/main/java/org/springframework/xml/transform/ResourceSource.java create mode 100644 xml/src/test/java/org/springframework/xml/transform/ResourceSourceTest.java create mode 100644 xml/src/test/resources/org/springframework/xml/transform/resourceSource.xml diff --git a/xml/src/main/java/org/springframework/xml/transform/ResourceSource.java b/xml/src/main/java/org/springframework/xml/transform/ResourceSource.java new file mode 100644 index 00000000..d05c34cb --- /dev/null +++ b/xml/src/main/java/org/springframework/xml/transform/ResourceSource.java @@ -0,0 +1,42 @@ +/* + * 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.IOException; +import javax.xml.transform.sax.SAXSource; + +import org.springframework.core.io.Resource; +import org.springframework.xml.sax.SaxUtils; + +/** + * Convenient subclass of StreamSource that reads from a Spring {@link + * org.springframework.core.io.Resource}. The resource to be read can be set via the constructor. + * + * @author Arjen Poutsma + */ +public class ResourceSource extends SAXSource { + + /** + * Initializes a new instance of the ResourceSource with the given resource. + * + * @param content the content + */ + public ResourceSource(Resource content) throws IOException { + super(SaxUtils.createInputSource(content)); + } + +} diff --git a/xml/src/test/java/org/springframework/xml/transform/ResourceSourceTest.java b/xml/src/test/java/org/springframework/xml/transform/ResourceSourceTest.java new file mode 100644 index 00000000..c4209aba --- /dev/null +++ b/xml/src/test/java/org/springframework/xml/transform/ResourceSourceTest.java @@ -0,0 +1,40 @@ +/* + * 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.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMResult; + +import org.custommonkey.xmlunit.XMLTestCase; +import org.springframework.core.io.ClassPathResource; +import org.w3c.dom.Element; + +public class ResourceSourceTest extends XMLTestCase { + + public void testStringSource() throws Exception { + Transformer transformer = TransformerFactory.newInstance().newTransformer(); + DOMResult result = new DOMResult(); + ResourceSource source = new ResourceSource(new ClassPathResource("resourceSource.xml", getClass())); + transformer.transform(source, result); + Element rootElement = (Element) result.getNode().getFirstChild(); + assertEquals("Invalid local name", "content", rootElement.getLocalName()); + assertEquals("Invalid prefix", "prefix", rootElement.getPrefix()); + assertEquals("Invalid namespace", "namespace", rootElement.getNamespaceURI()); + } + +} \ No newline at end of file diff --git a/xml/src/test/java/org/springframework/xml/transform/StringResultTest.java b/xml/src/test/java/org/springframework/xml/transform/StringResultTest.java index e626eb07..23604b6a 100644 --- a/xml/src/test/java/org/springframework/xml/transform/StringResultTest.java +++ b/xml/src/test/java/org/springframework/xml/transform/StringResultTest.java @@ -16,24 +16,18 @@ package org.springframework.xml.transform; -import java.io.IOException; - import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import org.custommonkey.xmlunit.XMLTestCase; import org.w3c.dom.Document; import org.w3c.dom.Element; -import org.xml.sax.SAXException; public class StringResultTest extends XMLTestCase { - public void testStringResult() - throws TransformerException, ParserConfigurationException, IOException, SAXException { + public void testStringResult() throws Exception { Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); Element element = document.createElementNS("namespace", "prefix:localName"); document.appendChild(element); diff --git a/xml/src/test/resources/org/springframework/xml/transform/resourceSource.xml b/xml/src/test/resources/org/springframework/xml/transform/resourceSource.xml new file mode 100644 index 00000000..b89ac28d --- /dev/null +++ b/xml/src/test/resources/org/springframework/xml/transform/resourceSource.xml @@ -0,0 +1,2 @@ + + \ No newline at end of file