Refactored QNameUtils.getQNameForSource() into separate helper class to fix code tangle.

This commit is contained in:
Arjen Poutsma
2007-05-12 20:53:35 +00:00
parent 5c32714869
commit 022c271db9
8 changed files with 100 additions and 124 deletions

View File

@@ -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.