Search in sources :

Example 1 with NodeHandle

use of org.exist.dom.persistent.NodeHandle in project exist by eXist-db.

the class EmbeddedXMLStreamReaderTest method assertNodesIn.

public void assertNodesIn(final NamedEvent[] expected, final Function<Document, NodeHandle> initialNodeFun, final Optional<Function<Document, NodeHandle>> containerFun) throws EXistException, PermissionDeniedException, IOException, XMLStreamException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Txn transaction = pool.getTransactionManager().beginTransaction()) {
        try (final LockedDocument lockedDocument = broker.getXMLResource(TEST_MIXED_XML_COLLECTION.append(MIXED_XML_NAME), Lock.LockMode.WRITE_LOCK)) {
            assertNotNull(lockedDocument);
            final Document document = lockedDocument.getDocument();
            assertNotNull(document);
            final NodeHandle initialNode = initialNodeFun.apply(document);
            final Optional<NodeHandle> maybeContainerNode = containerFun.map(f -> f.apply(document));
            final IEmbeddedXMLStreamReader xmlStreamReader = broker.getXMLStreamReader(initialNode, false);
            final NamedEvent[] actual = readAllEvents(maybeContainerNode, xmlStreamReader);
            assertArrayEquals(formatExpectedActual(expected, actual), expected, actual);
        }
        transaction.commit();
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) NodeHandle(org.exist.dom.persistent.NodeHandle) LockedDocument(org.exist.dom.persistent.LockedDocument) Txn(org.exist.storage.txn.Txn) Document(org.w3c.dom.Document) LockedDocument(org.exist.dom.persistent.LockedDocument) NamedEvent(org.exist.stax.EmbeddedXMLStreamReaderTest.NamedEvent) BrokerPool(org.exist.storage.BrokerPool)

Example 2 with NodeHandle

use of org.exist.dom.persistent.NodeHandle in project exist by eXist-db.

the class Modification method deepCopy.

protected Sequence deepCopy(Sequence inSeq) throws XPathException {
    context.pushDocumentContext();
    final MemTreeBuilder builder = context.getDocumentBuilder();
    final DocumentBuilderReceiver receiver = new DocumentBuilderReceiver(builder);
    final Serializer serializer = context.getBroker().borrowSerializer();
    serializer.setReceiver(receiver);
    try {
        final Sequence out = new ValueSequence();
        for (final SequenceIterator i = inSeq.iterate(); i.hasNext(); ) {
            Item item = i.nextItem();
            if (item.getType() == Type.DOCUMENT) {
                if (((NodeValue) item).getImplementationType() == NodeValue.PERSISTENT_NODE) {
                    final NodeHandle root = (NodeHandle) ((NodeProxy) item).getOwnerDocument().getDocumentElement();
                    item = new NodeProxy(root);
                } else {
                    item = (Item) ((Document) item).getDocumentElement();
                }
            }
            if (Type.subTypeOf(item.getType(), Type.NODE)) {
                if (((NodeValue) item).getImplementationType() == NodeValue.PERSISTENT_NODE) {
                    final int last = builder.getDocument().getLastNode();
                    final NodeProxy p = (NodeProxy) item;
                    serializer.toReceiver(p, false, false);
                    if (p.getNodeType() == Node.ATTRIBUTE_NODE) {
                        item = builder.getDocument().getLastAttr();
                    } else {
                        item = builder.getDocument().getNode(last + 1);
                    }
                } else {
                    ((org.exist.dom.memtree.NodeImpl) item).deepCopy();
                }
            }
            out.add(item);
        }
        return out;
    } catch (final SAXException | DOMException e) {
        throw new XPathException(this, e.getMessage(), e);
    } finally {
        context.getBroker().returnSerializer(serializer);
        context.popDocumentContext();
    }
}
Also used : ValueSequence(org.exist.xquery.value.ValueSequence) Sequence(org.exist.xquery.value.Sequence) DocumentBuilderReceiver(org.exist.dom.memtree.DocumentBuilderReceiver) Document(org.w3c.dom.Document) NodeProxy(org.exist.dom.persistent.NodeProxy) SAXException(org.xml.sax.SAXException) Item(org.exist.xquery.value.Item) DOMException(org.w3c.dom.DOMException) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) SequenceIterator(org.exist.xquery.value.SequenceIterator) NodeHandle(org.exist.dom.persistent.NodeHandle) ValueSequence(org.exist.xquery.value.ValueSequence) Serializer(org.exist.storage.serializers.Serializer)

Example 3 with NodeHandle

use of org.exist.dom.persistent.NodeHandle in project exist by eXist-db.

the class FunDoc method registerUpdateListener.

protected void registerUpdateListener() {
    if (listener == null) {
        listener = new UpdateListener() {

            @Override
            public void documentUpdated(DocumentImpl document, int event) {
            // clear all
            }

            @Override
            public void unsubscribe() {
                FunDoc.this.listener = null;
            }

            public void nodeMoved(NodeId oldNodeId, NodeHandle newNode) {
            // not relevant
            }

            @Override
            public void debug() {
                logger.debug("UpdateListener: Line: {}: {}", getLine(), FunDoc.this.toString());
            }
        };
        context.registerUpdateListener(listener);
    }
}
Also used : NodeHandle(org.exist.dom.persistent.NodeHandle) NodeId(org.exist.numbering.NodeId) UpdateListener(org.exist.storage.UpdateListener) DocumentImpl(org.exist.dom.persistent.DocumentImpl)

