Search in sources :

Example 1 with StartDocument

use of javax.xml.stream.events.StartDocument in project wso2-synapse by wso2.

the class SimpleXMLEventFactoryTest method testCreateStartDocument_Encoding_Version.

@Test
public void testCreateStartDocument_Encoding_Version() throws XMLStreamException {
    StartDocument event = factory.createStartDocument("UTF-8", "1.1");
    verify(event, XMLStreamConstants.START_DOCUMENT, "<?xml version=\"1.1\" encoding=\"UTF-8\"?>");
}
Also used : StartDocument(javax.xml.stream.events.StartDocument) Test(org.junit.Test)

Example 2 with StartDocument

use of javax.xml.stream.events.StartDocument in project wso2-synapse by wso2.

the class SimpleXMLEventFactoryTest method testCreateStartDocument_Encoding_Version_Standalone.

@Test
public void testCreateStartDocument_Encoding_Version_Standalone() throws XMLStreamException {
    StartDocument event = factory.createStartDocument("UTF-8", "1.1", true);
    verify(event, XMLStreamConstants.START_DOCUMENT, "<?xml version=\"1.1\" encoding=\"UTF-8\" standalone=\"yes\"?>");
}
Also used : StartDocument(javax.xml.stream.events.StartDocument) Test(org.junit.Test)

Example 3 with StartDocument

use of javax.xml.stream.events.StartDocument in project wso2-synapse by wso2.

the class XMLEventWriterDelegate method add.

public void add(XMLEvent event) throws XMLStreamException {
    switch(event.getEventType()) {
        case XMLStreamConstants.ATTRIBUTE:
            Attribute attribute = (Attribute) event;
            QName attrName = attribute.getName();
            delegate.writeAttribute(attrName.getPrefix(), attrName.getNamespaceURI(), attrName.getLocalPart(), attribute.getValue());
            break;
        case XMLStreamConstants.END_DOCUMENT:
            delegate.writeEndDocument();
            break;
        case XMLStreamConstants.END_ELEMENT:
            delegate.writeEndElement();
            break;
        case XMLStreamConstants.NAMESPACE:
            Namespace namespace = (Namespace) event;
            delegate.writeNamespace(namespace.getPrefix(), namespace.getNamespaceURI());
            break;
        case XMLStreamConstants.START_DOCUMENT:
            StartDocument startDocument = (StartDocument) event;
            if (startDocument.encodingSet()) {
                // encoding defined?
                delegate.writeStartDocument(startDocument.getCharacterEncodingScheme(), startDocument.getVersion());
            } else {
                delegate.writeStartDocument(startDocument.getVersion());
            }
            break;
        case XMLStreamConstants.START_ELEMENT:
            StartElement startElement = event.asStartElement();
            QName elemName = startElement.getName();
            delegate.writeStartElement(elemName.getPrefix(), elemName.getLocalPart(), elemName.getNamespaceURI());
            Iterator<?> namespaces = startElement.getNamespaces();
            while (namespaces.hasNext()) {
                add((Namespace) namespaces.next());
            }
            Iterator<?> attributes = startElement.getAttributes();
            while (attributes.hasNext()) {
                add((Attribute) attributes.next());
            }
            break;
        case XMLStreamConstants.CHARACTERS:
        case XMLStreamConstants.CDATA:
            Characters characters = event.asCharacters();
            if (characters.isCData()) {
                delegate.writeCData(characters.getData());
            } else {
                delegate.writeCharacters(characters.getData());
            }
            break;
        case XMLStreamConstants.COMMENT:
            delegate.writeComment(((Comment) event).getText());
            break;
        case XMLStreamConstants.DTD:
            delegate.writeDTD(((DTD) event).getDocumentTypeDeclaration());
            break;
        case XMLStreamConstants.ENTITY_REFERENCE:
            delegate.writeEntityRef(((EntityReference) event).getName());
            break;
        case XMLStreamConstants.PROCESSING_INSTRUCTION:
            ProcessingInstruction processingInstruction = (ProcessingInstruction) event;
            delegate.writeProcessingInstruction(processingInstruction.getTarget(), processingInstruction.getData());
            break;
        case XMLStreamConstants.SPACE:
            break;
        default:
            throw new XMLStreamException("Cannot write event " + event);
    }
}
Also used : StartElement(javax.xml.stream.events.StartElement) StartDocument(javax.xml.stream.events.StartDocument) XMLStreamException(javax.xml.stream.XMLStreamException) Attribute(javax.xml.stream.events.Attribute) QName(javax.xml.namespace.QName) Characters(javax.xml.stream.events.Characters) Namespace(javax.xml.stream.events.Namespace) ProcessingInstruction(javax.xml.stream.events.ProcessingInstruction)

Example 4 with StartDocument

use of javax.xml.stream.events.StartDocument in project wso2-synapse by wso2.

the class SimpleXMLEventAllocatorTest method testStartEndDocument.

@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());
}
Also used : StartDocument(javax.xml.stream.events.StartDocument) XMLStreamReader(javax.xml.stream.XMLStreamReader) Test(org.junit.Test)

Example 5 with StartDocument

use of javax.xml.stream.events.StartDocument in project wso2-synapse by wso2.

the class SimpleXMLEventFactoryTest method testCreateStartDocument_Encoding.

@Test
public void testCreateStartDocument_Encoding() throws XMLStreamException {
    StartDocument event = factory.createStartDocument("UTF-8");
    verify(event, XMLStreamConstants.START_DOCUMENT, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
}
Also used : StartDocument(javax.xml.stream.events.StartDocument) Test(org.junit.Test)

Aggregations

StartDocument (javax.xml.stream.events.StartDocument)9 Test (org.junit.Test)5 XMLStreamException (javax.xml.stream.XMLStreamException)3 QName (javax.xml.namespace.QName)2 Attribute (javax.xml.stream.events.Attribute)2 Characters (javax.xml.stream.events.Characters)2 Namespace (javax.xml.stream.events.Namespace)2 ProcessingInstruction (javax.xml.stream.events.ProcessingInstruction)2 StartElement (javax.xml.stream.events.StartElement)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Location (javax.xml.stream.Location)1 XMLEventReader (javax.xml.stream.XMLEventReader)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 XMLEvent (javax.xml.stream.events.XMLEvent)1 Nullable (org.springframework.lang.Nullable)1 ContentHandler (org.xml.sax.ContentHandler)1 Locator2 (org.xml.sax.ext.Locator2)1