Search in sources :

Example 6 with XPathError

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

the class MulOpAxis 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 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 7 with XPathError

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

the class OrExprTest method testOrQuery.

@Test
public void testOrQuery() throws SirixException {
    holder.getReader().moveTo(1L);
    final AbstractAxis axis1 = new XPathAxis(holder.getReader(), "text() or node()");
    assertEquals(true, axis1.hasNext());
    axis1.next();
    assertEquals(true, Boolean.parseBoolean(holder.getReader().getValue()));
    assertEquals(false, axis1.hasNext());
    final AbstractAxis axis2 = new XPathAxis(holder.getReader(), "comment() or node()");
    assertEquals(true, axis2.hasNext());
    axis2.next();
    assertEquals(true, Boolean.parseBoolean(holder.getReader().getValue()));
    assertEquals(false, axis2.hasNext());
    final AbstractAxis axis3 = new XPathAxis(holder.getReader(), "1 eq 1 or 2 eq 2");
    assertEquals(true, axis3.hasNext());
    axis3.next();
    assertEquals(true, Boolean.parseBoolean(holder.getReader().getValue()));
    assertEquals(false, axis3.hasNext());
    final AbstractAxis axis4 = new XPathAxis(holder.getReader(), "1 eq 1 or 2 eq 3");
    assertEquals(true, axis4.hasNext());
    axis4.next();
    assertEquals(true, Boolean.parseBoolean(holder.getReader().getValue()));
    assertEquals(false, axis4.hasNext());
    final AbstractAxis axis5 = new XPathAxis(holder.getReader(), "1 eq 2 or (3 idiv 0 = 1)");
    try {
        assertEquals(true, axis5.hasNext());
        axis5.next();
        assertEquals(false, Boolean.parseBoolean(holder.getReader().getValue()));
        assertEquals(false, axis5.hasNext());
        axis5.next();
        fail("Exprected XPathError");
    } catch (XPathError e) {
        assertEquals("err:FOAR0001: Division by zero.", e.getMessage());
    }
    final AbstractAxis axis6 = new XPathAxis(holder.getReader(), "1 eq 1 or (3 idiv 0 = 1)");
    assertEquals(true, axis6.hasNext());
    axis6.next();
    assertEquals(true, Boolean.parseBoolean(holder.getReader().getValue()));
}
Also used : XPathAxis(org.sirix.service.xml.xpath.XPathAxis) AbstractAxis(org.sirix.service.xml.xpath.AbstractAxis) XPathError(org.sirix.service.xml.xpath.XPathError) Test(org.junit.Test)

Example 8 with XPathError

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

the class CastableExprTest method testCastableExpr.

@Test
public void testCastableExpr() throws SirixException {
    final AbstractAxis axis1 = new XPathAxis(holder.getReader(), "1 castable as xs:decimal");
    assertEquals(true, axis1.hasNext());
    axis1.next();
    assertEquals(holder.getReader().keyForName("xs:boolean"), holder.getReader().getTypeKey());
    assertEquals(true, Boolean.parseBoolean(holder.getReader().getValue()));
    assertEquals(false, axis1.hasNext());
    final AbstractAxis axis2 = new XPathAxis(holder.getReader(), "10.0 castable as xs:anyAtomicType");
    try {
        assertEquals(true, axis2.hasNext());
        axis2.next();
    } catch (XPathError e) {
        assertThat(e.getMessage(), is("err:XPST0080 " + "Target type of a cast or castable expression must not be " + "xs:NOTATION or xs:anyAtomicType."));
    }
    // Token is not implemented yet.
    // final IAxis axis3 = new XPathAxis(holder.getRtx(),
    // "\"hello\" castable as xs:token");
    // assertEquals(true, axis3.hasNext());
    // assertEquals(Type.BOOLEAN, holder.getRtx().getValueTypeAsType());
    // assertEquals(true, holder.getRtx().getValueAsBoolean());
    // assertEquals(false, axis3.hasNext());
    final AbstractAxis axis4 = new XPathAxis(holder.getReader(), "\"hello\" castable as xs:string");
    assertEquals(true, axis4.hasNext());
    axis4.next();
    assertEquals(holder.getReader().keyForName("xs:boolean"), holder.getReader().getTypeKey());
    assertEquals(true, Boolean.parseBoolean(holder.getReader().getValue()));
    assertEquals(false, axis4.hasNext());
// final IAxis axis5 = new XPathAxis(holder.getRtx(),
// "\"hello\" castable as xs:decimal");
// assertEquals(true, axis5.hasNext());
// assertEquals(holder.getRtx().keyForName("xs:boolean"),
// holder.getRtx().getTypeKey());
// assertEquals(true, Boolean.parseBoolean(holder.getRtx().getValue()));
// assertEquals(false, axis5.hasNext());
}
Also used : XPathAxis(org.sirix.service.xml.xpath.XPathAxis) AbstractAxis(org.sirix.service.xml.xpath.AbstractAxis) XPathError(org.sirix.service.xml.xpath.XPathError) Test(org.junit.Test)

Example 9 with XPathError

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

the class IDivOpAxisTest method testGetReturnType.

