Search in sources :

Example 61 with QNm

use of org.brackit.xquery.atomic.QNm in project sirix by sirixdb.

the class SAXSerializer method generateElement.

/**
 * Generate a start element event.
 *
 * @param rtx {@link XdmNodeReadTrx} implementation
 */
private void generateElement(final XdmNodeReadTrx rtx) {
    final AttributesImpl atts = new AttributesImpl();
    final long key = rtx.getNodeKey();
    try {
        // Process namespace nodes.
        for (int i = 0, namesCount = rtx.getNamespaceCount(); i < namesCount; i++) {
            rtx.moveToNamespace(i);
            final QNm qName = rtx.getName();
            mContHandler.startPrefixMapping(qName.getPrefix(), qName.getNamespaceURI());
            final String mURI = qName.getNamespaceURI();
            if (qName.getPrefix() == null || qName.getPrefix().length() == 0) {
                atts.addAttribute(mURI, "xmlns", "xmlns", "CDATA", mURI);
            } else {
                atts.addAttribute(mURI, "xmlns", "xmlns:" + rtx.getName().getPrefix(), "CDATA", mURI);
            }
            rtx.moveTo(key);
        }
        // Process attributes.
        for (int i = 0, attCount = rtx.getAttributeCount(); i < attCount; i++) {
            rtx.moveToAttribute(i);
            final QNm qName = rtx.getName();
            final String mURI = qName.getNamespaceURI();
            atts.addAttribute(mURI, qName.getLocalName(), Utils.buildName(qName), rtx.getType(), rtx.getValue());
            rtx.moveTo(key);
        }
        // Create SAX events.
        final QNm qName = rtx.getName();
        mContHandler.startElement(qName.getNamespaceURI(), qName.getLocalName(), Utils.buildName(qName), atts);
        // Empty elements.
        if (!rtx.hasFirstChild()) {
            mContHandler.endElement(qName.getNamespaceURI(), qName.getLocalName(), Utils.buildName(qName));
        }
    } catch (final SAXException e) {
        LOGGER.error(e.getMessage(), e);
    }
}
Also used : QNm(org.brackit.xquery.atomic.QNm) AttributesImpl(org.xml.sax.helpers.AttributesImpl) SAXException(org.xml.sax.SAXException)

Example 62 with QNm

use of org.brackit.xquery.atomic.QNm in project sirix by sirixdb.

the class StAXSerializer method emitNode.

/**
 * Emit a node.
 *
 * @param rtx Sirix reading transaction {@link XdmNodeReadTrx}
 */
private void emitNode(final XdmNodeReadTrx rtx) {
    switch(rtx.getKind()) {
        case DOCUMENT:
            mEvent = mFac.createStartDocument();
            break;
        case ELEMENT:
            final long key = rtx.getNodeKey();
            final QNm qName = rtx.getName();
            mEvent = mFac.createStartElement(new QName(qName.getNamespaceURI(), qName.getLocalName(), qName.getPrefix()), new AttributeIterator(rtx), new NamespaceIterator(rtx));
            rtx.moveTo(key);
            break;
        case TEXT:
            mEvent = mFac.createCharacters(XMLToken.escapeContent(rtx.getValue()));
            break;
        case COMMENT:
            mEvent = mFac.createComment(XMLToken.escapeContent(rtx.getValue()));
            break;
        case PROCESSING_INSTRUCTION:
            mEvent = mFac.createProcessingInstruction(rtx.getName().getLocalName(), rtx.getValue());
            break;
        default:
            throw new IllegalStateException("Kind not known!");
    }
}
Also used : QNm(org.brackit.xquery.atomic.QNm) QName(javax.xml.namespace.QName)

Example 63 with QNm

use of org.brackit.xquery.atomic.QNm in project sirix by sirixdb.

the class FMSE method emitInsert.

/**
 * Emit an insert operation.
 *
 * @param parent parent of the current {@link Node} implementation reference to insert
 * @param child the current node to insert
 * @param pos position of the insert
 * @param wtx {@link XdmNodeWriteTrx} implementation reference on old revision
 * @param rtx {@link XdmNodeReadTrx} implementation reference on new revision
 * @return inserted {@link Node} implementation reference
 * @throws SirixException if anything in sirix fails
 */
