Search in sources :

Example 56 with StringValue

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

the class EchoFunction method eval.

public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    // is argument the empty sequence?
    if (args[0].isEmpty()) {
        return Sequence.EMPTY_SEQUENCE;
    }
    // iterate through the argument sequence and echo each item
    ValueSequence result = new ValueSequence();
    for (SequenceIterator i = args[0].iterate(); i.hasNext(); ) {
        String str = i.nextItem().getStringValue();
        result.add(new StringValue("echo: " + str));
    }
    return result;
}
Also used : SequenceIterator(org.exist.xquery.value.SequenceIterator) ValueSequence(org.exist.xquery.value.ValueSequence) StringValue(org.exist.xquery.value.StringValue)

Example 57 with StringValue

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

the class ExampleFunctions method eval.

@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
    switch(getName().getLocalPart()) {
        case FS_HELLO_WORLD_NAME:
            return sayHello(Optional.of(new StringValue("World")));
        case FS_SAY_HELLO_NAME:
            final Optional<StringValue> name = args[0].isEmpty() ? Optional.empty() : Optional.of((StringValue) args[0].itemAt(0));
            return sayHello(name);
        case FS_ADD_NAME:
            final IntegerValue a = (IntegerValue) args[0].itemAt(0);
            final IntegerValue b = (IntegerValue) args[1].itemAt(0);
            return add(a, b);
        default:
            throw new XPathException(this, "No function: " + getName() + "#" + getSignature().getArgumentCount());
    }
}
Also used : XPathException(org.exist.xquery.XPathException) IntegerValue(org.exist.xquery.value.IntegerValue) StringValue(org.exist.xquery.value.StringValue)

Example 58 with StringValue

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

the class InstallFunction method getBinaryDoc.

private LockedDocument getBinaryDoc(final String path) throws XPathException {
    try {
        final XmldbURI uri = XmldbURI.createInternal(path);
        final LockedDocument lockedDoc = context.getBroker().getXMLResource(uri, LockMode.READ_LOCK);
        if (lockedDoc == null) {
            throw new XPathException(this, EXPathErrorCode.EXPDY001, path + " is not .xar resource", new StringValue(path));
        } else if (lockedDoc.getDocument().getResourceType() != DocumentImpl.BINARY_FILE) {
            lockedDoc.close();
            throw new XPathException(this, EXPathErrorCode.EXPDY001, path + " is not a valid .xar, it's not a binary resource", new StringValue(path));
        }
        return lockedDoc;
    } catch (PermissionDeniedException e) {
        throw new XPathException(this, EXPathErrorCode.EXPDY003, e.getMessage(), new StringValue(path), e);
    }
}
Also used : XPathException(org.exist.xquery.XPathException) LockedDocument(org.exist.dom.persistent.LockedDocument) PermissionDeniedException(org.exist.security.PermissionDeniedException) StringValue(org.exist.xquery.value.StringValue) XmldbURI(org.exist.xmldb.XmldbURI)

Example 59 with StringValue

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

the class XQueryTrigger method getScript.

private CompiledXQuery getScript(boolean isBefore, DBBroker broker, Txn transaction, XmldbURI src) throws TriggerException {
    // get the query
    final Source query = getQuerySource(broker);
    if (query == null) {
        return null;
    }
    // avoid infinite recursion by allowing just one trigger per thread
    if (isBefore && !TriggerStatePerThread.verifyUniqueTriggerPerThreadBeforePrepare(this, src)) {
        return null;
    } else if (!isBefore && !TriggerStatePerThread.verifyUniqueTriggerPerThreadBeforeFinish(this, src)) {
        return null;
    }
    TriggerStatePerThread.setTransaction(transaction);
    final XQueryContext context = new XQueryContext(broker.getBrokerPool());
    if (query instanceof DBSource) {
        context.setModuleLoadPath(XmldbURI.EMBEDDED_SERVER_URI_PREFIX + ((DBSource) query).getDocumentPath().removeLastSegment().toString());
    }
    CompiledXQuery compiledQuery;
    try {
        // compile the XQuery
        compiledQuery = service.compile(context, query);
        // declare user defined parameters as external variables
        for (Object o : userDefinedVariables.keySet()) {
            final String varName = (String) o;
            final String varValue = userDefinedVariables.getProperty(varName);
            context.declareVariable(bindingPrefix + varName, new StringValue(varValue));
        }
        // reset & prepareForExecution for execution
        compiledQuery.reset();
        context.getWatchDog().reset();
        // do any preparation before execution
        context.prepareForExecution();
        return compiledQuery;
    } catch (final XPathException | IOException | PermissionDeniedException e) {
        LOG.warn(e.getMessage(), e);
        TriggerStatePerThread.setTriggerRunningState(TriggerStatePerThread.NO_TRIGGER_RUNNING, this, null);
        TriggerStatePerThread.setTransaction(null);
        throw new TriggerException(PREPARE_EXCEPTION_MESSAGE, e);
    }
}
Also used : DBSource(org.exist.source.DBSource) PermissionDeniedException(org.exist.security.PermissionDeniedException) IOException(java.io.IOException) StringValue(org.exist.xquery.value.StringValue) StringSource(org.exist.source.StringSource) Source(org.exist.source.Source) DBSource(org.exist.source.DBSource)

Example 60 with StringValue

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

the class FunIdRef method getIdRef.

private void getIdRef(NodeSet result, DocumentSet docs, String id) throws XPathException {
    final NodeSet attribs = context.getBroker().getValueIndex().find(context.getWatchDog(), Comparison.EQ, docs, null, -1, null, new StringValue(id, Type.IDREF));
    for (final NodeProxy n : attribs) {
        n.setNodeType(Node.ATTRIBUTE_NODE);
        result.add(n);
    }
}
Also used : ExtArrayNodeSet(org.exist.dom.persistent.ExtArrayNodeSet) NodeSet(org.exist.dom.persistent.NodeSet) StringValue(org.exist.xquery.value.StringValue) NodeProxy(org.exist.dom.persistent.NodeProxy)

Aggregations

StringValue (org.exist.xquery.value.StringValue)96 Sequence (org.exist.xquery.value.Sequence)49 XPathException (org.exist.xquery.XPathException)40 ValueSequence (org.exist.xquery.value.ValueSequence)27 IOException (java.io.IOException)11 PermissionDeniedException (org.exist.security.PermissionDeniedException)10 Item (org.exist.xquery.value.Item)10 XQueryContext (org.exist.xquery.XQueryContext)8 Txn (org.exist.storage.txn.Txn)7 AnyURIValue (org.exist.xquery.value.AnyURIValue)7 QName (org.exist.dom.QName)6 LockException (org.exist.util.LockException)6 NodeValue (org.exist.xquery.value.NodeValue)6 Path (java.nio.file.Path)5 EXistException (org.exist.EXistException)5 TriggerException (org.exist.collections.triggers.TriggerException)5 DocumentImpl (org.exist.dom.persistent.DocumentImpl)5 StoredNode (org.exist.dom.persistent.StoredNode)5 NotificationService (org.exist.storage.NotificationService)5 MimeType (org.exist.util.MimeType)5