Search in sources :

Example 1 with StringCodePointIterator

use of org.eclipse.wst.xml.xpath2.processor.internal.utils.StringCodePointIterator 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 2 with StringCodePointIterator

use of org.eclipse.wst.xml.xpath2.processor.internal.utils.StringCodePointIterator 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 3 with StringCodePointIterator

use of org.eclipse.wst.xml.xpath2.processor.internal.utils.StringCodePointIterator in project webtools.sourceediting by eclipse.

the class FnTranslate method buildReplacementMap.

/**
 * Build a replacement map from the mapstr and the transstr for translation. The function returns a Map<Integer, Integer> mapping each codepoint
 * mentioned in the mapstr into the corresponding codepoint in transstr, or null if there is no matching mapping in transstr.
 *
 * @param mapstr The "mapping from" string
 * @param transstr The "mapping into" string
 * @return A map which maps input codepoint to output codepoint (or null)
 */
private static Map buildReplacementMap(String mapstr, String transstr) {
    // Build mapping (map from codepoint -> codepoint)
    Map replacements = new HashMap(mapstr.length() * 4);
    CodePointIterator mapIter = new StringCodePointIterator(mapstr);
    CodePointIterator transIter = new StringCodePointIterator(transstr);
    // Iterate through both mapIter and transIter and produce the mapping
    int mapFrom = mapIter.current();
    int mapTo = transIter.current();
    while (mapFrom != CodePointIterator.DONE) {
        Integer codepointFrom = new Integer(mapFrom);
        if (!replacements.containsKey(codepointFrom)) {
            // only overwrite if it doesn't exist already
            Integer replacement = mapTo != CodePointIterator.DONE ? new Integer(mapTo) : null;
            replacements.put(codepointFrom, replacement);
        }
        mapFrom = mapIter.next();
        mapTo = transIter.next();
    }
    return replacements;
}
Also used : HashMap(java.util.HashMap) CodePointIterator(org.eclipse.wst.xml.xpath2.processor.internal.utils.CodePointIterator) StringCodePointIterator(org.eclipse.wst.xml.xpath2.processor.internal.utils.StringCodePointIterator) StringCodePointIterator(org.eclipse.wst.xml.xpath2.processor.internal.utils.StringCodePointIterator) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with StringCodePointIterator

use of org.eclipse.wst.xml.xpath2.processor.internal.utils.StringCodePointIterator in project webtools.sourceediting by eclipse.

the class FnTranslate method translate.

/**
 * Translate arguments.
 *
 * @param args
 *            are translated.
 * @throws DynamicError
 *             Dynamic error.
 * @return The result of translating the arguments.
 */
public static ResultSequence translate(Collection args) throws DynamicError {
    Collection cargs = Function.convert_arguments(args, expected_args());
    Iterator argi = cargs.iterator();
    ResultSequence arg1 = (ResultSequence) argi.next();
    ResultSequence arg2 = (ResultSequence) argi.next();
    ResultSequence arg3 = (ResultSequence) argi.next();
    if (arg1.empty()) {
        return new XSString("");
    }
    String str = ((XSString) arg1.first()).value();
    String mapstr = ((XSString) arg2.first()).value();
    String transstr = ((XSString) arg3.first()).value();
    Map replacements = buildReplacementMap(mapstr, transstr);
    StringBuffer sb = new StringBuffer(str.length());
    CodePointIterator strIter = new StringCodePointIterator(str);
    for (int input = strIter.current(); input != CodePointIterator.DONE; input = strIter.next()) {
        Integer inputCodepoint = new Integer(input);
        if (replacements.containsKey(inputCodepoint)) {
            Integer replaceWith = (Integer) replacements.get(inputCodepoint);
            if (replaceWith != null) {
                sb.append(UCharacter.toChars(replaceWith.intValue()));
            }
        } else {
            sb.append(UCharacter.toChars(input));
        }
    }
    return new XSString(sb.toString());
}
Also used : 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) Iterator(java.util.Iterator) CodePointIterator(org.eclipse.wst.xml.xpath2.processor.internal.utils.CodePointIterator) 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) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

CodePointIterator (org.eclipse.wst.xml.xpath2.processor.internal.utils.CodePointIterator)4 StringCodePointIterator (org.eclipse.wst.xml.xpath2.processor.internal.utils.StringCodePointIterator)4 Collection (java.util.Collection)3 ResultSequence (org.eclipse.wst.xml.xpath2.api.ResultSequence)3 XSString (org.eclipse.wst.xml.xpath2.processor.internal.types.XSString)3 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 Map (java.util.Map)2 ResultBuffer (org.eclipse.wst.xml.xpath2.api.ResultBuffer)1 XSDouble (org.eclipse.wst.xml.xpath2.processor.internal.types.XSDouble)1 XSInteger (org.eclipse.wst.xml.xpath2.processor.internal.types.XSInteger)1