1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.opensaml.xml.util;
18
19 import java.io.InputStream;
20 import java.io.OutputStream;
21 import java.io.Reader;
22 import java.io.Writer;
23
24 import org.opensaml.xml.Configuration;
25 import org.opensaml.xml.Namespace;
26 import org.opensaml.xml.XMLObject;
27 import org.opensaml.xml.XMLRuntimeException;
28 import org.opensaml.xml.io.Marshaller;
29 import org.opensaml.xml.io.MarshallingException;
30 import org.opensaml.xml.io.Unmarshaller;
31 import org.opensaml.xml.io.UnmarshallingException;
32 import org.opensaml.xml.parse.ParserPool;
33 import org.opensaml.xml.parse.XMLParserException;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.w3c.dom.Document;
37 import org.w3c.dom.Element;
38
39
40
41
42
43 public final class XMLObjectHelper {
44
45
46 private static final Logger LOG = LoggerFactory.getLogger(XMLObjectHelper.class);
47
48
49 private XMLObjectHelper() { }
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 public static <T extends XMLObject> T cloneXMLObject(T originalXMLObject)
74 throws MarshallingException, UnmarshallingException {
75 return cloneXMLObject(originalXMLObject, false);
76 }
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98 public static <T extends XMLObject> T cloneXMLObject(T originalXMLObject, boolean rootInNewDocument)
99 throws MarshallingException, UnmarshallingException {
100
101 if (originalXMLObject == null) {
102 return null;
103 }
104
105 Marshaller marshaller = Configuration.getMarshallerFactory().getMarshaller(originalXMLObject);
106 Element origElement = marshaller.marshall(originalXMLObject);
107
108 Element clonedElement = null;
109
110 if (rootInNewDocument) {
111 try {
112 Document newDocument = Configuration.getParserPool().newDocument();
113
114 clonedElement = (Element) newDocument.importNode(origElement, true);
115 newDocument.appendChild(clonedElement);
116 } catch (XMLParserException e) {
117 throw new XMLRuntimeException("Error obtaining new Document from parser pool", e);
118 }
119 } else {
120 clonedElement = (Element) origElement.cloneNode(true);
121 }
122
123 Unmarshaller unmarshaller = Configuration.getUnmarshallerFactory().getUnmarshaller(clonedElement);
124 T clonedXMLObject = (T) unmarshaller.unmarshall(clonedElement);
125
126 return clonedXMLObject;
127 }
128
129
130
131
132
133
134
135
136
137
138 public static XMLObject unmarshallFromInputStream(ParserPool parserPool, InputStream inputStream)
139 throws XMLParserException, UnmarshallingException {
140 LOG.debug("Parsing InputStream into DOM document");
141
142 Document messageDoc = parserPool.parse(inputStream);
143 Element messageElem = messageDoc.getDocumentElement();
144
145 if (LOG.isTraceEnabled()) {
146 LOG.trace("Resultant DOM message was:");
147 LOG.trace(XMLHelper.nodeToString(messageElem));
148 }
149
150 LOG.debug("Unmarshalling DOM parsed from InputStream");
151 Unmarshaller unmarshaller = Configuration.getUnmarshallerFactory().getUnmarshaller(messageElem);
152 if (unmarshaller == null) {
153 LOG.error("Unable to unmarshall InputStream, no unmarshaller registered for element "
154 + XMLHelper.getNodeQName(messageElem));
155 throw new UnmarshallingException(
156 "Unable to unmarshall InputStream, no unmarshaller registered for element "
157 + XMLHelper.getNodeQName(messageElem));
158 }
159
160 XMLObject message = unmarshaller.unmarshall(messageElem);
161
162 LOG.debug("InputStream succesfully unmarshalled");
163 return message;
164 }
165
166
167
168
169
170
171
172
173
174
175 public static XMLObject unmarshallFromReader(ParserPool parserPool, Reader reader)
176 throws XMLParserException, UnmarshallingException {
177 LOG.debug("Parsing Reader into DOM document");
178
179 Document messageDoc = parserPool.parse(reader);
180 Element messageElem = messageDoc.getDocumentElement();
181
182 if (LOG.isTraceEnabled()) {
183 LOG.trace("Resultant DOM message was:");
184 LOG.trace(XMLHelper.nodeToString(messageElem));
185 }
186
187 LOG.debug("Unmarshalling DOM parsed from Reader");
188 Unmarshaller unmarshaller = Configuration.getUnmarshallerFactory().getUnmarshaller(messageElem);
189 if (unmarshaller == null) {
190 LOG.error("Unable to unmarshall Reader, no unmarshaller registered for element "
191 + XMLHelper.getNodeQName(messageElem));
192 throw new UnmarshallingException(
193 "Unable to unmarshall Reader, no unmarshaller registered for element "
194 + XMLHelper.getNodeQName(messageElem));
195 }
196
197 XMLObject message = unmarshaller.unmarshall(messageElem);
198
199 LOG.debug("Reader succesfully unmarshalled");
200 return message;
201 }
202
203
204
205
206
207
208
209
210
211 public static Element marshall(XMLObject xmlObject) throws MarshallingException {
212 LOG.debug("Marshalling XMLObject");
213
214 if (xmlObject.getDOM() != null) {
215 LOG.debug("XMLObject already had cached DOM, returning that element");
216 return xmlObject.getDOM();
217 }
218
219 Marshaller marshaller = Configuration.getMarshallerFactory().getMarshaller(xmlObject);
220 if (marshaller == null) {
221 LOG.error("Unable to marshall XMLOBject, no marshaller registered for object: "
222 + xmlObject.getElementQName());
223 }
224
225 Element messageElem = marshaller.marshall(xmlObject);
226
227 if (LOG.isTraceEnabled()) {
228 LOG.trace("Marshalled XMLObject into DOM:");
229 LOG.trace(XMLHelper.nodeToString(messageElem));
230 }
231
232 return messageElem;
233 }
234
235
236
237
238
239
240
241
242 public static void marshallToOutputStream(XMLObject xmlObject, OutputStream outputStream)
243 throws MarshallingException {
244 Element element = marshall(xmlObject);
245 XMLHelper.writeNode(element, outputStream);
246 }
247
248
249
250
251
252
253
254
255 public static void marshallToWriter(XMLObject xmlObject, Writer writer) throws MarshallingException {
256 Element element = marshall(xmlObject);
257 XMLHelper.writeNode(element, writer);
258 }
259
260
261
262
263
264
265
266
267
268 public static String lookupNamespaceURI(XMLObject xmlObject, String prefix) {
269 XMLObject current = xmlObject;
270
271 while (current != null) {
272 for (Namespace ns : current.getNamespaces()) {
273 if (DatatypeHelper.safeEquals(ns.getNamespacePrefix(), prefix)) {
274 return ns.getNamespaceURI();
275 }
276 }
277 current = current.getParent();
278 }
279
280 return null;
281 }
282
283
284
285
286
287
288
289
290
291 public static String lookupNamespacePrefix(XMLObject xmlObject, String namespaceURI) {
292 XMLObject current = xmlObject;
293
294 while (current != null) {
295 for (Namespace ns : current.getNamespaces()) {
296 if (DatatypeHelper.safeEquals(ns.getNamespaceURI(), namespaceURI)) {
297 return ns.getNamespacePrefix();
298 }
299 }
300 current = current.getParent();
301 }
302
303 return null;
304 }
305
306 }