Search in sources :

Example 1 with AtomicValue

use of org.sirix.service.xml.xpath.AtomicValue in project sirix by sirixdb.

the class AbstractComparator method hasNext.

@Override
public final boolean hasNext() {
    resetToLastKey();
    if (mIsFirst) {
        mIsFirst = false;
        // TODO: why?
        if (!(mOperand1 instanceof LiteralExpr)) {
            mOperand1.reset(getTrx().getNodeKey());
        }
        // TODO: why?
        if (!(mOperand2 instanceof LiteralExpr)) {
            mOperand2.reset(getTrx().getNodeKey());
        }
        /*
       * Evaluates the comparison. First atomizes both operands and then executes the comparison on
       * them. At the end, the transaction is set to the retrieved result item.
       */
        if (mOperand1.hasNext()) {
            mKey = mOperand1.next();
            try {
                // atomize operands
                final AtomicValue[] operandOne = atomize(mOperand1);
                if (mOperand2.hasNext()) {
                    mKey = mOperand2.next();
                    final AtomicValue[] operandTwo = atomize(mOperand2);
                    hook(operandOne, operandTwo);
                    try {
                        // get comparison result
                        final boolean resultValue = compare(operandOne, operandTwo);
                        final AtomicValue result = new AtomicValue(resultValue);
                        // add retrieved AtomicValue to item list
                        final int itemKey = getTrx().getItemList().addItem(result);
                        mKey = itemKey;
                    } catch (SirixXPathException e) {
                        throw new RuntimeException(e);
                    }
                    return true;
                }
            } catch (final SirixXPathException exc) {
                throw new RuntimeException(exc);
            }
        }
    }
    // return empty sequence or function called more than once
    resetToStartKey();
    return false;
}
Also used : SirixXPathException(org.sirix.exception.SirixXPathException) LiteralExpr(org.sirix.service.xml.xpath.expr.LiteralExpr) AtomicValue(org.sirix.service.xml.xpath.AtomicValue)

Example 2 with AtomicValue

use of org.sirix.service.xml.xpath.AtomicValue in project sirix by sirixdb.

the class GeneralComp method atomize.

/**
 * {@inheritDoc}
 */
@Override
protected AtomicValue[] atomize(final Axis mOperand) {
    final XdmNodeReadTrx rtx = getTrx();
    final List<AtomicValue> op = new ArrayList<AtomicValue>();
    AtomicValue atomized;
    // cast to double, if compatible with XPath 1.0 and <, >, >=, <=
    final boolean convert = !(!XPATH_10_COMP || getCompKind() == CompKind.EQ || getCompKind() == CompKind.EQ);
    boolean first = true;
    do {
        if (first) {
            first = false;
        } else {
            mOperand.next();
        }
        if (convert) {
            // cast to double
            Function.fnnumber(rtx);
        }
        atomized = new AtomicValue(rtx.getValue().getBytes(), rtx.getTypeKey());
        op.add(atomized);
    } while (mOperand.hasNext());
    return op.toArray(new AtomicValue[op.size()]);
}
Also used : XdmNodeReadTrx(org.sirix.api.XdmNodeReadTrx) ArrayList(java.util.ArrayList) AtomicValue(org.sirix.service.xml.xpath.AtomicValue)

Example 3 with AtomicValue

use of org.sirix.service.xml.xpath.AtomicValue in project sirix by sirixdb.

the class GeneralComp method compare.

/**
 * {@inheritDoc}
 */
@Override
protected boolean compare(final AtomicValue[] mOperand1, final AtomicValue[] mOperand2) throws SirixXPathException {
    assert mOperand1.length >= 1 && mOperand2.length >= 1;
    for (AtomicValue op1 : mOperand1) {
        for (AtomicValue op2 : mOperand2) {
            String value1 = new String(op1.getRawValue());
            String value2 = new String(op2.getRawValue());
            if (getCompKind().compare(value1, value2, getType(op1.getTypeKey(), op2.getTypeKey()))) {
                return true;
            }
        }
    }
    return false;
}
Also used : AtomicValue(org.sirix.service.xml.xpath.AtomicValue)

Example 4 with AtomicValue

use of org.sirix.service.xml.xpath.AtomicValue in project sirix by sirixdb.

the class AndExpr method evaluate.

/**
 * {@inheritDoc}
 *
 * @throws SirixXPathException
 */
@Override
public void evaluate() throws SirixXPathException {
    // first find the effective boolean values of the two operands, then
    // determine value of the and-expression and store it in an item
    final boolean result = Function.ebv(mOp1) && Function.ebv(mOp2);
    // note: the error handling is implicitly done by the fnBoolean()
    // function.
    // add result item to list and set the item as the current item
    final int itemKey = getTrx().getItemList().addItem(new AtomicValue(TypedValue.getBytes(Boolean.toString(result)), getTrx().keyForName("xs:boolean")));
    mKey = itemKey;
}
Also used : AtomicValue(org.sirix.service.xml.xpath.AtomicValue)

Example 5 with AtomicValue

use of org.sirix.service.xml.xpath.AtomicValue in project sirix by sirixdb.

the class InstanceOfExpr method evaluate.

/**
 * {@inheritDoc}
 */
@Override
public void evaluate() {
    boolean isInstanceOf;
    if (mInputExpr.hasNext()) {
        mKey = mInputExpr.next();
        if (mSequenceType.isEmptySequence()) {
            isInstanceOf = false;
        } else {
            isInstanceOf = mSequenceType.getFilter().filter();
            switch(mSequenceType.getWildcard()) {
                case '*':
                case '+':
                    // result are no longer used, it might be not that bad
                    while (mInputExpr.hasNext() && isInstanceOf) {
                        mKey = mInputExpr.next();
                        isInstanceOf = isInstanceOf && mSequenceType.getFilter().filter();
                    }
                    break;
                default:
                    // no wildcard, or '?'
                    // only one result item is allowed
                    isInstanceOf = isInstanceOf && !mInputExpr.hasNext();
            }
        }
    } else {
        // empty sequence
        isInstanceOf = mSequenceType.isEmptySequence() || (mSequenceType.hasWildcard() && (mSequenceType.getWildcard() == '?' || mSequenceType.getWildcard() == '*'));
    }
    // create result item and move transaction to it.
    final int itemKey = getTrx().getItemList().addItem(new AtomicValue(TypedValue.getBytes(Boolean.toString(isInstanceOf)), getTrx().keyForName("xs:boolean")));
    mKey = itemKey;
}
Also used : AtomicValue(org.sirix.service.xml.xpath.AtomicValue)

Aggregations

AtomicValue (org.sirix.service.xml.xpath.AtomicValue)47 Test (org.junit.Test)12 AbstractAxis (org.sirix.service.xml.xpath.AbstractAxis)10 LiteralExpr (org.sirix.service.xml.xpath.expr.LiteralExpr)10 Type (org.sirix.service.xml.xpath.types.Type)7 XPathError (org.sirix.service.xml.xpath.XPathError)6 ErrorType (org.sirix.service.xml.xpath.XPathError.ErrorType)6 XdmNodeReadTrx (org.sirix.api.XdmNodeReadTrx)4 Axis (org.sirix.api.Axis)3 SirixXPathException (org.sirix.exception.SirixXPathException)3 ArrayList (java.util.ArrayList)1 DescendantAxis (org.sirix.axis.DescendantAxis)1 SingleType (org.sirix.service.xml.xpath.SingleType)1