Search in sources :

Example 6 with DefaultDocumentSet

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

the class LuceneIndexTest method configureAndStore.

private DocumentSet configureAndStore(String configuration, final String[] sampleNames) 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);
        }
        for (final String sampleName : sampleNames) {
            broker.storeDocument(transaction, XmldbURI.create(sampleName), new InputStreamSupplierInputSource(() -> SAMPLES.getShakespeareSample(sampleName)), MimeType.XML_TYPE, root);
            docs.add(root.getDocument(broker, XmldbURI.create(sampleName)));
        }
        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)

Example 7 with DefaultDocumentSet

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

the class FunDoctype method eval.

/* (non-Javadoc)
	 * @see org.exist.xquery.Expression#eval(org.exist.dom.persistent.DocumentSet, org.exist.xquery.value.Sequence, org.exist.xquery.value.Item)
	 */
public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().start(this);
        context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
        if (contextSequence != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
        }
        if (contextItem != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
        }
    }
    final MutableDocumentSet docs = new DefaultDocumentSet();
    for (int i = 0; i < getArgumentCount(); i++) {
        final Sequence seq = getArgument(i).eval(contextSequence, contextItem);
        for (final SequenceIterator j = seq.iterate(); j.hasNext(); ) {
            final String next = j.nextItem().getStringValue();
            try {
                context.getBroker().getXMLResourcesByDoctype(next, docs);
            } catch (final PermissionDeniedException | LockException e) {
                LOG.error(e.getMessage(), e);
                throw new XPathException(this, e);
            }
        }
    }
    final NodeSet result = new ExtArrayNodeSet(1);
    for (final Iterator<DocumentImpl> i = docs.getDocumentIterator(); i.hasNext(); ) {
        result.add(new NodeProxy(i.next(), NodeId.DOCUMENT_NODE));
    }
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : ExtArrayNodeSet(org.exist.dom.persistent.ExtArrayNodeSet) NodeSet(org.exist.dom.persistent.NodeSet) MutableDocumentSet(org.exist.dom.persistent.MutableDocumentSet) DefaultDocumentSet(org.exist.dom.persistent.DefaultDocumentSet) DocumentImpl(org.exist.dom.persistent.DocumentImpl) NodeProxy(org.exist.dom.persistent.NodeProxy) ExtArrayNodeSet(org.exist.dom.persistent.ExtArrayNodeSet) LockException(org.exist.util.LockException) PermissionDeniedException(org.exist.security.PermissionDeniedException)

Example 8 with DefaultDocumentSet

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

the class CustomIndexTest method setUp.

@Before
public void setUp() throws DatabaseConfigurationException, EXistException, PermissionDeniedException, IOException, SAXException, CollectionConfigurationException, LockException {
    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()) {
        Collection root = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI);
        assertNotNull(root);
        broker.saveCollection(transaction, root);
        CollectionConfigurationManager mgr = pool.getConfigurationManager();
        mgr.addConfiguration(transaction, broker, root, COLLECTION_CONFIG);
        docs = new DefaultDocumentSet();
        broker.storeDocument(transaction, XmldbURI.create("test_string.xml"), new StringInputSource(XML), MimeType.XML_TYPE, root);
        docs.add(root.getDocument(broker, XmldbURI.create("test_string.xml")));
        broker.storeDocument(transaction, XmldbURI.create("test_string2.xml"), new StringInputSource(XML2), MimeType.XML_TYPE, root);
        docs.add(root.getDocument(broker, XmldbURI.create("test_string2.xml")));
        transact.commit(transaction);
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) DefaultDocumentSet(org.exist.dom.persistent.DefaultDocumentSet) TransactionManager(org.exist.storage.txn.TransactionManager) Collection(org.exist.collections.Collection) Txn(org.exist.storage.txn.Txn) CollectionConfigurationManager(org.exist.collections.CollectionConfigurationManager) BrokerPool(org.exist.storage.BrokerPool)

Example 9 with DefaultDocumentSet

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

the class Dumper method prepare.

