Search in sources :

Example 46 with XMLEvent

use of javax.xml.stream.events.XMLEvent in project RecordManager2 by moravianlibrary.

the class ManuscriptoriumFulltextXmlStreamReader method next.

public String next() {
    String result = "";
    List<String> titles = new ArrayList<>();
    while (eventReader.hasNext()) {
        try {
            XMLEvent xmlEvent = eventReader.nextEvent();
            if (xmlEvent.isStartElement()) {
                switch(xmlEvent.asStartElement().getName().getLocalPart()) {
                    case ELEMENT_UNCIPIT:
                    case ELEMENT_EXPLICIT:
                    case ELEMENT_COLOFON:
                    case ELEMENT_QUOTE:
                    case ELEMENT_TITLE:
                    case ELEMENT_RUBRIC:
                        String xmlFragment = readElementBody(eventReader);
                        if (!titles.contains(xmlFragment)) {
                            result += xmlFragment + "\n";
                            titles.add(xmlFragment);
                        }
                        break;
                    default:
                        break;
                }
            }
        } catch (WstxEOFException e) {
            break;
        } catch (WstxParsingException e) {
        } catch (XMLStreamException e) {
            e.printStackTrace();
        }
    }
    return result;
}
Also used : WstxParsingException(com.ctc.wstx.exc.WstxParsingException) XMLStreamException(javax.xml.stream.XMLStreamException) ArrayList(java.util.ArrayList) XMLEvent(javax.xml.stream.events.XMLEvent) WstxEOFException(com.ctc.wstx.exc.WstxEOFException)

Example 47 with XMLEvent

use of javax.xml.stream.events.XMLEvent in project RecordManager2 by moravianlibrary.

the class ManuscriptoriumFulltextXmlStreamReader method readElementBody.

protected String readElementBody(XMLEventReader eventReader) throws XMLStreamException {
    StringWriter buf = new StringWriter();
    int depth = 0;
    while (eventReader.hasNext()) {
        // peek event
        XMLEvent xmlEvent = eventReader.peek();
        if (xmlEvent.isStartElement())
            ++depth;
        else if (xmlEvent.isEndElement()) {
            --depth;
            // break loop, leave event in stream
            if (depth < 0)
                break;
        }
        xmlEvent = eventReader.nextEvent();
        xmlEvent.writeAsEncodedUnicode(buf);
    }
    String result = buf.getBuffer().toString();
    result = result.replaceAll("<[^>]*>", "");
    return result;
}
Also used : StringWriter(java.io.StringWriter) XMLEvent(javax.xml.stream.events.XMLEvent)

Example 48 with XMLEvent

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

the class TextView method processStAX.

/**
 * Process StAX output.
 *
 * @param pState
 *          {@link State} enum, which determines if an initial or update of
 *          the view occurs.
 * @throws XMLStreamException
 *           if any parsing exception occurs
 * @throws BadLocationException
 *           if inserting strings into the {@link JTextPane} fails
 */
