Search in sources :

Example 81 with XPathException

use of org.exist.xquery.XPathException 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 82 with XPathException

use of org.exist.xquery.XPathException 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 83 with XPathException

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

the class Shutdown method eval.

/* (non-Javadoc)
	 * @see org.exist.xquery.BasicFunction#eval(org.exist.xquery.value.Sequence[], org.exist.xquery.value.Sequence)
	 */
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    if (context.getSubject().hasDbaRole()) {
        // determine the shutdown delay
        long delay = 0;
        if (args.length == 1) {
            if (!args[0].isEmpty()) {
                delay = ((NumericValue) args[0].itemAt(0)).getLong();
            }
        }
        // get the broker pool and shutdown
        final BrokerPool pool = context.getBroker().getBrokerPool();
        if (delay > 0) {
            logger.info("Shutdown in {} milliseconds.", delay);
            final Timer timer = new Timer("eXist-db shutdown schedule", true);
            final TimerTask task = new DelayedShutdownTask(timer, pool);
            timer.schedule(task, delay);
        } else {
            logger.info("Shutting down now.");
            pool.shutdown();
        }
    } else {
        final XPathException xPathException = new XPathException(this, "Permission denied, calling user '" + context.getSubject().getName() + "' must be a DBA to shutdown the database");
        logger.error("Invalid user", xPathException);
        throw xPathException;
    }
    return Sequence.EMPTY_SEQUENCE;
}
Also used : Timer(java.util.Timer) TimerTask(java.util.TimerTask) XPathException(org.exist.xquery.XPathException) BrokerPool(org.exist.storage.BrokerPool)

Example 84 with XPathException

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

the class TriggerSystemTask method eval.

public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    final String className = args[0].getStringValue();
    final Properties properties = new Properties();
    if (args[1].hasOne()) {
        parseParameters(((NodeValue) args[1].itemAt(0)).getNode(), properties);
    }
    try {
        final Class<?> clazz = Class.forName(className);
        final Object taskObject = clazz.newInstance();
        if (!(taskObject instanceof SystemTask)) {
            final XPathException xPathException = new XPathException(this, className + " is not an instance of org.exist.storage.SystemTask");
            logger.error("Java classname is not a SystemTask", xPathException);
            throw xPathException;
        }
        final SystemTask task = (SystemTask) taskObject;
        task.configure(context.getBroker().getConfiguration(), properties);
        LOG.info("Triggering SystemTask: {}", className);
        context.getBroker().getBrokerPool().triggerSystemTask(task);
    } catch (final ClassNotFoundException e) {
        final String message = "system task class '" + className + "' not found";
        logger.error(message, e);
        throw new XPathException(this, message);
    } catch (final InstantiationException e) {
        final String message = "system task '" + className + "' can not be instantiated";
        logger.error(message, e);
        throw new XPathException(this, message);
    } catch (final IllegalAccessException e) {
        final String message = "system task '" + className + "' can not be accessed";
        logger.error(message, e);
        throw new XPathException(this, message);
    } catch (final EXistException e) {
        final String message = "system task " + className + " reported an error during initialization: ";
        logger.error(message, e);
        throw new XPathException(this, message + e.getMessage(), e);
    }
    return Sequence.EMPTY_SEQUENCE;
}
Also used : XPathException(org.exist.xquery.XPathException) SystemTask(org.exist.storage.SystemTask) EXistException(org.exist.EXistException) Properties(java.util.Properties)

Example 85 with XPathException

use of org.exist.xquery.XPathException 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)

Aggregations

XPathException (org.exist.xquery.XPathException)306 Sequence (org.exist.xquery.value.Sequence)86 IOException (java.io.IOException)61 SAXException (org.xml.sax.SAXException)43 StringValue (org.exist.xquery.value.StringValue)40 PermissionDeniedException (org.exist.security.PermissionDeniedException)34 NodeValue (org.exist.xquery.value.NodeValue)34 DBBroker (org.exist.storage.DBBroker)32 IntegerValue (org.exist.xquery.value.IntegerValue)32 ValueSequence (org.exist.xquery.value.ValueSequence)27 Item (org.exist.xquery.value.Item)26 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)24 EXistException (org.exist.EXistException)23 Path (java.nio.file.Path)22 XmldbURI (org.exist.xmldb.XmldbURI)22 BrokerPool (org.exist.storage.BrokerPool)21 Txn (org.exist.storage.txn.Txn)21 XQueryContext (org.exist.xquery.XQueryContext)21 Element (org.w3c.dom.Element)21 XQuery (org.exist.xquery.XQuery)20