Search in sources :

Example 26 with Characters

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

the class SimpleXMLEventFactoryTest method testCreateCData.

@Test
public void testCreateCData() throws XMLStreamException {
    Characters event = factory.createCData("foobar");
    verify(event, XMLStreamConstants.CDATA, "<![CDATA[foobar]]>");
}
Also used : Characters(javax.xml.stream.events.Characters) Test(org.junit.Test)

Example 27 with Characters

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

the class SimpleXMLEventFactoryTest method testCreateIgnorableSpace.

@Test
public void testCreateIgnorableSpace() throws XMLStreamException {
    Characters event = factory.createIgnorableSpace(" ");
    verify(event, XMLStreamConstants.SPACE, "");
    Assert.assertTrue(event.isWhiteSpace());
    Assert.assertTrue(event.isIgnorableWhiteSpace());
}
Also used : Characters(javax.xml.stream.events.Characters) Test(org.junit.Test)

Example 28 with Characters

use of javax.xml.stream.events.Characters 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 29 with Characters

use of javax.xml.stream.events.Characters in project tomee by apache.

the class StaxCompare method compare.

public static boolean compare(final XMLEventReader a, final XMLEventReader b, final StringBuilder message) {
    XMLEvent eventA;
    XMLEvent eventB;
    int eventType;
    try {
        while ((eventA = nextInterestingEvent(a)) != null & (eventB = nextInterestingEvent(b)) != null) {
            if ((eventType = eventA.getEventType()) != eventB.getEventType()) {
                message.append("events of different types: ").append(eventA).append(", ").append(eventB);
                return false;
            }
            if (eventType == XMLStreamConstants.START_ELEMENT) {
                final StartElement startA = eventA.asStartElement();
                final StartElement startB = eventB.asStartElement();
                if (!startA.getName().getLocalPart().equals(startB.getName().getLocalPart())) {
                    message.append("Different elements ").append(startA.getName()).append(", ").append(startB.getName()).append(" at location ").append(eventA.getLocation());
                    return false;
                }
            } else if (eventType == XMLStreamConstants.END_ELEMENT) {
                final EndElement endA = eventA.asEndElement();
                final EndElement endB = eventB.asEndElement();
                if (!endA.getName().getLocalPart().equals(endB.getName().getLocalPart())) {
                    message.append("Different elements ").append(endA.getName()).append(", ").append(endB.getName()).append(" at location ").append(eventA.getLocation());
                    return false;
                }
            } else if (eventType == XMLStreamConstants.CHARACTERS) {
                final Characters endA = eventA.asCharacters();
                final Characters endB = eventB.asCharacters();
                if (!endA.getData().equals(endB.getData())) {
                    message.append("Different content ").append(endA.getData()).append(", ").append(endB.getData()).append(" at location ").append(eventA.getLocation());
                    return false;
                }
            }
        }
        if (eventA != null) {
            message.append("A is longer: ").append(eventA.getLocation());
            return false;
        }
        if (eventB != null) {
            message.append("B is longer: ").append(eventB.getLocation());
            return false;
        }
    } catch (final XMLStreamException e) {
        message.append("Exception processing ").append(e.getMessage());
        return false;
    }
    return true;
}
Also used : StartElement(javax.xml.stream.events.StartElement) XMLStreamException(javax.xml.stream.XMLStreamException) EndElement(javax.xml.stream.events.EndElement) Characters(javax.xml.stream.events.Characters) XMLEvent(javax.xml.stream.events.XMLEvent)

Example 30 with Characters

use of javax.xml.stream.events.Characters in project rpki-validator-3 by RIPE-NCC.

the class RrdpParser method snapshot.

public Snapshot snapshot(final InputStream inputStream) {
    final Map<String, SnapshotObject> objects = new HashMap<>();
    try {
        final XMLInputFactory factory = XMLInputFactory.newInstance();
        final XMLEventReader eventReader = factory.createXMLEventReader(inputStream);
        String sessionId = null;
        BigInteger serial = null;
        String uri = null;
        StringBuilder base64 = new StringBuilder();
        boolean inPublishElement = false;
        final Base64.Decoder decoder = Base64.getDecoder();
        while (eventReader.hasNext()) {
            final XMLEvent event = eventReader.nextEvent();
            switch(event.getEventType()) {
                case XMLStreamConstants.START_ELEMENT:
                    final StartElement startElement = event.asStartElement();
                    final String qName = startElement.getName().getLocalPart().toLowerCase(Locale.ROOT);
                    switch(qName) {
                        case "publish":
                            uri = getAttr(startElement, "uri", "Uri is not present in 'publish' element");
                            inPublishElement = true;
                            break;
                        case "snapshot":
                            serial = new BigInteger(getAttr(startElement, "serial", "Notification serial is not present"));
                            sessionId = getAttr(startElement, "session_id", "Session id is not present");
                            break;
                    }
                    break;
                case XMLStreamConstants.CHARACTERS:
                    final Characters characters = event.asCharacters();
                    if (inPublishElement) {
                        final String thisBase64 = characters.getData();
                        base64.append(thisBase64.replaceAll("\\s", ""));
                    }
                    break;
                case XMLStreamConstants.END_ELEMENT:
                    final EndElement endElement = event.asEndElement();
                    final String qqName = endElement.getName().getLocalPart().toLowerCase(Locale.ROOT);
                    if ("publish".equals(qqName)) {
                        final byte[] decoded = decoder.decode(base64.toString());
                        objects.put(uri, new SnapshotObject(decoded, uri));
                        inPublishElement = false;
                        base64 = new StringBuilder();
                    }
                    break;
            }
        }
        return new Snapshot(objects, sessionId, serial);
    } catch (XMLStreamException e) {
        throw new RrdpException("Couldn't parse snapshot: ", e);
    }
}
Also used : Base64(java.util.Base64) HashMap(java.util.HashMap) EndElement(javax.xml.stream.events.EndElement) Characters(javax.xml.stream.events.Characters) StartElement(javax.xml.stream.events.StartElement) XMLStreamException(javax.xml.stream.XMLStreamException) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) BigInteger(java.math.BigInteger) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Aggregations

Characters (javax.xml.stream.events.Characters)33 XMLEvent (javax.xml.stream.events.XMLEvent)21 XMLStreamException (javax.xml.stream.XMLStreamException)20 EndElement (javax.xml.stream.events.EndElement)11 StartElement (javax.xml.stream.events.StartElement)11 XMLEventReader (javax.xml.stream.XMLEventReader)6 QName (javax.xml.namespace.QName)5 XMLInputFactory (javax.xml.stream.XMLInputFactory)5 Test (org.junit.Test)5 Attribute (javax.xml.stream.events.Attribute)4 EntityDeclaration (javax.xml.stream.events.EntityDeclaration)4 EntityReference (javax.xml.stream.events.EntityReference)4 HashMap (java.util.HashMap)3 Namespace (javax.xml.stream.events.Namespace)3 StartDocument (javax.xml.stream.events.StartDocument)3 InputStream (java.io.InputStream)2 BigInteger (java.math.BigInteger)2 ArrayList (java.util.ArrayList)2 Base64 (java.util.Base64)2 ProcessingInstruction (javax.xml.stream.events.ProcessingInstruction)2