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.apache.axiom.om.OMContainer;
30 import org.apache.axiom.om.OMElement;
31 import org.apache.axiom.om.OMException;
32 import org.apache.axiom.om.OMNamespace;
33 import org.apache.axiom.om.OMNode;
34 import org.apache.axiom.soap.SOAPEnvelope;
35 import org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder;
36 import org.w3c.dom.DOMImplementation;
37 import org.w3c.dom.Document;
38 import org.w3c.dom.Element;
39 import org.w3c.dom.ls.DOMImplementationLS;
40 import org.w3c.dom.ls.LSOutput;
41 import org.w3c.dom.ls.LSSerializer;
42
43 import org.springframework.util.Assert;
44 import org.springframework.util.StringUtils;
45 import org.springframework.xml.namespace.QNameUtils;
46
47
48
49
50
51
52
53
54
55
56
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 OMNode child = (OMNode) iterator.next();
108 child.detach();
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 IllegalArgumentException iaex =
138 new IllegalArgumentException("Error in converting SOAP Envelope to Document");
139 iaex.initCause(ex);
140 throw iaex;
141 }
142 }
143
144
145
146
147
148
149
150
151
152 public static SOAPEnvelope toEnvelope(Document document) {
153 try {
154 DOMImplementation implementation = document.getImplementation();
155 Assert.isInstanceOf(DOMImplementationLS.class, implementation);
156
157 DOMImplementationLS loadSaveImplementation = (DOMImplementationLS) implementation;
158 LSOutput output = loadSaveImplementation.createLSOutput();
159 ByteArrayOutputStream bos = new ByteArrayOutputStream();
160 output.setByteStream(bos);
161
162 LSSerializer serializer = loadSaveImplementation.createLSSerializer();
163 serializer.write(document, output);
164
165 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
166
167 StAXSOAPModelBuilder stAXSOAPModelBuilder =
168 new StAXSOAPModelBuilder(XMLInputFactory.newInstance().createXMLStreamReader(bis), null);
169 SOAPEnvelope envelope = stAXSOAPModelBuilder.getSOAPEnvelope();
170
171
172 envelope.serialize(new NullOutputStream());
173
174 return envelope;
175 }
176 catch (Exception ex) {
177 IllegalArgumentException iaex =
178 new IllegalArgumentException("Error in converting Document to SOAP Envelope");
179 iaex.initCause(ex);
180 throw iaex;
181 }
182 }
183
184
185 private static class NullOutputStream extends OutputStream {
186
187 public void write(int b) throws IOException {
188 }
189
190 public void write(byte[] b) throws IOException {
191 }
192
193 public void write(byte[] b, int off, int len) throws IOException {
194 }
195 }
196
197 }