Пример #1
0
        /// <summary>
        ///   serialize an item (field/global param/...) to an XML format (applicable to be passed to the server).
        /// </summary>
        /// <param name = "itemVal">item's value</param>
        /// <param name = "itemAttr">item's attribute</param>
        /// <param name = "cellAttr">cell's attribute - relevant only if 'itemAttr' is vector</param>
        /// <param name = "toBase64">decide Base64 encoding is to be done</param>
        /// <returns>serialized buffer</returns>
        public static String serializeItemVal(String itemVal, StorageAttribute itemAttr, StorageAttribute cellAttr, bool toBase64)
        {
            Debug.Assert(itemVal != null);

            int significantNumSize = Manager.Environment.GetSignificantNumSize() * 2;

            String valueSize;
            int    j;
            var    tmpBuf = new StringBuilder();

            // for alpha type add the length of the value as hex number of 4 digits
            switch (itemAttr)
            {
            case StorageAttribute.NUMERIC:
            case StorageAttribute.DATE:
            case StorageAttribute.TIME:
                itemVal = !toBase64
                            ? itemVal.Substring(0, significantNumSize)
                            : Base64.encode(byteStreamToString(itemVal.Substring(0, significantNumSize)), Manager.Environment.GetEncoding());

                break;

            case StorageAttribute.ALPHA:
            case StorageAttribute.UNICODE:
                itemVal = StrUtil.rtrim(itemVal);
                int pos       = 0;
                int fldValLen = itemVal.Length;

                do
                {
                    int nullChrPos = itemVal.IndexOf((Char)0, pos);
                    if (nullChrPos == -1)
                    {
                        valueSize = (Convert.ToString(fldValLen - pos, 16)).ToUpper();
                        // add leading zeros (if needed)
                        for (j = 0; j < 4 - valueSize.Length; j++)
                        {
                            tmpBuf.Append('0');
                        }
                        tmpBuf.Append(valueSize);

                        if (pos > 0)
                        {
                            itemVal = itemVal.Substring(pos, (fldValLen) - (pos));
                        }

                        pos = fldValLen;
                    }
                    else
                    {
                        // If NULL chars exist in the middle of the value - create a spanned record
                        // Turn on the high most bit in the length (to indicate a segment)
                        valueSize = (Convert.ToString(nullChrPos - pos + 0x8000, 16)).ToUpper();
                        tmpBuf.Append(valueSize);
                        tmpBuf.Append(itemVal.Substring(pos, (nullChrPos) - (pos)));

                        // Count number of consecutive NULL chars, and add their count to XML
                        for (j = 1; j < fldValLen - nullChrPos && itemVal[nullChrPos + j] == 0; j++)
                        {
                        }
                        // add leading zeros (if needed)
                        valueSize = "0000" + (Convert.ToString(j, 16)).ToUpper();
                        tmpBuf.Append(valueSize.Substring(valueSize.Length - 4));

                        // Append a hex dump of special chars
                        for (pos = nullChrPos; j > 0; j--, pos++)
                        {
                            string tmpStr = "0" + (Convert.ToString(itemVal[nullChrPos], 16));
                            tmpBuf.Append(tmpStr.Substring(tmpStr.Length - 2));
                        }

                        // If special chars were last, add the length of the last segment (zero)
                        if (pos >= fldValLen)
                        {
                            tmpBuf.Append("0000");
                            itemVal = "";
                            break;
                        }
                    }
                } while (pos < fldValLen);
                break;


            case StorageAttribute.BLOB:
            case StorageAttribute.BLOB_VECTOR:
            case StorageAttribute.DOTNET:
                pos = 0;

                // convert dotnet object into magic equivalent and append as data into blob suffix.
                if (itemAttr == StorageAttribute.DOTNET)
                {
                    Object itmObj      = null;
                    int    key         = BlobType.getKey(itemVal);
                    String itmMagicVal = "";

                    if (key != 0)
                    {
                        itmObj = DNManager.getInstance().DNObjectsCollection.GetDNObj(key);
                    }

                    // convert dotnet object into magic type
                    if (itmObj != null)
                    {
                        StorageAttribute magicType = DNConvert.getDefaultMagicTypeForDotNetType(itmObj.GetType());
                        itmMagicVal = DNConvert.convertDotNetToMagic(itmObj, magicType);

                        // append to dotnet blob as data
                        if (itmMagicVal.Length > 0)
                        {
                            itemVal = BlobType.addDataToDotNetBlob(itemVal, itmMagicVal, magicType);
                        }
                    }
                }

                fldValLen = itemVal.Length;

                if (UtilStrByteMode.isLocaleDefLangDBCS() && itemAttr == StorageAttribute.BLOB_VECTOR)
                {
                    if (cellAttr == StorageAttribute.ALPHA || cellAttr == StorageAttribute.MEMO)
                    {
                        itemVal = VectorType.adjustAlphaStringsInFlatData(itemVal);

                        // The flat data will be divided by 0x3FFF characters.
                        // Each segment will be size in 0x3FFF ~ 0x7FFF bytes.
                        // The size depends on the number of DBCS characters, not fixed in 0x7FFF.
                        do
                        {
                            if (itemVal.Length < pos + 0x3FFF)
                            //(0x8000 - 1) / 2 = 0x3FFF
                            {
                                if (pos > 0)
                                {
                                    itemVal = itemVal.Substring(pos);
                                }

                                valueSize = (Convert.ToString(UtilStrByteMode.lenB(itemVal), 16)).ToUpper();
                                // add leading zeros (if needed)
                                for (j = 0; j < 4 - valueSize.Length; j++)
                                {
                                    tmpBuf.Append('0');
                                }
                                tmpBuf.Append(valueSize);

                                //hex encoding
                                itemVal = !toBase64
                                        ? StrUtil.stringToHexaDump(itemVal, 4)
                                        : Base64.encode(itemVal, true, Manager.Environment.GetEncoding());

                                pos = fldValLen;
                            }
                            else
                            {
                                String strSub = itemVal.Substring(pos, 0x3FFF);
                                // + 0x8000 ... to indicate not the last segment
                                valueSize = (Convert.ToString(UtilStrByteMode.lenB(strSub) + 0x8000, 16)).ToUpper();
                                tmpBuf.Append(valueSize);

                                //hex or base64 encoding
                                tmpBuf.Append(!toBase64
                                            ? StrUtil.stringToHexaDump(strSub, 4)
                                            : Base64.encode(strSub, true, Manager.Environment.GetEncoding()));

                                tmpBuf.Append("0000");
                                pos += 0x3FFF;
                            }
                        } while (pos < fldValLen);

                        break;
                    }
                }

                do
                {
                    if (fldValLen < pos + 0x7FFF)
                    //0x8000 -1 = 0x7FFF
                    {
                        valueSize = (Convert.ToString(fldValLen - pos, 16)).ToUpper();
                        // add leading zeros (if needed)
                        for (j = 0; j < 4 - valueSize.Length; j++)
                        {
                            tmpBuf.Append('0');
                        }
                        tmpBuf.Append(valueSize);

                        if (pos > 0)
                        {
                            itemVal = itemVal.Substring(pos, (fldValLen) - (pos));
                        }

                        //hex encoding
                        itemVal = !toBase64
                                  ? StrUtil.stringToHexaDump(itemVal, 4)
                                  : Base64.encode(itemVal, Manager.Environment.GetEncoding());

                        pos = fldValLen;
                    }
                    else
                    {
                        //to indicate the full segment
                        valueSize = "FFFF"; //(Integer.toHexString (0xFFFF)).toUpperCase()
                        tmpBuf.Append(valueSize);

                        //hex or base64 encoding
                        if (!toBase64)
                        {
                            tmpBuf.Append(StrUtil.stringToHexaDump(itemVal.Substring(pos, 0x7FFF), 4));
                        }
                        else
                        {
                            tmpBuf.Append(Base64.encode(itemVal.Substring(pos, 0x7FFF), Manager.Environment.GetEncoding()));
                        }

                        tmpBuf.Append("0000");
                        pos += 0x7FFF;
                    }
                } while (pos < fldValLen);

                break;
            } //end of the type case block

            tmpBuf.Append(itemVal);
            return(tmpBuf.ToString());
        }
