Search in sources :

Example 11 with XSDecimal

use of org.eclipse.wst.xml.xpath2.processor.internal.types.XSDecimal in project webtools.sourceediting by eclipse.

the class XSYearMonthDuration method times.

/**
 * Mathematical multiplication between this duration stored and the supplied
 * duration of time (of type XSYearMonthDuration)
 *
 * @param arg
 *            The duration of time to multiply by
 * @return New XSYearMonthDuration representing the resulting duration
 *         after the multiplication
 * @throws DynamicError
 */
public ResultSequence times(ResultSequence arg) throws DynamicError {
    ResultSequence convertedRS = arg;
    if (arg.size() == 1) {
        Item argValue = arg.first();
        if (argValue instanceof XSDecimal) {
            convertedRS = ResultSequenceFactory.create_new(new XSDouble(argValue.getStringValue()));
        }
    }
    XSDouble val = (XSDouble) NumericType.get_single_type(convertedRS, XSDouble.class);
    if (val.nan()) {
        throw DynamicError.nan();
    }
    if (val.infinite()) {
        throw DynamicError.overflowDateTime();
    }
    int res = (int) Math.round(monthValue() * val.double_value());
    return ResultSequenceFactory.create_new(new XSYearMonthDuration(res));
}
Also used : Item(org.eclipse.wst.xml.xpath2.api.Item) ResultSequence(org.eclipse.wst.xml.xpath2.api.ResultSequence)

Example 12 with XSDecimal

use of org.eclipse.wst.xml.xpath2.processor.internal.types.XSDecimal in project webtools.sourceediting by eclipse.

the class TestBugs method testNumberAggregationWithNill.

public void testNumberAggregationWithNill() throws Exception {
    URL fileURL = bundle.getEntry("/bugTestFiles/bugNilled.xml");
    URL schemaURL = bundle.getEntry("/bugTestFiles/bugNilled.xsd");
    loadDOMDocument(fileURL, schemaURL);
    // Get XSModel object for the Schema
    XSModel schema = getGrammar(schemaURL);
    setupDynamicContext(schema);
    String xpath = null;
    ResultSequence rs = null;
    String actual = null;
    // a
    xpath = "fn:count(( /root/element1, /root/element2, /root/element3 ))";
    compileXPath(xpath);
    rs = evaluate(domDoc);
    assertTrue(rs.size() > 0);
    actual = ((XSDecimal) rs.first()).getStringValue();
    assertEquals("3", actual);
    // b
    xpath = "fn:sum(( /root/element1, /root/element2, /root/element3 ))";
    compileXPath(xpath);
    rs = evaluate(domDoc);
    assertTrue(rs.size() > 0);
    actual = ((XSDecimal) rs.first()).getStringValue();
    assertEquals("43", actual);
    // b2
    xpath = "fn:sum(( /root/element1, /root/element2, /root/element3 ), 100)";
    compileXPath(xpath);
    rs = evaluate(domDoc);
    assertTrue(rs.size() > 0);
    actual = ((XSDecimal) rs.first()).getStringValue();
    assertEquals("143", actual);
    // c
    xpath = "fn:avg(( /root/element1, /root/element2, /root/element3, 1 ))";
    compileXPath(xpath);
    rs = evaluate(domDoc);
    assertTrue(rs.size() > 0);
    actual = ((XSDecimal) rs.first()).getStringValue();
    assertEquals("11", actual);
    // d
    xpath = "fn:max(( /root/element1, /root/element2, /root/element3 ))";
    compileXPath(xpath);
    rs = evaluate(domDoc);
    assertTrue(rs.size() > 0);
    actual = ((XSDecimal) rs.first()).getStringValue();
    assertEquals("42", actual);
    // e
    xpath = "fn:min(( /root/element1, /root/element2, /root/element3 ))";
    compileXPath(xpath);
    rs = evaluate(domDoc);
    assertTrue(rs.size() > 0);
    actual = ((XSDecimal) rs.first()).getStringValue();
    assertEquals("1", actual);
}
Also used : ResultSequence(org.eclipse.wst.xml.xpath2.processor.ResultSequence) XSModel(org.apache.xerces.xs.XSModel) XSString(org.eclipse.wst.xml.xpath2.processor.internal.types.XSString) URL(java.net.URL)

Example 13 with XSDecimal

use of org.eclipse.wst.xml.xpath2.processor.internal.types.XSDecimal in project webtools.sourceediting by eclipse.

the class TestBugs method testSequenceAggregationOnEmpty.

public void testSequenceAggregationOnEmpty() throws Exception {
    bundle = Platform.getBundle("org.w3c.xqts.testsuite");
    URL fileURL = bundle.getEntry("/TestSources/emptydoc.xml");
    loadDOMDocument(fileURL);
    // Get XML Schema Information for the Document
    XSModel schema = getGrammar();
    setupDynamicContext(schema);
    String xpath = null;
    ResultSequence rs = null;
    // a)
    xpath = "fn:sum((1,2,3,() ))";
    compileXPath(xpath);
    rs = evaluate(domDoc);
    XSDecimal val = (XSDecimal) rs.first();
    assertEquals("6", val.string_value());
}
Also used : ResultSequence(org.eclipse.wst.xml.xpath2.processor.ResultSequence) XSModel(org.apache.xerces.xs.XSModel) XSString(org.eclipse.wst.xml.xpath2.processor.internal.types.XSString) XSDecimal(org.eclipse.wst.xml.xpath2.processor.internal.types.XSDecimal) URL(java.net.URL)

