Search in sources :

Example 21 with Characters

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

the class AbstractXMLEventReader method getElementText.

@Override
public String getElementText() throws XMLStreamException {
    checkIfClosed();
    if (!peek().isStartElement()) {
        throw new XMLStreamException("Not at START_ELEMENT");
    }
    StringBuilder builder = new StringBuilder();
    while (true) {
        XMLEvent event = nextEvent();
        if (event.isEndElement()) {
            break;
        } else if (!event.isCharacters()) {
            throw new XMLStreamException("Unexpected event [" + event + "] in getElementText()");
        }
        Characters characters = event.asCharacters();
        if (!characters.isIgnorableWhiteSpace()) {
            builder.append(event.asCharacters().getData());
        }
    }
    return builder.toString();
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) Characters(javax.xml.stream.events.Characters) XMLEvent(javax.xml.stream.events.XMLEvent)

Example 22 with Characters

use of javax.xml.stream.events.Characters in project uPortal by Jasig.

the class IndentingXMLEventWriter method beforeEndElement.

/**
 * Prepare to end an element, by writing a new line and indentation.
 */
protected void beforeEndElement() {
    final Set<StackState> state = scopeState.getFirst();
    // but not data
    if (depth > 0 && state.contains(StackState.WROTE_MARKUP) && !state.contains(StackState.WROTE_DATA)) {
        final String indent = this.getIndent(depth - 1, indentSize);
        final Characters indentEvent = xmlEventFactory.createCharacters(indent);
        try {
            wrappedWriter.add(indentEvent);
        } catch (XMLStreamException e) {
        // Ignore exceptions caused by indentation
        }
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) Characters(javax.xml.stream.events.Characters)

Example 23 with Characters

use of javax.xml.stream.events.Characters in project uPortal by Jasig.

the class XMLEventBufferReader method getElementText.

@Override
public 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)

Example 24 with Characters

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

the class StaxUtils method readDocElement.

public static Node readDocElement(Document doc, Node parent, XMLEvent ev, StreamToDOMContext context) throws XMLStreamException {
    switch(ev.getEventType()) {
        case XMLStreamConstants.START_ELEMENT:
            {
                context.incrementCount();
                Element e;
                StartElement startElem = ev.asStartElement();
                QName name = startElem.getName();
                if (!StringUtils.isEmpty(name.getPrefix())) {
                    e = doc.createElementNS(name.getNamespaceURI(), name.getPrefix() + ":" + name.getLocalPart());
                } else {
                    e = doc.createElementNS(name.getNamespaceURI(), name.getLocalPart());
                }
                e = (Element) parent.appendChild(e);
                if (context.isRecordLoc()) {
                    context.setRecordLoc(addLocation(doc, e, startElem.getLocation(), context.isRecordLoc()));
                }
                if (context.isRepairing() && !isDeclared(e, name.getNamespaceURI(), name.getPrefix())) {
                    declare(e, name.getNamespaceURI(), name.getPrefix());
                }
                context.pushToStack(parent);
                if (context.isThreshold() && innerElementLevelThreshold != -1 && context.getStackSize() >= innerElementLevelThreshold) {
                    throw new DepthExceededStaxException("reach the innerElementLevelThreshold:" + innerElementLevelThreshold);
                }
                if (context.isThreshold() && innerElementCountThreshold != -1 && context.getCount() >= innerElementCountThreshold) {
                    throw new DepthExceededStaxException("reach the innerElementCountThreshold:" + innerElementCountThreshold);
                }
                parent = e;
                break;
            }
        case XMLStreamConstants.END_ELEMENT:
            if (context.isStackEmpty()) {
                return parent;
            }
            parent = context.popFromStack();
            if (parent instanceof Document || parent instanceof DocumentFragment) {
                return parent;
            }
            break;
        case XMLStreamConstants.NAMESPACE:
            Namespace ns = (Namespace) ev;
            declare((Element) parent, ns.getNamespaceURI(), ns.getPrefix());
            break;
        case XMLStreamConstants.ATTRIBUTE:
            Attribute at = (Attribute) ev;
            QName qname = at.getName();
            String attName = qname.getLocalPart();
            String attPrefix = qname.getPrefix();
            if (attPrefix != null && attPrefix.length() > 0) {
                attName = attPrefix + ":" + attName;
            }
            Attr attr = doc.createAttributeNS(qname.getNamespaceURI(), attName);
            attr.setValue(at.getValue());
            ((Element) parent).setAttributeNode(attr);
            break;
        case XMLStreamConstants.CHARACTERS:
            if (parent != null) {
                Characters characters = ev.asCharacters();
                context.setRecordLoc(addLocation(doc, parent.appendChild(doc.createTextNode(characters.getData())), characters.getLocation(), context.isRecordLoc()));
            }
            break;
        case XMLStreamConstants.COMMENT:
            if (parent != null) {
                parent.appendChild(doc.createComment(((javax.xml.stream.events.Comment) ev).getText()));
            }
            break;
        case XMLStreamConstants.CDATA:
            Characters characters = ev.asCharacters();
            context.setRecordLoc(addLocation(doc, parent.appendChild(doc.createCDATASection(characters.getData())), characters.getLocation(), context.isRecordLoc()));
            break;
        case XMLStreamConstants.PROCESSING_INSTRUCTION:
            parent.appendChild(doc.createProcessingInstruction(((ProcessingInstruction) ev).getTarget(), ((ProcessingInstruction) ev).getData()));
            break;
        case XMLStreamConstants.ENTITY_REFERENCE:
            javax.xml.stream.events.EntityReference er = (javax.xml.stream.events.EntityReference) ev;
            parent.appendChild(doc.createEntityReference(er.getName()));
            break;
        default:
            break;
    }
    return parent;
}
Also used : Comment(org.w3c.dom.Comment) Attribute(javax.xml.stream.events.Attribute) QName(javax.xml.namespace.QName) StartElement(javax.xml.stream.events.StartElement) Element(org.w3c.dom.Element) Characters(javax.xml.stream.events.Characters) Document(org.w3c.dom.Document) StartDocument(javax.xml.stream.events.StartDocument) Namespace(javax.xml.stream.events.Namespace) Attr(org.w3c.dom.Attr) StartElement(javax.xml.stream.events.StartElement) EntityReference(org.w3c.dom.EntityReference) DocumentFragment(org.w3c.dom.DocumentFragment) ProcessingInstruction(org.w3c.dom.ProcessingInstruction)

Example 25 with Characters

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

the class SimpleXMLEventFactoryTest method testCreateSpace.

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

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