Search in sources :

Example 1 with Comment

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

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

the class SimpleXMLEventAllocatorTest method testComment.

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

Example 3 with Comment

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

the class SimpleXMLEventFactoryTest method testCreateComment.

@Test
public void testCreateComment() throws XMLStreamException {
    Comment event = factory.createComment("foo");
    verify(event, XMLStreamConstants.COMMENT, "<!--foo-->");
}
Also used : Comment(javax.xml.stream.events.Comment) Test(org.junit.Test)

Aggregations

Comment (javax.xml.stream.events.Comment)3 Test (org.junit.Test)2 QName (javax.xml.namespace.QName)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 Attribute (javax.xml.stream.events.Attribute)1 EntityReference (javax.xml.stream.events.EntityReference)1 Namespace (javax.xml.stream.events.Namespace)1 ProcessingInstruction (javax.xml.stream.events.ProcessingInstruction)1 StartElement (javax.xml.stream.events.StartElement)1 Element (org.w3c.dom.Element)1