Example #1
0
        /// <summary>
        /// Creates a new ResultSequence consisting of the hexBinary value
        /// </summary>
        /// <param name="arg">
        ///            The ResultSequence from which to construct hexBinary value </param>
        /// <returns> New ResultSequence representing hexBinary value </returns>
        /// <exception cref="DynamicError"> </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public org.eclipse.wst.xml.xpath2.api.ResultSequence constructor(org.eclipse.wst.xml.xpath2.api.ResultSequence arg) throws org.eclipse.wst.xml.xpath2.processor.DynamicError
        public override ResultSequence constructor(ResultSequence arg)
        {
            if (arg.empty())
            {
                return(ResultBuffer.EMPTY);
            }

            AnyAtomicType aat = (AnyAtomicType)arg.first();

            if (aat is NumericType || aat is XSDuration || aat is CalendarType || aat is XSBoolean || aat is XSAnyURI)
            {
                throw DynamicError.invalidType();
            }

            string str_value = aat.StringValue;

            if (!(aat is XSHexBinary || aat is XSString || aat is XSUntypedAtomic || aat is XSBase64Binary))
            {
                throw DynamicError.cant_cast(null);
            }

            if (aat is XSUntypedAtomic || aat is XSString)
            {
                string[] nonHexValues = null;
                try
                {
                    nonHexValues = str_value.Split("[0-9a-fA-F]", true);
                }
                catch (Exception)
                {
                    throw DynamicError.throw_type_error();
                }

                string[] binValues = null;
                try
                {
                    binValues = str_value.Split("[0-1]", true);
                }
                catch (Exception)
                {
                    throw DynamicError.throw_type_error();
                }

                if (nonHexValues.Length > 0 || binValues.Length == 0)
                {
                    throw DynamicError.invalidForCastConstructor();
                }
            }


            sbyte[] decodedValue = null;

            if (aat is XSBase64Binary)
            {
                throw new Exception();
                //decodedValue = Base64.decode(str_value);
                //decodedValue = HexBin.encode(decodedValue).Bytes;
            }
            else
            {
                decodedValue = str_value.GetBytes();
            }

            if (decodedValue != null)
            {
                return(new XSHexBinary(StringHelperClass.NewString(decodedValue)));
            }
            else
            {
                // invalid hexBinary string
                throw DynamicError.throw_type_error();
            }
        }