Search in sources :

Example 31 with AtomicValue

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

the class AbstractObAxis method atomize.

/**
 * Atomizes an operand according to the rules specified in the XPath specification.
 *
 * @param mOperand the operand to atomize
 * @return the atomized operand. (always an atomic value)
 */
private AtomicValue atomize(final Axis mOperand) {
    final XdmNodeReadTrx rtx = getTrx();
    int type = rtx.getTypeKey();
    AtomicValue atom;
    if (XPATH_10_COMP) {
        if (type == rtx.keyForName("xs:double") || type == rtx.keyForName("xs:untypedAtomic") || type == rtx.keyForName("xs:boolean") || type == rtx.keyForName("xs:string") || type == rtx.keyForName("xs:integer") || type == rtx.keyForName("xs:float") || type == rtx.keyForName("xs:decimal")) {
            Function.fnnumber(mOperand.getTrx());
        }
        atom = new AtomicValue(rtx.getValue().getBytes(), rtx.getTypeKey());
    } else {
        // unatomicType is cast to double
        if (type == rtx.keyForName("xs:untypedAtomic")) {
            type = rtx.keyForName("xs:double");
        // TODO: throw error, of cast fails
        }
        atom = new AtomicValue(rtx.getValue().getBytes(), type);
    }
    return atom;
}
Also used : XdmNodeReadTrx(org.sirix.api.XdmNodeReadTrx) AtomicValue(org.sirix.service.xml.xpath.AtomicValue)

Example 32 with AtomicValue

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

the class DivOpAxis method operate.

/**
 * {@inheritDoc}
 */
@Override
public Node operate(final AtomicValue mOperand1, final AtomicValue mOperand2) throws SirixXPathException {
    final Type returnType = getReturnType(mOperand1.getTypeKey(), mOperand2.getTypeKey());
    final int typeKey = getTrx().keyForName(returnType.getStringRepr());
    final byte[] value;
    switch(returnType) {
        case DECIMAL:
        case FLOAT:
        case DOUBLE:
            final double aD = Double.parseDouble(new String(mOperand1.getRawValue()));
            final double dValue;
            if (aD == 0.0 || aD == -0.0) {
                dValue = Double.NaN;
            } else {
                dValue = aD / Double.parseDouble(new String(mOperand2.getRawValue()));
            }
            value = TypedValue.getBytes(dValue);
            return new AtomicValue(value, typeKey);
        case INTEGER:
            try {
                final int iValue = (int) Double.parseDouble(new String(mOperand1.getRawValue())) / (int) Double.parseDouble(new String(mOperand2.getRawValue()));
                value = TypedValue.getBytes(iValue);
                return new AtomicValue(value, typeKey);
            } catch (final ArithmeticException e) {
                throw new XPathError(ErrorType.FOAR0001);
            }
        case YEAR_MONTH_DURATION:
        case DAY_TIME_DURATION:
            throw new IllegalStateException("Add operator is not implemented for the type " + returnType.getStringRepr() + " yet.");
        default:
            throw new XPathError(ErrorType.XPTY0004);
    }
}
Also used : Type(org.sirix.service.xml.xpath.types.Type) ErrorType(org.sirix.service.xml.xpath.XPathError.ErrorType) AtomicValue(org.sirix.service.xml.xpath.AtomicValue) XPathError(org.sirix.service.xml.xpath.XPathError)

Example 33 with AtomicValue

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

the class IDivOpAxis method operate.

/**
 * {@inheritDoc}
 */
@Override
public Node operate(final AtomicValue mOperand1, final AtomicValue mOperand2) throws SirixXPathException {
    final Type returnType = getReturnType(mOperand1.getTypeKey(), mOperand2.getTypeKey());
    final int typeKey = getTrx().keyForName(returnType.getStringRepr());
    final byte[] value;
    try {
        final int op1 = (int) Double.parseDouble(new String(mOperand1.getRawValue()));
        final int op2 = (int) Double.parseDouble(new String(mOperand2.getRawValue()));
        final int iValue = op1 / op2;
        value = TypedValue.getBytes(iValue);
        return new AtomicValue(value, typeKey);
    } catch (final ArithmeticException e) {
        // LOGWRAPPER.error(e);
        throw new XPathError(ErrorType.FOAR0001);
    }
}
Also used : Type(org.sirix.service.xml.xpath.types.Type) ErrorType(org.sirix.service.xml.xpath.XPathError.ErrorType) AtomicValue(org.sirix.service.xml.xpath.AtomicValue) XPathError(org.sirix.service.xml.xpath.XPathError)

Example 34 with AtomicValue

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

the class Function method oneOrMore.

public static boolean oneOrMore(final XdmNodeReadTrx rtx, final AbstractAxis axis) throws SirixXPathException {
    if (!axis.hasNext()) {
        throw EXPathError.FORG0004.getEncapsulatedException();
    } else {
        final int itemKey = rtx.getItemList().addItem(new AtomicValue(true));
        rtx.moveTo(itemKey);
    }
    return true;
}
Also used : AtomicValue(org.sirix.service.xml.xpath.AtomicValue)

Example 35 with AtomicValue

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

the class Function method fnData.

/**
 * fn:data takes a sequence of items and returns a sequence of atomic values. The result of
 * fn:data is the sequence of atomic values produced by applying the following rules to each item
 * in the input sequence: If the item is an atomic value, it is returned. If the item is a node,
 * its typed value is returned (err:FOTY0012 is raised if the node has no typed value.)
 *
 * @param rtx the transaction to operate on
 * @param axis The sequence to atomize.
 * @return true, if an atomic value can be returned
 */
public static boolean fnData(final XdmNodeReadTrx rtx, final AbstractAxis axis) {
    if (axis.hasNext()) {
        if (rtx.getNodeKey() >= 0) {
            // set to typed value
            // if has no typed value
            // TODO // throw new XPathError(FOTY0012);
            final int itemKey = rtx.getItemList().addItem(new AtomicValue(rtx.getValue().getBytes(), rtx.getTypeKey()));
            rtx.moveTo(itemKey);
            return true;
        } else {
            // return current item -> do nothing
            return true;
        }
    } else {
        // no more items.
        return false;
    }
}
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