Search in sources :

Example 1 with DefaultDocumentSet

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

the class TestDataGenerator method generate.

public Path[] generate(final DBBroker broker, final Collection collection, final String xqueryContent) throws SAXException {
    try {
        final DocumentSet docs = collection.allDocs(broker, new DefaultDocumentSet(), true);
        final XQuery service = broker.getBrokerPool().getXQueryService();
        final XQueryContext context = new XQueryContext(broker.getBrokerPool());
        context.declareVariable("filename", "");
        context.declareVariable("count", "0");
        context.setStaticallyKnownDocuments(docs);
        final String query = IMPORT + xqueryContent;
        final CompiledXQuery compiled = service.compile(context, query);
        for (int i = 0; i < count; i++) {
            generatedFiles[i] = Files.createTempFile(prefix, ".xml");
            context.declareVariable("filename", generatedFiles[i].getFileName().toString());
            context.declareVariable("count", new Integer(i));
            final Sequence results = service.execute(broker, compiled, Sequence.EMPTY_SEQUENCE);
            final Serializer serializer = broker.borrowSerializer();
            try (final Writer out = Files.newBufferedWriter(generatedFiles[i], StandardCharsets.UTF_8)) {
                final SAXSerializer sax = new SAXSerializer(out, outputProps);
                serializer.setSAXHandlers(sax, sax);
                for (final SequenceIterator iter = results.iterate(); iter.hasNext(); ) {
                    final Item item = iter.nextItem();
                    if (!Type.subTypeOf(item.getType(), Type.NODE)) {
                        continue;
                    }
                    serializer.toSAX((NodeValue) item);
                }
            } finally {
                broker.returnSerializer(serializer);
            }
        }
    } catch (final XPathException | PermissionDeniedException | LockException | IOException e) {
        LOG.error(e.getMessage(), e);
        throw new SAXException(e.getMessage(), e);
    }
    return generatedFiles;
}
Also used : DefaultDocumentSet(org.exist.dom.persistent.DefaultDocumentSet) XPathException(org.exist.xquery.XPathException) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQueryContext(org.exist.xquery.XQueryContext) SAXException(org.xml.sax.SAXException) LockException(org.exist.util.LockException) PermissionDeniedException(org.exist.security.PermissionDeniedException) DefaultDocumentSet(org.exist.dom.persistent.DefaultDocumentSet) DocumentSet(org.exist.dom.persistent.DocumentSet) SAXSerializer(org.exist.util.serializer.SAXSerializer) SAXSerializer(org.exist.util.serializer.SAXSerializer) Serializer(org.exist.storage.serializers.Serializer)

Example 2 with DefaultDocumentSet

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

the class TestTrigger method addRecord.

private void addRecord(DBBroker broker, Txn transaction, String xupdate) throws TriggerException {
    MutableDocumentSet docs = new DefaultDocumentSet();
    docs.add(doc);
    final boolean triggersEnabled = broker.isTriggersEnabled();
    try {
        // IMPORTANT: temporarily disable triggers on the collection.
        // We would end up in infinite recursion if we don't do that
        broker.setTriggersEnabled(false);
        // create the XUpdate processor
        XUpdateProcessor processor = new XUpdateProcessor(broker, docs);
        // process the XUpdate
        Modification[] modifications = processor.parse(new InputSource(new StringReader(xupdate)));
        for (int i = 0; i < modifications.length; i++) {
            modifications[i].process(transaction);
        }
        broker.flush();
    } catch (Exception e) {
        throw new TriggerException(e.getMessage(), e);
    } finally {
        // IMPORTANT: reenable trigger processing for the collection.
        broker.setTriggersEnabled(triggersEnabled);
    }
}
Also used : XUpdateProcessor(org.exist.xupdate.XUpdateProcessor) MutableDocumentSet(org.exist.dom.persistent.MutableDocumentSet) Modification(org.exist.xupdate.Modification) InputSource(org.xml.sax.InputSource) StringInputSource(org.exist.util.StringInputSource) DefaultDocumentSet(org.exist.dom.persistent.DefaultDocumentSet) StringReader(java.io.StringReader)

Example 3 with DefaultDocumentSet

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

the class HistoryTriggerTest method checkHistoryOfOriginal.

