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