Refactored QNameUtils.getQNameForSource() into separate helper class to fix code tangle.
This commit is contained in:
@@ -17,19 +17,9 @@
|
||||
package org.springframework.xml.namespace;
|
||||
|
||||
import javax.xml.namespace.QName;
|
||||
import javax.xml.stream.XMLStreamConstants;
|
||||
import javax.xml.stream.XMLStreamReader;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerException;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.dom.DOMResult;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.xml.transform.StaxSource;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
/**
|
||||
@@ -128,44 +118,6 @@ public abstract class QNameUtils {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the root qualified name of the given source, transforming it if necessary.
|
||||
*
|
||||
* @param source the source to get the root element from
|
||||
* @param transformerFactory a transformer factory, necessary if the given source is not a <code>DOMSource</code>
|
||||
* @return the root element
|
||||
*/
|
||||
public static QName getQNameForSource(Source source, TransformerFactory transformerFactory)
|
||||
throws TransformerException {
|
||||
if (source instanceof DOMSource) {
|
||||
DOMSource domSource = (DOMSource) source;
|
||||
Node node = domSource.getNode();
|
||||
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||
return getQNameForNode(node);
|
||||
}
|
||||
else if (node.getNodeType() == Node.DOCUMENT_NODE) {
|
||||
Document document = (Document) node;
|
||||
return getQNameForNode(document.getDocumentElement());
|
||||
}
|
||||
}
|
||||
else if (source instanceof StaxSource) {
|
||||
StaxSource staxSource = (StaxSource) source;
|
||||
if (staxSource.getXMLStreamReader() != null) {
|
||||
XMLStreamReader streamReader = staxSource.getXMLStreamReader();
|
||||
if (streamReader.getEventType() == XMLStreamConstants.START_ELEMENT ||
|
||||
streamReader.getEventType() == XMLStreamConstants.END_ELEMENT) {
|
||||
return streamReader.getName();
|
||||
}
|
||||
}
|
||||
}
|
||||
// we have no other option than to transform
|
||||
Transformer transformer = transformerFactory.newTransformer();
|
||||
DOMResult domResult = new DOMResult();
|
||||
transformer.transform(source, domResult);
|
||||
Document document = (Document) domResult.getNode();
|
||||
return getQNameForNode(document.getDocumentElement());
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a <code>QName</code> to a qualified name, as used by DOM and SAX. The returned string has a format of
|
||||
* <code>prefix:localName</code> if the prefix is set, or just <code>localName</code> if not.
|
||||
|
||||
@@ -16,25 +16,14 @@
|
||||
|
||||
package org.springframework.xml.namespace;
|
||||
|
||||
import java.io.StringReader;
|
||||
import javax.xml.namespace.QName;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.stream.XMLInputFactory;
|
||||
import javax.xml.stream.XMLStreamConstants;
|
||||
import javax.xml.stream.XMLStreamReader;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.sax.SAXSource;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.xml.transform.StaxSource;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.xml.sax.InputSource;
|
||||
|
||||
public class QNameUtilsTest extends TestCase {
|
||||
|
||||
@@ -113,53 +102,5 @@ public class QNameUtilsTest extends TestCase {
|
||||
assertEquals("invalid localname", "localName", result.getLocalPart());
|
||||
}
|
||||
|
||||
public void testGetQNameForDomSource() throws Exception {
|
||||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder builder = factory.newDocumentBuilder();
|
||||
Document document = builder.newDocument();
|
||||
Element element = document.createElementNS("namespace", "prefix:localname");
|
||||
document.appendChild(element);
|
||||
Source source = new DOMSource(document);
|
||||
QName qName = QNameUtils.getQNameForSource(source, TransformerFactory.newInstance());
|
||||
assertNotNull("getQNameForNode returns null", qName);
|
||||
assertEquals("QName has invalid localname", "localname", qName.getLocalPart());
|
||||
assertEquals("Qname has invalid namespace", "namespace", qName.getNamespaceURI());
|
||||
assertEquals("Qname has invalid prefix", "prefix", qName.getPrefix());
|
||||
}
|
||||
|
||||
public void testGetQNameForStaxSource() throws Exception {
|
||||
String contents = "<prefix:localname xmlns:prefix='namespace'/>";
|
||||
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
|
||||
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(contents));
|
||||
while (streamReader.getEventType() != XMLStreamConstants.START_ELEMENT) {
|
||||
streamReader.next();
|
||||
}
|
||||
Source source = new StaxSource(streamReader);
|
||||
QName qName = QNameUtils.getQNameForSource(source, TransformerFactory.newInstance());
|
||||
assertNotNull("getQNameForNode returns null", qName);
|
||||
assertEquals("QName has invalid localname", "localname", qName.getLocalPart());
|
||||
assertEquals("Qname has invalid namespace", "namespace", qName.getNamespaceURI());
|
||||
assertEquals("Qname has invalid prefix", "prefix", qName.getPrefix());
|
||||
}
|
||||
|
||||
public void testGetQNameForStreamSource() throws Exception {
|
||||
String contents = "<prefix:localname xmlns:prefix='namespace'/>";
|
||||
Source source = new StreamSource(new StringReader(contents));
|
||||
QName qName = QNameUtils.getQNameForSource(source, TransformerFactory.newInstance());
|
||||
assertNotNull("getQNameForNode returns null", qName);
|
||||
assertEquals("QName has invalid localname", "localname", qName.getLocalPart());
|
||||
assertEquals("Qname has invalid namespace", "namespace", qName.getNamespaceURI());
|
||||
assertEquals("Qname has invalid prefix", "prefix", qName.getPrefix());
|
||||
}
|
||||
|
||||
public void testGetQNameForSaxSource() throws Exception {
|
||||
String contents = "<prefix:localname xmlns:prefix='namespace'/>";
|
||||
Source source = new SAXSource(new InputSource(new StringReader(contents)));
|
||||
QName qName = QNameUtils.getQNameForSource(source, TransformerFactory.newInstance());
|
||||
assertNotNull("getQNameForNode returns null", qName);
|
||||
assertEquals("QName has invalid localname", "localname", qName.getLocalPart());
|
||||
assertEquals("Qname has invalid namespace", "namespace", qName.getNamespaceURI());
|
||||
assertEquals("Qname has invalid prefix", "prefix", qName.getPrefix());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user