Search in sources :

Example 16 with StringValue

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

the class FunTranslate 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());
        }
    }
    if (contextItem != null) {
        contextSequence = contextItem.toSequence();
    }
    Sequence result;
    final Sequence seq = getArgument(0).eval(contextSequence);
    if (seq.isEmpty()) {
        result = StringValue.EMPTY_STRING;
    } else {
        final ValueSequence arg = FunStringToCodepoints.getCodePoints(seq.getStringValue());
        final ValueSequence mapStr = FunStringToCodepoints.getCodePoints(getArgument(1).eval(contextSequence).getStringValue());
        final ValueSequence transStr = FunStringToCodepoints.getCodePoints(getArgument(2).eval(contextSequence).getStringValue());
        int p;
        IntegerValue ch;
        final StringBuilder buf = new StringBuilder(arg.getItemCount());
        for (int i = 0; i < arg.getItemCount(); i++) {
            ch = (IntegerValue) arg.itemAt(i);
            p = FunStringToCodepoints.indexOf(mapStr, ch);
            if (p == Constants.STRING_NOT_FOUND) {
                buf.append(FunStringToCodepoints.codePointToString(ch));
            } else {
                if (p < transStr.getItemCount()) {
                    buf.append(FunStringToCodepoints.codePointToString((IntegerValue) transStr.itemAt(p)));
                }
            }
        }
        result = new StringValue(buf.toString());
    }
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : IntegerValue(org.exist.xquery.value.IntegerValue) ValueSequence(org.exist.xquery.value.ValueSequence) ValueSequence(org.exist.xquery.value.ValueSequence) Sequence(org.exist.xquery.value.Sequence) StringValue(org.exist.xquery.value.StringValue)

Example 17 with StringValue

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

the class FunUpperOrLowerCase 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());
        }
    }
    if (contextItem != null) {
        contextSequence = contextItem.toSequence();
    }
    Sequence result;
    final Sequence seq = getArgument(0).eval(contextSequence);
    if (seq.isEmpty()) {
        result = StringValue.EMPTY_STRING;
    } else {
        final String value = seq.getStringValue();
        if (isCalledAs("upper-case")) {
            result = new StringValue(value.toUpperCase());
        } else {
            result = new StringValue(value.toLowerCase());
        }
    }
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : Sequence(org.exist.xquery.value.Sequence) StringValue(org.exist.xquery.value.StringValue)

Example 18 with StringValue

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

the class FunReplace method eval.

@Override
public Sequence eval(final 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());
        }
    }
    final Sequence result;
    final Sequence stringArg = getArgument(0).eval(contextSequence, contextItem);
    if (stringArg.isEmpty()) {
        result = StringValue.EMPTY_STRING;
    } else {
        final String flags;
        if (getSignature().getArgumentCount() == 4) {
            flags = getArgument(3).eval(contextSequence, contextItem).getStringValue();
        } else {
            flags = "";
        }
        final String string = stringArg.getStringValue();
        final Sequence patternSeq = getArgument(1).eval(contextSequence, contextItem);
        final String pattern = patternSeq.getStringValue();
        final Sequence replaceSeq = getArgument(2).eval(contextSequence, contextItem);
        final String replace = replaceSeq.getStringValue();
        final Configuration config = context.getBroker().getBrokerPool().getSaxonConfiguration();
        final List<String> warnings = new ArrayList<>(1);
        try {
            final RegularExpression regularExpression = config.compileRegularExpression(pattern, flags, "XP30", warnings);
            if (!hasLiteral(flags)) {
                final String msg = Replace.checkReplacement(replace);
                if (msg != null) {
                    throw new XPathException(this, ErrorCodes.FORX0004, msg);
                }
            }
            final CharSequence res = regularExpression.replace(string, replace);
            result = new StringValue(res.toString());
        } catch (final net.sf.saxon.trans.XPathException e) {
            switch(e.getErrorCodeLocalPart()) {
                case "FORX0001":
                    throw new XPathException(this, ErrorCodes.FORX0001, e.getMessage());
                case "FORX0002":
                    throw new XPathException(this, ErrorCodes.FORX0002, e.getMessage());
                case "FORX0003":
                    throw new XPathException(this, ErrorCodes.FORX0003, e.getMessage());
                case "FORX0004":
                    throw new XPathException(this, ErrorCodes.FORX0004, e.getMessage());
                default:
                    throw new XPathException(this, e.getMessage());
            }
        }
    }
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : RegularExpression(net.sf.saxon.regex.RegularExpression) Configuration(net.sf.saxon.Configuration) XPathException(org.exist.xquery.XPathException) ArrayList(java.util.ArrayList) Sequence(org.exist.xquery.value.Sequence) StringValue(org.exist.xquery.value.StringValue)

Example 19 with StringValue

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

the class GetHeaderNames method eval.

@Override
public Sequence eval(final Sequence[] args, @Nonnull final RequestWrapper request) throws XPathException {
    final Enumeration<String> headerNames = request.getHeaderNames();
    if (!headerNames.hasMoreElements()) {
        return Sequence.EMPTY_SEQUENCE;
    }
    final ValueSequence result = new ValueSequence();
    while (headerNames.hasMoreElements()) {
        final String headerName = headerNames.nextElement();
        result.add(new StringValue(headerName));
    }
    return result;
}
Also used : ValueSequence(org.exist.xquery.value.ValueSequence) StringValue(org.exist.xquery.value.StringValue)

Example 20 with StringValue

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

the class ExtTestFailureFunction method actualToString.

private String actualToString(final MapType actual) throws XPathException, SAXException, IOException {
    final Sequence seqActualError = actual.get(new StringValue("error"));
    if (!seqActualError.isEmpty()) {
        return errorMapToString(seqActualError);
    }
    final Sequence seqActualResult = actual.get(new StringValue("result"));
    if (!seqActualResult.isEmpty()) {
        return seqToString(seqActualResult);
    } else {
        // empty-sequence()
        return "";
    }
}
Also used : 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