77import java .util .Locale ;
88import java .util .logging .Level ;
99import java .util .logging .Logger ;
10+ import javax .xml .parsers .DocumentBuilderFactory ;
1011import opennlp .tools .namefind .NameFinderME ;
1112import opennlp .tools .namefind .TokenNameFinderModel ;
1213import opennlp .tools .tokenize .TokenizerME ;
@@ -30,6 +31,7 @@ class Organizations {
3031 };
3132
3233 private static final Logger logger = Logger .getLogger (FirstNames .class .getCanonicalName ());
34+ private static final String JDK_DOCUMENT_BUILDER_FACTORY = "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl" ;
3335 private static List <NameFinderME > organizationNameFinders = new ArrayList <NameFinderME >();
3436 private static List <TokenizerME > tokenizers = new ArrayList <TokenizerME >();
3537
@@ -189,6 +191,18 @@ private void loadOrganizationModel(String modelFileName) throws IOException {
189191 InputStream fis = this .getClass ().getClassLoader ().getResourceAsStream (modelFileName );
190192 TokenNameFinderModel organizationModel = new TokenNameFinderModel (fis );
191193
192- organizationNameFinders .add (new NameFinderME (organizationModel ));
194+ String documentBuilderFactoryProperty = DocumentBuilderFactory .class .getName ();
195+ String previousDocumentBuilderFactory = System .getProperty (documentBuilderFactoryProperty );
196+ try {
197+ // OpenNLP 2.x sets JAXP security attributes that the external Xerces dependency does not recognize.
198+ System .setProperty (documentBuilderFactoryProperty , JDK_DOCUMENT_BUILDER_FACTORY );
199+ organizationNameFinders .add (new NameFinderME (organizationModel ));
200+ } finally {
201+ if (previousDocumentBuilderFactory == null ) {
202+ System .clearProperty (documentBuilderFactoryProperty );
203+ } else {
204+ System .setProperty (documentBuilderFactoryProperty , previousDocumentBuilderFactory );
205+ }
206+ }
193207 }
194208}
0 commit comments