private long emitInsert(final long child, final long parent, final int pos, final XdmNodeWriteTrx wtx, final XdmNodeReadTrx rtx) {
    assert child >= 0;
    assert parent >= 0;
    assert wtx != null;
    assert rtx != null;
    // Determines if node has been already inserted (for subtrees).
    if (mAlreadyInserted.get(child) != null) {
        // actually child'
        return child;
    }
    wtx.moveTo(parent);
    rtx.moveTo(child);
    try {
        switch(rtx.getKind()) {
            case ATTRIBUTE:
                try {
                    wtx.insertAttribute(rtx.getName(), rtx.getValue());
                } catch (final SirixUsageException e) {
                    mTotalMatching.remove(wtx.getNodeKey());
                    wtx.setValue(rtx.getValue());
                }
                process(wtx.getNodeKey(), rtx.getNodeKey());
                break;
            case NAMESPACE:
                // Note that the insertion is right (localPart as prefix).
                try {
                    wtx.insertNamespace(new QNm(rtx.getName().getNamespaceURI(), rtx.getName().getLocalName(), ""));
                } catch (final SirixUsageException e) {
                    mTotalMatching.remove(wtx.getNodeKey());
                }
                process(wtx.getNodeKey(), rtx.getNodeKey());
                break;
            default:
                // In case of other node types.
                long oldKey = 0;
                if (pos == 0) {
                    switch(rtx.getKind()) {
                        case ELEMENT:
                            oldKey = wtx.copySubtreeAsFirstChild(rtx).getNodeKey();
                            break;
                        case TEXT:
                            // is inserted.
                            if (wtx.hasFirstChild()) {
                                wtx.moveToFirstChild();
                                if (wtx.getKind() == Kind.TEXT) {
                                    mTotalMatching.remove(wtx.getNodeKey());
                                    wtx.remove();
                                }
                                wtx.moveTo(parent);
                            }
                            oldKey = wtx.insertTextAsFirstChild(rtx.getValue()).getNodeKey();
                            break;
                        default:
                    }
                } else {
                    assert wtx.hasFirstChild();
                    wtx.moveToFirstChild();
                    for (int i = 0; i < pos - 1; i++) {
                        assert wtx.hasRightSibling();
                        wtx.moveToRightSibling();
                    }
                    // Remove right sibl. text node if a text node already exists.
                    removeRightSiblingTextNode(wtx);
                    switch(rtx.getKind()) {
                        case ELEMENT:
                            oldKey = wtx.copySubtreeAsRightSibling(rtx).getNodeKey();
                            break;
                        case TEXT:
                            oldKey = wtx.insertTextAsRightSibling(rtx.getValue()).getNodeKey();
                            break;
                        default:
                            // Already inserted.
                            throw new IllegalStateException("Child should be already inserted!");
                    }
                }
                // Mark all nodes in subtree as inserted.
                wtx.moveTo(oldKey);
                rtx.moveTo(child);
                for (final Axis oldAxis = new DescendantAxis(wtx, IncludeSelf.YES), newAxis = new DescendantAxis(rtx, IncludeSelf.YES); oldAxis.hasNext() && newAxis.hasNext(); ) {
                    oldAxis.next();
                    newAxis.next();
                    final XdmNodeReadTrx oldRtx = oldAxis.getTrx();
                    final XdmNodeReadTrx newRtx = newAxis.getTrx();
                    process(oldRtx.getNodeKey(), newRtx.getNodeKey());
                    final long newNodeKey = newRtx.getNodeKey();
                    final long oldNodeKey = oldRtx.getNodeKey();
                    if (newRtx.getKind() == Kind.ELEMENT) {
                        assert newRtx.getKind() == oldRtx.getKind();
                        if (newRtx.getAttributeCount() > 0) {
                            for (int i = 0, attCount = newRtx.getAttributeCount(); i < attCount; i++) {
                                rtx.moveToAttribute(i);
                                for (int j = 0, oldAttCount = oldRtx.getAttributeCount(); i < oldAttCount; j++) {
                                    wtx.moveToAttribute(j);
                                    if (wtx.getName().equals(rtx.getName())) {
                                        process(oldAxis.getTrx().getNodeKey(), newAxis.getTrx().getNodeKey());
                                        break;
                                    }
                                    oldAxis.getTrx().moveTo(oldNodeKey);
                                }
                                newAxis.getTrx().moveTo(newNodeKey);
                            }
                        }
                        if (newRtx.getNamespaceCount() > 0) {
                            for (int i = 0, nspCount = newRtx.getNamespaceCount(); i < nspCount; i++) {
                                rtx.moveToNamespace(i);
                                for (int j = 0, oldNspCount = oldRtx.getNamespaceCount(); j < oldNspCount; j++) {
                                    wtx.moveToNamespace(j);
                                    if (wtx.getName().getNamespaceURI().equals(rtx.getName().getNamespaceURI()) && wtx.getName().getPrefix().equals(wtx.getName().getPrefix())) {
                                        process(wtx.getNodeKey(), rtx.getNodeKey());
                                        break;
                                    }
                                    oldAxis.getTrx().moveTo(oldNodeKey);
                                }
                                newAxis.getTrx().moveTo(newNodeKey);
                            }
                        }
                    }
                    newAxis.getTrx().moveTo(newNodeKey);
                }
        }
    } catch (final SirixException e) {
        LOGWRAPPER.error(e.getMessage(), e);
    }
    return wtx.getNodeKey();
}
Also used : QNm(org.brackit.xquery.atomic.QNm) XdmNodeReadTrx(org.sirix.api.XdmNodeReadTrx) SirixException(org.sirix.exception.SirixException) SirixUsageException(org.sirix.exception.SirixUsageException) VisitorDescendantAxis(org.sirix.axis.visitor.VisitorDescendantAxis) DescendantAxis(org.sirix.axis.DescendantAxis) Axis(org.sirix.api.Axis) VisitorDescendantAxis(org.sirix.axis.visitor.VisitorDescendantAxis) AbstractAxis(org.sirix.axis.AbstractAxis) ChildAxis(org.sirix.axis.ChildAxis) PostOrderAxis(org.sirix.axis.PostOrderAxis) DescendantAxis(org.sirix.axis.DescendantAxis) LevelOrderAxis(org.sirix.axis.LevelOrderAxis)