private void checkHistoryOfOriginal(final BrokerPool brokerPool, final XmldbURI originalDocName, final String orginalDocContent) throws EXistException, PermissionDeniedException, LockException {
    try (final DBBroker broker = brokerPool.get(Optional.of(brokerPool.getSecurityManager().getSystemSubject()));
        final Txn transaction = brokerPool.getTransactionManager().beginTransaction()) {
        try (final Collection historyCollection = broker.openCollection(HistoryTrigger.DEFAULT_ROOT_PATH.append(TEST_COLLECTION_URI).append(originalDocName), Lock.LockMode.READ_LOCK)) {
            assertNotNull(historyCollection);
            final DocumentSet documentSet = historyCollection.getDocuments(broker, new DefaultDocumentSet());
            assertEquals(1, documentSet.getDocumentCount());
            final Iterator<DocumentImpl> it = documentSet.getDocumentIterator();
            assertTrue(it.hasNext());
            final DocumentImpl doc = it.next();
            final Diff diff = DiffBuilder.compare(Input.from(orginalDocContent)).withTest(Input.from(doc)).build();
            assertFalse(diff.toString(), diff.hasDifferences());
            assertFalse(it.hasNext());
        }
        transaction.commit();
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) DefaultDocumentSet(org.exist.dom.persistent.DefaultDocumentSet) Diff(org.xmlunit.diff.Diff) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) DefaultDocumentSet(org.exist.dom.persistent.DefaultDocumentSet) DocumentSet(org.exist.dom.persistent.DocumentSet) DocumentImpl(org.exist.dom.persistent.DocumentImpl)

Example 4 with DefaultDocumentSet

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

the class SerializeAttrMatchesTest method configureAndStore.

private DocumentSet configureAndStore(final String configuration, final String data, final String docName) throws EXistException, CollectionConfigurationException, PermissionDeniedException, SAXException, TriggerException, LockException, IOException {
    final MutableDocumentSet docs = new DefaultDocumentSet();
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    final TransactionManager transact = pool.getTransactionManager();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Txn transaction = transact.beginTransaction()) {
        if (configuration != null) {
            final CollectionConfigurationManager mgr = pool.getConfigurationManager();
            mgr.addConfiguration(transaction, broker, test, configuration);
        }
        broker.storeDocument(transaction, XmldbURI.create(docName), new StringInputSource(data), MimeType.XML_TYPE, test);
        docs.add(test.getDocument(broker, XmldbURI.create(docName)));
        transact.commit(transaction);
    }
    return docs;
}
Also used : MutableDocumentSet(org.exist.dom.persistent.MutableDocumentSet) DBBroker(org.exist.storage.DBBroker) StringInputSource(org.exist.util.StringInputSource) DefaultDocumentSet(org.exist.dom.persistent.DefaultDocumentSet) TransactionManager(org.exist.storage.txn.TransactionManager) Txn(org.exist.storage.txn.Txn) CollectionConfigurationManager(org.exist.collections.CollectionConfigurationManager) BrokerPool(org.exist.storage.BrokerPool)

Example 5 with DefaultDocumentSet

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

the class LuceneIndexTest method configureAndStore.

private DocumentSet configureAndStore(final String configuration, final String data, final String docName) throws EXistException, CollectionConfigurationException, PermissionDeniedException, SAXException, TriggerException, LockException, IOException {
    final MutableDocumentSet docs = new DefaultDocumentSet();
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    final TransactionManager transact = pool.getTransactionManager();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
        final Txn transaction = transact.beginTransaction()) {
        if (configuration != null) {
            final CollectionConfigurationManager mgr = pool.getConfigurationManager();
            mgr.addConfiguration(transaction, broker, root, configuration);
        }
        broker.storeDocument(transaction, XmldbURI.create(docName), new StringInputSource(data), MimeType.XML_TYPE, root);
        docs.add(root.getDocument(broker, XmldbURI.create(docName)));
        transact.commit(transaction);
    }
    return docs;
}
Also used : MutableDocumentSet(org.exist.dom.persistent.MutableDocumentSet) DBBroker(org.exist.storage.DBBroker) DefaultDocumentSet(org.exist.dom.persistent.DefaultDocumentSet) TransactionManager(org.exist.storage.txn.TransactionManager) Txn(org.exist.storage.txn.Txn) CollectionConfigurationManager(org.exist.collections.CollectionConfigurationManager) BrokerPool(org.exist.storage.BrokerPool)

Aggregations

DefaultDocumentSet (org.exist.dom.persistent.DefaultDocumentSet)15 MutableDocumentSet (org.exist.dom.persistent.MutableDocumentSet)10 TransactionManager (org.exist.storage.txn.TransactionManager)7 Txn (org.exist.storage.txn.Txn)7 DocumentImpl (org.exist.dom.persistent.DocumentImpl)6 CollectionConfigurationManager (org.exist.collections.CollectionConfigurationManager)5 DBBroker (org.exist.storage.DBBroker)5 LockException (org.exist.util.LockException)5 Collection (org.exist.collections.Collection)4 PermissionDeniedException (org.exist.security.PermissionDeniedException)4 BrokerPool (org.exist.storage.BrokerPool)4 StringReader (java.io.StringReader)3 DocumentSet (org.exist.dom.persistent.DocumentSet)3 StringInputSource (org.exist.util.StringInputSource)3 Modification (org.exist.xupdate.Modification)3 XUpdateProcessor (org.exist.xupdate.XUpdateProcessor)3 InputSource (org.xml.sax.InputSource)3 ExtArrayNodeSet (org.exist.dom.persistent.ExtArrayNodeSet)2 XPathException (org.exist.xquery.XPathException)2 SAXException (org.xml.sax.SAXException)2