Search in sources :

Example 51 with NodeValue

use of org.exist.xquery.value.NodeValue in project exist by eXist-db.

the class Modification method selectAndLock.

/**
 * Acquire a lock on all documents processed by this modification.
 * We have to avoid that node positions change during the
 * operation.
 *
 * @param nodes sequence containing nodes from documents to lock
 * @param transaction current transaction
 * @return array of nodes for which lock was acquired
 *
 * @throws LockException in case locking failed
 * @throws TriggerException in case of error thrown by triggers
 * @throws XPathException in case of dynamic error
 */
protected StoredNode[] selectAndLock(Txn transaction, Sequence nodes) throws LockException, XPathException, TriggerException {
    final java.util.concurrent.locks.Lock globalLock = context.getBroker().getBrokerPool().getGlobalUpdateLock();
    globalLock.lock();
    try {
        final DocumentSet lockedDocuments = nodes.getDocumentSet();
        // acquire a lock on all documents
        // we have to avoid that node positions change
        // during the modification
        lockedDocumentsLocks = lockedDocuments.lock(context.getBroker(), true);
        final StoredNode[] ql = new StoredNode[nodes.getItemCount()];
        for (int i = 0; i < ql.length; i++) {
            final Item item = nodes.itemAt(i);
            if (!Type.subTypeOf(item.getType(), Type.NODE)) {
                throw new XPathException(this, "XQuery update expressions can only be applied to nodes. Got: " + item.getStringValue());
            }
            final NodeValue nv = (NodeValue) item;
            if (nv.getImplementationType() == NodeValue.IN_MEMORY_NODE) {
                throw new XPathException(this, "XQuery update expressions can not be applied to in-memory nodes.");
            }
            final Node n = nv.getNode();
            if (n.getNodeType() == Node.DOCUMENT_NODE) {
                throw new XPathException(this, "Updating the document object is not allowed.");
            }
            ql[i] = (StoredNode) n;
            final DocumentImpl doc = ql[i].getOwnerDocument();
            // prepare Trigger
            prepareTrigger(transaction, doc);
        }
        return ql;
    } finally {
        globalLock.unlock();
    }
}
Also used : Item(org.exist.xquery.value.Item) NodeValue(org.exist.xquery.value.NodeValue) StoredNode(org.exist.dom.persistent.StoredNode) Node(org.w3c.dom.Node) DefaultDocumentSet(org.exist.dom.persistent.DefaultDocumentSet) DocumentSet(org.exist.dom.persistent.DocumentSet) MutableDocumentSet(org.exist.dom.persistent.MutableDocumentSet) DocumentImpl(org.exist.dom.persistent.DocumentImpl) StoredNode(org.exist.dom.persistent.StoredNode)

Example 52 with NodeValue

use of org.exist.xquery.value.NodeValue in project exist by eXist-db.

the class Shared method getStreamSource.

