Search in sources :

Example 11 with Type

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

the class SubOpAxis 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 DOUBLE:
        case FLOAT:
        case DECIMAL:
        case INTEGER:
            final double dOp1 = Double.parseDouble(new String(mOperand1.getRawValue()));
            final double dOp2 = Double.parseDouble(new String(mOperand2.getRawValue()));
            value = TypedValue.getBytes(dOp1 - dOp2);
            break;
        case DATE:
        case TIME:
        case DATE_TIME:
        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);
    }
    return new AtomicValue(value, typeKey);
}
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 12 with Type

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

the class SubOpAxis method getReturnType.

/**
 * {@inheritDoc}
 */
@Override
protected Type getReturnType(final int mOp1, final int mOp2) throws SirixXPathException {
    Type type1;
    Type type2;
    try {
        type1 = Type.getType(mOp1).getPrimitiveBaseType();
        type2 = Type.getType(mOp2).getPrimitiveBaseType();
    } catch (final IllegalStateException e) {
        throw new XPathError(ErrorType.XPTY0004);
    }
    if (type1.isNumericType() && type2.isNumericType()) {
        // if both have the same numeric type, return it
        if (type1 == type2) {
            return type1;
        }
        if (type1 == Type.DOUBLE || type2 == Type.DOUBLE) {
            return Type.DOUBLE;
        } else if (type1 == Type.FLOAT || type2 == Type.FLOAT) {
            return Type.FLOAT;
        } else {
            assert (type1 == Type.DECIMAL || type2 == Type.DECIMAL);
            return Type.DECIMAL;
        }
    } else {
        switch(type1) {
            case DATE:
                if (type2 == Type.YEAR_MONTH_DURATION || type2 == Type.DAY_TIME_DURATION) {
                    return type1;
                } else if (type2 == Type.DATE) {
                    return Type.DAY_TIME_DURATION;
                }
                break;
            case TIME:
                if (type2 == Type.DAY_TIME_DURATION) {
                    return type1;
                } else if (type2 == Type.TIME) {
                    return Type.DAY_TIME_DURATION;
                }
                break;
            case DATE_TIME:
                if (type2 == Type.YEAR_MONTH_DURATION || type2 == Type.DAY_TIME_DURATION) {
                    return type1;
                } else if (type2 == Type.DATE_TIME) {
                    return Type.DAY_TIME_DURATION;
                }
                break;
            case YEAR_MONTH_DURATION:
                if (type2 == Type.YEAR_MONTH_DURATION) {
                    return type2;
                }
                break;
            case DAY_TIME_DURATION:
                if (type2 == Type.DAY_TIME_DURATION) {
                    return type2;
                }
                break;
            default:
                throw new XPathError(ErrorType.XPTY0004);
        }
        throw new XPathError(ErrorType.XPTY0004);
    }
}
Also used : Type(org.sirix.service.xml.xpath.types.Type) ErrorType(org.sirix.service.xml.xpath.XPathError.ErrorType) XPathError(org.sirix.service.xml.xpath.XPathError)

Example 13 with Type

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

the class FNBoolean method computeResult.

/**
 * {@inheritDoc}
 */
@Override
protected byte[] computeResult() throws SirixXPathException {
    final Axis axis = getArgs().get(0);
    boolean value = false;
    if (axis.hasNext()) {
        mKey = axis.next();
        final XdmNodeReadTrx rtx = axis.getTrx();
        if (rtx.getNodeKey() >= 0) {
            // first item is a real node
            // ->
            // true
            value = true;
        } else {
            final Type type = Type.getType(rtx.getTypeKey());
            if (type.derivesFrom(Type.BOOLEAN)) {
                value = Boolean.parseBoolean(rtx.getValue());
            // value = TypedValue.parseBoolean(rtx.getRawValue());
            } else if (type.derivesFrom(Type.STRING) || type.derivesFrom(Type.ANY_URI) || type.derivesFrom(Type.UNTYPED_ATOMIC)) {
                // if length = 0 -> false
                value = (rtx.getValue().length() > 0);
            } else if (type.isNumericType()) {
                final double dValue = Double.parseDouble(rtx.getValue());
                value = !(Double.isNaN(dValue) || dValue == 0.0d);
            } else {
                // for all other types throw error FORG0006
                throw EXPathError.FORG0006.getEncapsulatedException();
            }
            // if is not a singleton
            if (axis.hasNext()) {
                throw EXPathError.FORG0006.getEncapsulatedException();
            }
        }
    } else {
        // expression is an empty sequence -> false
        value = false;
    }
    return TypedValue.getBytes(Boolean.toString(value));
}
Also used : Type(org.sirix.service.xml.xpath.types.Type) XdmNodeReadTrx(org.sirix.api.XdmNodeReadTrx) Axis(org.sirix.api.Axis)

Example 14 with Type

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

the class AddOpAxis method getReturnType.

/**
 * {@inheritDoc}
 */
@Override
protected Type getReturnType(final int mOp1, final int mOp2) throws SirixXPathException {
    final Type mType1 = Type.getType(mOp1).getPrimitiveBaseType();
    final Type mType2 = Type.getType(mOp2).getPrimitiveBaseType();
    if (mType1.isNumericType() && mType2.isNumericType()) {
        // if both have the same numeric type, return it
        if (mType1 == mType2) {
            return mType1;
        } else if (mType1 == Type.DOUBLE || mType2 == Type.DOUBLE) {
            return Type.DOUBLE;
        } else if (mType1 == Type.FLOAT || mType2 == Type.FLOAT) {
            return Type.FLOAT;
        } else {
            assert (mType1 == Type.DECIMAL || mType2 == Type.DECIMAL);
            return Type.DECIMAL;
        }
    } else {
        switch(mType1) {
            case DATE:
                if (mType2 == Type.YEAR_MONTH_DURATION || mType2 == Type.DAY_TIME_DURATION) {
                    return mType1;
                }
                break;
            case TIME:
                if (mType2 == Type.DAY_TIME_DURATION) {
                    return mType1;
                }
                break;
            case DATE_TIME:
                if (mType2 == Type.YEAR_MONTH_DURATION || mType2 == Type.DAY_TIME_DURATION) {
                    return mType1;
                }
                break;
            case YEAR_MONTH_DURATION:
                if (mType2 == Type.DATE || mType2 == Type.DATE_TIME || mType2 == Type.YEAR_MONTH_DURATION) {
                    return mType2;
                }
                break;
            case DAY_TIME_DURATION:
                if (mType2 == Type.DATE || mType2 == Type.TIME || mType2 == Type.DATE_TIME || mType2 == Type.DAY_TIME_DURATION) {
                    return mType2;
                }
                break;
            default:
                throw EXPathError.XPTY0004.getEncapsulatedException();
        }
        throw EXPathError.XPTY0004.getEncapsulatedException();
    }
}
Also used : Type(org.sirix.service.xml.xpath.types.Type)

Example 15 with Type

use of org.sirix.service.xml.xpath.types.Type 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)

Aggregations

Type (org.sirix.service.xml.xpath.types.Type)18 XPathError (org.sirix.service.xml.xpath.XPathError)12 ErrorType (org.sirix.service.xml.xpath.XPathError.ErrorType)12 AtomicValue (org.sirix.service.xml.xpath.AtomicValue)7 SingleType (org.sirix.service.xml.xpath.SingleType)2 Axis (org.sirix.api.Axis)1 XdmNodeReadTrx (org.sirix.api.XdmNodeReadTrx)1