private void processStAX(final State pState) throws XMLStreamException, BadLocationException {
    assert pState != null;
    final GUIProp prop = new GUIProp();
    final int indent = prop.getIndentSpaces();
    final StringBuilder spaces = new StringBuilder();
    for (int i = 0; i < indent; i++) {
        spaces.append(" ");
    }
    final String indentSpaces = spaces.toString();
    // Style document.
    final StyledDocument doc = (StyledDocument) mText.getDocument();
    final Style styleElements = doc.addStyle("elements", null);
    StyleConstants.setForeground(styleElements, ELEMENT_COLOR);
    final Style styleNamespaces = doc.addStyle("namespaces", null);
    StyleConstants.setForeground(styleNamespaces, NAMESPACE_COLOR);
    final Style styleAttributes = doc.addStyle("attributes", null);
    StyleConstants.setForeground(styleAttributes, ATTRIBUTE_COLOR);
    final Style styleText = doc.addStyle("text", null);
    StyleConstants.setForeground(styleText, TEXT_COLOR);
    final Style[] styles = new Style[] { styleElements, styleNamespaces, styleAttributes, styleText };
    boolean doIndent = true;
    assert mSerializer != null;
    switch(pState) {
        case INITIAL:
            // Initialize variables.
            final int lineHeight = mText.getFontMetrics(this.getFont()).getHeight();
            final int frameHeight = mText.getHeight();
            int level = -1;
            long height = 0;
            while (mSerializer.hasNext() && height < frameHeight) {
                final XMLEvent event = mSerializer.nextEvent();
                if (mSerializer instanceof StAXDiffSerializer) {
                    style(((StAXDiffSerializer) mSerializer).getDiff(), styles);
                }
                switch(event.getEventType()) {
                    case XMLStreamConstants.START_DOCUMENT:
                        doIndent = true;
                        break;
                    case XMLStreamConstants.END_DOCUMENT:
                        if (mTempSerializer != null) {
                            mSerializer = mTempSerializer;
                            mTempSerializer = null;
                        }
                        break;
                    case XMLStreamConstants.START_ELEMENT:
                        level++;
                        if (mSerializer instanceof StAXDiffSerializer) {
                            if (((StAXDiffSerializer) mSerializer).getDiff() == DiffType.UPDATED) {
                                if (doIndent) {
                                    doIndent = false;
                                } else {
                                    if (!mTempEmptyElement) {
                                        level--;
                                    }
                                    doIndent = true;
                                }
                            } else {
                                doIndent = true;
                            }
                        }
                        indent(doc, level, indentSpaces);
                        mEmptyElement = processStartTag(event, doc);
                        if (mSerializer instanceof StAXDiffSerializer && ((StAXDiffSerializer) mSerializer).getDiff() == DiffType.UPDATED) {
                            mTempEmptyElement = mEmptyElement;
                        } else {
                            mTempEmptyElement = false;
                        }
                        height += lineHeight;
                        break;
                    case XMLStreamConstants.END_ELEMENT:
                        if (mSerializer instanceof StAXDiffSerializer) {
                            if (((StAXDiffSerializer) mSerializer).getDiff() == DiffType.UPDATED) {
                                if (doIndent) {
                                    doIndent = false;
                                } else {
                                    if (!mEmptyElement) {
                                        level++;
                                    }
                                    doIndent = true;
                                }
                            } else {
                                doIndent = true;
                            }
                        }
                        if (mEmptyElement) {
                            mEmptyElement = false;
                        } else {
                            processEndTag(event, level, indentSpaces, doc, styleElements);
                        }
                        level--;
                        break;
                    case XMLStreamConstants.CHARACTERS:
                        doIndent = true;
                        mTempEmptyElement = false;
                        level++;
                        indent(doc, level, indentSpaces);
                        level--;
                        doc.insertString(doc.getLength(), event.asCharacters().getData() + NEWLINE, styleText);
                        height += lineHeight;
                        break;
                    default:
                }
            }
            mTempLevel = level;
            mDimension = mText.getSize();
            break;
        case UPDATE:
            for (int i = 0; i < mLineChanges && mSerializer.hasNext(); i++) {
                final XMLEvent event = mSerializer.nextEvent();
                if (mSerializer instanceof StAXDiffSerializer) {
                    style(((StAXDiffSerializer) mSerializer).getDiff(), styles);
                }
                switch(event.getEventType()) {
                    case XMLStreamConstants.START_DOCUMENT:
                        doIndent = true;
                        break;
                    case XMLStreamConstants.END_DOCUMENT:
                        if (mTempSerializer != null) {
                            mSerializer = mTempSerializer;
                            mTempSerializer = null;
                        }
                        break;
                    case XMLStreamConstants.START_ELEMENT:
                        mTempLevel++;
                        if (mSerializer instanceof StAXDiffSerializer) {
                            if (((StAXDiffSerializer) mSerializer).getDiff() == DiffType.UPDATED) {
                                if (doIndent) {
                                    doIndent = false;
                                } else {
                                    if (!mTempEmptyElement) {
                                        mTempLevel--;
                                    }
                                    doIndent = true;
                                }
                            } else {
                                doIndent = true;
                            }
                        }
                        mTempEmptyElement = false;
                        indent(doc, mTempLevel, indentSpaces);
                        mEmptyElement = processStartTag(event, doc);
                        if (mSerializer instanceof StAXDiffSerializer && ((StAXDiffSerializer) mSerializer).getDiff() == DiffType.UPDATED) {
                            mTempEmptyElement = mEmptyElement;
                        } else {
                            mTempEmptyElement = false;
                        }
                        LOGWRAPPER.debug("start elem: " + event.asStartElement().getName().getLocalPart() + " " + mEmptyElement);
                        break;
                    case XMLStreamConstants.END_ELEMENT:
                        if (mSerializer instanceof StAXDiffSerializer) {
                            if (((StAXDiffSerializer) mSerializer).getDiff() == DiffType.UPDATED) {
                                if (doIndent) {
                                    doIndent = false;
                                } else {
                                    if (!mTempEmptyElement) {
                                        mTempLevel++;
                                    }
                                    doIndent = true;
                                }
                            } else {
                                doIndent = true;
                            }
                        }
                        if (mEmptyElement) {
                            mEmptyElement = false;
                        } else {
                            processEndTag(event, mTempLevel, indentSpaces, doc, styleElements);
                        }
                        mTempLevel--;
                        break;
                    case XMLStreamConstants.CHARACTERS:
                        doIndent = true;
                        mTempEmptyElement = false;
                        mTempLevel++;
                        indent(doc, mTempLevel, indentSpaces);
                        mTempLevel--;
                        doc.insertString(doc.getLength(), event.asCharacters().getData() + NEWLINE, styleText);
                        LOGWRAPPER.debug("characters: " + event.asCharacters().getData());
                        break;
                    default:
                }
            }
            break;
        default:
    }
}
Also used : StyledDocument(javax.swing.text.StyledDocument) XMLEvent(javax.xml.stream.events.XMLEvent) Style(javax.swing.text.Style) GUIProp(org.sirix.gui.GUIProp)