Example 64 with QNm

use of org.brackit.xquery.atomic.QNm in project sirix by sirixdb.

the class NameFilter method filter.

@Override
public <K extends Comparable<? super K>> boolean filter(final AVLNode<K, NodeReferences> node) {
    if (!(node.getKey() instanceof QNm))
        throw new IllegalStateException("Key is not of type QNm!");
    final QNm name = (QNm) node.getKey();
    final boolean included = (mIncludes.isEmpty() || mIncludes.contains(name));
    final boolean excluded = (!mExcludes.isEmpty() && mExcludes.contains(name));
    if (!included || excluded) {
        return false;
    }
    return true;
}
Also used : QNm(org.brackit.xquery.atomic.QNm)

Example 65 with QNm

use of org.brackit.xquery.atomic.QNm in project sirix by sirixdb.

the class NameIndexBuilder method visit.

@Override
public VisitResult visit(final ImmutableElement node) {
    final QNm name = node.getName();
    final boolean included = (mIncludes.isEmpty() || mIncludes.contains(name));
    final boolean excluded = (!mExcludes.isEmpty() && mExcludes.contains(name));
    if (!included || excluded) {
        return VisitResultType.CONTINUE;
    }
    final Optional<NodeReferences> textReferences = mAVLTreeWriter.get(name, SearchMode.EQUAL);
    try {
        if (textReferences.isPresent()) {
            setNodeReferences(node, textReferences.get(), name);
        } else {
            setNodeReferences(node, new NodeReferences(), name);
        }
    } catch (final SirixIOException e) {
        LOGGER.error(e.getMessage(), e);
    }
    return VisitResultType.CONTINUE;
}
Also used : QNm(org.brackit.xquery.atomic.QNm) NodeReferences(org.sirix.index.avltree.keyvalue.NodeReferences) SirixIOException(org.sirix.exception.SirixIOException)

Aggregations

QNm (org.brackit.xquery.atomic.QNm)89 XdmNodeReadTrx (org.sirix.api.XdmNodeReadTrx)20 Test (org.junit.Test)18 QueryException (org.brackit.xquery.QueryException)15 XdmNodeWriteTrx (org.sirix.api.XdmNodeWriteTrx)15 DBNode (org.sirix.xquery.node.DBNode)12 QName (javax.xml.namespace.QName)11 PathSummaryReader (org.sirix.index.path.summary.PathSummaryReader)11 IndexController (org.sirix.access.IndexController)10 IndexDef (org.sirix.index.IndexDef)10 Item (org.brackit.xquery.xdm.Item)7 Axis (org.sirix.api.Axis)7 DescendantAxis (org.sirix.axis.DescendantAxis)7 SirixException (org.sirix.exception.SirixException)7 Attribute (javax.xml.stream.events.Attribute)5 Namespace (javax.xml.stream.events.Namespace)5 DocumentException (org.brackit.xquery.xdm.DocumentException)5 SirixIOException (org.sirix.exception.SirixIOException)5 Ignore (org.junit.Ignore)4 SirixUsageException (org.sirix.exception.SirixUsageException)4