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();
}
}
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();
}
}
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);
}
}
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());
}
}
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);
}
}
Aggregations