/// <summary>
        /// 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.
        /// </summary>
        /// <param name="mapstr"> The "mapping from" string </param>
        /// <param name="transstr"> The "mapping into" string </param>
        /// <returns> A map which maps input codepoint to output codepoint (or null) </returns>
        private static IDictionary buildReplacementMap(string mapstr, string transstr)
        {
            // Build mapping (map from codepoint -> codepoint)
            IDictionary replacements = new Hashtable(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 != org.eclipse.wst.xml.xpath2.processor.@internal.utils.CodePointIterator_Fields.DONE)
            {
                int?codepointFrom = new int?(mapFrom);
                if (!replacements.Contains(codepointFrom))
                {
                    // only overwrite if it doesn't exist already
                    int?replacement = mapTo != org.eclipse.wst.xml.xpath2.processor.@internal.utils.CodePointIterator_Fields.DONE ? new int?(mapTo) : null;
                    replacements[codepointFrom] = replacement;
                }
                mapFrom = mapIter.next();
                mapTo   = transIter.next();
            }
            return(replacements);
        }
        /// <summary>
        /// Base-Uri operation.
        /// </summary>
        /// <param name="args">
        ///            Result from the expressions evaluation. </param>
        /// <exception cref="DynamicError">
        ///             Dynamic error. </exception>
        /// <returns> Result of fn:base-uri operation. </returns>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public static org.eclipse.wst.xml.xpath2.api.ResultSequence string_to_codepoints(java.util.Collection args) throws org.eclipse.wst.xml.xpath2.processor.DynamicError
        public static ResultSequence string_to_codepoints(ICollection args)
        {
            ICollection cargs = Function.convert_arguments(args, expected_args());

            var i = cargs.GetEnumerator();

            i.MoveNext();
            ResultSequence arg1 = (ResultSequence)i.Current;

            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 != org.eclipse.wst.xml.xpath2.processor.@internal.utils.CodePointIterator_Fields.DONE; codePoint = cpi.next())
            {
                rs.add(new XSInteger(new System.Numerics.BigInteger(codePoint)));
            }
            return(rs.Sequence);
        }
        /// <summary>
        /// Translate arguments.
        /// </summary>
        /// <param name="args">
        ///            are translated. </param>
        /// <exception cref="DynamicError">
        ///             Dynamic error. </exception>
        /// <returns> The result of translating the arguments. </returns>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public static org.eclipse.wst.xml.xpath2.api.ResultSequence translate(java.util.Collection args) throws org.eclipse.wst.xml.xpath2.processor.DynamicError
        public static ResultSequence translate(ICollection args)
        {
            ICollection cargs = Function.convert_arguments(args, expected_args());

            IEnumerator argi = cargs.GetEnumerator();

            argi.MoveNext();
            ResultSequence arg1 = (ResultSequence)argi.Current;

            argi.MoveNext();
            ResultSequence arg2 = (ResultSequence)argi.Current;

            argi.MoveNext();
            ResultSequence arg3 = (ResultSequence)argi.Current;

            if (arg1.empty())
            {
                return(new XSString(""));
            }

            string str      = ((XSString)arg1.first()).value();
            string mapstr   = ((XSString)arg2.first()).value();
            string transstr = ((XSString)arg3.first()).value();

            IDictionary replacements = buildReplacementMap(mapstr, transstr);

            StringBuilder     sb      = new StringBuilder(str.Length);
            CodePointIterator strIter = new StringCodePointIterator(str);

            for (int input = strIter.current(); input != org.eclipse.wst.xml.xpath2.processor.@internal.utils.CodePointIterator_Fields.DONE; input = strIter.next())
            {
                int?inputCodepoint = new int?(input);
                if (replacements.Contains(inputCodepoint))
                {
                    int?replaceWith = (int?)replacements[inputCodepoint];
                    if (replaceWith != null)
                    {
                        var c = (char)replaceWith.Value;
                        sb.Append(c);
                    }
                }
                else
                {
                    var c = (char)input;
                    sb.Append(c);
                }
            }

            return(new XSString(sb.ToString()));
        }
Exemple #4
0
        /// <summary>
        /// Obtain a substring from the arguments.
        /// </summary>
        /// <param name="args">
        ///            are used to obtain a substring. </param>
        /// <exception cref="DynamicError">
        ///             Dynamic error. </exception>
        /// <returns> The result of obtaining a substring from the arguments. </returns>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public static org.eclipse.wst.xml.xpath2.api.ResultSequence substring(java.util.Collection args) throws org.eclipse.wst.xml.xpath2.processor.DynamicError
        public static ResultSequence substring(ICollection args)
        {
            ICollection cargs = Function.convert_arguments(args, expected_args(args));

            IEnumerator argi = cargs.GetEnumerator();

            argi.MoveNext();
            ResultSequence stringArg = (ResultSequence)argi.Current;

            argi.MoveNext();
            ResultSequence startPosArg = (ResultSequence)argi.Current;
            ResultSequence lengthArg   = null;

            if (argi.MoveNext())
            {
                lengthArg = (ResultSequence)argi.Current;
            }

            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.NegativeInfinity == dstart)
            {
                return(emptyString());
            }

            double x      = Math.Round(dstart);
            long   istart = (long)x;

            long ilength = long.MaxValue;

            if (lengthArg != null)
            {
                double dlength = ((XSDouble)lengthArg.first()).double_value();
                if (double.IsNaN(dlength))
                {
                    return(emptyString());
                }

                // Switch to the rounded kind
                double y = Math.Round(dlength);
                ilength = (long)y;
                if (ilength <= 0)
                {
                    return(emptyString());
                }
            }


            // could guess too short in cases supplementary chars
            StringBuilder sb = new StringBuilder((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() != org.eclipse.wst.xml.xpath2.processor.@internal.utils.CodePointIterator_Fields.DONE;
                 ++p, strIter.next())
            {
                if (istart <= p && p - istart < ilength)
                {
                    sb.Append((char)strIter.current());
                }
            }

            return(new XSString(sb.ToString()));
        }