Search in sources :

Example 6 with StartDocument

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

the class SimpleXMLEventFactoryTest method testCreateStartDocument.

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

Example 7 with StartDocument

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

the class SimpleXMLEventWriter method add.

@Override
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 8 with StartDocument

use of javax.xml.stream.events.StartDocument in project Payara by payara.

the class GenericSniffer method readDeploymentConfig.

private String readDeploymentConfig(final InputStream is) throws IOException {
    String encoding = null;
    XMLEventReader rdr = null;
    try {
        is.mark(Integer.MAX_VALUE);
        rdr = xmlInputFactory.createXMLEventReader(new InputStreamReader(is));
        while (rdr.hasNext()) {
            final XMLEvent ev = rdr.nextEvent();
            if (ev.isStartDocument()) {
                final StartDocument sd = (StartDocument) ev;
                encoding = sd.getCharacterEncodingScheme();
                break;
            }
        }
    } catch (XMLStreamException e) {
        if (rdr != null) {
            try {
                rdr.close();
            } catch (XMLStreamException inner) {
                throw new IOException(e);
            }
        }
        throw new IOException(e);
    }
    if (encoding == null) {
        encoding = "UTF-8";
    }
    is.reset();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int bytesRead;
    final byte[] buffer = new byte[1024];
    while ((bytesRead = is.read(buffer)) != -1) {
        baos.write(buffer, 0, bytesRead);
    }
    try {
        rdr.close();
    } catch (XMLStreamException ex) {
        throw new IOException(ex);
    }
    is.close();
    return new String(baos.toByteArray(), encoding);
}
Also used : StartDocument(javax.xml.stream.events.StartDocument) InputStreamReader(java.io.InputStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 9 with StartDocument

use of javax.xml.stream.events.StartDocument in project spring-framework by spring-projects.

the class StaxEventXMLReader method handleStartDocument.

private void handleStartDocument(final XMLEvent event) throws SAXException {
    if (event.isStartDocument()) {
        StartDocument startDocument = (StartDocument) event;
        String xmlVersion = startDocument.getVersion();
        if (StringUtils.hasLength(xmlVersion)) {
            this.xmlVersion = xmlVersion;
        }
        if (startDocument.encodingSet()) {
            this.encoding = startDocument.getCharacterEncodingScheme();
        }
    }
    ContentHandler contentHandler = getContentHandler();
    if (contentHandler != null) {
        final Location location = event.getLocation();
        contentHandler.setDocumentLocator(new Locator2() {

            @Override
            public int getColumnNumber() {
                return (location != null ? location.getColumnNumber() : -1);
            }

            @Override
            public int getLineNumber() {
                return (location != null ? location.getLineNumber() : -1);
            }

            @Override
            @Nullable
            public String getPublicId() {
                return (location != null ? location.getPublicId() : null);
            }

            @Override
            @Nullable
            public String getSystemId() {
                return (location != null ? location.getSystemId() : null);
            }

            @Override
            public String getXMLVersion() {
                return xmlVersion;
            }

            @Override
            @Nullable
            public String getEncoding() {
                return encoding;
            }
        });
        contentHandler.startDocument();
    }
}
Also used : StartDocument(javax.xml.stream.events.StartDocument) Locator2(org.xml.sax.ext.Locator2) ContentHandler(org.xml.sax.ContentHandler) Nullable(org.springframework.lang.Nullable) Location(javax.xml.stream.Location)

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