Search in sources :

Example 36 with XMLEvent

use of javax.xml.stream.events.XMLEvent in project hibernate-orm by hibernate.

the class BaseXMLEventReader method getElementText.

@Override
public final String getElementText() throws XMLStreamException {
    XMLEvent event = this.previousEvent;
    if (event == null) {
        throw new XMLStreamException("Must be on START_ELEMENT to read next text, element was null");
    }
    if (!event.isStartElement()) {
        throw new XMLStreamException("Must be on START_ELEMENT to read next text", event.getLocation());
    }
    final StringBuilder text = new StringBuilder();
    while (!event.isEndDocument()) {
        switch(event.getEventType()) {
            case XMLStreamConstants.CHARACTERS:
            case XMLStreamConstants.SPACE:
            case XMLStreamConstants.CDATA:
                {
                    final Characters characters = event.asCharacters();
                    text.append(characters.getData());
                    break;
                }
            case XMLStreamConstants.ENTITY_REFERENCE:
                {
                    final EntityReference entityReference = (EntityReference) event;
                    final EntityDeclaration declaration = entityReference.getDeclaration();
                    text.append(declaration.getReplacementText());
                    break;
                }
            case XMLStreamConstants.COMMENT:
            case XMLStreamConstants.PROCESSING_INSTRUCTION:
                {
                    // Ignore
                    break;
                }
            default:
                {
                    throw new XMLStreamException("Unexpected event type '" + XMLStreamConstantsUtils.getEventName(event.getEventType()) + "' encountered. Found event: " + event, event.getLocation());
                }
        }
        event = this.nextEvent();
    }
    return text.toString();
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) EntityDeclaration(javax.xml.stream.events.EntityDeclaration) Characters(javax.xml.stream.events.Characters) XMLEvent(javax.xml.stream.events.XMLEvent) EntityReference(javax.xml.stream.events.EntityReference)

Example 37 with XMLEvent

use of javax.xml.stream.events.XMLEvent in project hibernate-orm by hibernate.

the class FilteringXMLEventReader method internalNext.

protected final XMLEvent internalNext(boolean peek) throws XMLStreamException {
    XMLEvent event = null;
    if (peekedEvent != null) {
        event = peekedEvent;
        peekedEvent = null;
        return event;
    }
    do {
        event = super.getParent().nextEvent();
        // If there are pruned elements in the queue filtering events is still needed
        if (!prunedElements.isEmpty()) {
            // If another start element add it to the queue
            if (event.isStartElement()) {
                final StartElement startElement = event.asStartElement();
                prunedElements.push(startElement.getName());
            } else // If end element pop the newest name of the queue and double check that the start/end elements match up
            if (event.isEndElement()) {
                final QName startElementName = prunedElements.pop();
                final EndElement endElement = event.asEndElement();
                final QName endElementName = endElement.getName();
                if (!startElementName.equals(endElementName)) {
                    throw new IllegalArgumentException("Malformed XMLEvent stream. Expected end element for " + startElementName + " but found end element for " + endElementName);
                }
            }
            event = null;
        } else {
            final XMLEvent filteredEvent = this.filterEvent(event, peek);
            // end element need to be removed as well
            if (filteredEvent == null && event.isStartElement()) {
                final StartElement startElement = event.asStartElement();
                final QName name = startElement.getName();
                prunedElements.push(name);
            }
            event = filteredEvent;
        }
    } while (event == null);
    return event;
}
Also used : StartElement(javax.xml.stream.events.StartElement) EndElement(javax.xml.stream.events.EndElement) QName(javax.xml.namespace.QName) XMLEvent(javax.xml.stream.events.XMLEvent)

Example 38 with XMLEvent

use of javax.xml.stream.events.XMLEvent in project aalto-xml by FasterXML.

the class TestAsyncViaEventReader method testSimpleByteBuffer.

public void testSimpleByteBuffer() throws Exception {
    final AsyncXMLInputFactory f = new InputFactoryImpl();
    AsyncXMLStreamReader<AsyncByteBufferFeeder> sr = f.createAsyncFor(ByteBuffer.wrap("<root>a</r".getBytes("UTF-8")));
    assertTokenType(START_DOCUMENT, sr.next());
    XMLEventReader er = f.createXMLEventReader(sr);
    XMLEvent evt = er.nextEvent();
    assertTokenType(START_DOCUMENT, evt.getEventType());
    evt = er.nextEvent();
    assertTokenType(START_ELEMENT, evt.getEventType());
    assertEquals("root", sr.getLocalName());
    evt = er.nextEvent();
    assertTokenType(CHARACTERS, evt.getEventType());
    assertEquals("a", sr.getText());
    // then need more input
    evt = er.nextEvent();
    assertTokenType(AsyncXMLStreamReader.EVENT_INCOMPLETE, evt.getEventType());
    byte[] b = "oot>".getBytes("UTF-8");
    sr.getInputFeeder().feedInput(ByteBuffer.wrap(b));
    evt = er.nextEvent();
    assertTokenType(END_ELEMENT, evt.getEventType());
    assertEquals("root", sr.getLocalName());
    evt = er.nextEvent();
    assertTokenType(AsyncXMLStreamReader.EVENT_INCOMPLETE, evt.getEventType());
    sr.getInputFeeder().endOfInput();
    evt = er.nextEvent();
    assertTokenType(END_DOCUMENT, evt.getEventType());
    assertFalse(er.hasNext());
    er.close();
    sr.close();
}
Also used : XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) InputFactoryImpl(com.fasterxml.aalto.stax.InputFactoryImpl)

