Search in sources :

Example 16 with DynamicContext

use of org.eclipse.wst.xml.xpath2.api.DynamicContext in project webtools.sourceediting by eclipse.

the class FsEq method do_cmp_value_op.

// voodoo 2
/**
 * Actual equality operation for fs_eq_value.
 *
 * @param args
 *            input arguments.
 * @param type
 *            type of the arguments.
 * @param mname
 *            Method name for template simulation.
 * @param dynamicContext
 *             Dynamic error.
 * @throws DynamicError
 *             Dynamic error.
 * @return Result of the operation.
 */
public static ResultSequence do_cmp_value_op(Collection args, Class type, String mname, DynamicContext context) throws DynamicError {
    // sanity check args + convert em
    if (args.size() != 2)
        DynamicError.throw_type_error();
    Collection cargs = value_convert_args(args);
    if (cargs.size() == 0)
        return ResultBuffer.EMPTY;
    // make sure arugments are comparable by equality
    Iterator argi = cargs.iterator();
    Item arg = ((ResultSequence) argi.next()).first();
    ResultSequence arg2 = (ResultSequence) argi.next();
    if (arg2.size() != 1)
        DynamicError.throw_type_error();
    if (!(type.isInstance(arg)))
        DynamicError.throw_type_error();
    try {
        Class[] margsdef = { AnyType.class, DynamicContext.class };
        Method method = null;
        method = type.getMethod(mname, margsdef);
        Object[] margs = { arg2.first(), context };
        Boolean cmpres = (Boolean) method.invoke(arg, margs);
        return ResultSequenceFactory.create_new(new XSBoolean(cmpres.booleanValue()));
    } catch (NoSuchMethodException err) {
        assert false;
        throw new RuntimeException("cannot compare using method " + mname, err);
    } catch (IllegalAccessException err) {
        assert false;
        throw new RuntimeException("cannot compare using method " + mname, err);
    } catch (InvocationTargetException err) {
        Throwable ex = err.getTargetException();
        if (ex instanceof DynamicError)
            throw (DynamicError) ex;
        throw new RuntimeException("cannot compare using method " + mname, ex);
    }
}
Also used : ResultSequence(org.eclipse.wst.xml.xpath2.api.ResultSequence) Method(java.lang.reflect.Method) DynamicError(org.eclipse.wst.xml.xpath2.processor.DynamicError) InvocationTargetException(java.lang.reflect.InvocationTargetException) Item(org.eclipse.wst.xml.xpath2.api.Item) XSBoolean(org.eclipse.wst.xml.xpath2.processor.internal.types.XSBoolean) Iterator(java.util.Iterator) Collection(java.util.Collection) XSBoolean(org.eclipse.wst.xml.xpath2.processor.internal.types.XSBoolean)

Example 17 with DynamicContext

use of org.eclipse.wst.xml.xpath2.api.DynamicContext in project webtools.sourceediting by eclipse.

the class FsLe method fs_le_value.

/**
 * Operation on the values of the arguments.
 *
 * @param args
 *            input arguments.
 * @param
 *         DynamicContext
 * @throws DynamicError
 *             Dynamic error.
 * @return Result of the operation.
 */
public static ResultSequence fs_le_value(Collection args, DynamicContext dc) throws DynamicError {
    ResultSequence less = FsLt.fs_lt_value(args, dc);
    if (((XSBoolean) less.first()).value())
        return less;
    ResultSequence equal = FsEq.fs_eq_value(args, dc);
    if (((XSBoolean) equal.first()).value())
        return equal;
    return ResultSequenceFactory.create_new(new XSBoolean(false));
}
Also used : ResultSequence(org.eclipse.wst.xml.xpath2.api.ResultSequence) XSBoolean(org.eclipse.wst.xml.xpath2.processor.internal.types.XSBoolean)

Example 18 with DynamicContext

use of org.eclipse.wst.xml.xpath2.api.DynamicContext in project webtools.sourceediting by eclipse.

the class XSDate method gt.

/**
 * Comparison on this and the supplied dates (taking timezones into account)
 *
 * @param arg
 *            XSDate representation of the date to compare to
 * @throws DynamicError
 * @return True if in time, this date lies after the date supplied. False
 *         otherwise.
 */
public boolean gt(AnyType arg, DynamicContext context) throws DynamicError {
    XSDate val = (XSDate) NumericType.get_single_type((Item) arg, XSDate.class);
    Calendar thiscal = normalizeCalendar(calendar(), tz());
    Calendar thatcal = normalizeCalendar(val.calendar(), val.tz());
    return thiscal.after(thatcal);
}
Also used : Item(org.eclipse.wst.xml.xpath2.api.Item) GregorianCalendar(java.util.GregorianCalendar) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) Calendar(java.util.Calendar)

