1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
38
39
40
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 }