public static StreamSource getStreamSource(Item item, XQueryContext context) throws XPathException, IOException {
    final StreamSource streamSource = new StreamSource();
    if (item.getType() == Type.JAVA_OBJECT) {
        LOG.debug("Streaming Java object");
        final Object obj = ((JavaObjectValue) item).getObject();
        if (!(obj instanceof File)) {
            throw new XPathException("Passed java object should be a File");
        }
        final File inputFile = (File) obj;
        final InputStream is = new FileInputStream(inputFile);
        streamSource.setInputStream(is);
        streamSource.setSystemId(inputFile.toURI().toURL().toString());
    } else if (item.getType() == Type.ANY_URI) {
        LOG.debug("Streaming xs:anyURI");
        // anyURI provided
        String url = item.getStringValue();
        // Fix URL
        if (url.startsWith("/")) {
            url = "xmldb:exist://" + url;
        }
        final InputStream is = new URL(url).openStream();
        streamSource.setInputStream(is);
        streamSource.setSystemId(url);
    } else if (item.getType() == Type.ELEMENT || item.getType() == Type.DOCUMENT) {
        LOG.debug("Streaming element or document node");
        if (item instanceof NodeProxy) {
            final NodeProxy np = (NodeProxy) item;
            final String url = "xmldb:exist://" + np.getOwnerDocument().getBaseURI();
            LOG.debug("Document detected, adding URL {}", url);
            streamSource.setSystemId(url);
        }
        // Node provided
        final DBBroker broker = context.getBroker();
        final ConsumerE<ConsumerE<Serializer, IOException>, IOException> withSerializerFn = fn -> {
            final Serializer serializer = broker.borrowSerializer();
            try {
                fn.accept(serializer);
            } finally {
                broker.returnSerializer(serializer);
            }
        };
        final NodeValue node = (NodeValue) item;
        final InputStream is = new NodeInputStream(context.getBroker().getBrokerPool(), withSerializerFn, node);
        streamSource.setInputStream(is);
    } else if (item.getType() == Type.BASE64_BINARY || item.getType() == Type.HEX_BINARY) {
        LOG.debug("Streaming base64 binary");
        final BinaryValue binary = (BinaryValue) item;
        final byte[] data = binary.toJavaObject(byte[].class);
        final InputStream is = new UnsynchronizedByteArrayInputStream(data);
        streamSource.setInputStream(is);
        if (item instanceof Base64BinaryDocument) {
            final Base64BinaryDocument b64doc = (Base64BinaryDocument) item;
            final String url = "xmldb:exist://" + b64doc.getUrl();
            LOG.debug("Base64BinaryDocument detected, adding URL {}", url);
            streamSource.setSystemId(url);
        }
    } else {
        LOG.error("Wrong item type {}", Type.getTypeName(item.getType()));
        throw new XPathException("wrong item type " + Type.getTypeName(item.getType()));
    }
    return streamSource;
}
Also used : ValidationReport(org.exist.validation.ValidationReport) URL(java.net.URL) StreamSource(javax.xml.transform.stream.StreamSource) SequenceIterator(org.exist.xquery.value.SequenceIterator) NodeProxy(org.exist.dom.persistent.NodeProxy) Base64BinaryDocument(org.exist.xquery.value.Base64BinaryDocument) ArrayList(java.util.ArrayList) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) NodeValue(org.exist.xquery.value.NodeValue) Item(org.exist.xquery.value.Item) NodeInputStream(org.exist.validation.internal.node.NodeInputStream) NodeImpl(org.exist.dom.memtree.NodeImpl) XQueryContext(org.exist.xquery.XQueryContext) AttributesImpl(org.xml.sax.helpers.AttributesImpl) InputSource(org.xml.sax.InputSource) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) BinaryValue(org.exist.xquery.value.BinaryValue) ValidationReportItem(org.exist.validation.ValidationReportItem) Type(org.exist.xquery.value.Type) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) File(java.io.File) Logger(org.apache.logging.log4j.Logger) JavaObjectValue(org.exist.xquery.value.JavaObjectValue) DBBroker(org.exist.storage.DBBroker) Serializer(org.exist.storage.serializers.Serializer) Sequence(org.exist.xquery.value.Sequence) ConsumerE(com.evolvedbinary.j8fu.function.ConsumerE) LogManager(org.apache.logging.log4j.LogManager) XPathException(org.exist.xquery.XPathException) InputStream(java.io.InputStream) NodeValue(org.exist.xquery.value.NodeValue) XPathException(org.exist.xquery.XPathException) NodeInputStream(org.exist.validation.internal.node.NodeInputStream) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) BinaryValue(org.exist.xquery.value.BinaryValue) IOException(java.io.IOException) NodeProxy(org.exist.dom.persistent.NodeProxy) FileInputStream(java.io.FileInputStream) URL(java.net.URL) DBBroker(org.exist.storage.DBBroker) Base64BinaryDocument(org.exist.xquery.value.Base64BinaryDocument) ConsumerE(com.evolvedbinary.j8fu.function.ConsumerE) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) JavaObjectValue(org.exist.xquery.value.JavaObjectValue) File(java.io.File) NodeInputStream(org.exist.validation.internal.node.NodeInputStream) Serializer(org.exist.storage.serializers.Serializer)

Aggregations

NodeValue (org.exist.xquery.value.NodeValue)52 XPathException (org.exist.xquery.XPathException)35 Sequence (org.exist.xquery.value.Sequence)21 Item (org.exist.xquery.value.Item)17 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)16 Node (org.w3c.dom.Node)15 NodeProxy (org.exist.dom.persistent.NodeProxy)11 IOException (java.io.IOException)10 QName (org.exist.dom.QName)10 Element (org.w3c.dom.Element)7 SAXException (org.xml.sax.SAXException)7 Properties (java.util.Properties)6 SequenceIterator (org.exist.xquery.value.SequenceIterator)6 DocumentBuilderReceiver (org.exist.dom.memtree.DocumentBuilderReceiver)5 BrokerPool (org.exist.storage.BrokerPool)5 DBBroker (org.exist.storage.DBBroker)5 StringValue (org.exist.xquery.value.StringValue)5 Path (java.nio.file.Path)4 NodeImpl (org.exist.dom.memtree.NodeImpl)4 Serializer (org.exist.storage.serializers.Serializer)4