Search in sources :

Example 16 with XPathError

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

the class CastableExpr method evaluate.

/**
 * {@inheritDoc}
 */
@Override
public void evaluate() throws SirixXPathException {
    // defines if current item is castable to the target type, or not
    boolean isCastable;
    // 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()) {
        // result sequence > 0
        mKey = mSourceExpr.next();
        final Type sourceType = Type.getType(getTrx().getTypeKey());
        final String sourceValue = getTrx().getValue();
        // determine castability
        isCastable = sourceType.isCastableTo(mTargetType, sourceValue);
        // item, a type error is raised.
        if (mSourceExpr.hasNext()) {
            // result sequence > 1
            throw new XPathError(ErrorType.XPTY0004);
        }
    } else {
        // result sequence = 0 (empty sequence)
        // empty sequence is allowed.
        isCastable = mPermitEmptySeq;
    }
    // create result item and move transaction to it.
    final int mItemKey = getTrx().getItemList().addItem(new AtomicValue(TypedValue.getBytes(Boolean.toString(isCastable)), getTrx().keyForName("xs:boolean")));
    mKey = mItemKey;
}
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) AtomicValue(org.sirix.service.xml.xpath.AtomicValue) XPathError(org.sirix.service.xml.xpath.XPathError)

Example 17 with XPathError

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

the class AndExprTest method testAndQuery.

@Test
public void testAndQuery() throws SirixException {
    holder.getReader().moveTo(1L);
    final AbstractAxis axis1 = new XPathAxis(holder.getReader(), "text() and 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() and node()");
    assertEquals(true, axis2.hasNext());
    axis2.next();
    assertEquals(false, Boolean.parseBoolean(holder.getReader().getValue()));
    assertEquals(false, axis2.hasNext());
    final AbstractAxis axis3 = new XPathAxis(holder.getReader(), "1 eq 1 and 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 and 2 eq 3");
    assertEquals(true, axis4.hasNext());
    axis4.next();
    assertEquals(false, Boolean.parseBoolean(holder.getReader().getValue()));
    assertEquals(false, axis4.hasNext());
    // is never evaluated.
    final AbstractAxis axis5 = new XPathAxis(holder.getReader(), "1 eq 2 and (3 idiv 0 = 1)");
    assertEquals(true, axis5.hasNext());
    axis5.next();
    assertEquals(false, Boolean.parseBoolean(holder.getReader().getValue()));
    assertEquals(false, axis5.hasNext());
    final AbstractAxis axis6 = new XPathAxis(holder.getReader(), "1 eq 1 and 3 idiv 0 = 1");
    try {
        assertEquals(true, axis6.hasNext());
        axis6.next();
        fail("Expected XPath exception, because of division by zero");
    } catch (XPathError e) {
        assertEquals("err:FOAR0001: Division by zero.", e.getMessage());
    }
}
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 18 with XPathError

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

the class DivOpAxisTest method testGetReturnType.

@Test
public final void testGetReturnType() throws SirixException {
    AbstractAxis op1 = new SequenceAxis(holder.getReader());
    AbstractAxis op2 = new SequenceAxis(holder.getReader());
    AbstractObAxis axis = new DivOpAxis(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")));
    // assertEquals(Type.INTEGER,
    // axis.getReturnType(holder.getRtx().keyForName("xs:integer"),
    // holder.getRtx().keyForName("xs:integer")));
    assertEquals(Type.YEAR_MONTH_DURATION, axis.getReturnType(holder.getReader().keyForName("xs:yearMonthDuration"), holder.getReader().keyForName("xs:double")));
    assertEquals(Type.DAY_TIME_DURATION, axis.getReturnType(holder.getReader().keyForName("xs:dayTimeDuration"), holder.getReader().keyForName("xs:double")));
    assertEquals(Type.DECIMAL, axis.getReturnType(holder.getReader().keyForName("xs:yearMonthDuration"), holder.getReader().keyForName("xs:yearMonthDuration")));
    assertEquals(Type.DECIMAL, axis.getReturnType(holder.getReader().keyForName("xs:dayTimeDuration"), holder.getReader().keyForName("xs:dayTimeDuration")));
    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 19 with XPathError

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

the class MulOpAxisTest method testGetReturnType.

@Test
public final void testGetReturnType() throws SirixException {
    AbstractAxis op1 = new SequenceAxis(holder.getReader());
    AbstractAxis op2 = new SequenceAxis(holder.getReader());
    AbstractObAxis axis = new MulOpAxis(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")));
    // assertEquals(Type.INTEGER,
    // axis.getReturnType(holder.getRtx().keyForName("xs:integer"),
    // holder.getRtx().keyForName("xs:integer")));
    assertEquals(Type.YEAR_MONTH_DURATION, axis.getReturnType(holder.getReader().keyForName("xs:yearMonthDuration"), holder.getReader().keyForName("xs:double")));
    assertEquals(Type.YEAR_MONTH_DURATION, axis.getReturnType(holder.getReader().keyForName("xs:integer"), holder.getReader().keyForName("xs:yearMonthDuration")));
    assertEquals(Type.DAY_TIME_DURATION, axis.getReturnType(holder.getReader().keyForName("xs:dayTimeDuration"), holder.getReader().keyForName("xs:double")));
    assertEquals(Type.DAY_TIME_DURATION, axis.getReturnType(holder.getReader().keyForName("xs:integer"), holder.getReader().keyForName("xs:dayTimeDuration")));
    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:yearMonthDuration"), 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."));
    }
}
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 20 with XPathError

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

the class SubOpAxisTest method testGetReturnType.

@Test
public final void testGetReturnType() throws SirixException {
    AbstractAxis op1 = new SequenceAxis(holder.getReader());
    AbstractAxis op2 = new SequenceAxis(holder.getReader());
    AbstractObAxis axis = new SubOpAxis(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")));
    // assertEquals(Type.INTEGER,
    // axis.getReturnType(holder.getRtx().keyForName("xs:integer"),
    // holder.getRtx().keyForName("xs:integer")));
    assertEquals(Type.YEAR_MONTH_DURATION, axis.getReturnType(holder.getReader().keyForName("xs:yearMonthDuration"), holder.getReader().keyForName("xs:yearMonthDuration")));
    assertEquals(Type.DAY_TIME_DURATION, axis.getReturnType(holder.getReader().keyForName("xs:dayTimeDuration"), holder.getReader().keyForName("xs:dayTimeDuration")));
    assertEquals(Type.DAY_TIME_DURATION, axis.getReturnType(holder.getReader().keyForName("xs:date"), holder.getReader().keyForName("xs:date")));
    assertEquals(Type.DATE, axis.getReturnType(holder.getReader().keyForName("xs:date"), holder.getReader().keyForName("xs:yearMonthDuration")));
    assertEquals(Type.DATE, axis.getReturnType(holder.getReader().keyForName("xs:date"), holder.getReader().keyForName("xs:dayTimeDuration")));
    assertEquals(Type.DAY_TIME_DURATION, axis.getReturnType(holder.getReader().keyForName("xs:time"), holder.getReader().keyForName("xs:time")));
    assertEquals(Type.DATE_TIME, axis.getReturnType(holder.getReader().keyForName("xs:dateTime"), holder.getReader().keyForName("xs:yearMonthDuration")));
    assertEquals(Type.DATE_TIME, axis.getReturnType(holder.getReader().keyForName("xs:dateTime"), holder.getReader().keyForName("xs:dayTimeDuration")));
    assertEquals(Type.DAY_TIME_DURATION, axis.getReturnType(holder.getReader().keyForName("xs:dateTime"), holder.getReader().keyForName("xs:dateTime")));
    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: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