Example 19 with DynamicContext

use of org.eclipse.wst.xml.xpath2.api.DynamicContext in project webtools.sourceediting by eclipse.

the class XSDate method eq.

// comparisons
/**
 * Equality comparison on this and the supplied dates (taking timezones into
 * account)
 *
 * @param arg
 *            XSDate representation of the date to compare to
 * @throws DynamicError
 * @return True if the two dates are represent the same exact point in time.
 *         False otherwise.
 */
public boolean eq(AnyType arg, DynamicContext dynamicContext) throws DynamicError {
    XSDate val = (XSDate) NumericType.get_single_type((Item) arg, XSDate.class);
    Calendar thiscal = normalizeCalendar(calendar(), tz());
    Calendar thatcal = normalizeCalendar(val.calendar(), val.tz());
    return thiscal.equals(thatcal);
}
Also used : Item(org.eclipse.wst.xml.xpath2.api.Item) GregorianCalendar(java.util.GregorianCalendar) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) Calendar(java.util.Calendar)

Example 20 with DynamicContext

use of org.eclipse.wst.xml.xpath2.api.DynamicContext in project webtools.sourceediting by eclipse.

the class FilteringPerformanceTest method evalXPath2.

protected Object evalXPath2(String xpath, Node doc, Class resultClass) {
    StaticContext sc = new StaticContextBuilder();
    XPath2Expression path = new Engine().parseExpression(xpath, sc);
    DynamicContext dynamicContext = new DynamicContextBuilder(sc);
    long before = System.nanoTime();
    // path.evaluate(dynamicContext, doc != null ? new Object[] { doc } : new Object[0]);
    // path.evaluate(dynamicContext, doc != null ? new Object[] { doc } : new Object[0]);
    org.eclipse.wst.xml.xpath2.api.ResultSequence rs = path.evaluate(dynamicContext, doc != null ? new Object[] { doc } : new Object[0]);
    assertEquals("Expected single result from \'" + xpath + "\'", 1, rs.size());
    Object result = rs.value(0);
    long after = System.nanoTime();
    System.out.println("XPath2 " + xpath + " evaluated to " + result + " in " + (after - before) / 1000 + " μs");
    assertTrue("Exected XPath result instanceof class " + resultClass.getSimpleName() + " from \'" + xpath + "\', got " + result.getClass(), resultClass.isInstance(result));
    return resultClass.cast(result);
}
Also used : StaticContextBuilder(org.eclipse.wst.xml.xpath2.processor.util.StaticContextBuilder) DynamicContextBuilder(org.eclipse.wst.xml.xpath2.processor.util.DynamicContextBuilder) XPath2Expression(org.eclipse.wst.xml.xpath2.api.XPath2Expression) Engine(org.eclipse.wst.xml.xpath2.processor.Engine) StaticContext(org.eclipse.wst.xml.xpath2.api.StaticContext) DynamicContext(org.eclipse.wst.xml.xpath2.api.DynamicContext)

Aggregations

XPath (org.eclipse.wst.xml.xpath2.processor.ast.XPath)77 DynamicContext (org.eclipse.wst.xml.xpath2.processor.DynamicContext)75 DynamicError (org.eclipse.wst.xml.xpath2.processor.DynamicError)75 URL (java.net.URL)73 DefaultEvaluator (org.eclipse.wst.xml.xpath2.processor.DefaultEvaluator)73 Evaluator (org.eclipse.wst.xml.xpath2.processor.Evaluator)73 ResultSequence (org.eclipse.wst.xml.xpath2.processor.ResultSequence)73 StaticError (org.eclipse.wst.xml.xpath2.processor.StaticError)72 XPathParserException (org.eclipse.wst.xml.xpath2.processor.XPathParserException)72 XSModel (org.apache.xerces.xs.XSModel)69 ResultSequence (org.eclipse.wst.xml.xpath2.api.ResultSequence)20 Item (org.eclipse.wst.xml.xpath2.api.Item)17 Iterator (java.util.Iterator)13 AnyType (org.eclipse.wst.xml.xpath2.processor.internal.types.AnyType)11 Collection (java.util.Collection)10 XSBoolean (org.eclipse.wst.xml.xpath2.processor.internal.types.XSBoolean)8 XSString (org.eclipse.wst.xml.xpath2.processor.internal.types.XSString)8 XMLGregorianCalendar (javax.xml.datatype.XMLGregorianCalendar)6 XSDayTimeDuration (org.eclipse.wst.xml.xpath2.processor.internal.types.XSDayTimeDuration)6 Duration (javax.xml.datatype.Duration)5