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();
// get a v1.x version of the same CAS
serCasStream = new FileInputStream(JUnitExtension.getFile("ExampleCas/v1cas.xml"));
deser = new XCASDeserializer(v1cas.getTypeSystem());
deserHandler = deser.getXCASHandler(v1cas);
fact = SAXParserFactory.newInstance();
parser = fact.newSAXParser();
xmlReader = parser.getXMLReader();
xmlReader.setContentHandler(deserHandler);
xmlReader.parse(new InputSource(serCasStream));
serCasStream.close();
// compare
assertEquals(cas.getAnnotationIndex().size(), v1cas.getAnnotationIndex().size());
// now a v1.x version of a multiple Sofa CAS
v1cas.reset();
serCasStream = new FileInputStream(JUnitExtension.getFile("ExampleCas/v1MultiSofaCas.xml"));
deser = new XCASDeserializer(v1cas.getTypeSystem());
deserHandler = deser.getXCASHandler(v1cas);
fact = SAXParserFactory.newInstance();
parser = fact.newSAXParser();
xmlReader = parser.getXMLReader();
xmlReader.setContentHandler(deserHandler);
xmlReader.parse(new InputSource(serCasStream));
serCasStream.close();
// test it
assertTrue(v1cas.getDocumentText().equals("some text for the default text sofa."));
CAS engView = v1cas.getView("EnglishDocument");
assertTrue(engView.getDocumentText().equals("this beer is good"));
assertTrue(engView.getAnnotationIndex().size() == 5); // 4 annots plus documentAnnotation
CAS gerView = v1cas.getView("GermanDocument");
assertTrue(gerView.getDocumentText().equals("das bier ist gut"));
assertTrue(gerView.getAnnotationIndex().size() == 5); // 4 annots plus documentAnnotation
// reserialize
StringWriter sw = new StringWriter();
XMLSerializer xmlSer = new XMLSerializer(sw, false);
XCASSerializer xcasSer = new XCASSerializer(v1cas.getTypeSystem());
xcasSer.serialize(v1cas, xmlSer.getContentHandler(), true);
String xml = sw.getBuffer().toString();
// deserialize into another CAS
cas.reset();
XCASDeserializer deser2 = new XCASDeserializer(cas.getTypeSystem());
ContentHandler deserHandler2 = deser2.getXCASHandler(cas);
xmlReader.setContentHandler(deserHandler2);
xmlReader.parse(new InputSource(new StringReader(xml)));
// test it
assertTrue(v1cas.getDocumentText().equals("some text for the default text sofa."));
engView = cas.getView("EnglishDocument");
assertTrue(engView.getDocumentText().equals("this beer is good"));