예제 #1
0
        internal int PackObject(XMLElement objElem, ClassDescriptor desc, int offs, ByteBuffer buf)
        {
            ClassDescriptor.FieldDescriptor[] flds = desc.allFields;
            for (int i = 0, n = flds.Length; i < n; i++)
            {
                ClassDescriptor.FieldDescriptor fd = flds[i];
                string fieldName = fd.fieldName;
                XMLElement elem = (objElem != null) ? objElem.GetSibling(fieldName) : null;

                switch (fd.type)
                {
                    case ClassDescriptor.tpByte:
                        buf.Extend(offs + 1);
                        if (elem != null)
                        {
                            if (elem.IsIntValue())
                            {
                                buf.arr[offs] = (byte) elem.GetIntValue();
                            }
                            else if (elem.IsRealValue())
                            {
                                //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions.
                                buf.arr[offs] = (byte) elem.GetRealValue();
                            }
                            else
                            {
                                ThrowException("Conversion for field " + fieldName + " is not possible");
                            }
                        }
                        offs += 1;
                        continue;

                    case ClassDescriptor.tpBoolean:
                        buf.Extend(offs + 1);
                        if (elem != null)
                        {
                            if (elem.IsIntValue())
                            {
                                buf.arr[offs] = (byte) (elem.GetIntValue() != 0 ? 1 : 0);
                            }
                            else if (elem.IsRealValue())
                            {
                                buf.arr[offs] = (byte) (elem.GetRealValue() != 0.0 ? 1 : 0);
                            }
                            else
                            {
                                ThrowException("Conversion for field " + fieldName + " is not possible");
                            }
                        }
                        offs += 1;
                        continue;

                    case ClassDescriptor.tpShort:
                    case ClassDescriptor.tpChar:
                        buf.Extend(offs + 2);
                        if (elem != null)
                        {
                            if (elem.IsIntValue())
                            {
                                Bytes.Pack2(buf.arr, offs, (short) elem.GetIntValue());
                            }
                            else if (elem.IsRealValue())
                            {
                                //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions.
                                Bytes.Pack2(buf.arr, offs, (short) elem.GetRealValue());
                            }
                            else
                            {
                                ThrowException("Conversion for field " + fieldName + " is not possible");
                            }
                        }
                        offs += 2;
                        continue;

                    case ClassDescriptor.tpInt:
                        buf.Extend(offs + 4);
                        if (elem != null)
                        {
                            if (elem.IsIntValue())
                            {
                                Bytes.Pack4(buf.arr, offs, (int) elem.GetIntValue());
                            }
                            else if (elem.IsRealValue())
                            {
                                //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions.
                                Bytes.Pack4(buf.arr, offs, (int) elem.GetRealValue());
                            }
                            else
                            {
                                ThrowException("Conversion for field " + fieldName + " is not possible");
                            }
                        }
                        offs += 4;
                        continue;

                    case ClassDescriptor.tpLong:
                        buf.Extend(offs + 8);
                        if (elem != null)
                        {
                            if (elem.IsIntValue())
                            {
                                Bytes.Pack8(buf.arr, offs, elem.GetIntValue());
                            }
                            else if (elem.IsRealValue())
                            {
                                //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions.
                                Bytes.Pack8(buf.arr, offs, (long) elem.GetRealValue());
                            }
                            else
                            {
                                ThrowException("Conversion for field " + fieldName + " is not possible");
                            }
                        }
                        offs += 8;
                        continue;

                    case ClassDescriptor.tpFloat:
                        buf.Extend(offs + 4);
                        if (elem != null)
                        {
                            if (elem.IsIntValue())
                            {
                                Bytes.PackF4(buf.arr, offs, (float) elem.GetIntValue());
                            }
                            else if (elem.IsRealValue())
                            {
                                Bytes.PackF4(buf.arr, offs, (float) elem.GetRealValue());
                            }
                            else
                            {
                                ThrowException("Conversion for field " + fieldName + " is not possible");
                            }
                        }
                        offs += 4;
                        continue;

                    case ClassDescriptor.tpDouble:
                        buf.Extend(offs + 8);
                        if (elem != null)
                        {
                            if (elem.IsIntValue())
                            {
                                //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions.
                                Bytes.PackF8(buf.arr, offs, (double) elem.GetIntValue());
                            }
                            else if (elem.IsRealValue())
                            {
                                Bytes.PackF8(buf.arr, offs, (double) elem.GetRealValue());
                            }
                            else
                            {
                                ThrowException("Conversion for field " + fieldName + " is not possible");
                            }
                        }
                        offs += 8;
                        continue;

                    case ClassDescriptor.tpDate:
                        buf.Extend(offs + 8);
                        if (elem != null)
                        {
                            if (elem.IsIntValue())
                            {
                                Bytes.Pack8(buf.arr, offs, elem.GetIntValue());
                            }
                            else if (elem.IsNullValue())
                            {
                                Bytes.Pack8(buf.arr, offs, -1);
                            }
                            else if (elem.IsStringValue())
                            {
                                /* TODOPORT:
                                //UPGRADE_ISSUE: Method 'java.text.DateFormat.parse' was not converted.
                                DateTime date = httpFormatter.parse(elem.GetStringValue(), 0);
                                //UPGRADE_TODO: The 'System.DateTime' structure does not have an equivalent to NULL.
                                if (date == null)
                                {
                                    ThrowException("Invalid date");
                                }
                                //UPGRADE_TODO: Method 'java.util.Date.getTime' was converted to 'DateTime.Ticks' which has a different behavior.
                                Bytes.Pack8(buf.arr, offs, date.Ticks);
                                */
                                ThrowException("Not implemented");
                            }
                            else
                            {
                                ThrowException("Conversion for field " + fieldName + " is not possible");
                            }
                        }
                        offs += 8;
                        continue;

                    case ClassDescriptor.tpString:
                        if (elem != null)
                        {
                            string val = null;
                            if (elem.IsIntValue())
                            {
                                val = Convert.ToString(elem.GetIntValue());
                            }
                            else if (elem.IsRealValue())
                            {
                                val = elem.GetRealValue().ToString();
                            }
                            else if (elem.IsStringValue())
                            {
                                val = elem.GetStringValue();
                            }
                            else if (elem.IsNullValue())
                            {
                                val = null;
                            }
                            else
                            {
                                ThrowException("Conversion for field " + fieldName + " is not possible");
                            }
                            offs = buf.PackString(offs, val, storage.encoding);
                            continue;
                        }

                        buf.Extend(offs + 4);
                        Bytes.Pack4(buf.arr, offs, -1);
                        offs += 4;
                        continue;

                    case ClassDescriptor.tpObject:
                    {
                        int oid = 0;
                        if (elem != null)
                        {
                            XMLElement ref_Renamed = elem.GetSibling("ref");
                            if (ref_Renamed == null)
                            {
                                ThrowException("<ref> element expected");
                            }
                            oid = MapId(GetIntAttribute(ref_Renamed, "id"));
                        }
                        buf.Extend(offs + 4);
                        Bytes.Pack4(buf.arr, offs, oid);
                        offs += 4;
                        continue;
                    }

                    case ClassDescriptor.tpValue:
                        offs = PackObject(elem, fd.valueDesc, offs, buf);
                        continue;

                    case ClassDescriptor.tpRaw:
                    case ClassDescriptor.tpArrayOfByte:
                        offs = ImportBinary(elem, offs, buf, fieldName);
                        continue;

                    case ClassDescriptor.tpArrayOfBoolean:
                        if (elem == null || elem.IsNullValue())
                        {
                            buf.Extend(offs + 4);
                            Bytes.Pack4(buf.arr, offs, -1);
                            offs += 4;
                        }
                        else
                        {
                            XMLElement item = elem.GetSibling("element");
                            int len = (item == null) ? 0 : item.Counter;
                            buf.Extend(offs + 4 + len);
                            Bytes.Pack4(buf.arr, offs, len);
                            offs += 4;
                            while (--len >= 0)
                            {
                                if (item.IsIntValue())
                                {
                                    buf.arr[offs] = (byte) (item.GetIntValue() != 0 ? 1 : 0);
                                }
                                else if (item.IsRealValue())
                                {
                                    buf.arr[offs] = (byte) (item.GetRealValue() != 0.0 ? 1 : 0);
                                }
                                else
                                {
                                    ThrowException("Conversion for field " + fieldName + " is not possible");
                                }
                                item = item.NextSibling;
                                offs += 1;
                            }
                        }
                        continue;

                    case ClassDescriptor.tpArrayOfChar:
                    case ClassDescriptor.tpArrayOfShort:
                        if (elem == null || elem.IsNullValue())
                        {
                            buf.Extend(offs + 4);
                            Bytes.Pack4(buf.arr, offs, -1);
                            offs += 4;
                        }
                        else
                        {
                            XMLElement item = elem.GetSibling("element");
                            int len = (item == null) ? 0 : item.Counter;
                            buf.Extend(offs + 4 + len * 2);
                            Bytes.Pack4(buf.arr, offs, len);
                            offs += 4;
                            while (--len >= 0)
                            {
                                if (item.IsIntValue())
                                {
                                    Bytes.Pack2(buf.arr, offs, (short) item.GetIntValue());
                                }
                                else if (item.IsRealValue())
                                {
                                    //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions.
                                    Bytes.Pack2(buf.arr, offs, (short) item.GetRealValue());
                                }
                                else
                                {
                                    ThrowException("Conversion for field " + fieldName + " is not possible");
                                }
                                item = item.NextSibling;
                                offs += 2;
                            }
                        }
                        continue;

                    case ClassDescriptor.tpArrayOfInt:
                        if (elem == null || elem.IsNullValue())
                        {
                            buf.Extend(offs + 4);
                            Bytes.Pack4(buf.arr, offs, -1);
                            offs += 4;
                        }
                        else
                        {
                            XMLElement item = elem.GetSibling("element");
                            int len = (item == null) ? 0 : item.Counter;
                            buf.Extend(offs + 4 + len * 4);
                            Bytes.Pack4(buf.arr, offs, len);
                            offs += 4;
                            while (--len >= 0)
                            {
                                if (item.IsIntValue())
                                {
                                    Bytes.Pack4(buf.arr, offs, (int) item.GetIntValue());
                                }
                                else if (item.IsRealValue())
                                {
                                    //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions.
                                    Bytes.Pack4(buf.arr, offs, (int) item.GetRealValue());
                                }
                                else
                                {
                                    ThrowException("Conversion for field " + fieldName + " is not possible");
                                }
                                item = item.NextSibling;
                                offs += 4;
                            }
                        }
                        continue;

                    case ClassDescriptor.tpArrayOfLong:
                        if (elem == null || elem.IsNullValue())
                        {
                            buf.Extend(offs + 4);
                            Bytes.Pack4(buf.arr, offs, -1);
                            offs += 4;
                        }
                        else
                        {
                            XMLElement item = elem.GetSibling("element");
                            int len = (item == null) ? 0 : item.Counter;
                            buf.Extend(offs + 4 + len * 8);
                            Bytes.Pack4(buf.arr, offs, len);
                            offs += 4;
                            while (--len >= 0)
                            {
                                if (item.IsIntValue())
                                {
                                    Bytes.Pack8(buf.arr, offs, item.GetIntValue());
                                }
                                else if (item.IsRealValue())
                                {
                                    //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions.
                                    Bytes.Pack8(buf.arr, offs, (long) item.GetRealValue());
                                }
                                else
                                {
                                    ThrowException("Conversion for field " + fieldName + " is not possible");
                                }
                                item = item.NextSibling;
                                offs += 8;
                            }
                        }
                        continue;

                    case ClassDescriptor.tpArrayOfFloat:
                        if (elem == null || elem.IsNullValue())
                        {
                            buf.Extend(offs + 4);
                            Bytes.Pack4(buf.arr, offs, -1);
                            offs += 4;
                        }
                        else
                        {
                            XMLElement item = elem.GetSibling("element");
                            int len = (item == null) ? 0 : item.Counter;
                            buf.Extend(offs + 4 + len * 4);
                            Bytes.Pack4(buf.arr, offs, len);
                            offs += 4;
                            while (--len >= 0)
                            {
                                if (item.IsIntValue())
                                {
                                    Bytes.PackF4(buf.arr, offs, (float) item.GetIntValue());
                                }
                                else if (item.IsRealValue())
                                {
                                    Bytes.PackF4(buf.arr, offs, (float) item.GetRealValue());
                                }
                                else
                                {
                                    ThrowException("Conversion for field " + fieldName + " is not possible");
                                }
                                item = item.NextSibling;
                                offs += 4;
                            }
                        }
                        continue;

                    case ClassDescriptor.tpArrayOfDouble:
                        if (elem == null || elem.IsNullValue())
                        {
                            buf.Extend(offs + 4);
                            Bytes.Pack4(buf.arr, offs, -1);
                            offs += 4;
                        }
                        else
                        {
                            XMLElement item = elem.GetSibling("element");
                            int len = (item == null) ? 0 : item.Counter;
                            buf.Extend(offs + 4 + len * 8);
                            Bytes.Pack4(buf.arr, offs, len);
                            offs += 4;
                            while (--len >= 0)
                            {
                                if (item.IsIntValue())
                                {
                                    //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions.
                                    Bytes.PackF8(buf.arr, offs, (double) item.GetIntValue());
                                }
                                else if (item.IsRealValue())
                                {
                                    Bytes.PackF8(buf.arr, offs, (double) item.GetRealValue());
                                }
                                else
                                {
                                    ThrowException("Conversion for field " + fieldName + " is not possible");
                                }
                                item = item.NextSibling;
                                offs += 8;
                            }
                        }
                        continue;

                    case ClassDescriptor.tpArrayOfDate:
                        if (elem == null || elem.IsNullValue())
                        {
                            buf.Extend(offs + 4);
                            Bytes.Pack4(buf.arr, offs, -1);
                            offs += 4;
                        }
                        else
                        {
                            XMLElement item = elem.GetSibling("element");
                            int len = (item == null) ? 0 : item.Counter;
                            buf.Extend(offs + 4 + len * 8);
                            Bytes.Pack4(buf.arr, offs, len);
                            offs += 4;
                            while (--len >= 0)
                            {
                                if (item.IsNullValue())
                                {
                                    Bytes.Pack8(buf.arr, offs, -1);
                                }
                                else if (item.IsStringValue())
                                {
                                    /* TODOPORT:
                                    //UPGRADE_ISSUE: Method 'java.text.DateFormat.parse' was not converted.
                                    DateTime date = httpFormatter.parse(item.GetStringValue(), 0);
                                    //UPGRADE_TODO: The 'System.DateTime' structure does not have an equivalent to NULL.
                                    if (date == null)
                                    {
                                        ThrowException("Invalid date");
                                    }
                                    //UPGRADE_TODO: Method 'java.util.Date.getTime' was converted to 'DateTime.Ticks' which has a different behavior.
                                    Bytes.Pack8(buf.arr, offs, date.Ticks);
                                    */
                                    ThrowException("Not implemented");
                                }
                                else
                                {
                                    ThrowException("Conversion for field " + fieldName + " is not possible");
                                }
                                item = item.NextSibling;
                                offs += 8;
                            }
                        }
                        continue;

                    case ClassDescriptor.tpArrayOfString:
                        if (elem == null || elem.IsNullValue())
                        {
                            buf.Extend(offs + 4);
                            Bytes.Pack4(buf.arr, offs, -1);
                            offs += 4;
                        }
                        else
                        {
                            XMLElement item = elem.GetSibling("element");
                            int len = (item == null) ? 0 : item.Counter;
                            buf.Extend(offs + 4);
                            Bytes.Pack4(buf.arr, offs, len);
                            offs += 4;
                            while (--len >= 0)
                            {
                                string val = null;
                                if (item.IsIntValue())
                                {
                                    val = Convert.ToString(item.GetIntValue());
                                }
                                else if (item.IsRealValue())
                                {
                                    val = item.GetRealValue().ToString();
                                }
                                else if (item.IsStringValue())
                                {
                                    val = item.GetStringValue();
                                }
                                else if (elem.IsNullValue())
                                {
                                    val = null;
                                }
                                else
                                {
                                    ThrowException("Conversion for field " + fieldName + " is not possible");
                                }

                                offs = buf.PackString(offs, val, storage.encoding);
                                item = item.NextSibling;
                            }
                        }
                        continue;

                    case ClassDescriptor.tpArrayOfObject:
                    case ClassDescriptor.tpLink:
                        if (elem == null || elem.IsNullValue())
                        {
                            buf.Extend(offs + 4);
                            Bytes.Pack4(buf.arr, offs, -1);
                            offs += 4;
                        }
                        else
                        {
                            XMLElement item = elem.GetSibling("element");
                            int len = (item == null) ? 0 : item.Counter;
                            buf.Extend(offs + 4 + len * 4);
                            Bytes.Pack4(buf.arr, offs, len);
                            offs += 4;
                            while (--len >= 0)
                            {
                                XMLElement ref_Renamed = item.GetSibling("ref");
                                if (ref_Renamed == null)
                                {
                                    ThrowException("<ref> element expected");
                                }
                                int oid = MapId(GetIntAttribute(ref_Renamed, "id"));
                                Bytes.Pack4(buf.arr, offs, oid);
                                item = item.NextSibling;
                                offs += 4;
                            }
                        }
                        continue;

                    case ClassDescriptor.tpArrayOfValue:
                        if (elem == null || elem.IsNullValue())
                        {
                            buf.Extend(offs + 4);
                            Bytes.Pack4(buf.arr, offs, -1);
                            offs += 4;
                        }
                        else
                        {
                            XMLElement item = elem.GetSibling("element");
                            int len = (item == null) ? 0 : item.Counter;
                            Bytes.Pack4(buf.arr, offs, len);
                            offs += 4;
                            ClassDescriptor elemDesc = fd.valueDesc;
                            while (--len >= 0)
                            {
                                offs = PackObject(item, elemDesc, offs, buf);
                                item = item.NextSibling;
                            }
                        }
                        continue;

                    case ClassDescriptor.tpArrayOfRaw:
                        if (elem == null || elem.IsNullValue())
                        {
                            buf.Extend(offs + 4);
                            Bytes.Pack4(buf.arr, offs, -1);
                            offs += 4;
                        }
                        else
                        {
                            XMLElement item = elem.GetSibling("element");
                            int len = (item == null) ? 0 : item.Counter;
                            Bytes.Pack4(buf.arr, offs, len);
                            offs += 4;
                            while (--len >= 0)
                            {
                                offs = ImportBinary(item, offs, buf, fieldName);
                                item = item.NextSibling;
                            }
                        }
                        continue;
                    }
            }
            return offs;
        }