Example 14 with XSDecimal

use of org.eclipse.wst.xml.xpath2.processor.internal.types.XSDecimal in project webtools.sourceediting by eclipse.

the class StaticContextAdapterTest method testFunctionCall.

public void testFunctionCall() {
    XPathParser xpp = new JFlexCupParser();
    XPath xpath = xpp.parse("fn:sum((1,2,3))");
    DefaultStaticContext sc = new DefaultStaticContext(null);
    sc.add_namespace("fn", "http://www.w3.org/2005/xpath-functions");
    sc.add_function_library(new FnFunctionLibrary());
    StaticChecker namecheck = new StaticNameResolver(sc);
    namecheck.check(xpath);
    DefaultDynamicContext dc = new DefaultDynamicContext(null, null);
    dc.add_namespace("fn", "http://www.w3.org/2005/xpath-functions");
    dc.add_function_library(new FnFunctionLibrary());
    Evaluator eval = new DefaultEvaluator(dc, null);
    ResultSequence rs = eval.evaluate(xpath);
    assertEquals(1, rs.size());
    XSDecimal result = (XSDecimal) rs.first();
    String actual = result.getStringValue();
    assertEquals("6", actual);
}
Also used : XPath(org.eclipse.wst.xml.xpath2.processor.ast.XPath) StaticChecker(org.eclipse.wst.xml.xpath2.processor.StaticChecker) XPathParser(org.eclipse.wst.xml.xpath2.processor.XPathParser) ResultSequence(org.eclipse.wst.xml.xpath2.processor.ResultSequence) DefaultEvaluator(org.eclipse.wst.xml.xpath2.processor.DefaultEvaluator) DefaultStaticContext(org.eclipse.wst.xml.xpath2.processor.internal.DefaultStaticContext) Evaluator(org.eclipse.wst.xml.xpath2.processor.Evaluator) DefaultEvaluator(org.eclipse.wst.xml.xpath2.processor.DefaultEvaluator) FnFunctionLibrary(org.eclipse.wst.xml.xpath2.processor.function.FnFunctionLibrary) StaticNameResolver(org.eclipse.wst.xml.xpath2.processor.StaticNameResolver) DefaultDynamicContext(org.eclipse.wst.xml.xpath2.processor.DefaultDynamicContext) XSDecimal(org.eclipse.wst.xml.xpath2.processor.internal.types.XSDecimal) JFlexCupParser(org.eclipse.wst.xml.xpath2.processor.JFlexCupParser)

Example 15 with XSDecimal

use of org.eclipse.wst.xml.xpath2.processor.internal.types.XSDecimal in project webtools.sourceediting by eclipse.

the class FnSecondsFromDuration method seconds_from_duration.

/**
 * Seconds-from-Duration operation.
 *
 * @param args
 *            Result from the expressions evaluation.
 * @throws DynamicError
 *             Dynamic error.
 * @return Result of fn:seconds-from-duration operation.
 */
public static ResultSequence seconds_from_duration(Collection args) throws DynamicError {
    Collection cargs = Function.convert_arguments(args, expected_args());
    ResultSequence arg1 = (ResultSequence) cargs.iterator().next();
    if (arg1.empty()) {
        return ResultBuffer.EMPTY;
    }
    XSDuration dtd = (XSDuration) arg1.first();
    double res = dtd.seconds();
    if (dtd.negative())
        res *= -1;
    return new XSDecimal(new BigDecimal(res));
}
Also used : XSDuration(org.eclipse.wst.xml.xpath2.processor.internal.types.XSDuration) ResultSequence(org.eclipse.wst.xml.xpath2.api.ResultSequence) Collection(java.util.Collection) XSDecimal(org.eclipse.wst.xml.xpath2.processor.internal.types.XSDecimal) BigDecimal(java.math.BigDecimal)

Aggregations

ResultSequence (org.eclipse.wst.xml.xpath2.api.ResultSequence)14 Item (org.eclipse.wst.xml.xpath2.api.Item)10 BigDecimal (java.math.BigDecimal)9 XSDecimal (org.eclipse.wst.xml.xpath2.processor.internal.types.XSDecimal)8 ResultSequence (org.eclipse.wst.xml.xpath2.processor.ResultSequence)7 URL (java.net.URL)6 XSModel (org.apache.xerces.xs.XSModel)6 XSString (org.eclipse.wst.xml.xpath2.processor.internal.types.XSString)5 Collection (java.util.Collection)4 DynamicError (org.eclipse.wst.xml.xpath2.processor.DynamicError)2 BigInteger (java.math.BigInteger)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 DefaultDynamicContext (org.eclipse.wst.xml.xpath2.processor.DefaultDynamicContext)1 DefaultEvaluator (org.eclipse.wst.xml.xpath2.processor.DefaultEvaluator)1 Evaluator (org.eclipse.wst.xml.xpath2.processor.Evaluator)1 JFlexCupParser (org.eclipse.wst.xml.xpath2.processor.JFlexCupParser)1 StaticChecker (org.eclipse.wst.xml.xpath2.processor.StaticChecker)1 StaticError (org.eclipse.wst.xml.xpath2.processor.StaticError)1 StaticNameResolver (org.eclipse.wst.xml.xpath2.processor.StaticNameResolver)1