use of org.sirix.api.Axis in project sirix by sirixdb.
the class PipelineBuilder method addOperatorExpression.
/**
* Adds an operator expression to the pipeline.
*
* @param mTransaction Transaction to operate with.
* @param mOperator Operator type.
*/
public void addOperatorExpression(final XdmNodeReadTrx mTransaction, final String mOperator) {
assert getPipeStack().size() >= 1;
final XdmNodeReadTrx rtx = mTransaction;
final Axis mOperand2 = getPipeStack().pop().getExpr();
// the unary operation only has one operator
final Axis mOperand1 = getPipeStack().pop().getExpr();
if (getPipeStack().empty() || getExpression().getSize() != 0) {
addExpressionSingle();
}
final Axis axis;
// TODO: use typeswitch of JAVA 7
if (mOperator.equals("+")) {
axis = new AddOpAxis(rtx, mOperand1, mOperand2);
} else if (mOperator.equals("-")) {
axis = new SubOpAxis(rtx, mOperand1, mOperand2);
} else if (mOperator.equals("*")) {
axis = new MulOpAxis(rtx, mOperand1, mOperand2);
} else if (mOperator.equals("div")) {
axis = new DivOpAxis(rtx, mOperand1, mOperand2);
} else if (mOperator.equals("idiv")) {
axis = new IDivOpAxis(rtx, mOperand1, mOperand2);
} else if (mOperator.equals("mod")) {
axis = new ModOpAxis(rtx, mOperand1, mOperand2);
} else {
// TODO: unary operator
throw new IllegalStateException(mOperator + " is not a valid operator.");
}
getExpression().add(axis);
}
use of org.sirix.api.Axis in project sirix by sirixdb.
the class PipelineBuilder method addCastableExpr.
/**
* Adds a castable expression to the pipeline.
*
* @param mTransaction Transaction to operate with.
* @param mSingleType single type the context item will be casted to.
*/
public void addCastableExpr(final XdmNodeReadTrx mTransaction, final SingleType mSingleType) {
assert getPipeStack().size() >= 1;
final Axis candidate = getPipeStack().pop().getExpr();
final Axis axis = new CastableExpr(mTransaction, candidate, mSingleType);
if (getPipeStack().empty() || getExpression().getSize() != 0) {
addExpressionSingle();
}
getExpression().add(axis);
}
use of org.sirix.api.Axis in project sirix by sirixdb.
the class PipelineBuilder method addAndExpression.
/**
* Adds a and expression to the pipeline.
*
* @param mTransaction Transaction to operate with.
*/
public void addAndExpression(final XdmNodeReadTrx mTransaction) {
assert getPipeStack().size() >= 2;
final Axis mOperand2 = getPipeStack().pop().getExpr();
final Axis operand1 = getPipeStack().pop().getExpr();
if (getPipeStack().empty() || getExpression().getSize() != 0) {
addExpressionSingle();
}
getExpression().add(new AndExpr(mTransaction, operand1, mOperand2));
}
use of org.sirix.api.Axis 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();
}
}
use of org.sirix.api.Axis in project sirix by sirixdb.
the class AbstractSerializer method call.
/**
* Serialize the storage.
*
* @return null.
* @throws SirixException if can't call serailzer
*/
@Override
public Void call() throws SirixException {
emitStartDocument();
final int nrOfRevisions = mRevisions.length;
final int length = (nrOfRevisions == 1 && mRevisions[0] < 0) ? (int) mSession.getMostRecentRevisionNumber() : nrOfRevisions;
if (length > 1) {
emitStartManualRootElement();
}
for (int i = 1; i <= length; i++) {
try (final XdmNodeReadTrx rtx = mSession.beginNodeReadTrx((nrOfRevisions == 1 && mRevisions[0] < 0) ? i : mRevisions[i - 1])) {
if (length > 1) {
emitStartManualElement(i);
}
rtx.moveTo(mNodeKey);
final Axis descAxis = new DescendantAxis(rtx, IncludeSelf.YES);
// Setup primitives.
boolean closeElements = false;
long key = rtx.getNodeKey();
// Iterate over all nodes of the subtree including self.
while (descAxis.hasNext()) {
key = descAxis.next();
// Emit all pending end elements.
if (closeElements) {
while (!mStack.isEmpty() && mStack.peek() != rtx.getLeftSiblingKey()) {
rtx.moveTo(mStack.pop());
emitEndElement(rtx);
rtx.moveTo(key);
}
if (!mStack.isEmpty()) {
rtx.moveTo(mStack.pop());
emitEndElement(rtx);
}
rtx.moveTo(key);
closeElements = false;
}
// Emit node.
emitStartElement(rtx);
// children.
if (rtx.getKind() == Kind.ELEMENT && rtx.hasFirstChild()) {
mStack.push(rtx.getNodeKey());
}
// required.
if (!rtx.hasFirstChild() && !rtx.hasRightSibling()) {
closeElements = true;
}
}
// Finally emit all pending end elements.
while (!mStack.isEmpty()) {
rtx.moveTo(mStack.pop());
emitEndElement(rtx);
}
if (length > 1) {
emitEndManualElement(i);
}
}
}
if (length > 1) {
emitEndManualRootElement();
}
emitEndDocument();
return null;
}
Aggregations