Search in sources :

Example 36 with StringValue

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

the class UserXQueryJob method executeXQuery.

private void executeXQuery(final BrokerPool pool, final DBBroker broker, final Source source, final Properties params) throws PermissionDeniedException, XPathException, JobExecutionException {
    XQueryPool xqPool = null;
    CompiledXQuery compiled = null;
    XQueryContext context = null;
    try {
        // execute the xquery
        final XQuery xquery = pool.getXQueryService();
        xqPool = pool.getXQueryPool();
        // try and get a pre-compiled query from the pool
        compiled = xqPool.borrowCompiledXQuery(broker, source);
        if (compiled == null) {
            context = new XQueryContext(pool);
        } else {
            context = compiled.getContext();
            context.prepareForReuse();
        }
        if (source instanceof DBSource) {
            final XmldbURI collectionUri = ((DBSource) source).getDocumentPath().removeLastSegment();
            context.setModuleLoadPath(XmldbURI.EMBEDDED_SERVER_URI.append(collectionUri.getCollectionPath()).toString());
            context.setStaticallyKnownDocuments(new XmldbURI[] { collectionUri });
        }
        if (compiled == null) {
            try {
                compiled = xquery.compile(context, source);
            } catch (final IOException e) {
                abort("Failed to read query from " + xqueryResource);
            }
        }
        // declare any parameters as external variables
        if (params != null) {
            String bindingPrefix = params.getProperty("bindingPrefix");
            if (bindingPrefix == null) {
                bindingPrefix = "local";
            }
            for (final Entry param : params.entrySet()) {
                final String key = (String) param.getKey();
                final String value = (String) param.getValue();
                context.declareVariable(bindingPrefix + ":" + key, new StringValue(value));
            }
        }
        xquery.execute(broker, compiled, null);
    } finally {
        if (context != null) {
            context.runCleanupTasks();
        }
        // return the compiled query to the pool
        if (xqPool != null && source != null && compiled != null) {
            xqPool.returnCompiledXQuery(source, compiled);
        }
    }
}
Also used : XQueryPool(org.exist.storage.XQueryPool) Entry(java.util.Map.Entry) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQueryContext(org.exist.xquery.XQueryContext) DBSource(org.exist.source.DBSource) IOException(java.io.IOException) StringValue(org.exist.xquery.value.StringValue) XmldbURI(org.exist.xmldb.XmldbURI)

Example 37 with StringValue

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

the class FunctionFactory method equals.

/**
 * equals(node-set, string)
 */
private static GeneralComparison equals(XQueryContext context, XQueryAST ast, PathExpr parent, List<Expression> params) throws XPathException {
    if (params.size() < 2) {
        throw new XPathException(ast.getLine(), ast.getColumn(), ErrorCodes.XPST0017, "Function equals() requires two or three arguments");
    }
    if (params.size() > 3) {
        throw new XPathException(ast.getLine(), ast.getColumn(), ErrorCodes.XPST0017, "Function equals() requires two or three arguments");
    }
    final PathExpr p0 = (PathExpr) params.get(0);
    final PathExpr p1 = (PathExpr) params.get(1);
    if (p1.getLength() == 0) {
        throw new XPathException(ast.getLine(), ast.getColumn(), "Second argument of equals() is empty");
    }
    final GeneralComparison op = new GeneralComparison(context, p0, p1, Comparison.EQ, StringTruncationOperator.EQUALS);
    // TODO : not sure for parent -pb
    context.getProfiler().message(parent, Profiler.OPTIMIZATIONS, "OPTIMIZATION", "Rewritten contains() as a general comparison with no truncations");
    op.setLocation(ast.getLine(), ast.getColumn());
    if (params.size() == 3) {
        op.setCollation((Expression) params.get(2));
    } else {
        op.setCollation(new StringValue("?strength=identical"));
    }
    return op;
}
Also used : StringValue(org.exist.xquery.value.StringValue)

Example 38 with StringValue

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

the class FunLocalName method eval.

