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.soap.axiom;
18  
19  import java.util.ArrayList;
20  import java.util.Iterator;
21  import java.util.List;
22  import javax.xml.namespace.QName;
23  import javax.xml.transform.Source;
24  
25  import org.apache.axiom.om.OMAttribute;
26  import org.apache.axiom.om.OMElement;
27  import org.apache.axiom.om.OMException;
28  import org.apache.axiom.om.OMNamespace;
29  import org.apache.axiom.soap.SOAPFactory;
30  
31  import org.springframework.util.Assert;
32  import org.springframework.ws.soap.SoapElement;
33  import org.springframework.xml.transform.StaxSource;
34  
35  /**
36   * Axiom-specific version of {@link SoapElement}.
37   *
38   * @author Arjen Poutsma
39   * @since 1.0.0
40   */
41  class AxiomSoapElement implements SoapElement {
42  
43      private final OMElement axiomElement;
44  
45      private final SOAPFactory axiomFactory;
46  
47      protected AxiomSoapElement(OMElement axiomElement, SOAPFactory axiomFactory) {
48          Assert.notNull(axiomElement, "axiomElement must not be null");
49          Assert.notNull(axiomFactory, "axiomFactory must not be null");
50          this.axiomElement = axiomElement;
51          this.axiomFactory = axiomFactory;
52      }
53  
54      public final QName getName() {
55          try {
56              return axiomElement.getQName();
57          }
58          catch (OMException ex) {
59              throw new AxiomSoapElementException(ex);
60          }
61      }
62  
63      public final Source getSource() {
64          try {
65              return new StaxSource(axiomElement.getXMLStreamReader());
66          }
67          catch (OMException ex) {
68              throw new AxiomSoapElementException(ex);
69          }
70      }
71  
72      public final void addAttribute(QName name, String value) {
73          try {
74              OMNamespace namespace = getAxiomFactory().createOMNamespace(name.getNamespaceURI(), name.getPrefix());
75              OMAttribute attribute = getAxiomFactory().createOMAttribute(name.getLocalPart(), namespace, value);
76              getAxiomElement().addAttribute(attribute);
77          }
78          catch (OMException ex) {
79              throw new AxiomSoapElementException(ex);
80          }
81      }
82  
83      public void removeAttribute(QName name) {
84          try {
85              OMAttribute attribute = getAxiomElement().getAttribute(name);
86              if (attribute != null) {
87                  getAxiomElement().removeAttribute(attribute);
88              }
89          }
90          catch (OMException ex) {
91              throw new AxiomSoapElementException(ex);
92          }
93      }
94  
95      public final String getAttributeValue(QName name) {
96          try {
97              return getAxiomElement().getAttributeValue(name);
98          }
99          catch (OMException ex) {
100             throw new AxiomSoapElementException(ex);
101         }
102     }
103 
104     public final Iterator getAllAttributes() {
105         try {
106             List results = new ArrayList();
107             for (Iterator iterator = getAxiomElement().getAllAttributes(); iterator.hasNext();) {
108                 OMAttribute attribute = (OMAttribute) iterator.next();
109                 results.add(attribute.getQName());
110             }
111             return results.iterator();
112 
113         }
114         catch (OMException ex) {
115             throw new AxiomSoapElementException(ex);
116         }
117     }
118 
119     protected final OMElement getAxiomElement() {
120         return axiomElement;
121     }
122 
123     protected final SOAPFactory getAxiomFactory() {
124         return axiomFactory;
125     }
126 }