View Javadoc

1   /*
2    * Copyright 2007 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
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.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   * Helper class for determining the root qualified name of a Web Service payload.
38   *
39   * @author Arjen Poutsma
40   * @since 1.0.0
41   */
42  public abstract class PayloadRootUtils {
43  
44      private PayloadRootUtils() {
45  
46      }
47  
48      /**
49       * Returns the root qualified name of the given source, transforming it if necessary.
50       *
51       * @param source             the source to get the root element from
52       * @param transformerFactory a transformer factory, necessary if the given source is not a <code>DOMSource</code>
53       * @return the root element, or <code>null</code> if <code>source</code> is <code>null</code>
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          // we have no other option than to transform
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  }