Added ResourceSource

This commit is contained in:
Arjen Poutsma
2007-01-12 09:19:35 +00:00
parent 0cae6a8ed2
commit 962caeab1e
4 changed files with 85 additions and 7 deletions

View File

@@ -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 <code>StreamSource</code> 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 <code>ResourceSource</code> with the given resource.
*
* @param content the content
*/
public ResourceSource(Resource content) throws IOException {
super(SaxUtils.createInputSource(content));
}
}

View File

@@ -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());
}
}

View File

@@ -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);

View File

@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<prefix:content xmlns:prefix='namespace'/>