1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.ws.server.endpoint.support;
18
19 import javax.xml.namespace.QName;
20 import javax.xml.stream.XMLStreamConstants;
21 import javax.xml.stream.XMLStreamException;
22 import javax.xml.stream.XMLStreamReader;
23 import javax.xml.transform.Source;
24 import javax.xml.transform.Transformer;
25 import javax.xml.transform.TransformerException;
26 import javax.xml.transform.TransformerFactory;
27 import javax.xml.transform.dom.DOMResult;
28 import javax.xml.transform.dom.DOMSource;
29
30 import org.w3c.dom.Document;
31 import org.w3c.dom.Node;
32
33 import org.springframework.xml.namespace.QNameUtils;
34 import org.springframework.xml.transform.TraxUtils;
35
36
37
38
39
40
41
42 public abstract class PayloadRootUtils {
43
44 private PayloadRootUtils() {
45
46 }
47
48
49
50
51
52
53
54
55 public static QName getPayloadRootQName(Source source, TransformerFactory transformerFactory)
56 throws TransformerException, XMLStreamException {
57 if (source == null) {
58 return null;
59 }
60 else if (source instanceof DOMSource) {
61 DOMSource domSource = (DOMSource) source;
62 Node node = domSource.getNode();
63 if (node.getNodeType() == Node.ELEMENT_NODE) {
64 return QNameUtils.getQNameForNode(node);
65 }
66 else if (node.getNodeType() == Node.DOCUMENT_NODE) {
67 Document document = (Document) node;
68 return QNameUtils.getQNameForNode(document.getDocumentElement());
69 }
70 }
71 else if (TraxUtils.isStaxSource(source)) {
72 XMLStreamReader streamReader = TraxUtils.getXMLStreamReader(source);
73 if (streamReader != null) {
74 if (streamReader.getEventType() == XMLStreamConstants.START_DOCUMENT) {
75 streamReader.nextTag();
76 }
77 if (streamReader.getEventType() == XMLStreamConstants.START_ELEMENT ||
78 streamReader.getEventType() == XMLStreamConstants.END_ELEMENT) {
79 return streamReader.getName();
80 }
81 }
82 }
83
84 Transformer transformer = transformerFactory.newTransformer();
85 DOMResult domResult = new DOMResult();
86 transformer.transform(source, domResult);
87 Document document = (Document) domResult.getNode();
88 return QNameUtils.getQNameForNode(document.getDocumentElement());
89 }
90
91
92 }