TigerXmlReader extends JCasResourceCollectionReader_ImplBase { @Override public void getNext(JCas aJCas) throws IOException, CollectionException { Resource res = nextFile(); initCas(aJCas, res); try { posMappingProvider.configure(aJCas.getCas()); } catch (AnalysisEngineProcessException e) { throw new IOException(e); } InputStream is = null; try { is = CompressionUtils.getInputStream(res.getLocation(), res.getInputStream()); XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance(); XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(is); JAXBContext context = JAXBContext.newInstance(Meta.class, AnnotationDecl.class, TigerSentence.class); Unmarshaller unmarshaller = context.createUnmarshaller(); JCasBuilder jb = new JCasBuilder(aJCas); XMLEvent e = null; while ((e = xmlEventReader.peek()) != null) { if (isStartElement(e, "s")) { TigerSentence sentence = unmarshaller .unmarshal(xmlEventReader, TigerSentence.class).getValue(); try { readSentence(jb, sentence); } catch (IllegalAnnotationStructureException ex) { if (ignoreIllegalSentences) { getLogger().warn("Unable to read sentence [" + sentence.id + "]: " + ex.getMessage()); } else { getLogger().error("Unable to read sentence [" + sentence.id + "]: " + ex.getMessage()); throw new CollectionException(ex); } } } else { xmlEventReader.next(); } } jb.close(); if (pennTreeEnabled) { for (ROOT root : select(aJCas, ROOT.class)) { PennTree pt = new PennTree(aJCas, root.getBegin(), root.getEnd()); PennTreeNode rootNode = PennTreeUtils.convertPennTree(root); pt.setPennTree(PennTreeUtils.toPennTree(rootNode)); pt.addToIndexes(); } } } catch (XMLStreamException ex1) { throw new IOException(ex1); } catch (JAXBException ex2) { throw new IOException(ex2); } finally { closeQuietly(is); } }  @Override void initialize(UimaContext aContext); @Override void getNext(JCas aJCas); static boolean isStartElement(XMLEvent aEvent, String aElement); static final String PARAM_MAPPING_ENABLED; static final String PARAM_POS_MAPPING_LOCATION; static final String PARAM_POS_TAG_SET; static final String PARAM_READ_PENN_TREE; static final String PARAM_IGNORE_ILLEGAL_SENTENCES; }
@Test public void test() throws Exception { CollectionReader reader = createReader(TigerXmlReader.class, TigerXmlReader.PARAM_SOURCE_LOCATION, "src/test/resources/", TigerXmlReader.PARAM_PATTERNS, "[+]tiger-sample.xml", TigerXmlReader.PARAM_LANGUAGE, "de", TigerXmlReader.PARAM_READ_PENN_TREE, true); JCas jcas = JCasFactory.createJCas(); reader.getNext(jcas.getCas()); String pennTree = "(VROOT ($( ``) (S (PN-SB (NE Ross) (NE Perot)) (VAFIN wäre) " + "(ADV vielleicht) (NP-PD (ART ein) (ADJA prächtiger) (NN Diktator))) ($( ''))"; AssertAnnotations.assertPennTree(pennTree, selectSingle(jcas, PennTree.class)); }