Search in sources :

Example 1 with StAXSerializer

use of org.sirix.service.xml.serialize.StAXSerializer 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)

Example 2 with StAXSerializer

use of org.sirix.service.xml.serialize.StAXSerializer in project sirix by sirixdb.

the class XdmNodeWriterTrxImpl method copy.

/**
 * Helper method for copy-operations.
 *
 * @param rtx the source {@link XdmNodeReadTrx}
 * @param insert the insertion strategy
 * @throws SirixException if anything fails in sirix
 */
private void copy(final XdmNodeReadTrx trx, final Insert insert) throws SirixException {
    assert trx != null;
    assert insert != null;
    final XdmNodeReadTrx rtx = trx.getResourceManager().beginNodeReadTrx(trx.getRevisionNumber());
    assert rtx.getRevisionNumber() == trx.getRevisionNumber();
    rtx.moveTo(trx.getNodeKey());
    assert rtx.getNodeKey() == trx.getNodeKey();
    if (rtx.getKind() == Kind.DOCUMENT) {
        rtx.moveToFirstChild();
    }
    if (!(rtx.isStructuralNode())) {
        throw new IllegalStateException("Node to insert must be a structural node (Text, PI, Comment, Document root or Element)!");
    }
    final Kind kind = rtx.getKind();
    switch(kind) {
        case TEXT:
            final String textValue = rtx.getValue();
            switch(insert) {
                case ASFIRSTCHILD:
                    insertTextAsFirstChild(textValue);
                    break;
                case ASLEFTSIBLING:
                    insertTextAsLeftSibling(textValue);
                    break;
                case ASRIGHTSIBLING:
                    insertTextAsRightSibling(textValue);
                    break;
                default:
                    throw new IllegalStateException();
            }
            break;
        case PROCESSING_INSTRUCTION:
            switch(insert) {
                case ASFIRSTCHILD:
                    insertPIAsFirstChild(rtx.getName().getLocalName(), rtx.getValue());
                    break;
                case ASLEFTSIBLING:
                    insertPIAsLeftSibling(rtx.getName().getLocalName(), rtx.getValue());
                    break;
                case ASRIGHTSIBLING:
                    insertPIAsRightSibling(rtx.getName().getLocalName(), rtx.getValue());
                    break;
                default:
                    throw new IllegalStateException();
            }
            break;
        case COMMENT:
            final String commentValue = rtx.getValue();
            switch(insert) {
                case ASFIRSTCHILD:
                    insertCommentAsFirstChild(commentValue);
                    break;
                case ASLEFTSIBLING:
                    insertCommentAsLeftSibling(commentValue);
                    break;
                case ASRIGHTSIBLING:
                    insertCommentAsRightSibling(commentValue);
                    break;
                default:
                    throw new IllegalStateException();
            }
            break;
        default:
            new XMLShredder.Builder(this, new StAXSerializer(rtx), insert).build().call();
    }
    rtx.close();
}
Also used : XdmNodeReadTrx(org.sirix.api.XdmNodeReadTrx) PageKind(org.sirix.page.PageKind) Kind(org.sirix.node.Kind) StAXSerializer(org.sirix.service.xml.serialize.StAXSerializer)

Aggregations

StAXSerializer (org.sirix.service.xml.serialize.StAXSerializer)2 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 XdmNodeReadTrx (org.sirix.api.XdmNodeReadTrx)1 SirixException (org.sirix.exception.SirixException)1 DiffAxis (org.sirix.gui.view.DiffAxis)1 Kind (org.sirix.node.Kind)1 PageKind (org.sirix.page.PageKind)1