List of usage examples for javax.xml.stream XMLEventReader peek
public XMLEvent peek() throws XMLStreamException;
From source file:org.apache.olingo.commons.core.serialization.AtomDeserializer.java
private ODataPropertyType guessPropertyType(final XMLEventReader reader, final EdmTypeInfo typeInfo) throws XMLStreamException { XMLEvent child = null;/* w ww. j a v a 2 s . c o m*/ while (reader.hasNext() && child == null) { final XMLEvent event = reader.peek(); if (event.isCharacters() && event.asCharacters().isWhiteSpace()) { reader.nextEvent(); } else { child = event; } } final ODataPropertyType type; if (child == null) { type = typeInfo == null || typeInfo.isPrimitiveType() ? ODataPropertyType.PRIMITIVE : ODataPropertyType.ENUM; } else { if (child.isStartElement()) { if (Constants.NS_GML.equals(child.asStartElement().getName().getNamespaceURI())) { type = ODataPropertyType.PRIMITIVE; } else if (elementQName.equals(child.asStartElement().getName())) { type = ODataPropertyType.COLLECTION; } else { type = ODataPropertyType.COMPLEX; } } else if (child.isCharacters()) { type = typeInfo == null || typeInfo.isPrimitiveType() ? ODataPropertyType.PRIMITIVE : ODataPropertyType.ENUM; } else { type = ODataPropertyType.EMPTY; } } return type; }
From source file:org.apache.olingo.commons.core.serialization.AtomDeserializer.java
private void inline(final XMLEventReader reader, final StartElement start, final LinkImpl link) throws XMLStreamException, EdmPrimitiveTypeException { boolean foundEndElement = false; while (reader.hasNext() && !foundEndElement) { final XMLEvent event = reader.nextEvent(); if (event.isStartElement()) { if (inlineQName.equals(event.asStartElement().getName())) { StartElement inline = null; while (reader.hasNext() && inline == null) { final XMLEvent innerEvent = reader.peek(); if (innerEvent.isCharacters() && innerEvent.asCharacters().isWhiteSpace()) { reader.nextEvent(); } else if (innerEvent.isStartElement()) { inline = innerEvent.asStartElement(); }//from w w w .ja va 2s .c o m } if (inline != null) { if (Constants.QNAME_ATOM_ELEM_ENTRY.equals(inline.getName())) { link.setInlineEntity(entity(reader, inline)); } if (Constants.QNAME_ATOM_ELEM_FEED.equals(inline.getName())) { link.setInlineEntitySet(entitySet(reader, inline)); } } } else if (annotationQName.equals(event.asStartElement().getName())) { link.getAnnotations().add(annotation(reader, event.asStartElement())); } } if (event.isEndElement() && start.getName().equals(event.asEndElement().getName())) { foundEndElement = true; } } }
From source file:org.codice.ddf.spatial.ogc.wfs.transformer.handlebars.HandlebarsWfsFeatureTransformer.java
private boolean processStartElement(XMLEventReader xmlEventReader, StartElement startElement, Map<String, String> namespaces, Map<String, String> contextMap, boolean featureTypeFound) throws XMLStreamException { mapNamespaces(startElement, namespaces); if (!featureTypeFound) { if (canHandleFeatureType(startElement)) { String id = getIdAttributeValue(startElement, namespaces, getNamespaceAlias(GML_NAMESPACE, namespaces)); contextMap.put(METACARD_ID, id); return true; }//from w w w. j a va 2s.co m } else { XMLEvent eventPeek = xmlEventReader.peek(); if (eventPeek.isStartElement() && isGmlElement(eventPeek.asStartElement(), namespaces)) { readGmlData(xmlEventReader, startElement.getName().getLocalPart(), contextMap); } } return false; }
From source file:org.codice.ddf.spatial.ogc.wfs.transformer.handlebars.HandlebarsWfsFeatureTransformer.java
private void readGmlData(XMLEventReader xmlEventReader, String elementName, Map<String, String> contextMap) throws XMLStreamException { if (!xmlEventReader.peek().isStartElement()) { LOGGER.debug("Problem reading gml data for element: {}. Invalid xml element provided.", elementName); return;//from ww w . ja v a2s . c o m } StringWriter stringWriter = new StringWriter(); XMLEventWriter eventWriter = XML_OUTPUT_FACTORY.createXMLEventWriter(stringWriter); if (eventWriter == null) { LOGGER.debug("Problem reading gml data for element: {}. Event writer is null", elementName); return; } int count = 0; boolean addEvent = true; try { while (addEvent) { XMLEvent xmlEvent = xmlEventReader.nextEvent(); // populate the start element with the namespaces if (xmlEvent.isStartElement()) { xmlEvent = addNamespacesToStartElement(xmlEvent.asStartElement()); count++; } else if (xmlEvent.isEndElement()) { if (count == 0) { addEvent = false; eventWriter.flush(); LOGGER.debug("String writer: {}", stringWriter); contextMap.put(elementName, stringWriter.toString()); } count--; } if (addEvent) { eventWriter.add(xmlEvent); } } } finally { eventWriter.close(); } }
From source file:org.dataconservancy.model.builder.WrappingEventReaderTest.java
@Test public void testPeekWithDefaultEventReader() throws Exception { // The input XML is: // <File><doc> </doc></File> final XMLEventReader reader = readerFactory .createXMLEventReader(IOUtils.toInputStream(SINGLE_ELEMENT_DOC_WHITESPACE_1)); // START_DOCUMENT assertEquals(START_DOCUMENT, reader.peek().getEventType()); reader.nextEvent();//ww w .j a v a 2 s . c o m commonPeekFileTest(reader); // END_DOCUMENT assertEquals(END_DOCUMENT, reader.peek().getEventType()); commonEndDocumentTest(reader); }
From source file:org.dataconservancy.model.builder.WrappingEventReaderTest.java
@Test public void testPeekWithWrapperEventReader() throws Exception { // The input XML is: // <File><doc> </doc></File> // But the wrapper should wrap it to: // <dcp xmlns="http://dataconservancy.org/schemas/dcm/1.0"><Files><File><doc> </doc></File></Files></dcp> final XMLEventReader reader = new WrappingEventReader( readerFactory.createXMLEventReader(IOUtils.toInputStream(SINGLE_ELEMENT_DOC_WHITESPACE_1))); // START_DOCUMENT assertEquals(START_DOCUMENT, reader.peek().getEventType()); // idempotent assertEquals(START_DOCUMENT, reader.peek().getEventType()); assertEquals(START_DOCUMENT, reader.nextEvent().getEventType()); // <dcp> assertEquals(START_ELEMENT, reader.peek().getEventType()); // idempotent assertEquals(START_ELEMENT, reader.peek().getEventType()); assertEquals(START_ELEMENT, reader.nextEvent().getEventType()); // <Files> assertEquals(START_ELEMENT, reader.peek().getEventType()); // idempotent assertEquals(START_ELEMENT, reader.peek().getEventType()); assertEquals(START_ELEMENT, reader.nextEvent().getEventType()); commonPeekFileTest(reader);/*from w w w . j av a 2 s .co m*/ // </Files> assertEquals(END_ELEMENT, reader.peek().getEventType()); // idempotent assertEquals(END_ELEMENT, reader.peek().getEventType()); assertEquals(END_ELEMENT, reader.nextEvent().getEventType()); // </dcp> assertEquals(END_ELEMENT, reader.peek().getEventType()); // idempotent assertEquals(END_ELEMENT, reader.peek().getEventType()); assertEquals(END_ELEMENT, reader.nextEvent().getEventType()); // END_DOCUMENT assertEquals(END_DOCUMENT, reader.peek().getEventType()); // idempotent assertEquals(END_DOCUMENT, reader.peek().getEventType()); commonEndDocumentTest(reader); }
From source file:org.dataconservancy.model.builder.WrappingEventReaderTest.java
private void commonPeekFileTest(XMLEventReader reader) throws XMLStreamException { // The input XML is: // <File><doc> </doc></File> // <File> XMLEvent peekEvent = reader.peek(); assertEquals(START_ELEMENT, peekEvent.getEventType()); assertEquals(E_FILE, peekEvent.asStartElement().getName().getLocalPart()); // idempotent assertEquals(peekEvent, reader.peek()); XMLEvent actualEvent = reader.nextEvent(); assertEquals(peekEvent, actualEvent); // <doc> peekEvent = reader.peek();//w w w . j av a 2 s. c o m assertEquals(START_ELEMENT, peekEvent.getEventType()); assertEquals("doc", peekEvent.asStartElement().getName().getLocalPart()); // idempotent assertEquals(peekEvent, reader.peek()); actualEvent = reader.nextEvent(); assertEquals(peekEvent, actualEvent); peekEvent = reader.peek(); assertEquals(CHARACTERS, peekEvent.getEventType()); // idempotent assertEquals(peekEvent, reader.peek()); actualEvent = reader.nextEvent(); assertEquals(peekEvent, actualEvent); // </doc> peekEvent = reader.peek(); assertEquals(END_ELEMENT, peekEvent.getEventType()); assertEquals("doc", peekEvent.asEndElement().getName().getLocalPart()); // idempotent assertEquals(peekEvent, reader.peek()); actualEvent = reader.nextEvent(); assertEquals(peekEvent, actualEvent); // </File> peekEvent = reader.peek(); assertEquals(END_ELEMENT, peekEvent.getEventType()); assertEquals(E_FILE, peekEvent.asEndElement().getName().getLocalPart()); // idempotent assertEquals(peekEvent, reader.peek()); actualEvent = reader.nextEvent(); // fails, bc we are preserving namespaces? assertEquals(peekEvent, actualEvent); // do this instead assertEquals(peekEvent.asEndElement().getName().getLocalPart(), actualEvent.asEndElement().getName().getLocalPart()); }
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 a va 2 s . c o m 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();/* www .j a v a 2 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.emonocot.job.io.StaxEventItemReader.java
/** * Responsible for moving the cursor before the StartElement of the fragment * root.//from w ww . java 2 s . c o m * * This implementation simply looks for the next corresponding element, it * does not care about element nesting. You will need to override this * method to correctly handle composite fragments. * @param xmlEventReader Set the XML event reader * @return <code>true</code> if next fragment was found, <code>false</code> * otherwise. */ protected final boolean moveCursorToNextFragment(final XMLEventReader xmlEventReader) { try { while (true) { while (xmlEventReader.peek() != null && !xmlEventReader.peek().isStartElement()) { xmlEventReader.nextEvent(); } if (xmlEventReader.peek() == null) { return false; } QName startElementName = ((StartElement) xmlEventReader.peek()).getName(); if (startElementName.getLocalPart().equals(fragmentRootElementName)) { if (fragmentRootElementNameSpace == null || startElementName.getNamespaceURI().equals(fragmentRootElementNameSpace)) { return true; } } xmlEventReader.nextEvent(); } } catch (XMLStreamException e) { throw new DataAccessResourceFailureException("Error while reading from event reader", e); } }