use of org.eclipse.wst.xml.xpath2.processor.internal.types.XSDouble in project webtools.sourceediting by eclipse.
the class FnMax method max.
/**
* Max operation.
*
* @param args
* Result from the expressions evaluation.
* @param context
* Relevant dynamic context
* @throws DynamicError
* Dynamic error.
* @return Result of fn:max operation.
*/
public static ResultSequence max(Collection args, DynamicContext dynamicContext) throws DynamicError {
ResultSequence arg = get_arg(args, CmpGt.class);
if (arg.empty())
return ResultSequenceFactory.create_new();
CmpGt max = null;
TypePromoter tp = new ComparableTypePromoter();
tp.considerSequence(arg);
for (Iterator i = arg.iterator(); i.hasNext(); ) {
AnyAtomicType conv = tp.promote((AnyType) i.next());
if (conv != null) {
if (conv instanceof XSDouble && ((XSDouble) conv).nan() || conv instanceof XSFloat && ((XSFloat) conv).nan()) {
return ResultSequenceFactory.create_new(tp.promote(new XSFloat(Float.NaN)));
}
if (max == null || ((CmpGt) conv).gt((AnyType) max, dynamicContext)) {
max = (CmpGt) conv;
}
}
}
return ResultSequenceFactory.create_new((AnyType) max);
}
use of org.eclipse.wst.xml.xpath2.processor.internal.types.XSDouble 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);
}
}
use of org.eclipse.wst.xml.xpath2.processor.internal.types.XSDouble in project webtools.sourceediting by eclipse.
the class FnMin method min.
/**
* Min operation.
*
* @param args
* Result from the expressions evaluation.
* @param dynamic
* Dynamic context
* @throws DynamicError
* Dynamic error.
* @return Result of fn:min operation.
*/
public static ResultSequence min(Collection args, DynamicContext context) throws DynamicError {
ResultSequence arg = FnMax.get_arg(args, CmpLt.class);
if (arg.empty())
return ResultSequenceFactory.create_new();
CmpLt max = null;
TypePromoter tp = new ComparableTypePromoter();
tp.considerSequence(arg);
for (Iterator i = arg.iterator(); i.hasNext(); ) {
AnyAtomicType conv = tp.promote((AnyType) i.next());
if (conv != null) {
if (conv instanceof XSDouble && ((XSDouble) conv).nan() || conv instanceof XSFloat && ((XSFloat) conv).nan()) {
return ResultSequenceFactory.create_new(tp.promote(new XSFloat(Float.NaN)));
}
if (max == null || ((CmpLt) conv).lt((AnyType) max, context)) {
max = (CmpLt) conv;
}
}
}
return ResultSequenceFactory.create_new((AnyType) max);
}
use of org.eclipse.wst.xml.xpath2.processor.internal.types.XSDouble in project webtools.sourceediting by eclipse.
the class FnSubstring method substring.
/**
* Obtain a substring from the arguments.
*
* @param args
* are used to obtain a substring.
* @throws DynamicError
* Dynamic error.
* @return The result of obtaining a substring from the arguments.
*/
public static ResultSequence substring(Collection args) throws DynamicError {
Collection cargs = Function.convert_arguments(args, expected_args(args));
Iterator argi = cargs.iterator();
ResultSequence stringArg = (ResultSequence) argi.next();
ResultSequence startPosArg = (ResultSequence) argi.next();
ResultSequence lengthArg = null;
if (argi.hasNext()) {
lengthArg = (ResultSequence) argi.next();
}
if (stringArg.empty()) {
return emptyString();
}
String str = ((XSString) stringArg.first()).value();
double dstart = ((XSDouble) startPosArg.first()).double_value();
// is start is NaN, no chars are returned
if (Double.isNaN(dstart) || Double.NEGATIVE_INFINITY == dstart) {
return emptyString();
}
long istart = Math.round(dstart);
long ilength = Long.MAX_VALUE;
if (lengthArg != null) {
double dlength = ((XSDouble) lengthArg.first()).double_value();
if (Double.isNaN(dlength))
return emptyString();
// Switch to the rounded kind
ilength = Math.round(dlength);
if (ilength <= 0)
return emptyString();
}
// could guess too short in cases supplementary chars
StringBuffer sb = new StringBuffer((int) Math.min(str.length(), ilength));
// This looks like an inefficient way to iterate, but due to surrogate handling,
// string indexes are no good here. Welcome to UTF-16!
CodePointIterator strIter = new StringCodePointIterator(str);
for (long p = 1; strIter.current() != CodePointIterator.DONE; ++p, strIter.next()) {
if (istart <= p && p - istart < ilength)
sb.append(UCharacter.toChars(strIter.current()));
}
return new XSString(sb.toString());
}
use of org.eclipse.wst.xml.xpath2.processor.internal.types.XSDouble in project webtools.sourceediting by eclipse.
the class FnAvg method avg.
/**
* Average value operation.
*
* @param args
* Result from the expressions evaluation.
* @throws DynamicError
* Dynamic error.
* @return Result of fn:avg operation.
*/
public static ResultSequence avg(Collection args) throws DynamicError {
ResultSequence arg = (ResultSequence) args.iterator().next();
if (arg.empty())
return ResultSequenceFactory.create_new();
int elems = 0;
MathPlus total = null;
TypePromoter tp = new ScalarTypePromoter();
tp.considerSequence(arg);
for (Iterator i = arg.iterator(); i.hasNext(); ) {
++elems;
AnyAtomicType conv = tp.promote((AnyType) i.next());
if (conv != null) {
if (conv instanceof XSDouble && ((XSDouble) conv).nan() || conv instanceof XSFloat && ((XSFloat) conv).nan()) {
return ResultSequenceFactory.create_new(tp.promote(new XSFloat(Float.NaN)));
}
if (total == null) {
total = (MathPlus) conv;
} else {
total = (MathPlus) total.plus(ResultSequenceFactory.create_new(conv)).first();
}
}
}
if (!(total instanceof MathDiv))
DynamicError.throw_type_error();
return ((MathDiv) total).div(ResultSequenceFactory.create_new(new XSInteger(BigInteger.valueOf(elems))));
}
Aggregations