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