Search in sources :

Example 26 with Serializer

use of org.exist.storage.serializers.Serializer in project exist by eXist-db.

the class XQueryUpdateTest method append.

@Test
public void append() throws EXistException, PermissionDeniedException, XPathException, SAXException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        XQuery xquery = pool.getXQueryService();
        String query = "   declare variable $i external;\n" + "	update insert\n" + "		<product id='id{$i}' num='{$i}'>\n" + "			<description>Description {$i}</description>\n" + "			<price>{$i + 1.0}</price>\n" + "			<stock>{$i * 10}</stock>\n" + "		</product>\n" + "	into /products";
        XQueryContext context = new XQueryContext(pool);
        CompiledXQuery compiled = xquery.compile(context, query);
        for (int i = 0; i < ITEMS_TO_APPEND; i++) {
            context.declareVariable("i", Integer.valueOf(i));
            xquery.execute(broker, compiled, null);
        }
        Sequence seq = xquery.execute(broker, "/products", null);
        assertEquals(seq.getItemCount(), 1);
        final Serializer serializer = broker.borrowSerializer();
        try {
            serializer.serialize((NodeValue) seq.itemAt(0));
        } finally {
            broker.returnSerializer(serializer);
        }
        seq = xquery.execute(broker, "//product", null);
        assertEquals(ITEMS_TO_APPEND, seq.getItemCount());
        seq = xquery.execute(broker, "//product[price > 0.0]", null);
        assertEquals(ITEMS_TO_APPEND, seq.getItemCount());
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool) Serializer(org.exist.storage.serializers.Serializer)

Example 27 with Serializer

use of org.exist.storage.serializers.Serializer in project exist by eXist-db.

the class XQueryUpdateTest method appendAttributes.

@Test
public void appendAttributes() throws EXistException, PermissionDeniedException, XPathException, SAXException, LockException, IOException {
    append();
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        XQuery xquery = pool.getXQueryService();
        String query = "   declare variable $i external;\n" + "	update insert\n" + "		attribute name { concat('n', $i) }\n" + "	into //product[@num = $i]";
        XQueryContext context = new XQueryContext(pool);
        CompiledXQuery compiled = xquery.compile(context, query);
        for (int i = 0; i < ITEMS_TO_APPEND; i++) {
            context.declareVariable("i", Integer.valueOf(i));
            xquery.execute(broker, compiled, null);
        }
        Sequence seq = xquery.execute(broker, "/products", null);
        assertEquals(seq.getItemCount(), 1);
        final Serializer serializer = broker.borrowSerializer();
        try {
            serializer.serialize((NodeValue) seq.itemAt(0));
            seq = xquery.execute(broker, "//product", null);
            assertEquals(ITEMS_TO_APPEND, seq.getItemCount());
            seq = xquery.execute(broker, "//product[@name = 'n20']", null);
            assertEquals(1, seq.getItemCount());
            store(broker, "attribs.xml", "<test attr1='aaa' attr2='bbb'>ccc</test>");
            query = "update insert attribute attr1 { 'eee' } into /test";
            // testing duplicate attribute ...
            xquery.execute(broker, query, null);
            seq = xquery.execute(broker, "doc('" + TEST_COLLECTION + "/attribs.xml')/test[@attr1 = 'eee']", null);
            assertEquals(1, seq.getItemCount());
            serializer.serialize((NodeValue) seq.itemAt(0));
        } finally {
            broker.returnSerializer(serializer);
        }
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool) Serializer(org.exist.storage.serializers.Serializer)

Example 28 with Serializer

use of org.exist.storage.serializers.Serializer in project exist by eXist-db.

the class MatchListenerTest method queryResult2String.

private String queryResult2String(DBBroker broker, Sequence seq, int index) throws SAXException, XPathException {
    Properties props = new Properties();
    props.setProperty(OutputKeys.INDENT, "no");
    props.setProperty(EXistOutputKeys.HIGHLIGHT_MATCHES, "elements");
    final Serializer serializer = broker.borrowSerializer();
    try {
        serializer.setProperties(props);
        return serializer.serialize((NodeValue) seq.itemAt(index));
    } finally {
        broker.returnSerializer(serializer);
    }
}
Also used : Properties(java.util.Properties) Serializer(org.exist.storage.serializers.Serializer)

Example 29 with Serializer

use of org.exist.storage.serializers.Serializer in project exist by eXist-db.

the class LuceneMatchListenerTest method queryResult2String.