Пример #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="itemVal"></param>
        /// <param name="itemAttr"></param>
        /// <returns></returns>
        public static byte[] serializeItemVal(String itemVal, StorageAttribute itemAttr)
        {
            Debug.Assert(itemVal != null);

            string valueSize;
            string tmpBufLen = string.Empty;

            Byte []     tmpBuf            = null;
            List <byte> contentWithLength = new List <byte>();
            int         pos         = 0;
            int         fldValLen   = 0;
            String      tempItemVal = string.Empty;
            int         noOfPackets = 0;

            Byte [] tmpNoOfPackets    = null;
            String  tmpStrNoOfPackets = string.Empty;

            switch (itemAttr)
            {
            case StorageAttribute.NUMERIC:
            case StorageAttribute.DATE:
            case StorageAttribute.TIME:
                NUM_TYPE numType = new NUM_TYPE(itemVal);
                tmpBuf = Misc.ToByteArray(numType.Data);
                break;

            case StorageAttribute.BOOLEAN:
                tmpBuf = Manager.Environment.GetEncoding().GetBytes(itemVal);
                break;

            case StorageAttribute.ALPHA:
                itemVal   = StrUtil.rtrim(itemVal);
                valueSize = (Convert.ToString(UtilStrByteMode.lenB(itemVal), 16)).ToUpper();

                // add leading zeros (if needed)
                for (int j = 0; j < 4 - valueSize.Length; j++)
                {
                    tmpBufLen += "0";
                }
                tmpBufLen += valueSize;

                contentWithLength.AddRange(Manager.Environment.GetEncoding().GetBytes(tmpBufLen));
                contentWithLength.AddRange(Manager.Environment.GetEncoding().GetBytes(itemVal));
                tmpBuf = new byte[contentWithLength.Count];
                contentWithLength.CopyTo(tmpBuf);
                break;

            case StorageAttribute.UNICODE:
                itemVal   = StrUtil.rtrim(itemVal);
                valueSize = (Convert.ToString(itemVal.Length, 16)).ToUpper();

                // add leading zeros (if needed)
                for (int j = 0; j < 4 - valueSize.Length; j++)
                {
                    tmpBufLen += "0";
                }
                tmpBufLen += valueSize;

                contentWithLength.AddRange(Manager.Environment.GetEncoding().GetBytes(tmpBufLen));
                contentWithLength.AddRange(Encoding.Unicode.GetBytes(itemVal));
                tmpBuf = new byte[contentWithLength.Count];
                contentWithLength.CopyTo(tmpBuf);
                break;

            case StorageAttribute.BLOB:
                pos = 0;
                // blob will be serialized in packet of size 0xFFFF.
                // So format of serialized buffer for blob is
                // no. of packets (n) + length1 + data1 + length2 + data2 + ......length n + datan
                fldValLen = ISO_8859_1_Encoding.getInstance().GetByteCount(itemVal);

                noOfPackets = (int)fldValLen / 0xFFFF;

                tmpBufLen      = "FFFF";
                tmpNoOfPackets = Manager.Environment.GetEncoding().GetBytes(tmpBufLen);

                for (int i = 0; i < noOfPackets; i++)
                {
                    tempItemVal = itemVal.Substring(pos, 0xFFFF);
                    pos        += 0xFFFF;
                    contentWithLength.AddRange(tmpNoOfPackets);
                    contentWithLength.AddRange(ISO_8859_1_Encoding.getInstance().GetBytes(tempItemVal));
                }

                int lastPacketSize = fldValLen % 0xFFFF;

                if (lastPacketSize > 0)
                {
                    tempItemVal = itemVal.Substring(pos, (fldValLen) - (pos));
                    byte[] tempItemValBytes = ISO_8859_1_Encoding.getInstance().GetBytes(tempItemVal);

                    tmpBufLen = tempItemValBytes.Length.ToString("X4");
                    contentWithLength.AddRange(Manager.Environment.GetEncoding().GetBytes(tmpBufLen));
                    contentWithLength.AddRange(ISO_8859_1_Encoding.getInstance().GetBytes(tempItemVal));
                    noOfPackets++;
                }

                tmpStrNoOfPackets = noOfPackets.ToString("D4");
                tmpNoOfPackets    = Manager.Environment.GetEncoding().GetBytes(tmpStrNoOfPackets);

                tmpBuf = new byte[contentWithLength.Count + tmpNoOfPackets.Length];

                tmpNoOfPackets.CopyTo(tmpBuf, 0);
                contentWithLength.CopyTo(tmpBuf, tmpNoOfPackets.Length);
                break;
            } //end of the type case block

            return(tmpBuf);
        }
