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 java.io.InputStream;
20 import java.io.Reader;
21 import javax.xml.namespace.QName;
22 import javax.xml.stream.XMLEventReader;
23 import javax.xml.stream.XMLStreamConstants;
24 import javax.xml.stream.XMLStreamException;
25 import javax.xml.stream.XMLStreamReader;
26 import javax.xml.stream.events.XMLEvent;
27 import javax.xml.transform.Source;
28 import javax.xml.transform.TransformerException;
29 import javax.xml.transform.TransformerFactory;
30 import javax.xml.transform.dom.DOMResult;
31
32 import org.springframework.xml.namespace.QNameUtils;
33 import org.springframework.xml.transform.TransformerHelper;
34 import org.springframework.xml.transform.TraxUtils;
35
36 import org.w3c.dom.Document;
37 import org.w3c.dom.Node;
38 import org.xml.sax.InputSource;
39 import org.xml.sax.XMLReader;
40
41
42
43
44
45
46
47 public abstract class PayloadRootUtils {
48
49 private PayloadRootUtils() {
50 }
51
52
53
54
55
56
57
58
59 public static QName getPayloadRootQName(Source source, TransformerFactory transformerFactory)
60 throws TransformerException {
61 return getPayloadRootQName(source, new TransformerHelper(transformerFactory));
62 }
63
64 public static QName getPayloadRootQName(Source source, TransformerHelper transformerHelper)
65 throws TransformerException {
66 if (source == null) {
67 return null;
68 }
69 try {
70 PayloadRootSourceCallback callback = new PayloadRootSourceCallback();
71 TraxUtils.doWithSource(source, callback);
72 if (callback.result != null) {
73 return callback.result;
74 }
75 else {
76
77 DOMResult domResult = new DOMResult();
78 transformerHelper.transform(source, domResult);
79 Document document = (Document) domResult.getNode();
80 return QNameUtils.getQNameForNode(document.getDocumentElement());
81 }
82 }
83 catch (TransformerException ex) {
84 throw ex;
85 }
86 catch (Exception ex) {
87 return null;
88 }
89 }
90
91 private static class PayloadRootSourceCallback implements TraxUtils.SourceCallback {
92
93 private QName result;
94
95 public void domSource(Node node) throws Exception {
96 if (node.getNodeType() == Node.ELEMENT_NODE) {
97 result = QNameUtils.getQNameForNode(node);
98 }
99 else if (node.getNodeType() == Node.DOCUMENT_NODE) {
100 Document document = (Document) node;
101 result = QNameUtils.getQNameForNode(document.getDocumentElement());
102 }
103 }
104
105 public void staxSource(XMLEventReader eventReader) throws Exception {
106 XMLEvent event = eventReader.peek();
107 if (event != null && event.isStartDocument()) {
108 event = eventReader.nextTag();
109 }
110 if (event != null) {
111 if (event.isStartElement()) {
112 result = event.asStartElement().getName();
113 }
114 else if (event.isEndElement()) {
115 result = event.asEndElement().getName();
116 }
117 }
118 }
119
120 public void staxSource(XMLStreamReader streamReader) throws Exception {
121 if (streamReader.getEventType() == XMLStreamConstants.START_DOCUMENT) {
122 try {
123 streamReader.nextTag();
124 }
125 catch (XMLStreamException ex) {
126 throw new IllegalStateException("Could not read next tag: " + ex.getMessage(), ex);
127 }
128 }
129 if (streamReader.getEventType() == XMLStreamConstants.START_ELEMENT ||
130 streamReader.getEventType() == XMLStreamConstants.END_ELEMENT) {
131 result = streamReader.getName();
132 }
133 }
134
135 public void saxSource(XMLReader reader, InputSource inputSource) throws Exception {
136
137 }
138
139 public void streamSource(InputStream inputStream) throws Exception {
140
141 }
142
143 public void streamSource(Reader reader) throws Exception {
144
145 }
146
147 public void source(String systemId) throws Exception {
148
149 }
150 }
151
152
153 }