Search in sources :

Example 6 with ProcessingInstruction

use of javax.xml.stream.events.ProcessingInstruction in project sirix by sirixdb.

the class XMLShredder method insertNewContent.

/**
 * Insert new content based on a StAX parser {@link XMLStreamReader}.
 *
 * @throws SirixException if something went wrong while inserting
 */
protected final void insertNewContent() throws SirixException {
    try {
        boolean firstElement = true;
        int level = 0;
        QName rootElement = null;
        boolean endElemReached = false;
        final StringBuilder sBuilder = new StringBuilder();
        long insertedRootNodeKey = -1;
        // Iterate over all nodes.
        while (mReader.hasNext() && !endElemReached) {
            final XMLEvent event = mReader.nextEvent();
            switch(event.getEventType()) {
                case XMLStreamConstants.START_ELEMENT:
                    level++;
                    addNewElement(event.asStartElement());
                    if (firstElement) {
                        firstElement = false;
                        insertedRootNodeKey = mWtx.getNodeKey();
                        rootElement = event.asStartElement().getName();
                    }
                    break;
                case XMLStreamConstants.END_ELEMENT:
                    level--;
                    if (level == 0 && rootElement != null && rootElement.equals(event.asEndElement().getName())) {
                        endElemReached = true;
                    }
                    final QName name = event.asEndElement().getName();
                    processEndTag(new QNm(name.getNamespaceURI(), name.getPrefix(), name.getLocalPart()));
                    break;
                case XMLStreamConstants.CHARACTERS:
                    if (mReader.peek().getEventType() == XMLStreamConstants.CHARACTERS) {
                        sBuilder.append(event.asCharacters().getData().trim());
                    } else {
                        sBuilder.append(event.asCharacters().getData().trim());
                        processText(sBuilder.toString());
                        sBuilder.setLength(0);
                    }
                    break;
                case XMLStreamConstants.COMMENT:
                    if (mIncludeComments) {
                        processComment(((Comment) event).getText());
                    }
                    break;
                case XMLStreamConstants.PROCESSING_INSTRUCTION:
                    if (mIncludePIs) {
                        final ProcessingInstruction pi = (ProcessingInstruction) event;
                        processPI(pi.getData(), pi.getTarget());
                    }
                    break;
                default:
            }
        }
        mWtx.moveTo(insertedRootNodeKey);
    } catch (final XMLStreamException e) {
        throw new SirixIOException(e);
    }
}
Also used : QNm(org.brackit.xquery.atomic.QNm) XMLStreamException(javax.xml.stream.XMLStreamException) QName(javax.xml.namespace.QName) XMLEvent(javax.xml.stream.events.XMLEvent) SirixIOException(org.sirix.exception.SirixIOException) 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