use of org.sirix.service.xml.xpath.functions.FuncDef in project sirix by sirixdb.
the class PipelineBuilder method addFunction.
/**
* Adds a function to the pipeline.
*
* @param mTransaction Transaction to operate with.
* @param mFuncName The name of the function
* @param mNum The number of arguments that are passed to the function
* @throws SirixXPathException if function can't be added
*/
public void addFunction(final XdmNodeReadTrx mTransaction, final String mFuncName, final int mNum) throws SirixXPathException {
assert getPipeStack().size() >= mNum;
final List<Axis> args = new ArrayList<Axis>(mNum);
// order
for (int i = 0; i < mNum; i++) {
args.add(getPipeStack().pop().getExpr());
}
// get right function type
final FuncDef func;
try {
func = FuncDef.fromString(mFuncName);
} catch (final NullPointerException e) {
throw EXPathError.XPST0017.getEncapsulatedException();
}
// get function class
final Class<? extends AbstractFunction> function = func.getFunc();
final Integer min = func.getMin();
final Integer max = func.getMax();
final Integer returnType = mTransaction.keyForName(func.getReturnType());
// parameter types of the function's constructor
final Class<?>[] paramTypes = { XdmNodeReadTrx.class, List.class, Integer.TYPE, Integer.TYPE, Integer.TYPE };
try {
// instantiate function class with right constructor
final Constructor<?> cons = function.getConstructor(paramTypes);
final Axis axis = (Axis) cons.newInstance(mTransaction, args, min, max, returnType);
if (getPipeStack().empty() || getExpression().getSize() != 0) {
addExpressionSingle();
}
getExpression().add(axis);
} catch (final NoSuchMethodException e) {
throw EXPathError.XPST0017.getEncapsulatedException();
} catch (final IllegalArgumentException e) {
throw EXPathError.XPST0017.getEncapsulatedException();
} catch (final InstantiationException e) {
throw new IllegalStateException("Function not implemented yet.");
} catch (final IllegalAccessException e) {
throw EXPathError.XPST0017.getEncapsulatedException();
} catch (final InvocationTargetException e) {
throw EXPathError.XPST0017.getEncapsulatedException();
}
}
Aggregations