List of usage examples for javax.xml.stream XMLEventReader next
E next();
From source file:de.tudarmstadt.ukp.dkpro.core.io.tiger.TigerXmlReader.java
@Override public void getNext(JCas aJCas) throws IOException, CollectionException { Resource res = nextFile();//from ww w.j av a 2 s. co m initCas(aJCas, res); posMappingProvider.configure(aJCas.getCas()); 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")) { readSentence(jb, unmarshaller.unmarshal(xmlEventReader, TigerSentence.class).getValue()); } else { xmlEventReader.next(); } } jb.close(); // Can only do that after the builder is closed, otherwise the text is not yet set in // the // CAS and we get "null" for all token strings. 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); } }
From source file:com.marklogic.client.functionaltest.BasicJavaClientREST.java
/** * Convert XMLEventReader To String/*from www . ja v a 2 s . co m*/ * @param XMLEventReader * @return String * @throws XMLStreamException, TransformerException, IOException, ParserConfigurationException, SAXException */ public String convertXMLEventReaderToString(XMLEventReader fileRead) throws IOException, TransformerException, XMLStreamException { //BufferedReader br = (BufferedReader) fileRead; String readContent = ""; String line = null; while (fileRead.hasNext()) readContent = readContent + fileRead.next(); return readContent; }
From source file:org.alex73.osm.converters.bel.Convert.java
public static void main(String[] args) throws Exception { loadStreetNamesForHouses();/*from w ww.ja v a2s.c om*/ InputStream in = new BZip2CompressorInputStream( new BufferedInputStream(new FileInputStream("tmp/belarus-latest.osm.bz2"), BUFFER_SIZE)); // create xml event reader for input stream XMLEventFactory eventFactory = XMLEventFactory.newInstance(); XMLEvent newLine = eventFactory.createCharacters("\n"); XMLInputFactory xif = XMLInputFactory.newInstance(); XMLOutputFactory xof = XMLOutputFactory.newInstance(); XMLEventReader reader = xif.createXMLEventReader(in); XMLEventWriter wrCyr = xof.createXMLEventWriter( new BufferedOutputStream(new FileOutputStream("tmp/belarus-bel.osm"), BUFFER_SIZE)); XMLEventWriter wrInt = xof.createXMLEventWriter( new BufferedOutputStream(new FileOutputStream("tmp/belarus-intl.osm"), BUFFER_SIZE)); // initialize jaxb JAXBContext jaxbCtx = JAXBContext.newInstance(Node.class, Way.class, Relation.class); Unmarshaller um = jaxbCtx.createUnmarshaller(); Marshaller m = jaxbCtx.createMarshaller(); m.setProperty(Marshaller.JAXB_FRAGMENT, true); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); XMLEvent e = null; while ((e = reader.peek()) != null) { boolean processed = false; if (e.isStartElement()) { StartElement se = (StartElement) e; switch (se.getName().getLocalPart()) { case "way": Way way = um.unmarshal(reader, Way.class).getValue(); if (way.getId() == 25439425) { System.out.println(); } fixBel(way.getTag(), "name:be", "name"); String nameBeHouse = houseStreetBe.get(way.getId()); if (nameBeHouse != null) { setTag(way.getTag(), "addr:street", nameBeHouse); } m.marshal(way, wrCyr); fixInt(way.getTag()); m.marshal(way, wrInt); wrCyr.add(newLine); wrInt.add(newLine); processed = true; break; case "node": Node node = um.unmarshal(reader, Node.class).getValue(); fixBel(node.getTag(), "name:be", "name"); // fixBel(node.getTag(),"addr:street:be","addr:street"); m.marshal(node, wrCyr); fixInt(node.getTag()); m.marshal(node, wrInt); wrCyr.add(newLine); wrInt.add(newLine); processed = true; break; case "relation": Relation relation = um.unmarshal(reader, Relation.class).getValue(); fixBel(relation.getTag(), "name:be", "name"); // fixBel(relation.getTag(),"addr:street:be","addr:street"); m.marshal(relation, wrCyr); fixInt(relation.getTag()); m.marshal(relation, wrInt); wrCyr.add(newLine); wrInt.add(newLine); processed = true; break; } } if (!processed) { wrCyr.add(e); wrInt.add(e); } reader.next(); } wrCyr.flush(); wrCyr.close(); wrInt.flush(); wrInt.close(); System.out.println("UniqueTranslatedTags: " + uniqueTranslatedTags); }
From source file:org.dkpro.core.io.xces.XcesBasicXmlReader.java
@Override public void getNext(JCas aJCas) throws IOException, CollectionException { Resource res = nextFile();//from ww w . j av a 2 s . com initCas(aJCas, res); InputStream is = null; try { is = CompressionUtils.getInputStream(res.getLocation(), res.getInputStream()); XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance(); XMLEventReader xmlEventReaderBasic = xmlInputFactory.createXMLEventReader(is); //JAXB context for XCES body with basic type JAXBContext contextBasic = JAXBContext.newInstance(XcesBodyBasic.class); Unmarshaller unmarshallerBasic = contextBasic.createUnmarshaller(); unmarshallerBasic.setEventHandler(new ValidationEventHandler() { public boolean handleEvent(ValidationEvent event) { throw new RuntimeException(event.getMessage(), event.getLinkedException()); } }); JCasBuilder jb = new JCasBuilder(aJCas); XMLEvent eBasic = null; while ((eBasic = xmlEventReaderBasic.peek()) != null) { if (isStartElement(eBasic, "body")) { try { XcesBodyBasic parasBasic = (XcesBodyBasic) unmarshallerBasic .unmarshal(xmlEventReaderBasic, XcesBodyBasic.class).getValue(); readPara(jb, parasBasic); } catch (RuntimeException ex) { getLogger().warn("Input is not in basic xces format."); } } else { xmlEventReaderBasic.next(); } } jb.close(); } catch (XMLStreamException ex1) { throw new IOException(ex1); } catch (JAXBException e1) { throw new IOException(e1); } finally { closeQuietly(is); } }
From source file:org.dkpro.core.io.xces.XcesXmlReader.java
@Override public void getNext(JCas aJCas) throws IOException, CollectionException { Resource res = nextFile();//from w w w.j a v a2 s . c o m initCas(aJCas, res); InputStream is = null; try { is = CompressionUtils.getInputStream(res.getLocation(), res.getInputStream()); XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance(); XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(is); JAXBContext context = JAXBContext.newInstance(XcesBody.class); Unmarshaller unmarshaller = context.createUnmarshaller(); unmarshaller.setEventHandler(new ValidationEventHandler() { public boolean handleEvent(ValidationEvent event) { throw new RuntimeException(event.getMessage(), event.getLinkedException()); } }); JCasBuilder jb = new JCasBuilder(aJCas); XMLEvent e = null; while ((e = xmlEventReader.peek()) != null) { if (isStartElement(e, "body")) { try { XcesBody paras = (XcesBody) unmarshaller.unmarshal(xmlEventReader, XcesBody.class) .getValue(); readPara(jb, paras); } catch (RuntimeException ex) { System.out.println("Unable to parse XCES format: " + ex); } } else { xmlEventReader.next(); } } jb.close(); } catch (XMLStreamException ex1) { throw new IOException(ex1); } catch (JAXBException e1) { throw new IOException(e1); } finally { closeQuietly(is); } }
From source file:org.mcisb.subliminal.SubliminalUtils.java
/** * // w w w.ja v a 2 s.c om * @param elementName * @param is * @param onlyValues * @return Collection * @throws XMLStreamException * @throws UnsupportedEncodingException */ private static Collection<String> getElements(final String elementName, final InputStream is, final boolean onlyValues) throws XMLStreamException, UnsupportedEncodingException { final Collection<String> elements = new ArrayList<>(); final ByteArrayOutputStream os = new ByteArrayOutputStream(); final XMLEventReader reader = XMLInputFactory.newInstance() .createXMLEventReader(new InputStreamReader(is, Charset.defaultCharset().name())); final XMLEventWriter writer = XMLOutputFactory.newInstance() .createXMLEventWriter(new OutputStreamWriter(os, Charset.defaultCharset().name())); boolean read = false; String characters = null; while (reader.peek() != null) { final XMLEvent event = (XMLEvent) reader.next(); switch (event.getEventType()) { case XMLStreamConstants.START_DOCUMENT: case XMLStreamConstants.END_DOCUMENT: { // Ignore. break; } case XMLStreamConstants.START_ELEMENT: { read = read || elementName.equals(event.asStartElement().getName().getLocalPart()); if (read && !onlyValues) { writer.add(event); } break; } case XMLStreamConstants.ATTRIBUTE: { if (read && !onlyValues) { writer.add(event); } break; } case XMLStreamConstants.CHARACTERS: { if (read && !onlyValues) { writer.add(event); } characters = event.asCharacters().getData(); break; } case XMLStreamConstants.END_ELEMENT: { if (read && !onlyValues) { writer.add(event); } if (elementName.equals(event.asEndElement().getName().getLocalPart())) { writer.flush(); if (characters != null) { elements.add(characters); } os.reset(); read = false; } break; } default: { // Ignore break; } } } return elements; }