1
1
/*
2
- * Copyright 2002-2015 the original author or authors.
2
+ * Copyright 2002-2016 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
41
41
42
42
import org .apache .commons .logging .Log ;
43
43
import org .apache .commons .logging .LogFactory ;
44
+ import org .w3c .dom .Document ;
44
45
import org .w3c .dom .Node ;
45
46
import org .xml .sax .ContentHandler ;
46
47
import org .xml .sax .EntityResolver ;
@@ -121,6 +122,28 @@ public boolean isProcessExternalEntities() {
121
122
}
122
123
123
124
125
+ /**
126
+ * Build a new {@link Document} from this marshaller's {@link DocumentBuilderFactory},
127
+ * as a placeholder for a DOM node.
128
+ * @see #createDocumentBuilderFactory()
129
+ * @see #createDocumentBuilder(DocumentBuilderFactory)
130
+ */
131
+ protected Document buildDocument () {
132
+ try {
133
+ DocumentBuilder documentBuilder ;
134
+ synchronized (this .documentBuilderFactoryMonitor ) {
135
+ if (this .documentBuilderFactory == null ) {
136
+ this .documentBuilderFactory = createDocumentBuilderFactory ();
137
+ }
138
+ documentBuilder = createDocumentBuilder (this .documentBuilderFactory );
139
+ }
140
+ return documentBuilder .newDocument ();
141
+ }
142
+ catch (ParserConfigurationException ex ) {
143
+ throw new UnmarshallingFailureException ("Could not create document placeholder: " + ex .getMessage (), ex );
144
+ }
145
+ }
146
+
124
147
/**
125
148
* Create a {@code DocumentBuilder} that this marshaller will use for creating
126
149
* DOM documents when passed an empty {@code DOMSource}.
@@ -226,19 +249,7 @@ else if (result instanceof StreamResult) {
226
249
*/
227
250
protected void marshalDomResult (Object graph , DOMResult domResult ) throws XmlMappingException {
228
251
if (domResult .getNode () == null ) {
229
- try {
230
- synchronized (this .documentBuilderFactoryMonitor ) {
231
- if (this .documentBuilderFactory == null ) {
232
- this .documentBuilderFactory = createDocumentBuilderFactory ();
233
- }
234
- }
235
- DocumentBuilder documentBuilder = createDocumentBuilder (this .documentBuilderFactory );
236
- domResult .setNode (documentBuilder .newDocument ());
237
- }
238
- catch (ParserConfigurationException ex ) {
239
- throw new UnmarshallingFailureException (
240
- "Could not create document placeholder for DOMResult: " + ex .getMessage (), ex );
241
- }
252
+ domResult .setNode (buildDocument ());
242
253
}
243
254
marshalDomNode (graph , domResult .getNode ());
244
255
}
@@ -249,7 +260,7 @@ protected void marshalDomResult(Object graph, DOMResult domResult) throws XmlMap
249
260
* {@code marshalXMLEventConsumer}, depending on what is contained in the
250
261
* {@code StaxResult}.
251
262
* @param graph the root of the object graph to marshal
252
- * @param staxResult a Spring {@link org.springframework.util.xml.StaxSource} or JAXP 1.4 {@link StAXSource}
263
+ * @param staxResult a JAXP 1.4 {@link StAXSource}
253
264
* @throws XmlMappingException if the given object cannot be marshalled to the result
254
265
* @throws IllegalArgumentException if the {@code domResult} is empty
255
266
* @see #marshalDomNode(Object, org.w3c.dom.Node)
@@ -358,19 +369,7 @@ else if (source instanceof StreamSource) {
358
369
*/
359
370
protected Object unmarshalDomSource (DOMSource domSource ) throws XmlMappingException {
360
371
if (domSource .getNode () == null ) {
361
- try {
362
- synchronized (this .documentBuilderFactoryMonitor ) {
363
- if (this .documentBuilderFactory == null ) {
364
- this .documentBuilderFactory = createDocumentBuilderFactory ();
365
- }
366
- }
367
- DocumentBuilder documentBuilder = createDocumentBuilder (this .documentBuilderFactory );
368
- domSource .setNode (documentBuilder .newDocument ());
369
- }
370
- catch (ParserConfigurationException ex ) {
371
- throw new UnmarshallingFailureException (
372
- "Could not create document placeholder for DOMSource: " + ex .getMessage (), ex );
373
- }
372
+ domSource .setNode (buildDocument ());
374
373
}
375
374
try {
376
375
return unmarshalDomNode (domSource .getNode ());
@@ -437,7 +436,7 @@ protected Object unmarshalSaxSource(SAXSource saxSource) throws XmlMappingExcept
437
436
if (!isSupportDtd ()) {
438
437
throw new UnmarshallingFailureException ("NPE while unmarshalling. " +
439
438
"This can happen on JDK 1.6 due to the presence of DTD " +
440
- "declarations, which are disabled." , ex );
439
+ "declarations, which are disabled." );
441
440
}
442
441
throw ex ;
443
442
}
0 commit comments