Search in sources :

Example 1 with XPathError

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

the class CastExpr method evaluate.

/**
 * {@inheritDoc}
 */
@Override
public void evaluate() throws SirixXPathException {
    // atomic type must not be xs:anyAtomicType or xs:NOTATION
    if (mTargetType == Type.ANY_ATOMIC_TYPE || mTargetType == Type.NOTATION) {
        throw new XPathError(ErrorType.XPST0080);
    }
    if (mSourceExpr.hasNext()) {
        mSourceExpr.next();
        final Type sourceType = Type.getType(getTrx().getTypeKey());
        final String sourceValue = getTrx().getValue();
        // cast source to target type, if possible
        if (sourceType.isCastableTo(mTargetType, sourceValue)) {
            throw new IllegalStateException("casts not implemented yet.");
        // ((XPathReadTransaction)
        // getTransaction()).castTo(mTargetType);
        }
        // items, a type error is raised.
        if (mSourceExpr.hasNext()) {
            throw new XPathError(ErrorType.XPTY0004);
        }
    } else {
        // 3. if is empty sequence:
        if (!mPermitEmptySeq) {
            // otherwise an error is raised.
            throw new XPathError(ErrorType.XPTY0004);
        }
    }
}
Also used : Type(org.sirix.service.xml.xpath.types.Type) ErrorType(org.sirix.service.xml.xpath.XPathError.ErrorType) SingleType(org.sirix.service.xml.xpath.SingleType) XPathError(org.sirix.service.xml.xpath.XPathError)

Example 2 with XPathError

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

the class DivOpAxis 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 YEAR_MONTH_DURATION:
                if (type2 == Type.YEAR_MONTH_DURATION) {
                    return Type.DECIMAL;
                }
                if (type2.isNumericType()) {
                    return type1;
                }
                break;
            case DAY_TIME_DURATION:
                if (type2 == Type.DAY_TIME_DURATION) {
                    return Type.DECIMAL;
                }
                if (type2.isNumericType()) {
                    return type1;
                }
                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 3 with XPathError

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

the class IDivOpAxis 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()) {
        return Type.INTEGER;
    } else {
        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 4 with XPathError

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

the class ModOpAxis 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:
            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 INTEGER:
            try {
                final int iOp1 = (int) Double.parseDouble(new String(mOperand1.getRawValue()));
                final int iOp2 = (int) Double.parseDouble(new String(mOperand2.getRawValue()));
                value = TypedValue.getBytes(iOp1 % iOp2);
            } catch (final ArithmeticException e) {
                throw new XPathError(ErrorType.FOAR0001);
            }
            break;
        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 5 with XPathError

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

the class MulOpAxis 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 YEAR_MONTH_DURATION:
                if (type2.isNumericType()) {
                    return type1;
                }
                break;
            case DAY_TIME_DURATION:
                if (type2.isNumericType()) {
                    return type1;
                }
                break;
            case DOUBLE:
            case FLOAT:
            case DECIMAL:
            case INTEGER:
                if (type2 == Type.DAY_TIME_DURATION || type2 == Type.YEAR_MONTH_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)

Aggregations

XPathError (org.sirix.service.xml.xpath.XPathError)20 ErrorType (org.sirix.service.xml.xpath.XPathError.ErrorType)12 Type (org.sirix.service.xml.xpath.types.Type)12 Test (org.junit.Test)8 AbstractAxis (org.sirix.service.xml.xpath.AbstractAxis)8 AtomicValue (org.sirix.service.xml.xpath.AtomicValue)6 SequenceAxis (org.sirix.service.xml.xpath.expr.SequenceAxis)5 XPathAxis (org.sirix.service.xml.xpath.XPathAxis)3 SingleType (org.sirix.service.xml.xpath.SingleType)2