private String queryResult2String(final DBBroker broker, final Sequence seq, final boolean indent) throws SAXException, XPathException {
    final Properties props = new Properties();
    props.setProperty(OutputKeys.INDENT, indent ? "yes" : "no");
    props.setProperty(EXistOutputKeys.HIGHLIGHT_MATCHES, "elements");
    final Serializer serializer = broker.borrowSerializer();
    try {
        serializer.setProperties(props);
        return serializer.serialize((NodeValue) seq.itemAt(0));
    } finally {
        broker.returnSerializer(serializer);
    }
}
Also used : Properties(java.util.Properties) Serializer(org.exist.storage.serializers.Serializer)

Example 30 with Serializer

use of org.exist.storage.serializers.Serializer in project exist by eXist-db.

the class HighlightMatches method eval.

public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    if (args[0].isEmpty())
        return Sequence.EMPTY_SEQUENCE;
    context.pushDocumentContext();
    final Serializer serializer = context.getBroker().borrowSerializer();
    try (FunctionReference func = (FunctionReference) args[1].itemAt(0)) {
        MemTreeBuilder builder = context.getDocumentBuilder();
        NGramIndexWorker index = (NGramIndexWorker) context.getBroker().getIndexController().getWorkerByIndexId(NGramIndex.ID);
        DocumentBuilderReceiver docBuilder = new DocumentBuilderReceiver(builder);
        MatchCallback matchCb = new MatchCallback(func, docBuilder);
        ValueSequence result = new ValueSequence();
        for (SequenceIterator i = args[0].iterate(); i.hasNext(); ) {
            NodeValue v = (NodeValue) i.nextItem();
            try {
                int nodeNr = builder.getDocument().getLastNode();
                if (v.getImplementationType() == NodeValue.IN_MEMORY_NODE) {
                    ((NodeImpl) v).copyTo(context.getBroker(), docBuilder);
                } else {
                    NodeProxy p = (NodeProxy) v;
                    MatchListener ml = index.getMatchListener(context.getBroker(), p, matchCb);
                    Receiver receiver;
                    if (ml == null)
                        receiver = docBuilder;
                    else {
                        ml.setNextInChain(docBuilder);
                        receiver = ml;
                    }
                    serializer.setReceiver(receiver);
                    serializer.toReceiver((NodeProxy) v, false);
                }
                result.add(builder.getDocument().getNode(++nodeNr));
            } catch (SAXException e) {
                LOG.warn(e.getMessage(), e);
                throw new XPathException(this, e.getMessage());
            }
        }
        return result;
    } finally {
        context.getBroker().returnSerializer(serializer);
        context.popDocumentContext();
    }
}
Also used : NodeValue(org.exist.xquery.value.NodeValue) NodeImpl(org.exist.dom.memtree.NodeImpl) XPathException(org.exist.xquery.XPathException) NGramIndexWorker(org.exist.indexing.ngram.NGramIndexWorker) NGramMatchCallback(org.exist.indexing.ngram.NGramMatchCallback) DocumentBuilderReceiver(org.exist.dom.memtree.DocumentBuilderReceiver) Receiver(org.exist.util.serializer.Receiver) DocumentBuilderReceiver(org.exist.dom.memtree.DocumentBuilderReceiver) NodeProxy(org.exist.dom.persistent.NodeProxy) SAXException(org.xml.sax.SAXException) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) SequenceIterator(org.exist.xquery.value.SequenceIterator) ValueSequence(org.exist.xquery.value.ValueSequence) FunctionReference(org.exist.xquery.value.FunctionReference) MatchListener(org.exist.indexing.MatchListener) Serializer(org.exist.storage.serializers.Serializer)

Aggregations

Serializer (org.exist.storage.serializers.Serializer)64 SAXException (org.xml.sax.SAXException)21 DBBroker (org.exist.storage.DBBroker)16 LockedDocument (org.exist.dom.persistent.LockedDocument)15 SAXSerializer (org.exist.util.serializer.SAXSerializer)14 BrokerPool (org.exist.storage.BrokerPool)11 Properties (java.util.Properties)10 Sequence (org.exist.xquery.value.Sequence)10 Txn (org.exist.storage.txn.Txn)8 XPathException (org.exist.xquery.XPathException)8 StringWriter (java.io.StringWriter)7 Collection (org.exist.collections.Collection)7 IOException (java.io.IOException)6 Item (org.exist.xquery.value.Item)6 Path (java.nio.file.Path)5 EXistException (org.exist.EXistException)5 SequenceIterator (org.exist.xquery.value.SequenceIterator)5 NodeProxy (org.exist.dom.persistent.NodeProxy)4 TransactionManager (org.exist.storage.txn.TransactionManager)4 NodeValue (org.exist.xquery.value.NodeValue)4