1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.ws.soap.axiom.support;
18
19 import java.io.ByteArrayInputStream;
20 import java.io.ByteArrayOutputStream;
21 import java.io.IOException;
22 import java.io.OutputStream;
23 import java.util.Iterator;
24 import java.util.Locale;
25 import javax.xml.namespace.QName;
26 import javax.xml.parsers.DocumentBuilderFactory;
27 import javax.xml.stream.XMLInputFactory;
28
29 import org.springframework.util.Assert;
30 import org.springframework.util.StringUtils;
31 import org.springframework.xml.namespace.QNameUtils;
32
33 import org.apache.axiom.om.OMContainer;
34 import org.apache.axiom.om.OMElement;
35 import org.apache.axiom.om.OMException;
36 import org.apache.axiom.om.OMNamespace;
37 import org.apache.axiom.soap.SOAPEnvelope;
38 import org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder;
39 import org.w3c.dom.DOMImplementation;
40 import org.w3c.dom.Document;
41 import org.w3c.dom.Element;
42 import org.w3c.dom.ls.DOMImplementationLS;
43 import org.w3c.dom.ls.LSOutput;
44 import org.w3c.dom.ls.LSSerializer;
45
46
47
48
49
50
51
52
53
54
55
56 @SuppressWarnings("Since15")
57 public abstract class AxiomUtils {
58
59
60
61
62
63
64
65
66
67
68
69 public static OMNamespace toNamespace(QName qName, OMElement resolveElement) throws OMException {
70 String prefix = QNameUtils.getPrefix(qName);
71 if (StringUtils.hasLength(qName.getNamespaceURI()) && StringUtils.hasLength(prefix)) {
72 return resolveElement.declareNamespace(qName.getNamespaceURI(), prefix);
73 }
74 else if (StringUtils.hasLength(qName.getNamespaceURI())) {
75
76 return resolveElement.declareNamespace(qName.getNamespaceURI(), "");
77 }
78 else {
79 throw new IllegalArgumentException("qName [" + qName + "] does not contain a namespace");
80 }
81 }
82
83
84
85
86
87
88
89 public static String toLanguage(Locale locale) {
90 return locale.toString().replace('_', '-');
91 }
92
93
94
95
96
97
98
99 public static Locale toLocale(String language) {
100 language = language.replace('-', '_');
101 return StringUtils.parseLocaleString(language);
102 }
103
104
105 public static void removeContents(OMContainer container) {
106 for (Iterator<?> iterator = container.getChildren(); iterator.hasNext();) {
107 iterator.next();
108 iterator.remove();
109 }
110 }
111
112
113
114
115
116
117
118
119
120 public static Document toDocument(SOAPEnvelope envelope) {
121 try {
122 if (envelope instanceof Element) {
123 return ((Element) envelope).getOwnerDocument();
124 }
125 else {
126 ByteArrayOutputStream bos = new ByteArrayOutputStream();
127 envelope.build();
128 envelope.serialize(bos);
129
130 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
131 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
132 documentBuilderFactory.setNamespaceAware(true);
133 return documentBuilderFactory.newDocumentBuilder().parse(bis);
134 }
135 }
136 catch (Exception ex) {
137 throw new IllegalArgumentException("Error in converting SOAP Envelope to Document", ex);
138 }
139 }
140
141
142
143
144
145
146
147
148
149 public static SOAPEnvelope toEnvelope(Document document) {
150 try {
151 DOMImplementation implementation = document.getImplementation();
152 Assert.isInstanceOf(DOMImplementationLS.class, implementation);
153
154 DOMImplementationLS loadSaveImplementation = (DOMImplementationLS) implementation;
155 LSOutput output = loadSaveImplementation.createLSOutput();
156 ByteArrayOutputStream bos = new ByteArrayOutputStream();
157 output.setByteStream(bos);
158
159 LSSerializer serializer = loadSaveImplementation.createLSSerializer();
160 serializer.write(document, output);
161
162 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
163
164 StAXSOAPModelBuilder stAXSOAPModelBuilder =
165 new StAXSOAPModelBuilder(XMLInputFactory.newInstance().createXMLStreamReader(bis), null);
166 SOAPEnvelope envelope = stAXSOAPModelBuilder.getSOAPEnvelope();
167
168
169 envelope.serialize(new NullOutputStream());
170
171 return envelope;
172 }
173 catch (Exception ex) {
174 IllegalArgumentException iaex =
175 new IllegalArgumentException("Error in converting Document to SOAP Envelope");
176 iaex.initCause(ex);
177 throw iaex;
178 }
179 }
180
181
182 private static class NullOutputStream extends OutputStream {
183
184 @Override
185 public void write(int b) throws IOException {
186 }
187
188 @Override
189 public void write(byte[] b) throws IOException {
190 }
191
192 @Override
193 public void write(byte[] b, int off, int len) throws IOException {
194 }
195 }
196
197 }