Search in sources :

Example 1 with ProcessingInstruction

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

the class SimpleXMLEventFactoryTest method testCreateProcessingInstruction.

@Test
public void testCreateProcessingInstruction() throws XMLStreamException {
    ProcessingInstruction event = factory.createProcessingInstruction("foo", "bar");
    verify(event, XMLStreamConstants.PROCESSING_INSTRUCTION, "<?foo bar?>");
}
Also used : ProcessingInstruction(javax.xml.stream.events.ProcessingInstruction) Test(org.junit.Test)

Example 2 with ProcessingInstruction

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

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

the class DOMEventConsumer method add.

@Override
public void add(XMLEvent event) throws XMLStreamException {
    switch(event.getEventType()) {
        case XMLStreamConstants.CDATA:
            node.appendChild(document.createCDATASection(event.asCharacters().getData()));
            break;
        case XMLStreamConstants.CHARACTERS:
            node.appendChild(document.createTextNode(event.asCharacters().getData()));
            break;
        case XMLStreamConstants.COMMENT:
            node.appendChild(document.createComment(((Comment) event).getText()));
            break;
        case XMLStreamConstants.END_ELEMENT:
            node = node.getParentNode();
            break;
        case XMLStreamConstants.ENTITY_REFERENCE:
            node.appendChild(document.createEntityReference(((EntityReference) event).getName()));
            break;
        case XMLStreamConstants.PROCESSING_INSTRUCTION:
            ProcessingInstruction pi = (ProcessingInstruction) event;
            node.appendChild(document.createProcessingInstruction(pi.getTarget(), pi.getData()));
            break;
        case XMLStreamConstants.START_ELEMENT:
            StartElement startElement = event.asStartElement();
            QName elementName = startElement.getName();
            Element element;
            if (namespaceAware) {
                element = document.createElementNS(elementName.getNamespaceURI(), qName(elementName));
            } else {
                element = document.createElement(elementName.getLocalPart());
            }
            Iterator<?> namespaces = startElement.getNamespaces();
            while (namespaces.hasNext()) {
                Namespace ns = (Namespace) namespaces.next();
                element.setAttributeNS(ns.getName().getNamespaceURI(), qName(ns.getName()), ns.getNamespaceURI());
            }
            Iterator<?> attributes = startElement.getAttributes();
            while (attributes.hasNext()) {
                Attribute at = (Attribute) attributes.next();
                if (namespaceAware) {
                    element.setAttributeNS(at.getName().getNamespaceURI(), qName(at.getName()), at.getValue());
                } else {
                    element.setAttribute(at.getName().getLocalPart(), at.getValue());
                }
            }
            node = node.appendChild(element);
            break;
        case XMLStreamConstants.SPACE:
        case XMLStreamConstants.ENTITY_DECLARATION:
        case XMLStreamConstants.NOTATION_DECLARATION:
        case XMLStreamConstants.START_DOCUMENT:
        case XMLStreamConstants.END_DOCUMENT:
        case XMLStreamConstants.DTD:
            break;
        default:
            throw new XMLStreamException("Unexpected event type " + event.getEventType() + "; " + event);
    }
}
Also used : StartElement(javax.xml.stream.events.StartElement) Comment(javax.xml.stream.events.Comment) XMLStreamException(javax.xml.stream.XMLStreamException) Attribute(javax.xml.stream.events.Attribute) QName(javax.xml.namespace.QName) Element(org.w3c.dom.Element) StartElement(javax.xml.stream.events.StartElement) EntityReference(javax.xml.stream.events.EntityReference) ProcessingInstruction(javax.xml.stream.events.ProcessingInstruction) Namespace(javax.xml.stream.events.Namespace)

Example 4 with ProcessingInstruction

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

the class SimpleXMLEventAllocatorTest method testProcessingInstruction.

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

Example 5 with ProcessingInstruction

use of javax.xml.stream.events.ProcessingInstruction 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)

Aggregations

ProcessingInstruction (javax.xml.stream.events.ProcessingInstruction)6 QName (javax.xml.namespace.QName)4 XMLStreamException (javax.xml.stream.XMLStreamException)4 Attribute (javax.xml.stream.events.Attribute)3 Namespace (javax.xml.stream.events.Namespace)3 StartElement (javax.xml.stream.events.StartElement)3 Characters (javax.xml.stream.events.Characters)2 StartDocument (javax.xml.stream.events.StartDocument)2 Test (org.junit.Test)2 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 Comment (javax.xml.stream.events.Comment)1 EntityReference (javax.xml.stream.events.EntityReference)1 XMLEvent (javax.xml.stream.events.XMLEvent)1 QNm (org.brackit.xquery.atomic.QNm)1 SirixIOException (org.sirix.exception.SirixIOException)1 Element (org.w3c.dom.Element)1