Search in sources :

Example 1 with NumericType

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

the class FsPlus method fs_plus_unary.

/**
 * Unary operation on the arguments.
 *
 * @param args
 *            input arguments.
 * @throws DynamicError
 *             Dynamic error.
 * @return Result of the operation.
 */
public static ResultSequence fs_plus_unary(Collection args) throws DynamicError {
    // make sure we got only one arg
    if (args.size() != 1)
        DynamicError.throw_type_error();
    ResultSequence arg = (ResultSequence) args.iterator().next();
    // make sure we got only one numeric atom
    if (arg.size() != 1)
        DynamicError.throw_type_error();
    Item at = arg.first();
    if (!(at instanceof NumericType))
        DynamicError.throw_type_error();
    // no-op
    return arg;
}
Also used : NumericType(org.eclipse.wst.xml.xpath2.processor.internal.types.NumericType) Item(org.eclipse.wst.xml.xpath2.api.Item) ResultSequence(org.eclipse.wst.xml.xpath2.api.ResultSequence)

Example 2 with NumericType

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

the class Function method convert_argument.

// convert argument according to section 3.1.5 of xpath 2.0 spec
/**
 * Convert the input argument according to section 3.1.5 of specification.
 *
 * @param arg
 *            input argument.
 * @param expected
 *            Expected Sequence type.
 * @throws DynamicError
 *             Dynamic error.
 * @return Converted argument.
 */
public static org.eclipse.wst.xml.xpath2.api.ResultSequence convert_argument(org.eclipse.wst.xml.xpath2.api.ResultSequence arg, SeqType expected) throws DynamicError {
    ResultBuffer result = new ResultBuffer();
    // XXX: Should use type_class instead and use item.getClass().isAssignableTo(expected.type_class())
    AnyType expected_type = expected.type();
    // expected is atomic
    if (expected_type instanceof AnyAtomicType) {
        AnyAtomicType expected_aat = (AnyAtomicType) expected_type;
        // atomize
        org.eclipse.wst.xml.xpath2.api.ResultSequence rs = FnData.atomize(arg);
        // cast untyped to expected type
        for (Iterator i = rs.iterator(); i.hasNext(); ) {
            AnyType item = (AnyType) i.next();
            if (item instanceof XSUntypedAtomic) {
                // create a new item of the expected
                // type initialized with from the string
                // value of the item
                ResultSequence converted = null;
                if (expected_aat instanceof XSString) {
                    XSString strType = new XSString(item.getStringValue());
                    converted = ResultSequenceFactory.create_new(strType);
                } else {
                    converted = ResultSequenceFactory.create_new(item);
                }
                result.concat(converted);
            } else // xs:anyURI promotion to xs:string
            if (item instanceof XSAnyURI && expected_aat instanceof XSString) {
                result.add(new XSString(item.getStringValue()));
            } else // numeric type promotion
            if (item instanceof NumericType) {
                if (expected_aat instanceof XSDouble) {
                    XSDouble doubleType = new XSDouble(item.getStringValue());
                    result.add(doubleType);
                } else {
                    result.add(item);
                }
            } else {
                result.add(item);
            }
        }
        // do sequence type matching on converted arguments
        return expected.match(result.getSequence());
    } else {
        // do sequence type matching on converted arguments
        return expected.match(arg);
    }
}
Also used : NumericType(org.eclipse.wst.xml.xpath2.processor.internal.types.NumericType) ResultBuffer(org.eclipse.wst.xml.xpath2.api.ResultBuffer) XSAnyURI(org.eclipse.wst.xml.xpath2.processor.internal.types.XSAnyURI) XSDouble(org.eclipse.wst.xml.xpath2.processor.internal.types.XSDouble) ResultSequence(org.eclipse.wst.xml.xpath2.processor.ResultSequence) XSString(org.eclipse.wst.xml.xpath2.processor.internal.types.XSString) Iterator(java.util.Iterator) AnyAtomicType(org.eclipse.wst.xml.xpath2.processor.internal.types.AnyAtomicType) XSUntypedAtomic(org.eclipse.wst.xml.xpath2.processor.internal.types.XSUntypedAtomic) AnyType(org.eclipse.wst.xml.xpath2.processor.internal.types.AnyType)

