Search in sources :

Example 11 with Characters

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

the class SimpleXMLEventAllocatorTest method testCharactersCData.

@Test
public void testCharactersCData() throws XMLStreamException {
    XMLStreamReader reader = createXmlStreamReader("<alice>bob</alice>");
    reader.next();
    Characters characters = eventAllocator.allocate(reader).asCharacters();
    verify(characters, XMLStreamConstants.CHARACTERS, "bob");
    Assert.assertEquals("bob", characters.getData());
    Assert.assertFalse(characters.isCData());
    Assert.assertFalse(characters.isWhiteSpace());
    Assert.assertFalse(characters.isIgnorableWhiteSpace());
    reader = createXmlStreamReader("<alice><![CDATA[bob]]></alice>");
    reader.next();
    characters = eventAllocator.allocate(reader).asCharacters();
    verify(characters, XMLStreamConstants.CDATA, "<![CDATA[bob]]>");
    Assert.assertEquals("bob", characters.getData());
    Assert.assertTrue(characters.isCData());
    reader = createXmlStreamReader("<alice><![CDATA[ ]]></alice>");
    reader.next();
    characters = eventAllocator.allocate(reader).asCharacters();
    verify(characters, XMLStreamConstants.CDATA, "<![CDATA[ ]]>");
    Assert.assertEquals(" ", characters.getData());
    Assert.assertTrue(characters.isWhiteSpace());
    Assert.assertFalse(characters.isIgnorableWhiteSpace());
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) Characters(javax.xml.stream.events.Characters) Test(org.junit.Test)

Example 12 with Characters

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

the class SimpleXMLEventFactoryTest method testCreateCharacters.

@Test
public void testCreateCharacters() throws XMLStreamException {
    Characters event = factory.createCharacters("foobar");
    verify(event, XMLStreamConstants.CHARACTERS, "foobar");
}
Also used : Characters(javax.xml.stream.events.Characters) Test(org.junit.Test)

Example 13 with Characters

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

use of javax.xml.stream.events.Characters in project nebula.widgets.nattable by eclipse.

the class RegexMarkupValue method applyMarkup.

@Override
public String applyMarkup(String input) {
    String result = "";
    if (getOriginalRegexValue() != null && !getOriginalRegexValue().isEmpty()) {
        XMLEventReader parser = null;
        try {
            parser = this.factory.createXMLEventReader(new StringReader(RichTextPainter.FAKE_ROOT_TAG_START + input + RichTextPainter.FAKE_ROOT_TAG_END));
            while (parser.hasNext()) {
                XMLEvent event = parser.nextEvent();
                switch(event.getEventType()) {
                    case XMLStreamConstants.START_DOCUMENT:
                        break;
                    case XMLStreamConstants.END_DOCUMENT:
                        parser.close();
                        break;
                    case XMLStreamConstants.CHARACTERS:
                        Characters characters = event.asCharacters();
                        String text = characters.getData();
                        if (this.caseInsensitive) {
                            int flags = this.unicodeCase ? Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE : Pattern.CASE_INSENSITIVE;
                            result += Pattern.compile(getOriginalRegexValue(), flags).matcher(text).replaceAll(this.markupValue);
                        } else {
                            result += Pattern.compile(getOriginalRegexValue()).matcher(text).replaceAll(this.markupValue);
                        }
                        break;
                    default:
                        result += event.toString();
                }
            }
        } catch (XMLStreamException e) {
            e.printStackTrace();
        } finally {
            if (parser != null) {
                try {
                    parser.close();
                } catch (XMLStreamException e) {
                    e.printStackTrace();
                }
            }
        }
        result = result.replace(RichTextPainter.FAKE_ROOT_TAG_START, "").replace(RichTextPainter.FAKE_ROOT_TAG_END, "");
    } else {
        result = input;
    }
    return result;
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) Characters(javax.xml.stream.events.Characters) StringReader(java.io.StringReader) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader)

Example 15 with Characters

use of javax.xml.stream.events.Characters in project hibernate-orm by hibernate.

the class BaseXMLEventReader method getElementText.

@Override
public final String getElementText() throws XMLStreamException {
    XMLEvent event = this.previousEvent;
    if (event == null) {
        throw new XMLStreamException("Must be on START_ELEMENT to read next text, element was null");
    }
    if (!event.isStartElement()) {
        throw new XMLStreamException("Must be on START_ELEMENT to read next text", event.getLocation());
    }
    final StringBuilder text = new StringBuilder();
    while (!event.isEndDocument()) {
        switch(event.getEventType()) {
            case XMLStreamConstants.CHARACTERS:
            case XMLStreamConstants.SPACE:
            case XMLStreamConstants.CDATA:
                {
                    final Characters characters = event.asCharacters();
                    text.append(characters.getData());
                    break;
                }
            case XMLStreamConstants.ENTITY_REFERENCE:
                {
                    final EntityReference entityReference = (EntityReference) event;
                    final EntityDeclaration declaration = entityReference.getDeclaration();
                    text.append(declaration.getReplacementText());
                    break;
                }
            case XMLStreamConstants.COMMENT:
            case XMLStreamConstants.PROCESSING_INSTRUCTION:
                {
                    // Ignore
                    break;
                }
            default:
                {
                    throw new XMLStreamException("Unexpected event type '" + XMLStreamConstantsUtils.getEventName(event.getEventType()) + "' encountered. Found event: " + event, event.getLocation());
                }
        }
        event = this.nextEvent();
    }
    return text.toString();
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) EntityDeclaration(javax.xml.stream.events.EntityDeclaration) Characters(javax.xml.stream.events.Characters) XMLEvent(javax.xml.stream.events.XMLEvent) EntityReference(javax.xml.stream.events.EntityReference)

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