Пример #3
0
        /// <returns> translation of this argument into Magic URL style (e.g. -Aalpha or -N17 etc.)</returns>
        protected internal void toURL(StringBuilder htmlArgs, bool makePrintable)
        {
            if (!skipArg())
            {
                String           argValue = null, rangeStr = null;
                StorageAttribute attribute = StorageAttribute.NONE;
                bool             isNull    = false;
                PIC      pic = null;
                NUM_TYPE num1, num2;
                Expression.ReturnValue retVal;
                int compIdx = 0;

                // Get the value and attribute and set the "is null" flag according
                // to the argument type
                switch (_type)
                {
                case ConstInterface.ARG_TYPE_VALUE:
                    isNull    = _valueIsNull;
                    attribute = _valueAttr;
                    argValue  = _val;
                    break;

                case ConstInterface.ARG_TYPE_EXP:
                    retVal   = _exp.evaluate(2000);
                    argValue = retVal.mgVal;
                    if (argValue == null)
                    {
                        isNull = true;
                    }
                    else
                    {
                        attribute = retVal.type;
                    }
                    compIdx = _exp.getTask().getCompIdx();
                    break;

                case ConstInterface.ARG_TYPE_FIELD:
                    if (_fld.isNull())
                    {
                        isNull = true;
                    }
                    else
                    {
                        argValue  = _fld.getValue(false);
                        attribute = _fld.getType();
                    }
                    compIdx = _fld.getTask().getCompIdx();
                    break;

                case ConstInterface.ARG_TYPE_SKIP:
                    isNull = true; //#919535 If argument is skipped then pass NULL it will handled in server.
                    break;
                }

                // Create the argument string
                if (isNull)
                {
                    htmlArgs.Append(ConstInterface.REQ_ARG_NULL);
                }
                else
                {
                    switch (attribute)
                    {
                    case StorageAttribute.NUMERIC:
                        num1 = new NUM_TYPE(argValue);
                        num2 = new NUM_TYPE(argValue);
                        num1.round(0);
                        if (NUM_TYPE.num_cmp(num1, num2) == 0)
                        {
                            pic = new PIC("" + UtilStrByteMode.lenB(argValue),
                                          StorageAttribute.ALPHA, compIdx);
                            String numDispVal = num2.to_a(pic).Trim();
                            if (numDispVal.Length <= 9)
                            {
                                htmlArgs.Append(ConstInterface.REQ_ARG_NUMERIC + num2.to_a(pic).Trim());
                            }
                            else
                            {
                                htmlArgs.Append(ConstInterface.REQ_ARG_DOUBLE + num2.to_double());
                            }
                        }
                        else
                        {
                            htmlArgs.Append(ConstInterface.REQ_ARG_DOUBLE + num2.to_double());
                        }
                        break;

                    case StorageAttribute.DATE:
                        pic      = new PIC(Manager.GetDefaultDateFormat(), attribute, compIdx);
                        rangeStr = "";
                        htmlArgs.Append(ConstInterface.REQ_ARG_ALPHA);
                        break;

                    case StorageAttribute.TIME:
                        pic      = new PIC(Manager.GetDefaultTimeFormat(), attribute, compIdx);
                        rangeStr = "";
                        htmlArgs.Append(ConstInterface.REQ_ARG_ALPHA);
                        break;

                    case StorageAttribute.ALPHA:
                    // alpha strings are kept internally as Unicode, so fall through to unicode case...

                    case StorageAttribute.UNICODE:
                        pic      = new PIC("" + UtilStrByteMode.lenB(argValue), attribute, compIdx);
                        rangeStr = "";

                        /* TODO: Kaushal. this should be "-U".
                         * "-U" is currently used for null value.
                         */
                        htmlArgs.Append(ConstInterface.REQ_ARG_UNICODE);
                        break;

                    case StorageAttribute.BLOB:
                        pic      = new PIC("", attribute, compIdx);
                        rangeStr = "";

                        char contentType = BlobType.getContentType(argValue);
                        // ANSI blobs are later translated to Unicode
                        if (contentType == BlobType.CONTENT_TYPE_UNICODE || contentType == BlobType.CONTENT_TYPE_ANSI)
                        {
                            htmlArgs.Append(ConstInterface.REQ_ARG_UNICODE);
                        }
                        else
                        {
                            htmlArgs.Append(ConstInterface.REQ_ARG_ALPHA);
                        }
                        break;

                    case StorageAttribute.BLOB_VECTOR:
                        pic      = new PIC("", attribute, compIdx);
                        rangeStr = "";
                        htmlArgs.Append(ConstInterface.REQ_ARG_ALPHA);
                        //QCR 970794 appending eye catcher for vectors passed as arguments on hyperlink
                        argValue = ConstInterface.MG_HYPER_ARGS + BlobType.removeBlobPrefix(argValue) +
                                   ConstInterface.MG_HYPER_ARGS;
                        break;

                    case StorageAttribute.BOOLEAN:
                        pic      = new PIC("5", attribute, compIdx);
                        rangeStr = "TRUE,FALSE";
                        htmlArgs.Append(ConstInterface.REQ_ARG_LOGICAL);
                        break;
                    }

                    if (attribute != StorageAttribute.NUMERIC)
                    {
                        string finalValue = StrUtil.rtrim(DisplayConvertor.Instance.mg2disp(argValue, rangeStr, pic, compIdx, false));

                        //QCR 970794 converting the url to a legal format
                        htmlArgs.Append(makePrintable
                                    ? GUIManager.Instance.makeURLPrintable(finalValue)
                                    : finalValue);
                    }
                }
            }
        }