Search in sources :

Example 1 with DiffAxis

use of org.sirix.gui.view.DiffAxis in project sirix by sirixdb.

the class TextView method serialize.

/**
 * Serialize a tree.
 */
private void serialize() {
    final NodeReadTrx rtx = mRtx;
    // 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("attributes", 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 long nodeKey = rtx.getNodeKey();
    try {
        switch(rtx.getKind()) {
            case DOCUMENT:
            case ELEMENT:
                mText.setText("");
                if (mAxis == null) {
                    mSerializer = new StAXSerializer(rtx);
                } else {
                    mSerializer = new StAXDiffSerializer(new DiffAxis(IncludeSelf.YES, mDb.getSession().beginNodeReadTrx(mDb.getCompareRevisionNumber()), rtx, mAxis));
                }
                processStAX(State.INITIAL);
                break;
            case TEXT:
                rtx.moveTo(nodeKey);
                mText.setText("");
                doc.insertString(doc.getLength(), new String(rtx.getRawValue()), styleText);
                break;
            case NAMESPACE:
                // Move transaction to parent of given namespace node.
                rtx.moveToParent();
                mText.setText("");
                final long nNodeKey = rtx.getNodeKey();
                for (int i = 0, namespCount = rtx.getNamespaceCount(); i < namespCount; i++) {
                    rtx.moveToNamespace(i);
                    if (rtx.getNodeKey() == nodeKey) {
                        break;
                    }
                    rtx.moveTo(nNodeKey);
                }
                if (rtx.nameForKey(rtx.getPrefixKey()).length() == 0) {
                    doc.insertString(doc.getLength(), new StringBuilder().append("xmlns='").append(rtx.nameForKey(rtx.getURIKey())).append("'").toString(), styleNamespaces);
                } else {
                    doc.insertString(doc.getLength(), new StringBuilder().append("xmlns:").append(rtx.nameForKey(rtx.getPrefixKey())).append("='").append(rtx.nameForKey(rtx.getURIKey())).append("'").toString(), styleNamespaces);
                }
                break;
            case ATTRIBUTE:
                // Move transaction to parent of given attribute node.
                rtx.moveToParent();
                mText.setText("");
                final long aNodeKey = rtx.getNodeKey();
                for (int i = 0, attsCount = rtx.getAttributeCount(); i < attsCount; i++) {
                    rtx.moveToAttribute(i);
                    if (rtx.getNodeKey() == nodeKey) {
                        break;
                    }
                    rtx.moveTo(aNodeKey);
                }
                // Display value.
                final String attPrefix = rtx.getName().getPrefix();
                final QNm attQName = rtx.getName();
                if (attPrefix == null || attPrefix.isEmpty()) {
                    doc.insertString(doc.getLength(), new StringBuilder().append(attQName.getLocalName()).append("='").append(rtx.getValue()).append("'").toString(), styleAttributes);
                } else {
                    doc.insertString(doc.getLength(), new StringBuilder().append(attPrefix).append(":").append(attQName.getLocalName()).append("='").append(rtx.getValue()).append("'").toString(), styleAttributes);
                }
                break;
            default:
                throw new IllegalStateException("Node kind not known!");
        }
    } catch (final SirixException | BadLocationException | XMLStreamException e) {
        LOGWRAPPER.error(e.getMessage(), e);
    }
}
Also used : StAXSerializer(org.sirix.service.xml.serialize.StAXSerializer) StyledDocument(javax.swing.text.StyledDocument) DiffAxis(org.sirix.gui.view.DiffAxis) QNm(org.brackit.xquery.atomic.QNm) NodeReadTrx(org.sirix.api.NodeReadTrx) XMLStreamException(javax.xml.stream.XMLStreamException) Style(javax.swing.text.Style) SirixException(org.sirix.exception.SirixException) BadLocationException(javax.swing.text.BadLocationException)

Aggregations

BadLocationException (javax.swing.text.BadLocationException)1 Style (javax.swing.text.Style)1 StyledDocument (javax.swing.text.StyledDocument)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 QNm (org.brackit.xquery.atomic.QNm)1 NodeReadTrx (org.sirix.api.NodeReadTrx)1 SirixException (org.sirix.exception.SirixException)1 DiffAxis (org.sirix.gui.view.DiffAxis)1 StAXSerializer (org.sirix.service.xml.serialize.StAXSerializer)1