01: /*
02: * Copyright 2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
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.XMLStreamReader;
22: import javax.xml.transform.Source;
23: import javax.xml.transform.Transformer;
24: import javax.xml.transform.TransformerException;
25: import javax.xml.transform.TransformerFactory;
26: import javax.xml.transform.dom.DOMResult;
27: import javax.xml.transform.dom.DOMSource;
28:
29: import org.springframework.xml.namespace.QNameUtils;
30: import org.springframework.xml.transform.StaxSource;
31: import org.w3c.dom.Document;
32: import org.w3c.dom.Node;
33:
34: /**
35: * Helper class for determining the root qualified name of a Web Service payload.
36: *
37: * @author Arjen Poutsma
38: * @since 1.0.0
39: */
40: public abstract class PayloadRootUtils {
41:
42: private PayloadRootUtils() {
43:
44: }
45:
46: /**
47: * Returns the root qualified name of the given source, transforming it if necessary.
48: *
49: * @param source the source to get the root element from
50: * @param transformerFactory a transformer factory, necessary if the given source is not a <code>DOMSource</code>
51: * @return the root element
52: */
53: public static QName getPayloadRootQName(Source source,
54: TransformerFactory transformerFactory)
55: throws TransformerException {
56: if (source instanceof DOMSource) {
57: DOMSource domSource = (DOMSource) source;
58: Node node = domSource.getNode();
59: if (node.getNodeType() == Node.ELEMENT_NODE) {
60: return QNameUtils.getQNameForNode(node);
61: } else if (node.getNodeType() == Node.DOCUMENT_NODE) {
62: Document document = (Document) node;
63: return QNameUtils.getQNameForNode(document
64: .getDocumentElement());
65: }
66: } else if (source instanceof StaxSource) {
67: StaxSource staxSource = (StaxSource) source;
68: if (staxSource.getXMLStreamReader() != null) {
69: XMLStreamReader streamReader = staxSource
70: .getXMLStreamReader();
71: if (streamReader.getEventType() == XMLStreamConstants.START_ELEMENT
72: || streamReader.getEventType() == XMLStreamConstants.END_ELEMENT) {
73: return streamReader.getName();
74: }
75: }
76: }
77: // we have no other option than to transform
78: Transformer transformer = transformerFactory.newTransformer();
79: DOMResult domResult = new DOMResult();
80: transformer.transform(source, domResult);
81: Document document = (Document) domResult.getNode();
82: return QNameUtils
83: .getQNameForNode(document.getDocumentElement());
84: }
85:
86: }
|