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 XMLObjectHelper() { }
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70 public static <T extends XMLObject> T cloneXMLObject(T originalXMLObject)
71 throws MarshallingException, UnmarshallingException {
72 return cloneXMLObject(originalXMLObject, false);
73 }
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95 public static <T extends XMLObject> T cloneXMLObject(T originalXMLObject, boolean rootInNewDocument)
96 throws MarshallingException, UnmarshallingException {
97
98 if (originalXMLObject == null) {
99 return null;
100 }
101
102 Marshaller marshaller = Configuration.getMarshallerFactory().getMarshaller(originalXMLObject);
103 Element origElement = marshaller.marshall(originalXMLObject);
104
105 Element clonedElement = null;
106
107 if (rootInNewDocument) {
108 try {
109 Document newDocument = Configuration.getParserPool().newDocument();
110
111 clonedElement = (Element) newDocument.importNode(origElement, true);
112 newDocument.appendChild(clonedElement);
113 } catch (XMLParserException e) {
114 throw new XMLRuntimeException("Error obtaining new Document from parser pool", e);
115 }
116 } else {
117 clonedElement = (Element) origElement.cloneNode(true);
118 }
119
120 Unmarshaller unmarshaller = Configuration.getUnmarshallerFactory().getUnmarshaller(clonedElement);
121 T clonedXMLObject = (T) unmarshaller.unmarshall(clonedElement);
122
123 return clonedXMLObject;
124 }
125
126
127
128
129
130
131
132
133
134
135 public static XMLObject unmarshallFromInputStream(ParserPool parserPool, InputStream inputStream)
136 throws XMLParserException, UnmarshallingException {
137 Logger log = getLogger();
138 log.debug("Parsing InputStream into DOM document");
139
140 Document messageDoc = parserPool.parse(inputStream);
141 Element messageElem = messageDoc.getDocumentElement();
142
143 if (log.isTraceEnabled()) {
144 log.trace("Resultant DOM message was:");
145 log.trace(XMLHelper.nodeToString(messageElem));
146 }
147
148 log.debug("Unmarshalling DOM parsed from InputStream");
149 Unmarshaller unmarshaller = Configuration.getUnmarshallerFactory().getUnmarshaller(messageElem);
150 if (unmarshaller == null) {
151 log.error("Unable to unmarshall InputStream, no unmarshaller registered for element "
152 + XMLHelper.getNodeQName(messageElem));
153 throw new UnmarshallingException(
154 "Unable to unmarshall InputStream, no unmarshaller registered for element "
155 + XMLHelper.getNodeQName(messageElem));
156 }
157
158 XMLObject message = unmarshaller.unmarshall(messageElem);
159
160 log.debug("InputStream succesfully unmarshalled");
161 return message;
162 }
163
164
165
166
167
168
169
170
171
172
173 public static XMLObject unmarshallFromReader(ParserPool parserPool, Reader reader)
174 throws XMLParserException, UnmarshallingException {
175 Logger log = getLogger();
176 log.debug("Parsing Reader into DOM document");
177
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 Logger log = getLogger();
213 log.debug("Marshalling XMLObject");
214
215 if (xmlObject.getDOM() != null) {
216 log.debug("XMLObject already had cached DOM, returning that element");
217 return xmlObject.getDOM();
218 }
219
220 Marshaller marshaller = Configuration.getMarshallerFactory().getMarshaller(xmlObject);
221 if (marshaller == null) {
222 log.error("Unable to marshall XMLOBject, no marshaller registered for object: "
223 + xmlObject.getElementQName());
224 }
225
226 Element messageElem = marshaller.marshall(xmlObject);
227
228 if (log.isTraceEnabled()) {
229 log.trace("Marshalled XMLObject into DOM:");
230 log.trace(XMLHelper.nodeToString(messageElem));
231 }
232
233 return messageElem;
234 }
235
236
237
238
239
240
241
242
243 public static void marshallToOutputStream(XMLObject xmlObject, OutputStream outputStream)
244 throws MarshallingException {
245 Element element = marshall(xmlObject);
246 XMLHelper.writeNode(element, outputStream);
247 }
248
249
250
251
252
253
254
255
256 public static void marshallToWriter(XMLObject xmlObject, Writer writer) throws MarshallingException {
257 Element element = marshall(xmlObject);
258 XMLHelper.writeNode(element, writer);
259 }
260
261
262
263
264
265
266
267
268
269 public static String lookupNamespaceURI(XMLObject xmlObject, String prefix) {
270 XMLObject current = xmlObject;
271
272 while (current != null) {
273 for (Namespace ns : current.getNamespaces()) {
274 if (DatatypeHelper.safeEquals(ns.getNamespacePrefix(), prefix)) {
275 return ns.getNamespaceURI();
276 }
277 }
278 current = current.getParent();
279 }
280
281 return null;
282 }
283
284
285
286
287
288
289
290
291
292 public static String lookupNamespacePrefix(XMLObject xmlObject, String namespaceURI) {
293 XMLObject current = xmlObject;
294
295 while (current != null) {
296 for (Namespace ns : current.getNamespaces()) {
297 if (DatatypeHelper.safeEquals(ns.getNamespaceURI(), namespaceURI)) {
298 return ns.getNamespacePrefix();
299 }
300 }
301 current = current.getParent();
302 }
303
304 return null;
305 }
306
307
308
309
310
311
312 private static Logger getLogger() {
313 return LoggerFactory.getLogger(XMLObjectHelper.class);
314 }
315
316 }