MappingProvider extends CasConfigurableProviderBase<Map<String, String>> { @Override public void configure(CAS aCas) throws AnalysisEngineProcessException { typeSystem = aCas.getTypeSystem(); if ("false".equalsIgnoreCase(getOverride(MAPPING_ENABLED))) { notFound = true; return; } tagMappings = new HashMap<>(); for (Entry<String, HasResourceMetadata> imp : tagMappingImports.entrySet()) { String prefix = imp.getKey() + ".tag.map."; Properties props = imp.getValue().getResourceMetaData(); for (String key : props.stringPropertyNames()) { if (key.startsWith(prefix)) { String originalTag = key.substring(prefix.length()); String mappedTag = props.getProperty(key); tagMappings.put(originalTag, mappedTag); } } } try { notFound = false; super.configure(aCas); } catch (AnalysisEngineProcessException e) { if (getOverride(LOCATION) != null) { throw e; } notFound = true; } }  @Override void configure(CAS aCas); String getTag(String aTag); Type getTagType(String aTag); String getTagTypeName(String aTag); Set<String> getTags(); void addTagMappingImport(String aLayerPrefix, HasResourceMetadata aSource); static final String MAPPING_ENABLED; static final String BASE_TYPE; }
@Test public void testLanguageChange() throws Exception { MappingProvider mappingProvider = new MappingProvider(); mappingProvider.setDefault(MappingProvider.LOCATION, "src/test/resources/${language}.map"); CAS cas = CasCreationUtils.createCas(new TypeSystemDescription_impl(), null, null); cas.setDocumentLanguage("en"); mappingProvider.configure(cas); Map<String, String> enMap = mappingProvider.getResource(); assertEquals("en", enMap.get("value")); cas.setDocumentLanguage("de"); mappingProvider.configure(cas); Map<String, String> deMap = mappingProvider.getResource(); assertEquals("de", deMap.get("value")); }
@Test public void testDefaultVariantWithLanguageOverride() throws Exception { Properties defaultVariants = new Properties(); defaultVariants.setProperty("de", "variant1"); defaultVariants.setProperty("en", "variant2"); MappingProvider mappingProvider = new MappingProvider(); mappingProvider.setDefaultVariants(defaultVariants); mappingProvider.setDefault(MappingProvider.LOCATION, "${language}-${variant}.map"); CAS cas = CasCreationUtils.createCas(new TypeSystemDescription_impl(), null, null); cas.setDocumentLanguage("en"); mappingProvider.configure(cas); assertEquals("en-variant2.map", mappingProvider.getModelLocation()); mappingProvider.setOverride(MappingProvider.LANGUAGE, "de"); mappingProvider.configure(cas); assertEquals("de-variant1.map", mappingProvider.getModelLocation()); mappingProvider.setOverride(MappingProvider.LANGUAGE, "de"); mappingProvider.setOverride(MappingProvider.VARIANT, "variant3"); mappingProvider.configure(cas); assertEquals("de-variant3.map", mappingProvider.getModelLocation()); }
@Test public void testTagsetChange() throws Exception { CasConfigurableProviderBase<String> modelProvider = new CasConfigurableProviderBase<String>() { { setDefault(LOCATION, "src/test/resources/${language}.model"); } @Override protected String produceResource(URL aUrl) throws IOException { return aUrl.toString(); } }; MappingProvider mappingProvider = new MappingProvider(); mappingProvider.setDefault(MappingProvider.LOCATION, "src/test/resources/${language}-${tagset}.map"); mappingProvider.addImport("tagset", modelProvider); CAS cas = CasCreationUtils.createCas(new TypeSystemDescription_impl(), null, null); cas.setDocumentLanguage("en"); modelProvider.configure(cas); mappingProvider.configure(cas); Map<String, String> enMap = mappingProvider.getResource(); assertEquals("en", enMap.get("value")); assertEquals("mytags1", enMap.get("tagset")); cas.setDocumentLanguage("de"); modelProvider.configure(cas); mappingProvider.configure(cas); Map<String, String> deMap = mappingProvider.getResource(); assertEquals("de", deMap.get("value")); assertEquals("mytags2", deMap.get("tagset")); }
@Test public void testRedirectedModel() throws Exception { CasConfigurableProviderBase<String> modelProvider = new CasConfigurableProviderBase<String>() { { setDefault(LOCATION, "src/test/resources/${language}-redirect2.properties"); } @Override protected String produceResource(URL aUrl) throws IOException { return aUrl.toString(); } }; MappingProvider mappingProvider = new MappingProvider(); mappingProvider.setDefault(MappingProvider.LOCATION, "src/test/resources/${language}-${tagset}.map"); mappingProvider.addImport("tagset", modelProvider); CAS cas = CasCreationUtils.createCas(new TypeSystemDescription_impl(), null, null); cas.setDocumentLanguage("de"); modelProvider.configure(cas); mappingProvider.configure(cas); Map<String, String> deMap = mappingProvider.getResource(); assertEquals("de-override", deMap.get("value")); Properties meta = modelProvider.getResourceMetaData(); assertEquals("mytags1", meta.getProperty("tagset")); assertEquals("true", meta.getProperty("redirect")); assertEquals("true", meta.getProperty("redirect2")); }
@Test public void testRedirectedTagset() throws Exception { MappingProvider mappingProvider = new MappingProvider(); mappingProvider.setOverride(MappingProvider.LOCATION, "src/test/resources/${language}-${tagset}.map"); mappingProvider.setDefault("tagset", "redirect"); CAS cas = CasCreationUtils.createCas(new TypeSystemDescription_impl(), null, null); cas.setDocumentLanguage("de"); mappingProvider.configure(cas); Map<String, String> deMap = mappingProvider.getResource(); assertEquals("de", deMap.get("value")); Properties meta = mappingProvider.getResourceMetaData(); assertEquals("redirected", meta.getProperty("tagset")); }