8
8
import com .fasterxml .jackson .dataformat .xml .ser .ToXmlGenerator ;
9
9
10
10
/**
11
- * {@link com.fasterxml.jackson.core.TSFBuilder}
12
- * implementation for constructing {@link XmlFactory}
13
- * instances.
14
- *
15
- * @since 3.0
11
+ * {@link com.fasterxml.jackson.core.TSFBuilder} implementation
12
+ * for constructing {@link XmlFactory} instances.
16
13
*/
17
14
public class XmlFactoryBuilder extends TSFBuilder <XmlFactory , XmlFactoryBuilder >
18
15
{
@@ -51,11 +48,21 @@ public class XmlFactoryBuilder extends TSFBuilder<XmlFactory, XmlFactoryBuilder>
51
48
*<p>
52
49
* Name used for pseudo-property used for returning XML Text value (which does
53
50
* not have actual element name to use). Defaults to empty String, but
54
- * may be changed for inter-operability reasons: JAXB, for example, uses
51
+ * may be changed for interoperability reasons: JAXB, for example, uses
55
52
* "value" as name.
56
53
*/
57
54
protected String _nameForTextElement ;
58
55
56
+ /**
57
+ * Optional {@link ClassLoader} to use for constructing
58
+ * {@link XMLInputFactory} and {@kink XMLOutputFactory} instances if
59
+ * not explicitly specified by caller. If not specified, will
60
+ * default to {@link ClassLoader} that loaded this class.
61
+ *
62
+ * @since 2.13
63
+ */
64
+ protected ClassLoader _classLoaderForStax ;
65
+
59
66
/*
60
67
/**********************************************************
61
68
/* Life cycle
@@ -65,6 +72,7 @@ public class XmlFactoryBuilder extends TSFBuilder<XmlFactory, XmlFactoryBuilder>
65
72
protected XmlFactoryBuilder () {
66
73
_formatParserFeatures = XmlFactory .DEFAULT_XML_PARSER_FEATURE_FLAGS ;
67
74
_formatGeneratorFeatures = XmlFactory .DEFAULT_XML_GENERATOR_FEATURE_FLAGS ;
75
+ _classLoaderForStax = null ;
68
76
}
69
77
70
78
public XmlFactoryBuilder (XmlFactory base ) {
@@ -74,6 +82,7 @@ public XmlFactoryBuilder(XmlFactory base) {
74
82
_xmlInputFactory = base ._xmlInputFactory ;
75
83
_xmlOutputFactory = base ._xmlOutputFactory ;
76
84
_nameForTextElement = base ._cfgNameForTextElement ;
85
+ _classLoaderForStax = null ;
77
86
}
78
87
79
88
// // // Accessors
@@ -90,8 +99,11 @@ public XMLInputFactory xmlInputFactory() {
90
99
return _xmlInputFactory ;
91
100
}
92
101
93
- protected static XMLInputFactory defaultInputFactory () {
94
- XMLInputFactory xmlIn = XMLInputFactory .newInstance ();
102
+ protected XMLInputFactory defaultInputFactory () {
103
+ // 05-Jul-2021, tatu: as per [dataformat-xml#483], consider ClassLoader
104
+ XMLInputFactory xmlIn = XMLInputFactory .newFactory (XMLInputFactory .class .getName (),
105
+ staxClassLoader ());
106
+
95
107
// as per [dataformat-xml#190], disable external entity expansion by default
96
108
xmlIn .setProperty (XMLInputFactory .IS_SUPPORTING_EXTERNAL_ENTITIES , Boolean .FALSE );
97
109
// and ditto wrt [dataformat-xml#211], SUPPORT_DTD
@@ -106,13 +118,21 @@ public XMLOutputFactory xmlOutputFactory() {
106
118
return _xmlOutputFactory ;
107
119
}
108
120
109
- protected static XMLOutputFactory defaultOutputFactory () {
110
- XMLOutputFactory xmlOut = XMLOutputFactory .newInstance ();
121
+ protected XMLOutputFactory defaultOutputFactory () {
122
+ // 05-Jul-2021, tatu: as per [dataformat-xml#483], consider ClassLoader
123
+ XMLOutputFactory xmlOut = XMLOutputFactory .newFactory (XMLOutputFactory .class .getName (),
124
+ staxClassLoader ());
111
125
// [dataformat-xml#326]: Better ensure namespaces get built properly:
112
126
xmlOut .setProperty (XMLOutputFactory .IS_REPAIRING_NAMESPACES , Boolean .TRUE );
113
127
return xmlOut ;
114
128
}
115
129
130
+ // @since 2.13
131
+ protected ClassLoader staxClassLoader () {
132
+ return (_classLoaderForStax == null ) ?
133
+ getClass ().getClassLoader () : _classLoaderForStax ;
134
+ }
135
+
116
136
// // // Parser features
117
137
118
138
public XmlFactoryBuilder enable (FromXmlParser .Feature f ) {
@@ -217,11 +237,27 @@ public XmlFactoryBuilder outputFactory(XMLOutputFactory xmlOut) {
217
237
return xmlOutputFactory (xmlOut );
218
238
}
219
239
240
+ /**
241
+ * Method that can be used to specific {@link ClassLoader} for creating
242
+ * {@link XMLInputFactory} and {@link XMLOutputFactory} instances if
243
+ * those are not explicitly defined by caller: passed to respective
244
+ * {@code newFactory()} methods.
245
+ *<br>
246
+ * NOTE: recommended approach is to explicitly pass {@link XMLInputFactory}
247
+ * and {@link XMLOutputFactory} methods instead of relying on JDK SPI
248
+ * mechanism.
249
+ *
250
+ * @since 2.13
251
+ */
252
+ public XmlFactoryBuilder staxClassLoader (ClassLoader cl ) {
253
+ _classLoaderForStax = cl ;
254
+ return _this ();
255
+ }
256
+
220
257
// // // Actual construction
221
258
222
259
@ Override
223
260
public XmlFactory build () {
224
- // 28-Dec-2017, tatu: No special settings beyond base class ones, so:
225
261
return new XmlFactory (this );
226
262
}
227
263
}
0 commit comments