@@ -114,7 +114,7 @@ public DefaultSafeXmlParserFactory() {
114
114
115
115
@ Override
116
116
public DocumentBuilder createDocumentBuilderInstance (boolean namespaceAware , boolean ignoringComments ) {
117
- DocumentBuilderFactory factory = XmlUtil . getDocumentBuilderFactory ();
117
+ DocumentBuilderFactory factory = createDocumentBuilderFactory ();
118
118
configureSafeDocumentBuilderFactory (factory );
119
119
factory .setNamespaceAware (namespaceAware );
120
120
factory .setIgnoringComments (ignoringComments );
@@ -130,7 +130,7 @@ public DocumentBuilder createDocumentBuilderInstance(boolean namespaceAware, boo
130
130
131
131
@ Override
132
132
public XMLReader createXMLReaderInstance (boolean namespaceAware , boolean validating ) {
133
- SAXParserFactory factory = XmlUtil . createSAXParserFactory ();
133
+ SAXParserFactory factory = createSAXParserFactory ();
134
134
factory .setNamespaceAware (namespaceAware );
135
135
factory .setValidating (validating );
136
136
configureSafeSAXParserFactory (factory );
@@ -145,7 +145,30 @@ public XMLReader createXMLReaderInstance(boolean namespaceAware, boolean validat
145
145
return xmlReader ;
146
146
}
147
147
148
- private void configureSafeDocumentBuilderFactory (DocumentBuilderFactory factory ) {
148
+ /**
149
+ * Creates a document builder factory implementation.
150
+ *
151
+ * @return result of {@link DocumentBuilderFactory#newInstance()} call
152
+ */
153
+ protected DocumentBuilderFactory createDocumentBuilderFactory () {
154
+ return XmlUtil .getDocumentBuilderFactory ();
155
+ }
156
+
157
+ /**
158
+ * Creates a SAX parser factory implementation.
159
+ *
160
+ * @return result of {@link SAXParserFactory#newInstance()} call
161
+ */
162
+ protected SAXParserFactory createSAXParserFactory () {
163
+ return XmlUtil .createSAXParserFactory ();
164
+ }
165
+
166
+ /**
167
+ * Configures document builder factory to make it secure against xml attacks.
168
+ *
169
+ * @param factory {@link DocumentBuilderFactory} instance to be configured
170
+ */
171
+ protected void configureSafeDocumentBuilderFactory (DocumentBuilderFactory factory ) {
149
172
tryToSetFeature (factory , DISALLOW_DOCTYPE_DECL , true );
150
173
tryToSetFeature (factory , EXTERNAL_GENERAL_ENTITIES , false );
151
174
tryToSetFeature (factory , EXTERNAL_PARAMETER_ENTITIES , false );
@@ -155,7 +178,12 @@ private void configureSafeDocumentBuilderFactory(DocumentBuilderFactory factory)
155
178
factory .setExpandEntityReferences (false );
156
179
}
157
180
158
- private void configureSafeSAXParserFactory (SAXParserFactory factory ) {
181
+ /**
182
+ * Configures SAX parser factory to make it secure against xml attacks.
183
+ *
184
+ * @param factory {@link SAXParserFactory} instance to be configured
185
+ */
186
+ protected void configureSafeSAXParserFactory (SAXParserFactory factory ) {
159
187
tryToSetFeature (factory , DISALLOW_DOCTYPE_DECL , true );
160
188
tryToSetFeature (factory , EXTERNAL_GENERAL_ENTITIES , false );
161
189
tryToSetFeature (factory , EXTERNAL_PARAMETER_ENTITIES , false );
0 commit comments