Example 39 with XMLEvent

use of javax.xml.stream.events.XMLEvent in project aalto-xml by FasterXML.

the class TestAsyncViaEventReader method testSimpleByteArray.

public void testSimpleByteArray() throws Exception {
    final AsyncXMLInputFactory f = newAsyncInputFactory();
    AsyncXMLStreamReader<AsyncByteArrayFeeder> sr = f.createAsyncFor("<root>a</r".getBytes("UTF-8"));
    assertTokenType(START_DOCUMENT, sr.next());
    XMLEventReader er = f.createXMLEventReader(sr);
    XMLEvent evt = er.nextEvent();
    assertTokenType(START_DOCUMENT, evt.getEventType());
    evt = er.nextEvent();
    assertTokenType(START_ELEMENT, evt.getEventType());
    assertEquals("root", sr.getLocalName());
    evt = er.nextEvent();
    assertTokenType(CHARACTERS, evt.getEventType());
    assertEquals("a", sr.getText());
    // then need more input
    evt = er.nextEvent();
    assertTokenType(AsyncXMLStreamReader.EVENT_INCOMPLETE, evt.getEventType());
    byte[] b = "oot>".getBytes("UTF-8");
    sr.getInputFeeder().feedInput(b, 0, b.length);
    evt = er.nextEvent();
    assertTokenType(END_ELEMENT, evt.getEventType());
    assertEquals("root", sr.getLocalName());
    evt = er.nextEvent();
    assertTokenType(AsyncXMLStreamReader.EVENT_INCOMPLETE, evt.getEventType());
    sr.getInputFeeder().endOfInput();
    evt = er.nextEvent();
    assertTokenType(END_DOCUMENT, evt.getEventType());
    assertFalse(er.hasNext());
    er.close();
    sr.close();
}
Also used : XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader)

Example 40 with XMLEvent

use of javax.xml.stream.events.XMLEvent in project aalto-xml by FasterXML.

the class TestLaziness method testEventReaderNonLaziness.

/**
 * This test verifies that lazy parsing is not enabled, by making
 * use of a specific side effect of lazy parsing. Specifically, parts
 * of Stax API do not expose XMLStreamException: such as getText()
 * method of the stream reader. Thus we can infer whether lazy mode
 * is enabled or not by whether we get a regular XMLStreamException
 * when trying to access the event, or not.
 *<p>
 * Note: whether this test reliably detecs "laziness" may depend
 * on the event reader implementation.
 */
public void testEventReaderNonLaziness() throws XMLStreamException {
    final String XML = "<root>Some text and &amp; ...\n\n &error;</root>";
    XMLEventReader er = getReader(XML);
    // First things first: what does it say about mode?
    assertEquals("Event reader should have P_LAZY_PARSING == false", Boolean.FALSE, er.getProperty(XMLInputFactory2.P_LAZY_PARSING));
    // start document
    XMLEvent evt = er.nextEvent();
    assertTrue(evt.isStartDocument());
    assertTrue(er.nextEvent().isStartElement());
    // Ok, and now...
    try {
        evt = er.nextEvent();
        // should NOT get this far...
        fail("Expected an exception for invalid content: something not working with event reader");
    } catch (WFCException wex) {
    // This is correct... parsing exc for entity, hopefully
    // System.err.println("GOOD: got "+wex.getClass()+": "+wex);
    } catch (UncheckedStreamException ue) {
        // Not good; lazy exception
        fail("Should not get a lazy exception via (default) event reader, but did get one: " + ue);
    } catch (Throwable t) {
        fail("Unexpected excpetion (class " + t.getClass() + ") caught: " + t);
    }
}
Also used : UncheckedStreamException(com.fasterxml.aalto.UncheckedStreamException) XMLEvent(javax.xml.stream.events.XMLEvent) WFCException(com.fasterxml.aalto.WFCException)

Aggregations

XMLEvent (javax.xml.stream.events.XMLEvent)269 XMLEventReader (javax.xml.stream.XMLEventReader)114 StartElement (javax.xml.stream.events.StartElement)107 XMLStreamException (javax.xml.stream.XMLStreamException)96 XMLInputFactory (javax.xml.stream.XMLInputFactory)65 QName (javax.xml.namespace.QName)60 Attribute (javax.xml.stream.events.Attribute)52 EndElement (javax.xml.stream.events.EndElement)52 IOException (java.io.IOException)39 ArrayList (java.util.ArrayList)32 InputStream (java.io.InputStream)29 XMLEventWriter (javax.xml.stream.XMLEventWriter)24 Characters (javax.xml.stream.events.Characters)22 ByteArrayInputStream (java.io.ByteArrayInputStream)17 StringWriter (java.io.StringWriter)17 Test (org.junit.Test)17 StringReader (java.io.StringReader)14 HashMap (java.util.HashMap)14 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)14 XMLEventFactory (javax.xml.stream.XMLEventFactory)13