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();
}
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;
}
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();
}
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();
}
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 & ...\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);
}
}
Aggregations