use of org.eclipse.wst.xml.xpath2.api.ResultSequence in project webtools.sourceediting by eclipse.
the class FnStringToCodepoints method string_to_codepoints.
/**
* Base-Uri operation.
*
* @param args
* Result from the expressions evaluation.
* @throws DynamicError
* Dynamic error.
* @return Result of fn:base-uri operation.
*/
public static ResultSequence string_to_codepoints(Collection args) throws DynamicError {
Collection cargs = Function.convert_arguments(args, expected_args());
ResultSequence arg1 = (ResultSequence) cargs.iterator().next();
if (arg1.empty())
return ResultBuffer.EMPTY;
XSString xstr = (XSString) arg1.first();
CodePointIterator cpi = new StringCodePointIterator(xstr.value());
ResultBuffer rs = new ResultBuffer();
for (int codePoint = cpi.current(); codePoint != CodePointIterator.DONE; codePoint = cpi.next()) {
rs.add(new XSInteger(BigInteger.valueOf(codePoint)));
}
return rs.getSequence();
}
use of org.eclipse.wst.xml.xpath2.api.ResultSequence 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.api.ResultSequence in project webtools.sourceediting by eclipse.
the class FnTrace method trace.
/**
* Trace operation.
*
* @param args
* Result from the expressions evaluation.
* @throws DynamicError
* Dynamic error.
* @return Result of fn:trace operation.
*/
public static ResultSequence trace(Collection args) throws DynamicError {
// sanity check args
if (args.size() != 2)
DynamicError.throw_type_error();
Iterator argsi = args.iterator();
ResultSequence arg1 = (ResultSequence) argsi.next();
ResultSequence arg2 = (ResultSequence) argsi.next();
if (arg2.size() != 1)
DynamicError.throw_type_error();
Item at = arg2.first();
if (!(at instanceof XSString))
DynamicError.throw_type_error();
XSString label = (XSString) at;
int index = 1;
for (Iterator i = arg1.iterator(); i.hasNext(); index++) {
at = (AnyType) i.next();
System.out.println(label.value() + " [" + index + "] " + ((AnyType) at).string_type() + ":" + at.getStringValue());
}
return arg1;
}
use of org.eclipse.wst.xml.xpath2.api.ResultSequence in project webtools.sourceediting by eclipse.
the class FnPrefixFromQName method prefix.
/**
* Prefix-from-QName operation.
*
* @param args
* Result from the expressions evaluation.
* @throws DynamicError
* Dynamic error.
* @return Result of fn:prefix-from-QName operation.
*/
public static ResultSequence prefix(Collection args, StaticContext sc) throws DynamicError {
Collection cargs = Function.convert_arguments(args, expected_args());
// get arg
ResultSequence arg1 = (ResultSequence) cargs.iterator().next();
if (arg1.empty())
return ResultBuffer.EMPTY;
QName qname = (QName) arg1.first();
String prefix = qname.prefix();
if (prefix != null) {
if (!XMLConstants.NULL_NS_URI.equals(sc.getNamespaceContext().getNamespaceURI(prefix))) {
return new XSNCName(prefix);
} else {
throw DynamicError.invalidPrefix();
}
}
return ResultBuffer.EMPTY;
}
use of org.eclipse.wst.xml.xpath2.api.ResultSequence in project webtools.sourceediting by eclipse.
the class FnRemove method remove.
/**
* Remove operation.
*
* @param args
* Result from the expressions evaluation.
* @throws DynamicError
* Dynamic error.
* @return Result of fn:remove operation.
*/
public static ResultSequence remove(Collection args) throws DynamicError {
assert args.size() == 2;
ResultBuffer rs = new ResultBuffer();
// get args
Iterator citer = args.iterator();
ResultSequence target = (ResultSequence) citer.next();
ResultSequence arg2 = (ResultSequence) citer.next();
// sanity chex
if (arg2.size() != 1)
DynamicError.throw_type_error();
Item at = arg2.first();
if (!(at instanceof XSInteger))
DynamicError.throw_type_error();
int position = ((XSInteger) at).int_value().intValue();
if (position < 1)
return target;
if (position > target.size())
return target;
if (target.empty())
return rs.getSequence();
int curpos = 1;
for (Iterator i = target.iterator(); i.hasNext(); ) {
at = (AnyType) i.next();
if (curpos != position)
rs.add(at);
curpos++;
}
return rs.getSequence();
}
Aggregations