Search in sources :

Example 26 with MemTreeBuilder

use of org.exist.dom.memtree.MemTreeBuilder 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 27 with MemTreeBuilder

use of org.exist.dom.memtree.MemTreeBuilder in project exist by eXist-db.

the class ExceptTest method createInMemoryDocument.

private Document createInMemoryDocument() {
    final MemTreeBuilder memtree = new MemTreeBuilder();
    memtree.startDocument();
    memtree.startElement(new QName("m1", XMLConstants.NULL_NS_URI), null);
    memtree.startElement(new QName("m2", XMLConstants.NULL_NS_URI), null);
    memtree.characters("test data");
    memtree.endElement();
    memtree.endElement();
    memtree.endDocument();
    return memtree.getDocument();
}
Also used : MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) QName(org.exist.dom.QName)

Example 28 with MemTreeBuilder

use of org.exist.dom.memtree.MemTreeBuilder in project exist by eXist-db.

the class ValueSequenceTest method sortInDocumentOrder.

@Test
public void sortInDocumentOrder() throws EXistException, PermissionDeniedException, AuthenticationException {
    final ValueSequence seq = new ValueSequence(true);
    seq.keepUnOrdered(true);
    // in-memory doc
    final MemTreeBuilder memtree = new MemTreeBuilder();
    memtree.startDocument();
    memtree.startElement(new QName("m1", XMLConstants.NULL_NS_URI), null);
    memtree.startElement(new QName("m2", XMLConstants.NULL_NS_URI), null);
    memtree.characters("test data");
    memtree.endElement();
    memtree.endElement();
    memtree.endDocument();
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    final Subject admin = pool.getSecurityManager().authenticate("admin", "");
    try (final DBBroker broker = pool.get(Optional.of(admin))) {
        // persistent doc
        final Collection sysCollection = broker.getCollection(SecurityManager.SECURITY_COLLECTION_URI);
        final DocumentImpl doc = sysCollection.getDocument(broker, XmldbURI.create("config.xml"));
        final NodeProxy docProxy = new NodeProxy(doc);
        final NodeProxy nodeProxy = new NodeProxy(doc, ((NodeImpl) doc.getFirstChild()).getNodeId());
        seq.add(memtree.getDocument());
        seq.add(docProxy);
        seq.add((org.exist.dom.memtree.NodeImpl) memtree.getDocument().getFirstChild());
        seq.add(nodeProxy);
        // call sort
        seq.sortInDocumentOrder();
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) QName(org.exist.dom.QName) Collection(org.exist.collections.Collection) DocumentImpl(org.exist.dom.persistent.DocumentImpl) NodeProxy(org.exist.dom.persistent.NodeProxy) BrokerPool(org.exist.storage.BrokerPool)

Example 29 with MemTreeBuilder

use of org.exist.dom.memtree.MemTreeBuilder in project exist by eXist-db.

the class Sync method startSync.

private Sequence startSync(final String target, final String collectionPath, final Map<String, Sequence> options) throws XPathException {
    final Date startDate = options.get(AFTER_OPT).hasOne() ? ((DateTimeValue) options.get(AFTER_OPT)).getDate() : null;
    final boolean prune = ((BooleanValue) options.get(PRUNE_OPT)).getValue();
    final List<String> excludes = new ArrayList<>(Collections.emptyList());
    for (final SequenceIterator si = options.get(EXCLUDES_OPT).iterate(); si.hasNext(); ) {
        excludes.add(si.nextItem().getStringValue());
    }
    final Path p = FileModuleHelper.getFile(target);
    context.pushDocumentContext();
    final MemTreeBuilder output = context.getDocumentBuilder();
    final Path targetDir;
    try {
        if (p.isAbsolute()) {
            targetDir = p;
        } else {
            final Optional<Path> home = context.getBroker().getConfiguration().getExistHome();
            targetDir = FileUtils.resolve(home, target);
        }
        output.startDocument();
        output.startElement(FILE_SYNC_ELEMENT, null);
        output.addAttribute(FILE_COLLECTION_ATTRIBUTE, collectionPath);
        output.addAttribute(FILE_DIR_ATTRIBUTE, targetDir.toAbsolutePath().toString());
        final String rootTargetAbsPath = targetDir.toAbsolutePath().toString();
        final String separator = rootTargetAbsPath.endsWith(File.separator) ? "" : File.separator;
        syncCollection(XmldbURI.create(collectionPath), rootTargetAbsPath + separator, targetDir, startDate, prune, excludes, output);
        output.endElement();
        output.endDocument();
    } catch (final PermissionDeniedException | LockException e) {
        throw new XPathException(this, e);
    } finally {
        context.popDocumentContext();
    }
    return output.getDocument();
}
Also used : Path(java.nio.file.Path) XPathException(org.exist.xquery.XPathException) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) LockException(org.exist.util.LockException) PermissionDeniedException(org.exist.security.PermissionDeniedException)

Example 30 with MemTreeBuilder

use of org.exist.dom.memtree.MemTreeBuilder in project exist by eXist-db.

the class Deploy method statusReport.

private Sequence statusReport(final Optional<String> target) {
    context.pushDocumentContext();
    try {
        final MemTreeBuilder builder = context.getDocumentBuilder();
        final AttributesImpl attrs = new AttributesImpl();
        if (target.isPresent()) {
            attrs.addAttribute("", "result", "result", "CDATA", "ok");
            attrs.addAttribute("", "target", "target", "CDATA", target.get());
        } else {
            attrs.addAttribute("", "result", "result", "CDATA", "fail");
        }
        builder.startElement(STATUS_ELEMENT, attrs);
        builder.endElement();
        return builder.getDocument().getNode(1);
    } finally {
        context.popDocumentContext();
    }
}
Also used : AttributesImpl(org.xml.sax.helpers.AttributesImpl) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder)

Aggregations

MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)81 QName (org.exist.dom.QName)28 XPathException (org.exist.xquery.XPathException)23 Sequence (org.exist.xquery.value.Sequence)19 DocumentBuilderReceiver (org.exist.dom.memtree.DocumentBuilderReceiver)17 SAXException (org.xml.sax.SAXException)16 NodeValue (org.exist.xquery.value.NodeValue)15 IOException (java.io.IOException)14 NodeImpl (org.exist.dom.memtree.NodeImpl)14 Item (org.exist.xquery.value.Item)8 SequenceIterator (org.exist.xquery.value.SequenceIterator)8 Test (org.junit.Test)8 XQueryContext (org.exist.xquery.XQueryContext)7 ValueSequence (org.exist.xquery.value.ValueSequence)7 AttributesImpl (org.xml.sax.helpers.AttributesImpl)7 DocumentImpl (org.exist.dom.memtree.DocumentImpl)6 BrokerPool (org.exist.storage.BrokerPool)6 InputSource (org.xml.sax.InputSource)6 MalformedURLException (java.net.MalformedURLException)5 Path (java.nio.file.Path)5