Skip to content

Commit 812a0fa

Browse files
committed
Creation of DocumentBuilderFactory allowed to override.
1 parent 5d30d4b commit 812a0fa

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

xstream/src/java/com/thoughtworks/xstream/io/xml/DomDriver.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
public class DomDriver extends AbstractXmlDriver {
4040

4141
private final String encoding;
42-
private final DocumentBuilderFactory documentBuilderFactory;
42+
private DocumentBuilderFactory documentBuilderFactory;
4343

4444
/**
4545
* Construct a DomDriver.
@@ -62,12 +62,6 @@ public DomDriver(final String encoding) {
6262
public DomDriver(final String encoding, final NameCoder nameCoder) {
6363
super(nameCoder);
6464
this.encoding = encoding;
65-
documentBuilderFactory = DocumentBuilderFactory.newInstance();
66-
try {
67-
documentBuilderFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
68-
} catch (final ParserConfigurationException e) {
69-
throw new StreamException(e);
70-
}
7165
}
7266

7367
/**
@@ -101,6 +95,13 @@ public HierarchicalStreamReader createReader(final File in) {
10195

10296
private HierarchicalStreamReader createReader(final InputSource source) {
10397
try {
98+
if (documentBuilderFactory == null) {
99+
synchronized (this) {
100+
if (documentBuilderFactory == null) {
101+
documentBuilderFactory = createDocumentBuilderFactory();
102+
}
103+
}
104+
}
104105
final DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
105106
if (encoding != null) {
106107
source.setEncoding(encoding);
@@ -131,4 +132,20 @@ public HierarchicalStreamWriter createWriter(final OutputStream out) {
131132
throw new StreamException(e);
132133
}
133134
}
135+
136+
/**
137+
* Create the DocumentBuilderFactory instance.
138+
*
139+
* @return the new instance
140+
* @since upcoming
141+
*/
142+
protected DocumentBuilderFactory createDocumentBuilderFactory() {
143+
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
144+
try {
145+
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
146+
} catch (final ParserConfigurationException e) {
147+
throw new StreamException(e);
148+
}
149+
return factory;
150+
}
134151
}

0 commit comments

Comments
 (0)