Example 49 with XMLEvent

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

the class QueueEventReader method getElementText.

@Override
public String getElementText() throws XMLStreamException {
    final StringBuffer buffer = new StringBuffer();
    if (mEvents.peek().getEventType() != XMLStreamConstants.START_ELEMENT) {
        throw new XMLStreamException("Current event is not a start tag!");
    }
    final QName root = mEvents.poll().asStartElement().getName();
    int level = 0;
    while (level >= 0 && !(mEvents.peek().isEndElement() && mEvents.peek().asEndElement().getName().equals(root))) {
        final XMLEvent event = mEvents.poll();
        switch(event.getEventType()) {
            case XMLStreamConstants.START_ELEMENT:
                level++;
                break;
            case XMLStreamConstants.CHARACTERS:
                buffer.append(event.asCharacters().getData());
                break;
            case XMLStreamConstants.END_ELEMENT:
                level--;
                break;
            default:
        }
    }
    return buffer.toString();
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) QName(javax.xml.namespace.QName) XMLEvent(javax.xml.stream.events.XMLEvent)

Example 50 with XMLEvent

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

the class XMLUpdateShredder method updateOnly.

/**
 * Update a shreddered file.
 *
 * @throws SirixException if sirix encounters something went wrong
 */
private void updateOnly() throws SirixException {
    try {
        // Initialize variables.
        mLevelInToShredder = 0;
        // mIsLastNode = false;
        mMovedToRightSibling = false;
        boolean firstEvent = true;
        // If structure already exists, make a sync against the current structure.
        if (mMaxNodeKey == 0) {
            // If no content is in the XML, a normal insertNewContent is executed.
            new XMLShredder.Builder(mWtx, mReader, mInsert).build().call();
        } else {
            if (mWtx.getKind() == Kind.DOCUMENT) {
                // Find the start key for the update operation.
                long startkey = Fixed.DOCUMENT_NODE_KEY.getStandardProperty() + 1;
                while (!mWtx.moveTo(startkey).hasMoved()) {
                    startkey++;
                }
            }
            XMLEvent event = null;
            StringBuilder sBuilder = new StringBuilder();
            final XMLEventFactory fac = XMLEventFactory.newInstance();
            // Iterate over all nodes.
            while (mReader.hasNext()) {
                // Parsing the next event.
                if (mDelete == EDelete.ATSTARTMIDDLE) {
                    /*
             * Do not move StAX parser forward if nodes have been deleted at the start or in the
             * middle of a subtree.
             */
                    mDelete = EDelete.NODELETE;
                } else {
                    // After an insert or after nodes were equal.
                    event = mReader.nextEvent();
                    if (event.isCharacters() && event.asCharacters().isWhiteSpace()) {
                        continue;
                    }
                    assert event != null;
                    if (firstEvent) {
                        // Setup start element from StAX parser.
                        firstEvent = false;
                        if (event.getEventType() == XMLStreamConstants.START_DOCUMENT) {
                            while (mReader.hasNext() && event.getEventType() != XMLStreamConstants.START_ELEMENT) {
                                event = mReader.nextEvent();
                            }
                            assert event.getEventType() == XMLStreamConstants.START_ELEMENT;
                        }
                        if (event.getEventType() != XMLStreamConstants.START_ELEMENT) {
                            throw new IllegalStateException("StAX parser has to be on START_DOCUMENT or START_ELEMENT event!");
                        }
                        // Get root element of subtree or whole XML document to shredder.
                        mRootElem = event.asStartElement().getName();
                    } else if (event != null && event.isEndElement() && mRootElem.equals(event.asEndElement().getName()) && mLevelInToShredder == 1) {
                        // End with shredding if end_elem equals root-elem.
                        break;
                    }
                }
                assert event != null;
                switch(event.getEventType()) {
                    case XMLStreamConstants.START_ELEMENT:
                        processStartTag(event.asStartElement());
                        break;
                    case XMLStreamConstants.CHARACTERS:
                        sBuilder.append(event.asCharacters().getData());
                        while (mReader.peek().getEventType() == XMLStreamConstants.CHARACTERS) {
                            sBuilder.append(mReader.nextEvent().asCharacters().getData());
                        }
                        final Characters text = fac.createCharacters(sBuilder.toString().trim());
                        processCharacters(text);
                        sBuilder = new StringBuilder();
                        break;
                    case XMLStreamConstants.END_ELEMENT:
                        processEndTag();
                        break;
                    default:
                }
            }
            // if (!mIsLastNode) {
            // if (mInserted) {
            // // Remove next node after node, which was inserted, because it must
            // have been deleted.
            // if (mWtx.moveToRightSibling()) {
            // mWtx.remove();
            // }
            // } else {
            // // Remove current node (cursor has been moved to the next node
            // already).
            // mWtx.remove();
            // }
            // 
            // // Also remove any siblings.
            // boolean hasRightSibling = false;
            // while (mWtx.getStructuralNode().hasRightSibling()) {
            // hasRightSibling = true;
            // mWtx.remove();
            // }
            // if (hasRightSibling) {
            // mWtx.remove();
            // }
            // }
            mReader.close();
        }
    // TODO: use Java7 multi-catch feature.
    } catch (final XMLStreamException e) {
        throw new SirixIOException(e);
    } catch (final IOException e) {
        throw new SirixIOException(e);
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) XMLEventFactory(javax.xml.stream.XMLEventFactory) Characters(javax.xml.stream.events.Characters) XMLEvent(javax.xml.stream.events.XMLEvent) SirixIOException(org.sirix.exception.SirixIOException) IOException(java.io.IOException) SirixIOException(org.sirix.exception.SirixIOException)

Aggregations

XMLEvent (javax.xml.stream.events.XMLEvent)242 XMLEventReader (javax.xml.stream.XMLEventReader)111 XMLStreamException (javax.xml.stream.XMLStreamException)91 StartElement (javax.xml.stream.events.StartElement)86 XMLInputFactory (javax.xml.stream.XMLInputFactory)63 QName (javax.xml.namespace.QName)47 IOException (java.io.IOException)40 Attribute (javax.xml.stream.events.Attribute)40 EndElement (javax.xml.stream.events.EndElement)33 ArrayList (java.util.ArrayList)31 InputStream (java.io.InputStream)28 XMLEventWriter (javax.xml.stream.XMLEventWriter)23 Characters (javax.xml.stream.events.Characters)21 Test (org.junit.Test)17 ByteArrayInputStream (java.io.ByteArrayInputStream)16 StringWriter (java.io.StringWriter)16 StringReader (java.io.StringReader)14 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)14 HashMap (java.util.HashMap)13 XMLEventFactory (javax.xml.stream.XMLEventFactory)13