Example 3 with NumericType

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

the class FnDeepEqual method deep_equal_atomic.

/**
 * Deep-Equal boolean operation for inputs of any atomic type.
 *
 * @param one
 *            input1 xpath expression/variable.
 * @param two
 *            input2 xpath expression/variable.
 * @return Result of fn:deep-equal operation.
 */
public static boolean deep_equal_atomic(AnyAtomicType one, AnyAtomicType two, DynamicContext context, String collationURI) {
    if (!(one instanceof CmpEq))
        return false;
    if (!(two instanceof CmpEq))
        return false;
    CmpEq a = (CmpEq) one;
    try {
        if (isNumeric(one, two)) {
            NumericType numeric = (NumericType) one;
            if (numeric.eq(two, context)) {
                return true;
            } else {
                XSString value1 = new XSString(one.getStringValue());
                if (value1.eq(two, context)) {
                    return true;
                }
            }
        }
        if (a.eq(two, context))
            return true;
        if (needsStringComparison(one, two)) {
            XSString xstr1 = new XSString(one.getStringValue());
            XSString xstr2 = new XSString(two.getStringValue());
            if (FnCompare.compare_string(collationURI, xstr1, xstr2, context).equals(BigInteger.ZERO)) {
                return true;
            }
        }
        return false;
    } catch (DynamicError err) {
        // XXX ???
        return false;
    }
}
Also used : NumericType(org.eclipse.wst.xml.xpath2.processor.internal.types.NumericType) XSString(org.eclipse.wst.xml.xpath2.processor.internal.types.XSString) DynamicError(org.eclipse.wst.xml.xpath2.processor.DynamicError)

Example 4 with NumericType

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

the class FnIndexOf method index_of.

/**
 * Index-Of operation.
 *
 * @param args
 *            Result from the expressions evaluation.
 * @param dynamicContext
 * @throws DynamicError
 *             Dynamic error.
 * @return Result of fn:index-of operation.
 */
public static ResultSequence index_of(Collection args, DynamicContext dc) {
    Function.convert_arguments(args, expected_args());
    // get args
    Iterator citer = args.iterator();
    ResultSequence arg1 = (ResultSequence) citer.next();
    ResultSequence arg2 = (ResultSequence) citer.next();
    if (arg1.empty()) {
        return ResultBuffer.EMPTY;
    }
    // sanity chex
    if (arg2.size() != 1)
        DynamicError.throw_type_error();
    String collationUri = dc.getCollationProvider().getDefaultCollation();
    if (citer.hasNext()) {
        ResultSequence arg3 = (ResultSequence) citer.next();
        if (!arg3.empty()) {
            XSString collation = (XSString) arg3.first();
            collationUri = collation.getStringValue();
        }
    }
    ResultBuffer rb = new ResultBuffer();
    AnyAtomicType at = (AnyAtomicType) arg2.first();
    get_comparable(at);
    int index = 1;
    for (Iterator i = arg1.iterator(); i.hasNext(); ) {
        AnyType cmptype = (AnyType) i.next();
        get_comparable(cmptype);
        if (!(at instanceof CmpEq))
            continue;
        if (isBoolean(cmptype, at)) {
            XSBoolean boolat = (XSBoolean) cmptype;
            if (boolat.eq(at, dc)) {
                rb.add(new XSInteger(BigInteger.valueOf(index)));
            }
        } else if (isNumeric(cmptype, at)) {
            NumericType numericat = (NumericType) at;
            if (numericat.eq(cmptype, dc)) {
                rb.add(new XSInteger(BigInteger.valueOf(index)));
            }
        } else if (isDuration(cmptype, at)) {
            XSDuration durat = (XSDuration) at;
            if (durat.eq(cmptype, dc)) {
                rb.add(new XSInteger(BigInteger.valueOf(index)));
            }
        } else if (at instanceof QName && cmptype instanceof QName) {
            QName qname = (QName) at;
            if (qname.eq(cmptype, dc)) {
                rb.add(new XSInteger(BigInteger.valueOf(index)));
            }
        } else if (needsStringComparison(cmptype, at)) {
            XSString xstr1 = new XSString(cmptype.getStringValue());
            XSString itemStr = new XSString(at.getStringValue());
            if (FnCompare.compare_string(collationUri, xstr1, itemStr, dc).equals(BigInteger.ZERO)) {
                rb.add(new XSInteger(BigInteger.valueOf(index)));
            }
        }
        index++;
    }
    return rb.getSequence();
}
Also used : NumericType(org.eclipse.wst.xml.xpath2.processor.internal.types.NumericType) XSDuration(org.eclipse.wst.xml.xpath2.processor.internal.types.XSDuration) ResultBuffer(org.eclipse.wst.xml.xpath2.api.ResultBuffer) ResultSequence(org.eclipse.wst.xml.xpath2.api.ResultSequence) QName(org.eclipse.wst.xml.xpath2.processor.internal.types.QName) XSString(org.eclipse.wst.xml.xpath2.processor.internal.types.XSString) XSString(org.eclipse.wst.xml.xpath2.processor.internal.types.XSString) XSBoolean(org.eclipse.wst.xml.xpath2.processor.internal.types.XSBoolean) XSInteger(org.eclipse.wst.xml.xpath2.processor.internal.types.XSInteger) Iterator(java.util.Iterator) AnyAtomicType(org.eclipse.wst.xml.xpath2.processor.internal.types.AnyAtomicType) AnyType(org.eclipse.wst.xml.xpath2.processor.internal.types.AnyType)