public void prepare(int event, DBBroker broker, Txn txn, XmldbURI documentName, DocumentImpl existingDocument) throws TriggerException {
    System.out.println("\nstoring document " + documentName + " into collection " + getCollection().getURI());
    if (existingDocument != null) {
        System.out.println("replacing document " + ((DocumentImpl) existingDocument).getFileURI());
    }
    System.out.println("collection contents:");
    final DefaultDocumentSet docs = new DefaultDocumentSet();
    try {
        getCollection().getDocuments(broker, docs);
    } catch (final PermissionDeniedException | LockException e) {
        throw new TriggerException(e.getMessage(), e);
    }
    for (final Iterator<DocumentImpl> i = docs.getDocumentIterator(); i.hasNext(); ) {
        System.out.println("\t" + i.next().getFileURI());
    }
}
Also used : DefaultDocumentSet(org.exist.dom.persistent.DefaultDocumentSet) LockException(org.exist.util.LockException) PermissionDeniedException(org.exist.security.PermissionDeniedException) DocumentImpl(org.exist.dom.persistent.DocumentImpl)

Example 10 with DefaultDocumentSet

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

the class FunIdRef method eval.

/**
 * @see org.exist.xquery.Expression#eval(Sequence, Item)
 */
public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().start(this);
        context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
        if (contextSequence != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
        }
        if (contextItem != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
        }
    }
    if (getArgumentCount() < 1) {
        throw new XPathException(this, ErrorCodes.XPST0017, "function id requires one argument");
    }
    if (contextItem != null) {
        contextSequence = contextItem.toSequence();
    }
    Sequence result;
    boolean processInMem = false;
    final Expression arg = getArgument(0);
    final Sequence idrefval = arg.eval(contextSequence);
    if (idrefval.isEmpty()) {
        result = Sequence.EMPTY_SEQUENCE;
    } else {
        String nextId;
        DocumentSet docs = null;
        if (getArgumentCount() == 2) {
            // second argument should be a node, whose owner document will be
            // searched for the id
            final Sequence nodes = getArgument(1).eval(contextSequence);
            if (nodes.isEmpty()) {
                throw new XPathException(this, ErrorCodes.XPDY0002, "no node or context item for fn:idref");
            }
            if (!Type.subTypeOf(nodes.itemAt(0).getType(), Type.NODE)) {
                throw new XPathException(this, ErrorCodes.XPTY0004, "fn:idref() argument is not a node");
            }
            NodeValue node = (NodeValue) nodes.itemAt(0);
            if (node.getImplementationType() == NodeValue.IN_MEMORY_NODE) // TODO : how to enforce this ?
            // If $node, or the context item if the second argument is omitted,
            // is a node in a tree whose root is not a document node [err:FODC0001] is raised                    processInMem = true;
            {
                processInMem = true;
            } else {
                MutableDocumentSet ndocs = new DefaultDocumentSet();
                ndocs.add(((NodeProxy) node).getOwnerDocument());
                docs = ndocs;
            }
            contextSequence = node;
        } else if (contextSequence == null) {
            throw new XPathException(this, ErrorCodes.XPDY0002, "no context item specified");
        } else if (!Type.subTypeOf(contextSequence.getItemType(), Type.NODE)) {
            throw new XPathException(this, ErrorCodes.XPTY0004, "context item is not a node");
        } else {
            if (contextSequence.isPersistentSet()) {
                docs = contextSequence.toNodeSet().getDocumentSet();
            } else {
                processInMem = true;
            }
        }
        if (processInMem) {
            result = new ValueSequence();
        } else {
            result = new ExtArrayNodeSet();
        }
        for (final SequenceIterator i = idrefval.iterate(); i.hasNext(); ) {
            nextId = i.nextItem().getStringValue();
            if (nextId.isEmpty()) {
                continue;
            }
            if (XMLNames.isNCName(nextId)) {
                if (processInMem) {
                    getIdRef(result, contextSequence, nextId);
                } else {
                    getIdRef((NodeSet) result, docs, nextId);
                }
            }
        }
    }
    result.removeDuplicates();
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : NodeValue(org.exist.xquery.value.NodeValue) MutableDocumentSet(org.exist.dom.persistent.MutableDocumentSet) DefaultDocumentSet(org.exist.dom.persistent.DefaultDocumentSet) ExtArrayNodeSet(org.exist.dom.persistent.ExtArrayNodeSet) SequenceIterator(org.exist.xquery.value.SequenceIterator) XPathException(org.exist.xquery.XPathException) Expression(org.exist.xquery.Expression) ValueSequence(org.exist.xquery.value.ValueSequence) ValueSequence(org.exist.xquery.value.ValueSequence) Sequence(org.exist.xquery.value.Sequence) DefaultDocumentSet(org.exist.dom.persistent.DefaultDocumentSet) DocumentSet(org.exist.dom.persistent.DocumentSet) MutableDocumentSet(org.exist.dom.persistent.MutableDocumentSet)

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