GeneratorFactory { public static AdaptiveFeatureGenerator create(InputStream xmlDescriptorIn, FeatureGeneratorResourceProvider resourceManager) throws IOException, InvalidFormatException { DocumentBuilderFactory documentBuilderFacoty = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder; try { documentBuilder = documentBuilderFacoty.newDocumentBuilder(); } catch (ParserConfigurationException e) { throw new IllegalStateException(e); } org.w3c.dom.Document xmlDescriptorDOM; try { xmlDescriptorDOM = documentBuilder.parse(xmlDescriptorIn); } catch (SAXException e) { throw new InvalidFormatException("Descriptor is not valid XML!", e); } Element generatorElement = xmlDescriptorDOM.getDocumentElement(); return createGenerator(generatorElement, resourceManager); }  static AdaptiveFeatureGenerator create(InputStream xmlDescriptorIn,
      FeatureGeneratorResourceProvider resourceManager);  }
@Test public void testCreationWihtSimpleDescriptor() throws Exception { InputStream generatorDescriptorIn = getClass().getResourceAsStream( "/opennlp/tools/util/featuregen/TestFeatureGeneratorConfig.xml"); Assert.assertNotNull(generatorDescriptorIn); Collection<String> expectedGenerators = new ArrayList<String>(); expectedGenerators.add(OutcomePriorFeatureGenerator.class.getName()); AggregatedFeatureGenerator aggregatedGenerator = (AggregatedFeatureGenerator) GeneratorFactory.create(generatorDescriptorIn, null); for (AdaptiveFeatureGenerator generator : aggregatedGenerator.getGenerators()) { expectedGenerators.remove(generator.getClass().getName()); } Assert.assertEquals(0, expectedGenerators.size()); }
@Test public void testCreationWithCustomGenerator() throws Exception { InputStream generatorDescriptorIn = getClass().getResourceAsStream( "/opennlp/tools/util/featuregen/CustomClassLoading.xml"); Assert.assertNotNull(generatorDescriptorIn); AggregatedFeatureGenerator aggregatedGenerator = (AggregatedFeatureGenerator) GeneratorFactory.create(generatorDescriptorIn, null); Collection<AdaptiveFeatureGenerator> embeddedGenerator = aggregatedGenerator.getGenerators(); Assert.assertEquals(1, embeddedGenerator.size()); for (AdaptiveFeatureGenerator generator : embeddedGenerator) { Assert.assertEquals(TokenFeatureGenerator.class.getName(), generator.getClass().getName()); } }
@Test(expected = IOException.class) public void testCreationWithUnkownElement() throws IOException { InputStream descIn = getClass().getResourceAsStream( "/opennlp/tools/util/featuregen/FeatureGeneratorConfigWithUnkownElement.xml"); try { GeneratorFactory.create(descIn, null); } finally { descIn.close(); } }