View Javadoc
1   /*
2    * Copyright 2005-2010 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 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   * Helper class for determining the root qualified name of a Web Service payload.
43   *
44   * @author Arjen Poutsma
45   * @since 1.0.0
46   */
47  public abstract class PayloadRootUtils {
48  
49      private PayloadRootUtils() {
50      }
51  
52      /**
53       * Returns the root qualified name of the given source, transforming it if necessary.
54       *
55       * @param source             the source to get the root element from
56       * @param transformerFactory a transformer factory, necessary if the given source is not a <code>DOMSource</code>
57       * @return the root element, or <code>null</code> if <code>source</code> is <code>null</code>
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                  // we have no other option than to transform
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             // Do nothing
137         }
138 
139         public void streamSource(InputStream inputStream) throws Exception {
140             // Do nothing
141         }
142 
143         public void streamSource(Reader reader) throws Exception {
144             // Do nothing
145         }
146 
147         public void source(String systemId) throws Exception {
148             // Do nothing
149         }
150     }
151 
152 
153 }