Example 4 with NodeHandle

use of org.exist.dom.persistent.NodeHandle in project exist by eXist-db.

the class DLNStorageTest method nodeStorage.

@Test
public void nodeStorage() throws Exception {
    BrokerPool pool = BrokerPool.getInstance();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        XQuery xquery = pool.getXQueryService();
        assertNotNull(xquery);
        // test element ids
        Sequence seq = xquery.execute(broker, "doc('/db/test/test_string.xml')/test/para", null);
        assertEquals(3, seq.getItemCount());
        NodeProxy comment = (NodeProxy) seq.itemAt(0);
        assertEquals("1.1", comment.getNodeId().toString());
        comment = (NodeProxy) seq.itemAt(1);
        assertEquals("1.3", comment.getNodeId().toString());
        comment = (NodeProxy) seq.itemAt(2);
        assertEquals("1.5", comment.getNodeId().toString());
        seq = xquery.execute(broker, "doc('/db/test/test_string.xml')/test//a", null);
        assertEquals(1, seq.getItemCount());
        NodeProxy a = (NodeProxy) seq.itemAt(0);
        assertEquals("1.3.2", a.getNodeId().toString());
        // test attribute id
        seq = xquery.execute(broker, "doc('/db/test/test_string.xml')/test//a/@href", null);
        assertEquals(1, seq.getItemCount());
        NodeProxy href = (NodeProxy) seq.itemAt(0);
        StorageAddress.toString(href);
        assertEquals("1.3.2.1", href.getNodeId().toString());
        // test Attr deserialization
        Attr attr = (Attr) href.getNode();
        StorageAddress.toString(((NodeHandle) attr));
        // test Attr fields
        assertEquals("href", attr.getNodeName());
        assertEquals("href", attr.getName());
        assertEquals("#", attr.getValue());
        // test DOMFile.getNodeValue()
        assertEquals("#", href.getStringValue());
        // test text node
        seq = xquery.execute(broker, "doc('/db/test/test_string.xml')/test//b/text()", null);
        assertEquals(1, seq.getItemCount());
        NodeProxy text = (NodeProxy) seq.itemAt(0);
        assertEquals("1.5.2.1", text.getNodeId().toString());
        // test DOMFile.getNodeValue()
        assertEquals("paragraph", text.getStringValue());
        // test Text deserialization
        Text node = (Text) text.getNode();
        assertEquals("paragraph", node.getNodeValue());
        assertEquals("paragraph", node.getData());
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) NodeHandle(org.exist.dom.persistent.NodeHandle) XQuery(org.exist.xquery.XQuery) Text(org.w3c.dom.Text) Sequence(org.exist.xquery.value.Sequence) NodeProxy(org.exist.dom.persistent.NodeProxy) BrokerPool(org.exist.storage.BrokerPool) Attr(org.w3c.dom.Attr)

Example 5 with NodeHandle

use of org.exist.dom.persistent.NodeHandle in project exist by eXist-db.

the class RootNode method registerUpdateListener.

protected void registerUpdateListener() {
    if (listener == null) {
        listener = new UpdateListener() {

            @Override
            public void documentUpdated(DocumentImpl document, int event) {
                // clear all
                cachedDocs = null;
                cached = null;
            }

            @Override
            public void unsubscribe() {
                RootNode.this.listener = null;
            }

            @Override
            public void nodeMoved(NodeId oldNodeId, NodeHandle newNode) {
            // not relevant
            }

            @Override
            public void debug() {
                LOG.debug("UpdateListener: Line: {}", RootNode.this.toString());
            }
        };
        context.registerUpdateListener(listener);
    }
}
Also used : NodeHandle(org.exist.dom.persistent.NodeHandle) NodeId(org.exist.numbering.NodeId) UpdateListener(org.exist.storage.UpdateListener) DocumentImpl(org.exist.dom.persistent.DocumentImpl)

Aggregations

NodeHandle (org.exist.dom.persistent.NodeHandle)5 DocumentImpl (org.exist.dom.persistent.DocumentImpl)2 NodeProxy (org.exist.dom.persistent.NodeProxy)2 NodeId (org.exist.numbering.NodeId)2 BrokerPool (org.exist.storage.BrokerPool)2 DBBroker (org.exist.storage.DBBroker)2 UpdateListener (org.exist.storage.UpdateListener)2 Sequence (org.exist.xquery.value.Sequence)2 Document (org.w3c.dom.Document)2 DocumentBuilderReceiver (org.exist.dom.memtree.DocumentBuilderReceiver)1 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)1 LockedDocument (org.exist.dom.persistent.LockedDocument)1 NamedEvent (org.exist.stax.EmbeddedXMLStreamReaderTest.NamedEvent)1 Serializer (org.exist.storage.serializers.Serializer)1 Txn (org.exist.storage.txn.Txn)1 XQuery (org.exist.xquery.XQuery)1 Item (org.exist.xquery.value.Item)1 SequenceIterator (org.exist.xquery.value.SequenceIterator)1 ValueSequence (org.exist.xquery.value.ValueSequence)1 Attr (org.w3c.dom.Attr)1