View Javadoc

1   /*
2    * Copyright 2002-2013 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.util.xml.StaxUtils;
34  import org.springframework.ws.soap.SoapElement;
35  
36  /**
37   * Axiom-specific version of [email protected] 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 StaxUtils.createCustomStaxSource(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              String namespaceUri = name.getNamespaceURI();
76              String prefix = name.getPrefix();
77  	        if (StringUtils.hasLength(namespaceUri) && !StringUtils.hasLength(prefix)) {
78  		        prefix = null;
79  	        }
80  	        OMNamespace namespace =
81  			        getAxiomFactory().createOMNamespace(namespaceUri, prefix);
82  	        OMAttribute attribute = getAxiomFactory().createOMAttribute(name.getLocalPart(), namespace, value);
83              getAxiomElement().addAttribute(attribute);
84          }
85          catch (OMException ex) {
86              throw new AxiomSoapElementException(ex);
87          }
88      }
89  
90      public void removeAttribute(QName name) {
91          try {
92              OMAttribute attribute = getAxiomElement().getAttribute(name);
93              if (attribute != null) {
94                  getAxiomElement().removeAttribute(attribute);
95              }
96          }
97          catch (OMException ex) {
98              throw new AxiomSoapElementException(ex);
99          }
100     }
101 
102     public final String getAttributeValue(QName name) {
103         try {
104             return getAxiomElement().getAttributeValue(name);
105         }
106         catch (OMException ex) {
107             throw new AxiomSoapElementException(ex);
108         }
109     }
110 
111     public final Iterator<QName> getAllAttributes() {
112         try {
113             List<QName> results = new ArrayList<QName>();
114             for (Iterator<?> iterator = getAxiomElement().getAllAttributes(); iterator.hasNext();) {
115                 OMAttribute attribute = (OMAttribute) iterator.next();
116                 results.add(attribute.getQName());
117             }
118             return results.iterator();
119 
120         }
121         catch (OMException ex) {
122             throw new AxiomSoapElementException(ex);
123         }
124     }
125 
126     public void addNamespaceDeclaration(String prefix, String namespaceUri) {
127         try {
128             if (StringUtils.hasLength(prefix)) {
129                 getAxiomElement().declareNamespace(namespaceUri, prefix);
130             }
131             else {
132                 getAxiomElement().declareDefaultNamespace(namespaceUri);
133             }
134         }
135         catch (OMException ex) {
136             throw new AxiomSoapElementException(ex);
137         }
138     }
139 
140     protected final OMElement getAxiomElement() {
141         return axiomElement;
142     }
143 
144     protected final SOAPFactory getAxiomFactory() {
145         return axiomFactory;
146     }
147 }