Search in sources :

Example 41 with XMLEvent

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

the class ZakonyProLidiFulltextXmlStreamReader method next.

public String next() {
    boolean fulltext = false;
    String result = "";
    int countDiv = 0;
    while (eventReader.hasNext()) {
        try {
            XMLEvent xmlEvent = eventReader.nextEvent();
            if (xmlEvent.isStartElement()) {
                switch(xmlEvent.asStartElement().getName().getLocalPart()) {
                    case ELEMENT_DIV:
                        if (isAttribute(xmlEvent.asStartElement(), ATTR_NAME_CLASS, ATTR_VALUE_FRAGS_NOVEL)) {
                            fulltext = true;
                        }
                        if (fulltext)
                            ++countDiv;
                        break;
                    case ELEMENT_H3:
                    case ELEMENT_P:
                        if (fulltext) {
                            String xmlFragment = readElementBody(eventReader);
                            result += xmlFragment + "\n";
                        }
                        break;
                    default:
                        break;
                }
            } else if (xmlEvent.isEndElement()) {
                switch(xmlEvent.asEndElement().getName().getLocalPart()) {
                    case ELEMENT_DIV:
                        if (fulltext)
                            --countDiv;
                        if (countDiv == 0)
                            fulltext = false;
                        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) XMLEvent(javax.xml.stream.events.XMLEvent) WstxEOFException(com.ctc.wstx.exc.WstxEOFException)

Example 42 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 43 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 44 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 45 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)

Aggregations

XMLEvent (javax.xml.stream.events.XMLEvent)269 XMLEventReader (javax.xml.stream.XMLEventReader)114 StartElement (javax.xml.stream.events.StartElement)107 XMLStreamException (javax.xml.stream.XMLStreamException)96 XMLInputFactory (javax.xml.stream.XMLInputFactory)65 QName (javax.xml.namespace.QName)60 Attribute (javax.xml.stream.events.Attribute)52 EndElement (javax.xml.stream.events.EndElement)52 IOException (java.io.IOException)39 ArrayList (java.util.ArrayList)32 InputStream (java.io.InputStream)29 XMLEventWriter (javax.xml.stream.XMLEventWriter)24 Characters (javax.xml.stream.events.Characters)22 ByteArrayInputStream (java.io.ByteArrayInputStream)17 StringWriter (java.io.StringWriter)17 Test (org.junit.Test)17 StringReader (java.io.StringReader)14 HashMap (java.util.HashMap)14 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)14 XMLEventFactory (javax.xml.stream.XMLEventFactory)13