Search in sources :

Example 21 with StringValue

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

the class Hash 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[] args, Sequence contextSequence) throws XPathException {
    boolean base64 = false;
    final String message = args[0].itemAt(0).getStringValue();
    final String algorithm = args[1].itemAt(0).getStringValue();
    if (args.length > 2) {
        base64 = args[2].effectiveBooleanValue();
    }
    String md = null;
    try {
        md = MessageDigester.calculate(message, algorithm, base64);
    } catch (final IllegalArgumentException ex) {
        throw new XPathException(ex.getMessage());
    }
    return (new StringValue(md));
}
Also used : XPathException(org.exist.xquery.XPathException) StringValue(org.exist.xquery.value.StringValue)

Example 22 with StringValue

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

the class BaseConversionFunctions method eval.

@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
    if (isCalledAs(qnIntToOctal.getLocalPart())) {
        final int i = args[0].toJavaObject(Integer.class);
        final String octal = i == 0 ? "0" : "0" + Integer.toOctalString(i);
        return new StringValue(octal);
    } else if (isCalledAs(qnOctalToInt.getLocalPart())) {
        final String octal = args[0].toString();
        return new IntegerValue(Integer.parseInt(octal, 8));
    } else {
        throw new XPathException("Unknown function call: " + getSignature());
    }
}
Also used : XPathException(org.exist.xquery.XPathException) IntegerValue(org.exist.xquery.value.IntegerValue) StringValue(org.exist.xquery.value.StringValue)

Example 23 with StringValue

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

the class BaseConverter 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[] args, Sequence contextSequence) throws XPathException {
    int intValue;
    String stringValue;
    final String number = args[0].itemAt(0).getStringValue();
    final int intBase = ((IntegerValue) args[1].itemAt(0)).getInt();
    if (isCalledAs("base-to-integer")) {
        intValue = Integer.parseInt(number, intBase);
        return new IntegerValue(intValue);
    } else {
        switch(Base.getBase(intBase)) {
            case BINARY:
                stringValue = Integer.toBinaryString(Integer.parseInt(number));
                break;
            case OCTAL:
                stringValue = Integer.toOctalString(Integer.parseInt(number));
                break;
            // break;
            case HEXADECIMAL:
                stringValue = Integer.toHexString(Integer.parseInt(number));
                break;
            default:
                {
                    logger.error("Unhandled base for conversion target in integer-to-base().");
                    throw new XPathException("Unhandled base for conversion target in integer-to-base().");
                }
        }
        return new StringValue(stringValue);
    }
}
Also used : XPathException(org.exist.xquery.XPathException) IntegerValue(org.exist.xquery.value.IntegerValue) StringValue(org.exist.xquery.value.StringValue)

Example 24 with StringValue

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

the class PermissionsFunction method functionModeToOctal.

private Sequence functionModeToOctal(final String modeStr) throws XPathException {
    try {
        final int mode = AbstractUnixStylePermission.simpleSymbolicModeToInt(modeStr);
        final String octal = mode == 0 ? "0" : "0" + Integer.toOctalString(mode);
        return new StringValue(octal);
    } catch (final SyntaxException se) {
        throw new XPathException(se.getMessage(), se);
    }
}
Also used : XPathException(org.exist.xquery.XPathException) SyntaxException(org.exist.util.SyntaxException) StringValue(org.exist.xquery.value.StringValue)

Example 25 with StringValue

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

the class GetAttributeNames method eval.

@Override
public Sequence eval(final Sequence[] args, @Nonnull final SessionWrapper session) throws XPathException {
    final Optional<Enumeration<String>> maybeAttributeNames = withValidSession(session, SessionWrapper::getAttributeNames);
    if (!maybeAttributeNames.isPresent()) {
        return Sequence.EMPTY_SEQUENCE;
    }
    final Enumeration<String> attributeNames = maybeAttributeNames.get();
    if (!attributeNames.hasMoreElements()) {
        return Sequence.EMPTY_SEQUENCE;
    }
    final ValueSequence result = new ValueSequence();
    while (attributeNames.hasMoreElements()) {
        final String attributeName = attributeNames.nextElement();
        result.add(new StringValue(attributeName));
    }
    return result;
}
Also used : Enumeration(java.util.Enumeration) ValueSequence(org.exist.xquery.value.ValueSequence) StringValue(org.exist.xquery.value.StringValue) SessionWrapper(org.exist.http.servlets.SessionWrapper)

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