Example 5 with NumericType

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

the class FnAbs method get_single_numeric_arg.

/**
 * Obtain numeric value from expression.
 *
 * @param arg
 *            input expression.
 * @throws DynamicError
 *             Dynamic error.
 * @return Resulting numeric type from the operation.
 */
public static NumericType get_single_numeric_arg(ResultSequence arg) throws DynamicError {
    int size = arg.size();
    if (size > 1)
        DynamicError.throw_type_error();
    if (size == 0)
        return null;
    arg = FnData.atomize(arg);
    AnyType at = (AnyType) arg.item(0);
    if (!(at instanceof NumericType))
        throw DynamicError.invalidType();
    return (NumericType) at;
}
Also used : NumericType(org.eclipse.wst.xml.xpath2.processor.internal.types.NumericType) AnyType(org.eclipse.wst.xml.xpath2.processor.internal.types.AnyType)

Aggregations

NumericType (org.eclipse.wst.xml.xpath2.processor.internal.types.NumericType)10 ResultSequence (org.eclipse.wst.xml.xpath2.api.ResultSequence)7 XSString (org.eclipse.wst.xml.xpath2.processor.internal.types.XSString)6 Iterator (java.util.Iterator)5 Item (org.eclipse.wst.xml.xpath2.api.Item)4 ResultBuffer (org.eclipse.wst.xml.xpath2.api.ResultBuffer)4 AnyType (org.eclipse.wst.xml.xpath2.processor.internal.types.AnyType)4 XSDouble (org.eclipse.wst.xml.xpath2.processor.internal.types.XSDouble)4 AnyAtomicType (org.eclipse.wst.xml.xpath2.processor.internal.types.AnyAtomicType)3 XSUntypedAtomic (org.eclipse.wst.xml.xpath2.processor.internal.types.XSUntypedAtomic)3 DynamicError (org.eclipse.wst.xml.xpath2.processor.DynamicError)2 ResultSequence (org.eclipse.wst.xml.xpath2.processor.ResultSequence)2 XSBoolean (org.eclipse.wst.xml.xpath2.processor.internal.types.XSBoolean)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 XSModel (org.apache.xerces.xs.XSModel)1 CtrType (org.eclipse.wst.xml.xpath2.processor.internal.types.CtrType)1 QName (org.eclipse.wst.xml.xpath2.processor.internal.types.QName)1