SimpleXMLEventAllocator implements XMLEventAllocator { @Override public XMLEvent allocate(XMLStreamReader reader) throws XMLStreamException { switch (reader.getEventType()) { case XMLStreamConstants.CDATA: case XMLStreamConstants.CHARACTERS: case XMLStreamConstants.SPACE: return new CharactersEvent(reader); case XMLStreamConstants.COMMENT: return new CommentEvent(reader); case XMLStreamConstants.DTD: throw new UnsupportedOperationException(); case XMLStreamConstants.END_DOCUMENT: return new EndDocumentEvent(reader); case XMLStreamConstants.END_ELEMENT: return new EndElementEvent(reader); case XMLStreamConstants.ENTITY_REFERENCE: return new EntityReferenceEvent(reader); case XMLStreamConstants.NOTATION_DECLARATION: throw new UnsupportedOperationException(); case XMLStreamConstants.PROCESSING_INSTRUCTION: return new ProcessingInstructionEvent(reader); case XMLStreamConstants.START_DOCUMENT: return new StartDocumentEvent(reader); case XMLStreamConstants.START_ELEMENT: return new StartElementEvent(reader); default: throw new XMLStreamException("Unexpected event type: " + reader.getEventType()); } }  @Override XMLEventAllocator newInstance(); @Override XMLEvent allocate(XMLStreamReader reader); @Override void allocate(XMLStreamReader reader, XMLEventConsumer consumer);  }
@Test public void testStartEndDocument() throws XMLStreamException { XMLStreamReader reader = createXmlStreamReader("<? xml ?>"); StartDocument startDocument = (StartDocument)eventAllocator.allocate(reader); verify(startDocument, XMLStreamConstants.START_DOCUMENT, "<?xml version=\"1.0\"?>"); Assert.assertEquals("UTF-8", startDocument.getCharacterEncodingScheme()); Assert.assertFalse(startDocument.encodingSet()); Assert.assertEquals("1.0", startDocument.getVersion()); Assert.assertFalse(startDocument.isStandalone()); Assert.assertFalse(startDocument.standaloneSet()); reader.next(); verify((EndDocument)eventAllocator.allocate(reader), XMLStreamConstants.END_DOCUMENT, ""); reader = createXmlStreamReader("<? xml version=\"1.1\" ?>"); startDocument = (StartDocument)eventAllocator.allocate(reader); Assert.assertEquals("1.1", startDocument.getVersion()); reader = createXmlStreamReader("<? xml encoding=\"UTF-16\"?>"); startDocument = (StartDocument)eventAllocator.allocate(reader); Assert.assertEquals("UTF-16", startDocument.getCharacterEncodingScheme()); Assert.assertTrue(startDocument.encodingSet()); }
@Test public void testStartEndElement() throws XMLStreamException { XMLStreamReader reader = createXmlStreamReader("<alice/>"); StartElement startElement = eventAllocator.allocate(reader).asStartElement(); verify(startElement, XMLStreamConstants.START_ELEMENT, "<alice>"); Assert.assertEquals(new QName("alice"), startElement.getName()); Assert.assertNotNull(startElement.getNamespaceContext()); Assert.assertFalse(startElement.getAttributes().hasNext()); Assert.assertFalse(startElement.getNamespaces().hasNext()); Assert.assertNull(startElement.getNamespaceURI("foo")); reader.next(); EndElement endElement = eventAllocator.allocate(reader).asEndElement(); verify(endElement, XMLStreamConstants.END_ELEMENT, "</alice>"); Assert.assertEquals(new QName("alice"), endElement.getName()); Assert.assertFalse(endElement.getNamespaces().hasNext()); }
@Test public void testCharactersCData() throws XMLStreamException { XMLStreamReader reader = createXmlStreamReader("<alice>bob</alice>"); reader.next(); Characters characters = eventAllocator.allocate(reader).asCharacters(); verify(characters, XMLStreamConstants.CHARACTERS, "bob"); Assert.assertEquals("bob", characters.getData()); Assert.assertFalse(characters.isCData()); Assert.assertFalse(characters.isWhiteSpace()); Assert.assertFalse(characters.isIgnorableWhiteSpace()); reader = createXmlStreamReader("<alice><![CDATA[bob]]></alice>"); reader.next(); characters = eventAllocator.allocate(reader).asCharacters(); verify(characters, XMLStreamConstants.CDATA, "<![CDATA[bob]]>"); Assert.assertEquals("bob", characters.getData()); Assert.assertTrue(characters.isCData()); reader = createXmlStreamReader("<alice><![CDATA[ ]]></alice>"); reader.next(); characters = eventAllocator.allocate(reader).asCharacters(); verify(characters, XMLStreamConstants.CDATA, "<![CDATA[ ]]>"); Assert.assertEquals(" ", characters.getData()); Assert.assertTrue(characters.isWhiteSpace()); Assert.assertFalse(characters.isIgnorableWhiteSpace()); }
@Test public void testAttributeNamespace() throws XMLStreamException { XMLStreamReader reader = createXmlStreamReader("<alice david=\"edgar\" xmlns=\"http: StartElement startElement = eventAllocator.allocate(reader).asStartElement(); verify(startElement, XMLStreamConstants.START_ELEMENT, "<alice xmlns=\"http: Iterator<?> attributes = startElement.getAttributes(); Assert.assertTrue(attributes.hasNext()); Attribute attribute = (Attribute)attributes.next(); verify(attribute, XMLStreamConstants.ATTRIBUTE, "david=\"edgar\""); Assert.assertEquals(new QName("david"), attribute.getName()); Assert.assertEquals("edgar", attribute.getValue()); Assert.assertFalse(attributes.hasNext()); Iterator<?> namespaces = startElement.getNamespaces(); Assert.assertTrue(namespaces.hasNext()); Namespace namespace = (Namespace)namespaces.next(); verify(namespace, XMLStreamConstants.NAMESPACE, "xmlns=\"http: Assert.assertEquals(new QName(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns"), namespace.getName()); Assert.assertEquals("http: Assert.assertEquals(XMLConstants.DEFAULT_NS_PREFIX, namespace.getPrefix()); Assert.assertEquals("http: Assert.assertFalse(namespaces.hasNext()); reader = createXmlStreamReader("<alice p:david=\"edgar\" xmlns:p=\"http: startElement = eventAllocator.allocate(reader).asStartElement(); verify(startElement, XMLStreamConstants.START_ELEMENT, "<alice xmlns:p=\"http: attributes = startElement.getAttributes(); Assert.assertTrue(attributes.hasNext()); attribute = (Attribute)attributes.next(); verify(attribute, XMLStreamConstants.ATTRIBUTE, "p:david=\"edgar\""); Assert.assertEquals(new QName("http: Assert.assertEquals("edgar", attribute.getValue()); Assert.assertFalse(attributes.hasNext()); namespaces = startElement.getNamespaces(); Assert.assertTrue(namespaces.hasNext()); namespace = (Namespace)namespaces.next(); verify(namespace, XMLStreamConstants.NAMESPACE, "xmlns:p=\"http: Assert.assertEquals(new QName(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "p", "xmlns"), namespace.getName()); Assert.assertEquals("http: Assert.assertEquals("p", namespace.getPrefix()); Assert.assertEquals("http: Assert.assertFalse(namespaces.hasNext()); }
@Test public void testComment() throws XMLStreamException { XMLStreamReader reader = createXmlStreamReader("<!-- james -->"); Comment comment = (Comment)eventAllocator.allocate(reader); verify(comment, XMLStreamConstants.COMMENT, "<!-- james -->"); Assert.assertEquals(" james ", comment.getText()); }
@Test public void testProcessingInstruction() throws XMLStreamException { XMLStreamReader reader = createXmlStreamReader("<? joe ?>"); ProcessingInstruction processingInstruction = (ProcessingInstruction)eventAllocator.allocate(reader); verify(processingInstruction, XMLStreamConstants.PROCESSING_INSTRUCTION, "<?joe?>"); Assert.assertEquals("joe", processingInstruction.getTarget()); Assert.assertNull(processingInstruction.getData()); reader = createXmlStreamReader("<?joe jim?>"); processingInstruction = (ProcessingInstruction)eventAllocator.allocate(reader); Assert.assertEquals("joe", processingInstruction.getTarget()); Assert.assertEquals("jim", processingInstruction.getData()); }