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.soap.saaj;
18  
19  import java.util.Iterator;
20  import javax.xml.namespace.QName;
21  import javax.xml.soap.SOAPElement;
22  import javax.xml.soap.SOAPException;
23  import javax.xml.transform.Source;
24  
25  import org.springframework.util.Assert;
26  import org.springframework.ws.soap.SoapElement;
27  import org.springframework.ws.soap.saaj.support.SaajUtils;
28  
29  /**
30   * SAAJ-specific implementation of the <code>SoapElement</code> interface. Wraps a {@link javax.xml.soap.SOAPElement}.
31   *
32   * @author Arjen Poutsma
33   * @since 1.0.0
34   */
35  class SaajSoapElement<T extends SOAPElement> implements SoapElement {
36  
37      private final T element;
38  
39      private SaajImplementation implementation;
40  
41      SaajSoapElement(T element) {
42          Assert.notNull(element, "element must not be null");
43          this.element = element;
44      }
45  
46      public Source getSource() {
47          return getImplementation().getSource(element);
48      }
49  
50      public QName getName() {
51          return getImplementation().getName(element);
52      }
53  
54      public void addAttribute(QName name, String value) {
55          try {
56              getImplementation().addAttribute(element, name, value);
57          }
58          catch (SOAPException ex) {
59              throw new SaajSoapElementException(ex);
60          }
61      }
62  
63      public void removeAttribute(QName name) {
64          try {
65              getImplementation().removeAttribute(element, name);
66          }
67          catch (SOAPException ex) {
68              throw new SaajSoapElementException(ex);
69          }
70      }
71  
72      public String getAttributeValue(QName name) {
73          try {
74              return getImplementation().getAttributeValue(element, name);
75          }
76          catch (SOAPException ex) {
77              throw new SaajSoapElementException(ex);
78          }
79      }
80  
81      public Iterator<QName> getAllAttributes() {
82          return getImplementation().getAllAttributes(element);
83      }
84  
85      public void addNamespaceDeclaration(String prefix, String namespaceUri) {
86          try {
87              getImplementation().addNamespaceDeclaration(element, prefix, namespaceUri);
88          }
89          catch (SOAPException ex) {
90              throw new SaajSoapElementException(ex);
91          }
92      }
93  
94      protected final T getSaajElement() {
95          return element;
96      }
97  
98      protected final SaajImplementation getImplementation() {
99          if (implementation == null) {
100             if (SaajUtils.getSaajVersion(element) == SaajUtils.SAAJ_13) {
101                 implementation = Saaj13Implementation.getInstance();
102             }
103             else if (SaajUtils.getSaajVersion(element) == SaajUtils.SAAJ_12) {
104                 implementation = Saaj12Implementation.getInstance();
105             }
106             else if (SaajUtils.getSaajVersion(element) == SaajUtils.SAAJ_11) {
107                 implementation = Saaj11Implementation.getInstance();
108             }
109             else {
110                 throw new IllegalStateException("Could not find SAAJ on the classpath");
111             }
112         }
113         return implementation;
114     }
115 }