private void doTestDeserializeAndReserialize(File typeSystemDescriptorFile) throws Exception {
// deserialize a complex CAS from XCAS
TypeSystemDescription typeSystemDescription = UIMAFramework.getXMLParser().parseTypeSystemDescription(
new XMLInputSource(typeSystemDescriptorFile));
CAS cas = CasCreationUtils.createCas(typeSystemDescription, new TypePriorities_impl(), indexes);
InputStream serCasStream = new FileInputStream(JUnitExtension.getFile("ExampleCas/cas.xml"));
XCASDeserializer deser = new XCASDeserializer(cas.getTypeSystem());
ContentHandler deserHandler = deser.getXCASHandler(cas);
SAXParserFactory fact = SAXParserFactory.newInstance();
SAXParser parser = fact.newSAXParser();
XMLReader xmlReader = parser.getXMLReader();
xmlReader.setContentHandler(deserHandler);
xmlReader.parse(new InputSource(serCasStream));
serCasStream.close();
// reserialize as XMI
String xml = serialize(cas, null);
System.out.println(xml);
// deserialize into another CAS
CAS cas2 = CasCreationUtils.createCas(typeSystemDescription, new TypePriorities_impl(), indexes);
XmiCasDeserializer deser2 = new XmiCasDeserializer(cas2.getTypeSystem());
ContentHandler deserHandler2 = deser2.getXmiCasHandler(cas2);
xmlReader.setContentHandler(deserHandler2);
xmlReader.parse(new InputSource(new StringReader(xml)));
// compare
assertEquals(cas.getAnnotationIndex().size(), cas2.getAnnotationIndex().size());
assertEquals(cas.getDocumentText(), cas2.getDocumentText());
CasComparer.assertEquals(cas,cas2);
// check that array refs are not null
Type entityType = cas2.getTypeSystem().getType("org.apache.uima.testTypeSystem.Entity");
Feature classesFeat = entityType.getFeatureByBaseName("classes");
Iterator iter = cas2.getIndexRepository().getIndex("testEntityIndex").iterator();
assertTrue(iter.hasNext());
while (iter.hasNext()) {
FeatureStructure fs = (FeatureStructure) iter.next();
StringArrayFS arrayFS = (StringArrayFS) fs.getFeatureValue(classesFeat);
assertNotNull(arrayFS);
for (int i = 0; i < arrayFS.size(); i++) {
assertNotNull(arrayFS.get(i));
}
}
Type annotArrayTestType = cas2.getTypeSystem().getType("org.apache.uima.testTypeSystem.AnnotationArrayTest");
Feature annotArrayFeat = annotArrayTestType.getFeatureByBaseName("arrayOfAnnotations");
Iterator iter2 = cas2.getAnnotationIndex(annotArrayTestType).iterator();
assertTrue(iter2.hasNext());
while (iter2.hasNext()) {
FeatureStructure fs = (FeatureStructure) iter2.next();
ArrayFS arrayFS = (ArrayFS) fs.getFeatureValue(annotArrayFeat);
assertNotNull(arrayFS);
for (int i = 0; i < arrayFS.size(); i++) {
assertNotNull(arrayFS.get(i));
}
}
// test that lenient mode does not report errors
CAS cas3 = CasCreationUtils.createCas(new TypeSystemDescription_impl(),
new TypePriorities_impl(), new FsIndexDescription[0]);
XmiCasDeserializer deser3 = new XmiCasDeserializer(cas3.getTypeSystem());
ContentHandler deserHandler3 = deser3.getXmiCasHandler(cas3, true);
xmlReader.setContentHandler(deserHandler3);
xmlReader.parse(new InputSource(new StringReader(xml)));
}