public void testSimpleValid() throws XMLStreamException { /* Whether prolog/epilog white space is reported is not defined * by StAX specs, thus, let's not add any */ String XML = "<?xml version='1.0' ?>" + "<!DOCTYPE root [ ]>" + "<root attr='123'><!-- comment -->\n" + "</root>"; for (int i = 0; i < 4; ++i) { boolean ns = (i & 1) != 0; boolean coal = (i & 2) != 0; XMLEventReader er = getReader(XML, ns, coal); assertTokenType(START_DOCUMENT, er.nextEvent().getEventType()); assertTokenType(DTD, er.nextEvent().getEventType()); assertTokenType(START_ELEMENT, er.nextEvent().getEventType()); assertTokenType(COMMENT, er.nextEvent().getEventType()); // for fun, let's just use next() instead of nextEvent() XMLEvent evt = (XMLEvent) er.next(); assertTokenType(CHARACTERS, evt.getEventType()); assertTokenType(END_ELEMENT, er.nextEvent().getEventType()); assertTokenType(END_DOCUMENT, er.nextEvent().getEventType()); assertFalse(er.hasNext()); er.close(); } }
/** * This test was inspired by an actual bug in one of implementations: initial state was not * properly set if nextTag() was called (instead of nextEvent()), and subsequent peek() failed. */ public void testPeek() throws XMLStreamException { String XML = "<root>text</root>"; for (int i = 0; i < 4; ++i) { boolean ns = (i & 1) != 0; boolean coal = (i & 2) != 0; XMLEventReader er = getReader(XML, ns, coal); assertTokenType(START_DOCUMENT, er.nextEvent().getEventType()); XMLEvent tag = er.nextTag(); assertTokenType(START_ELEMENT, tag.getEventType()); // Now, peek() should produce text.. XMLEvent text = er.peek(); assertTokenType(CHARACTERS, text.getEventType()); Characters chars = text.asCharacters(); assertNotNull(chars); assertEquals("text", chars.getData()); // and need nextEvent() to get rid of it, too: text = er.nextEvent(); // Let's verify it again: assertTokenType(CHARACTERS, text.getEventType()); chars = text.asCharacters(); assertNotNull(chars); assertEquals("text", chars.getData()); assertTokenType(END_ELEMENT, er.nextTag().getEventType()); assertTokenType(END_DOCUMENT, er.nextEvent().getEventType()); // And at the end, peek() should return null assertNull(er.peek()); } }
@Override public QName getSchemaType() { return delegate.getSchemaType(); }
@Override public Characters asCharacters() { return delegate.asCharacters(); }
@Override public EndElement asEndElement() { return delegate.asEndElement(); }
@Override public StartElement asStartElement() { return delegate.asStartElement(); }
@Override public boolean isEndDocument() { return delegate.isEndDocument(); }
@Override public boolean isCharacters() { return delegate.isCharacters(); }
@Override public boolean isEntityReference() { return delegate.isEntityReference(); }
@Override public boolean isNamespace() { return delegate.isNamespace(); }
@Override public boolean isAttribute() { return delegate.isAttribute(); }
@Override public boolean isStartElement() { return delegate.isStartElement(); }
@Override public Location getLocation() { return delegate.getLocation(); }
@Override public int getEventType() { return delegate.getEventType(); }
public void testElementText() throws XMLStreamException { String TEXT1 = "some\ntest"; String TEXT2 = "inside CDATA too!"; String XML = "<root>" + TEXT1 + "<![CDATA[" + TEXT2 + "]]></root>"; // First, let's see how things work without peeking: for (int i = 0; i < 4; ++i) { boolean ns = (i & 1) != 0; boolean coal = (i & 2) != 0; XMLEventReader er = getReader(XML, ns, coal); assertTokenType(START_DOCUMENT, er.nextEvent().getEventType()); XMLEvent elem = er.nextEvent(); assertTokenType(START_ELEMENT, elem.getEventType()); try { assertEquals(TEXT1 + TEXT2, er.getElementText()); } catch (XMLStreamException sex) { fail("Failed on XMLEventReader.getElementText(): " + sex); } /* 06-Jan-2006, TSa: I'm really not sure whether to expect * END_ELEMENT, or END_DOCUMENT here... maybe the latter * makes more sense? For now let's accept both, however. */ elem = er.nextEvent(); if (elem.getEventType() != END_DOCUMENT && elem.getEventType() != END_ELEMENT) { fail("Expected END_DOCUMENT or END_ELEMENT, got " + tokenTypeDesc(elem.getEventType())); } } // and then with peeking: for (int i = 0; i < 4; ++i) { boolean ns = (i & 1) != 0; boolean coal = (i & 2) != 0; XMLEventReader er = getReader(XML, ns, coal); assertTokenType(START_DOCUMENT, er.nextEvent().getEventType()); XMLEvent elem = er.nextEvent(); assertTokenType(START_ELEMENT, elem.getEventType()); XMLEvent peeked = er.peek(); assertTokenType(CHARACTERS, peeked.getEventType()); assertEquals(TEXT1 + TEXT2, er.getElementText()); /* 06-Jan-2006, TSa: I'm really not sure whether to expect * END_ELEMENT, or END_DOCUMENT here... maybe the latter * makes more sense? For now let's accept both, however. */ elem = er.nextEvent(); if (elem.getEventType() != END_DOCUMENT && elem.getEventType() != END_ELEMENT) { fail("Expected END_DOCUMENT or END_ELEMENT, got " + tokenTypeDesc(elem.getEventType())); } } }
@Override public void writeAsEncodedUnicode(Writer writer) throws XMLStreamException { delegate.writeAsEncodedUnicode(writer); }
public void doFilter( ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) servletRequest; RequestURIParts parts = parseParts(request); if (parts != null) { String realBaseAddress = parts.getBaseAddress(); String idlPath = parts.getFilePath(); InputStream idl = this.servletContext.getResourceAsStream(idlPath); if (idl != null) { servletResponse.setContentType("text/xml"); String assumedBaseAddress = null; try { XMLEventReader eventReader = this.inputFactory.createXMLEventReader(idl); XMLEventWriter eventWriter = this.outputFactory.createXMLEventWriter(servletResponse.getWriter()); while (eventReader.hasNext()) { XMLEvent event = eventReader.nextEvent(); if (event.isProcessingInstruction()) { String target = ((ProcessingInstruction) event).getTarget(); if ("enunciate-assumed-base-uri".equals(target)) { assumedBaseAddress = ((ProcessingInstruction) event).getData(); if (assumedBaseAddress.endsWith("/")) { assumedBaseAddress = assumedBaseAddress.substring(0, assumedBaseAddress.length() - 1); } } continue; } else if (event.getEventType() == XMLStreamConstants.CDATA || event.getEventType() == XMLStreamConstants.CHARACTERS) { String data = ((Characters) event).getData(); if (assumedBaseAddress != null && data.contains(assumedBaseAddress)) { data = data.replace(assumedBaseAddress, realBaseAddress); event = new DelegatingCharacters(((Characters) event), data); } } else if (event.getEventType() == XMLStreamConstants.START_ELEMENT) { List<Attribute> attributes = new ArrayList<Attribute>(); Iterator attributesIt = ((StartElement) event).getAttributes(); while (attributesIt.hasNext()) { Attribute attribute = (Attribute) attributesIt.next(); String value = attribute.getValue(); if (assumedBaseAddress != null && value.contains(assumedBaseAddress)) { value = value.replace(assumedBaseAddress, realBaseAddress); attribute = new DelegatingAttribute(attribute, value); event = new DelegatingStartElement(((StartElement) event), attributes); } attributes.add(attribute); } } eventWriter.add(event); } eventReader.close(); eventWriter.flush(); eventWriter.close(); return; } catch (XMLStreamException e) { throw new ServletException(e); } } } chain.doFilter(servletRequest, servletResponse); }
@Override public boolean isProcessingInstruction() { return delegate.isProcessingInstruction(); }