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));
}
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());
}
}
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;
}
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;
}
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);
}
}
Aggregations