예제 #2
0
 internal int ImportBinary(XMLElement elem, int offs, ByteBuffer buf, string fieldName)
 {
     if (elem == null || elem.IsNullValue())
     {
         buf.Extend(offs + 4);
         Bytes.Pack4(buf.arr, offs, -1);
         offs += 4;
     }
     else if (elem.IsStringValue())
     {
         string hexStr = elem.GetStringValue();
         int len = hexStr.Length;
         if (hexStr.StartsWith("#"))
         {
             buf.Extend(offs + 4 + len / 2 - 1);
             Bytes.Pack4(buf.arr, offs, -2 - GetHexValue(hexStr[1]));
             offs += 4;
             for (int j = 2; j < len; j += 2)
             {
                 buf.arr[offs++] = (byte) ((GetHexValue(hexStr[j]) << 4) | GetHexValue(hexStr[j + 1]));
             }
         }
         else
         {
             buf.Extend(offs + 4 + len / 2);
             Bytes.Pack4(buf.arr, offs, len / 2);
             offs += 4;
             for (int j = 0; j < len; j += 2)
             {
                 buf.arr[offs++] = (byte) ((GetHexValue(hexStr[j]) << 4) | GetHexValue(hexStr[j + 1]));
             }
         }
     }
     else
     {
         XMLElement ref_Renamed = elem.GetSibling("ref");
         if (ref_Renamed != null)
         {
             buf.Extend(offs + 4);
             Bytes.Pack4(buf.arr, offs, MapId(GetIntAttribute(ref_Renamed, "id")));
             offs += 4;
         }
         else
         {
             XMLElement item = elem.GetSibling("element");
             int len = (item == null) ? 0 : item.Counter;
             buf.Extend(offs + 4 + len);
             Bytes.Pack4(buf.arr, offs, len);
             offs += 4;
             while (--len >= 0)
             {
                 if (item.IsIntValue())
                 {
                     buf.arr[offs] = (byte) item.GetIntValue();
                 }
                 else if (item.IsRealValue())
                 {
                     //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions.
                     buf.arr[offs] = (byte) item.GetRealValue();
                 }
                 else
                 {
                     ThrowException("Conversion for field " + fieldName + " is not possible");
                 }
                 item = item.NextSibling;
                 offs += 1;
             }
         }
     }
     return offs;
 }