AXMLReader { public static void read(Reader in, final CAS cas) throws IOException, SAXException { XMLReader xmlReader = XMLReaderFactory.createXMLReader(); AXMLContentHandler contentHandler = new AXMLContentHandler(cas.getTypeSystem()); xmlReader.setContentHandler(contentHandler); InputSource inputSource = new InputSource(in); xmlReader.parse(inputSource); cas.setDocumentText(contentHandler.getText()); final Map<Annotation, FeatureStructure> mapped = Maps.newHashMap(); List<Runnable> delayedFeatureAssignments = Lists.newLinkedList(); for (Annotation _anno : contentHandler.getAnnotations()) { String typeName = _anno.getType(); Type type = cas.getTypeSystem().getType(typeName); if (type == null) { throw new IllegalStateException(String.format("Unknown type: %s", typeName)); } final AnnotationFS anno = cas.createAnnotation(type, _anno.getBegin(), _anno.getEnd()); for (String featName : _anno.getFeatureNames()) { final Feature feat = type.getFeatureByBaseName(featName); if (feat == null) throw new IllegalStateException(String.format( "%s does not have feature %s", type.getName(), featName)); if (feat.getRange().isPrimitive()) { String featValStr = _anno.getFeatureStringValue(featName); if (featValStr != null) { anno.setFeatureValueFromString(feat, featValStr); } } else { if (feat.getRange().isArray()) { final List<Annotation> srcFSes = _anno.getFeatureFSArrayValue(featName); delayedFeatureAssignments.add(new Runnable() { @Override public void run() { List<FeatureStructure> mappedFSes = Lists.transform(srcFSes, new Function<Annotation, FeatureStructure>() { @Override public FeatureStructure apply(Annotation srcFS) { FeatureStructure mappedFS = mapped.get(srcFS); if (mappedFS == null) throw new IllegalStateException(); return mappedFS; } }); anno.setFeatureValue(feat, FSCollectionFactory.createArrayFS(cas, mappedFSes)); } }); } else { final Annotation srcFS = _anno.getFeatureFSValue(featName); delayedFeatureAssignments.add(new Runnable() { @Override public void run() { FeatureStructure mappedFS = mapped.get(srcFS); if (mappedFS == null) throw new IllegalStateException(); anno.setFeatureValue(feat, mappedFS); } }); } } } cas.addFsToIndexes(anno); mapped.put(_anno, anno); } for (Runnable r : delayedFeatureAssignments) { r.run(); } } private  AXMLReader(); static void read(Reader in, final CAS cas); static void read(File file, CAS cas);  }
@Test public void readTest1Xml() throws ResourceInitializationException, IOException, SAXException, CASException { CAS cas = CasCreationUtils.createCas(inputTSD, null, null); JCas jCas = cas.getJCas(); AXMLReader.read(new File("test-data/test1.xml"), cas); String expectedText = readFileToString(new File("test-data/test1.txt"), "utf-8"); expectedText = expectedText.replaceAll("\r\n", "\n"); assertEquals(expectedText, cas.getDocumentText()); List<String> expectedPersons = asList( "Представители молодого поколения сицилийской мафии", "Современные «авторитеты» Cosa Nostra", "молодые мафиози", "мафиози из Палермо Доменико Палацотто", "он", "он", "Палацотто", "Представители органов правопорядка", "итальянских преступников", "Они", "членов мафии", "своих рядах", "воспитанной на соцсетях молодежи", "Ее члены" ); List<String> expectedPerMentionTypes = asList( "nominal", null, null, null, "pronoun", "pronoun", "named", null, null, null, null, null, null, null ); List<String> expectedOrgs = asList( "сицилийской мафии", "The Telegraph", "Cosa Nostra", "издание", "полиции", "органов правопорядка", "мафия", "Преступная организация", "она" ); List<String> expectedOrgCanonicals = asList( "сицилийская мафия", null, null, null, null, "органы правопорядка", null, null, null ); List<String> expectedGPEs = asList( "Палермо", "Сицилии" ); List<String> expectedTimes = asList( "в начале XIX века", "к началу XX века" ); Type perType = cas.getTypeSystem().getType(Person.class.getName()); Type orgType = cas.getTypeSystem().getType(Organization.class.getName()); assertEquals(expectedPersons, transform(newArrayList(select(jCas, Person.class)), coveredTextFunction())); assertEquals(expectedPerMentionTypes, transform( newArrayList(select(jCas, Person.class)), FSUtils.stringFeatureFunction(perType, "mentionType"))); assertEquals( expectedOrgs, transform(newArrayList(select(jCas, Organization.class)), coveredTextFunction())); assertEquals( expectedOrgCanonicals, transform( newArrayList(select(jCas, Organization.class)), FSUtils.stringFeatureFunction(orgType, "canonical"))); assertEquals(expectedGPEs, transform(newArrayList(select(jCas, GPE.class)), coveredTextFunction())); assertEquals(expectedTimes, transform(newArrayList(select(jCas, Time.class)), coveredTextFunction())); assertEquals(findByCoveredText(jCas, PersonName.class, "Доменико Палацотто"), findByCoveredText(jCas, Person.class, "мафиози из Палермо Доменико Палацотто").getName()); assertEquals(findByCoveredText(jCas, PersonName.class, "Палацотто"), findByCoveredText(jCas, Person.class, "Палацотто").getName()); assertEquals( Lists.newArrayList( findByCoveredText(jCas, Word.class, "молодые"), findByCoveredText(jCas, Word.class, "мафиози") ), FSCollectionFactory.create( findByCoveredText(jCas, Person.class, "молодые мафиози").getWords(), Word.class)); }