@Override
public Sequence eval(Sequence contextSequence, final 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 (contextItem != null) {
        contextSequence = contextItem.toSequence();
    }
    final Item item;
    // the context sequence
    if (getArgumentCount() > 0) {
        final Sequence seq = getArgument(0).eval(contextSequence);
        if (!seq.isEmpty()) {
            item = seq.itemAt(0);
        } else {
            item = null;
        }
    } else {
        if (contextSequence == null) {
            throw new XPathException(this, ErrorCodes.XPDY0002, "Undefined context item");
        }
        item = contextSequence.itemAt(0);
    }
    final Sequence result;
    if (item == null) {
        result = StringValue.EMPTY_STRING;
    } else {
        if (!Type.subTypeOf(item.getType(), Type.NODE)) {
            throw new XPathException(this, ErrorCodes.XPTY0004, "item is not a node; got '" + Type.getTypeName(item.getType()) + "'");
        }
        // TODO : how to improve performance ?
        final Node n = ((NodeValue) item).getNode();
        final String localName = n.getLocalName();
        if (localName != null) {
            result = new StringValue(localName);
        } else {
            result = StringValue.EMPTY_STRING;
        }
    }
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : Item(org.exist.xquery.value.Item) NodeValue(org.exist.xquery.value.NodeValue) XPathException(org.exist.xquery.XPathException) Node(org.w3c.dom.Node) Sequence(org.exist.xquery.value.Sequence) StringValue(org.exist.xquery.value.StringValue)

Example 39 with StringValue

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

the class FunName method eval.

@Override
public Sequence eval(Sequence contextSequence, final 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 (contextItem != null) {
        contextSequence = contextItem.toSequence();
    }
    // If we have one argument, we take it into account
    final Sequence seq;
    if (getSignature().getArgumentCount() > 0) {
        seq = getArgument(0).eval(contextSequence, contextItem);
    } else {
        // Otherwise, we take the context sequence and we iterate over it
        seq = contextSequence;
    }
    if (seq == null) {
        throw new XPathException(this, ErrorCodes.XPDY0002, "Undefined context item");
    }
    final Sequence result;
    if (seq.isEmpty()) {
        result = StringValue.EMPTY_STRING;
    } else {
        final Item item = seq.itemAt(0);
        if (!Type.subTypeOf(item.getType(), Type.NODE)) {
            throw new XPathException(this, ErrorCodes.XPTY0004, "item is not a node; got '" + Type.getTypeName(item.getType()) + "'");
        }
        // TODO : how to improve performance ?
        final Node n = ((NodeValue) item).getNode();
        if (n instanceof INode) {
            result = new StringValue(((INode) n).getQName().getStringValue());
        } else {
            result = StringValue.EMPTY_STRING;
        }
    }
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : Item(org.exist.xquery.value.Item) NodeValue(org.exist.xquery.value.NodeValue) INode(org.exist.dom.INode) XPathException(org.exist.xquery.XPathException) INode(org.exist.dom.INode) Node(org.w3c.dom.Node) Sequence(org.exist.xquery.value.Sequence) StringValue(org.exist.xquery.value.StringValue)

Example 40 with StringValue

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

the class FunNormalizeUnicode method eval.

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 (contextItem != null) {
        contextSequence = contextItem.toSequence();
    }
    Sequence result;
    final Sequence s1 = getArgument(0).eval(contextSequence);
    if (s1.isEmpty()) {
        result = StringValue.EMPTY_STRING;
    } else {
        String newNormalizationForm = "NFC";
        if (getArgumentCount() > 1) {
            newNormalizationForm = getArgument(1).eval(contextSequence).getStringValue().toUpperCase().trim();
        }
        // TODO : handle the "FULLY-NORMALIZED" string...
        if (newNormalizationForm.isEmpty()) {
            result = new StringValue(s1.getStringValue());
        } else {
            try {
                Normalizer.Form form = Normalizer.Form.valueOf(newNormalizationForm);
                result = new StringValue(Normalizer.normalize(s1.getStringValue(), form));
            } catch (IllegalArgumentException e) {
                throw new XPathException(this, ErrorCodes.FOCH0003, "Unknown normalization form: " + newNormalizationForm);
            }
        }
    }
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : XPathException(org.exist.xquery.XPathException) Normalizer(java.text.Normalizer) Sequence(org.exist.xquery.value.Sequence) StringValue(org.exist.xquery.value.StringValue)

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