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