Search in sources :

Example 36 with ResultSequence

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();
}
Also used : ResultBuffer(org.eclipse.wst.xml.xpath2.api.ResultBuffer) ResultSequence(org.eclipse.wst.xml.xpath2.api.ResultSequence) CodePointIterator(org.eclipse.wst.xml.xpath2.processor.internal.utils.CodePointIterator) StringCodePointIterator(org.eclipse.wst.xml.xpath2.processor.internal.utils.StringCodePointIterator) XSInteger(org.eclipse.wst.xml.xpath2.processor.internal.types.XSInteger) Collection(java.util.Collection) XSString(org.eclipse.wst.xml.xpath2.processor.internal.types.XSString) StringCodePointIterator(org.eclipse.wst.xml.xpath2.processor.internal.utils.StringCodePointIterator)

Example 37 with ResultSequence

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());
}
Also used : XSDouble(org.eclipse.wst.xml.xpath2.processor.internal.types.XSDouble) ResultSequence(org.eclipse.wst.xml.xpath2.api.ResultSequence) CodePointIterator(org.eclipse.wst.xml.xpath2.processor.internal.utils.CodePointIterator) StringCodePointIterator(org.eclipse.wst.xml.xpath2.processor.internal.utils.StringCodePointIterator) CodePointIterator(org.eclipse.wst.xml.xpath2.processor.internal.utils.CodePointIterator) Iterator(java.util.Iterator) StringCodePointIterator(org.eclipse.wst.xml.xpath2.processor.internal.utils.StringCodePointIterator) Collection(java.util.Collection) XSString(org.eclipse.wst.xml.xpath2.processor.internal.types.XSString) XSString(org.eclipse.wst.xml.xpath2.processor.internal.types.XSString) StringCodePointIterator(org.eclipse.wst.xml.xpath2.processor.internal.utils.StringCodePointIterator)

Example 38 with ResultSequence

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;
}
Also used : Item(org.eclipse.wst.xml.xpath2.api.Item) ResultSequence(org.eclipse.wst.xml.xpath2.api.ResultSequence) Iterator(java.util.Iterator) XSString(org.eclipse.wst.xml.xpath2.processor.internal.types.XSString) AnyType(org.eclipse.wst.xml.xpath2.processor.internal.types.AnyType)

Example 39 with ResultSequence

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;
}
Also used : XSNCName(org.eclipse.wst.xml.xpath2.processor.internal.types.XSNCName) ResultSequence(org.eclipse.wst.xml.xpath2.api.ResultSequence) QName(org.eclipse.wst.xml.xpath2.processor.internal.types.QName) Collection(java.util.Collection)

Example 40 with ResultSequence

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();
}
Also used : Item(org.eclipse.wst.xml.xpath2.api.Item) ResultBuffer(org.eclipse.wst.xml.xpath2.api.ResultBuffer) ResultSequence(org.eclipse.wst.xml.xpath2.api.ResultSequence) XSInteger(org.eclipse.wst.xml.xpath2.processor.internal.types.XSInteger) Iterator(java.util.Iterator)

Aggregations

ResultSequence (org.eclipse.wst.xml.xpath2.processor.ResultSequence)8383 URL (java.net.URL)8366 XSModel (org.apache.xerces.xs.XSModel)8363 StaticError (org.eclipse.wst.xml.xpath2.processor.StaticError)8279 XPathParserException (org.eclipse.wst.xml.xpath2.processor.XPathParserException)8279 DynamicError (org.eclipse.wst.xml.xpath2.processor.DynamicError)8208 ResultSequence (org.eclipse.wst.xml.xpath2.api.ResultSequence)173 AnyType (org.eclipse.wst.xml.xpath2.processor.internal.types.AnyType)156 XSString (org.eclipse.wst.xml.xpath2.processor.internal.types.XSString)130 Schema (javax.xml.validation.Schema)102 Iterator (java.util.Iterator)92 Collection (java.util.Collection)83 XSBoolean (org.eclipse.wst.xml.xpath2.processor.internal.types.XSBoolean)83 DefaultEvaluator (org.eclipse.wst.xml.xpath2.processor.DefaultEvaluator)74 Evaluator (org.eclipse.wst.xml.xpath2.processor.Evaluator)74 XPath (org.eclipse.wst.xml.xpath2.processor.ast.XPath)74 DynamicContext (org.eclipse.wst.xml.xpath2.processor.DynamicContext)72 Item (org.eclipse.wst.xml.xpath2.api.Item)56 ResultBuffer (org.eclipse.wst.xml.xpath2.api.ResultBuffer)34 XSInteger (org.eclipse.wst.xml.xpath2.processor.internal.types.XSInteger)32