@Test
public final void testGetReturnType() throws SirixException {
    AbstractAxis op1 = new SequenceAxis(holder.getReader());
    AbstractAxis op2 = new SequenceAxis(holder.getReader());
    AbstractObAxis axis = new IDivOpAxis(holder.getReader(), op1, op2);
    assertEquals(Type.INTEGER, axis.getReturnType(holder.getReader().keyForName("xs:double"), holder.getReader().keyForName("xs:double")));
    assertEquals(Type.INTEGER, axis.getReturnType(holder.getReader().keyForName("xs:decimal"), holder.getReader().keyForName("xs:double")));
    assertEquals(Type.INTEGER, axis.getReturnType(holder.getReader().keyForName("xs:float"), holder.getReader().keyForName("xs:decimal")));
    assertEquals(Type.INTEGER, axis.getReturnType(holder.getReader().keyForName("xs:decimal"), holder.getReader().keyForName("xs:integer")));
    assertEquals(Type.INTEGER, axis.getReturnType(holder.getReader().keyForName("xs:integer"), holder.getReader().keyForName("xs:integer")));
    try {
        axis.getReturnType(holder.getReader().keyForName("xs:dateTime"), holder.getReader().keyForName("xs:yearMonthDuration"));
        fail("Expected an XPathError-Exception.");
    } catch (XPathError e) {
        assertThat(e.getMessage(), is("err:XPTY0004 The type is not appropriate the expression or the " + "typedoes not match a required type as specified by the matching rules."));
    }
    try {
        axis.getReturnType(holder.getReader().keyForName("xs:dateTime"), holder.getReader().keyForName("xs:double"));
        fail("Expected an XPathError-Exception.");
    } catch (XPathError e) {
        assertThat(e.getMessage(), is("err:XPTY0004 The type is not appropriate the expression or the " + "typedoes not match a required type as specified by the matching rules."));
    }
    try {
        axis.getReturnType(holder.getReader().keyForName("xs:string"), holder.getReader().keyForName("xs:yearMonthDuration"));
        fail("Expected an XPathError-Exception.");
    } catch (XPathError e) {
        assertThat(e.getMessage(), is("err:XPTY0004 The type is not appropriate the expression or the " + "typedoes not match a required type as specified by the matching rules."));
    }
    try {
        axis.getReturnType(holder.getReader().keyForName("xs:dateTime"), holder.getReader().keyForName("xs:IDREF"));
        fail("Expected an XPathError-Exception.");
    } catch (XPathError e) {
        assertThat(e.getMessage(), is("err:XPTY0004 The type is not appropriate the expression or the " + "typedoes not match a required type as specified by the matching rules."));
    }
}
Also used : SequenceAxis(org.sirix.service.xml.xpath.expr.SequenceAxis) AbstractAxis(org.sirix.service.xml.xpath.AbstractAxis) XPathError(org.sirix.service.xml.xpath.XPathError) Test(org.junit.Test)

Example 10 with XPathError

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

the class ModOpAxisTest method testGetReturnType.

@Test
public final void testGetReturnType() throws SirixException {
    AbstractAxis op1 = new SequenceAxis(holder.getReader());
    AbstractAxis op2 = new SequenceAxis(holder.getReader());
    AbstractObAxis axis = new ModOpAxis(holder.getReader(), op1, op2);
    assertEquals(Type.DOUBLE, axis.getReturnType(holder.getReader().keyForName("xs:double"), holder.getReader().keyForName("xs:double")));
    assertEquals(Type.DOUBLE, axis.getReturnType(holder.getReader().keyForName("xs:decimal"), holder.getReader().keyForName("xs:double")));
    assertEquals(Type.FLOAT, axis.getReturnType(holder.getReader().keyForName("xs:float"), holder.getReader().keyForName("xs:decimal")));
    assertEquals(Type.DECIMAL, axis.getReturnType(holder.getReader().keyForName("xs:decimal"), holder.getReader().keyForName("xs:integer")));
    try {
        axis.getReturnType(holder.getReader().keyForName("xs:dateTime"), holder.getReader().keyForName("xs:yearMonthDuration"));
        fail("Expected an XPathError-Exception.");
    } catch (XPathError e) {
        assertThat(e.getMessage(), is("err:XPTY0004 The type is not appropriate the expression or the " + "typedoes not match a required type as specified by the matching rules."));
    }
    try {
        axis.getReturnType(holder.getReader().keyForName("xs:dateTime"), holder.getReader().keyForName("xs:double"));
        fail("Expected an XPathError-Exception.");
    } catch (XPathError e) {
        assertThat(e.getMessage(), is("err:XPTY0004 The type is not appropriate the expression or the " + "typedoes not match a required type as specified by the matching rules."));
    }
    try {
        axis.getReturnType(holder.getReader().keyForName("xs:string"), holder.getReader().keyForName("xs:yearMonthDuration"));
        fail("Expected an XPathError-Exception.");
    } catch (XPathError e) {
        assertThat(e.getMessage(), is("err:XPTY0004 The type is not appropriate the expression or the " + "typedoes not match a required type as specified by the matching rules."));
    }
    try {
        axis.getReturnType(holder.getReader().keyForName("xs:dateTime"), holder.getReader().keyForName("xs:IDREF"));
        fail("Expected an XPathError-Exception.");
    } catch (XPathError e) {
        assertThat(e.getMessage(), is("err:XPTY0004 The type is not appropriate the expression or the " + "typedoes not match a required type as specified by the matching rules."));
    }
}
Also used : SequenceAxis(org.sirix.service.xml.xpath.expr.SequenceAxis) AbstractAxis(org.sirix.service.xml.xpath.AbstractAxis) XPathError(org.sirix.service.xml.xpath.XPathError) Test(org.junit.Test)

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