XmiReader extends ResourceCollectionReaderBase { @Override public void getNext(CAS aCAS) throws IOException, CollectionException { if (nonNull(typeSystemFile)) { try { List<TypeSystemDescription> tsds = new ArrayList<>(); if (mergeTypeSystem) { tsds.add(TypeSystemUtil.typeSystem2TypeSystemDescription(aCAS.getTypeSystem())); } tsds.add(UIMAFramework.getXMLParser() .parseTypeSystemDescription(new XMLInputSource(typeSystemFile))); TypeSystemDescription merged = CasCreationUtils.mergeTypeSystems(tsds); CAS mergedCas = CasCreationUtils.createCas(merged, null, null, null); CASMgrSerializer casMgrSerializer = Serialization .serializeCASMgr((CASImpl) mergedCas); ((CASImpl) aCAS).getBinaryCasSerDes() .setupCasFromCasMgrSerializer(casMgrSerializer); } catch (InvalidXMLException | ResourceInitializationException e) { throw new IOException(e); } } Resource res = nextFile(); try (InputStream is = CompressionUtils.getInputStream(res.getLocation(), res.getInputStream())) { XmiCasDeserializer.deserialize(is, aCAS, lenient); } catch (SAXException e) { throw new IOException(e); } AnnotationFS docAnno = aCAS.getDocumentAnnotation(); if (docAnno.getType().getName().equals(DocumentMetaData.class.getName())) { if (overrideDocumentMetadata) { String language = getLanguage(); if (language == null) { language = aCAS.getDocumentLanguage(); } aCAS.removeFsFromIndexes(docAnno); initCas(aCAS, res); aCAS.setDocumentLanguage(language); } } else if (addDocumentMetadata) { initCas(aCAS, res); } }  @Override void getNext(CAS aCAS); static final String PARAM_LENIENT; static final String PARAM_ADD_DOCUMENT_METADATA; static final String PARAM_OVERRIDE_DOCUMENT_METADATA; static final String PARAM_MERGE_TYPE_SYSTEM; static final String PARAM_TYPE_SYSTEM_FILE; }
@Test public void testTypeSystemMerge() throws Exception { JCas jcas = JCasFactory.createJCas(); CollectionReader reader = createReader(XmiReader.class, XmiReader.PARAM_SOURCE_LOCATION, "src/test/resources/xmi/english.xmi", XmiReader.PARAM_TYPE_SYSTEM_FILE, "src/test/resources/ts/typesystem.xml", XmiReader.PARAM_MERGE_TYPE_SYSTEM, true); reader.getNext(jcas.getCas()); Type spanType = jcas.getTypeSystem().getType("de.tudarmstadt.ukp.dkpro.core.io.xmi.Span"); AnnotationFS span = jcas.getCas().createAnnotation(spanType, 0, 1); jcas.getCas().addFsToIndexes(span); }
@Test public void testNoTypeSystemMerge() throws Exception { JCas jcas = JCasFactory.createJCas(); CollectionReader reader = createReader(XmiReader.class, XmiReader.PARAM_SOURCE_LOCATION, "src/test/resources/xmi/english.xmi"); reader.getNext(jcas.getCas()); Type spanType = jcas.getTypeSystem().getType("de.tudarmstadt.ukp.dkpro.core.io.xmi.Span"); assertNull(spanType); }
@Test public void testTypeSystemReplace() throws Exception { JCas jcas = JCasFactory.createJCas(); CollectionReader reader = createReader(XmiReader.class, XmiReader.PARAM_SOURCE_LOCATION, "src/test/resources/xmi/english.xmi", XmiReader.PARAM_TYPE_SYSTEM_FILE, "src/test/resources/ts/typesystem.xml"); assertThatThrownBy(() -> { reader.getNext(jcas.getCas()); }).isInstanceOf(IOException.class).hasCauseInstanceOf(XCASParsingException.class); }