public virtual bool WritesCustom(Object obj, Type type, IWriter writer)
        {
            if (type.IsArray || !typeof(ICollection).IsAssignableFrom(type))
            {
                return(false);
            }
            ICollection coll = (ICollection)obj;
            String      collElement;
            int         length = coll.Count;

            if (obj is IList)
            {
                collElement = XmlDictionary.ListElement;
            }
            else if (obj is ICollection)
            {
                collElement = XmlDictionary.SetElement;
            }
            else
            {
                throw new Exception("Collection of type '" + type.FullName + "' not supported");
            }
            writer.WriteStartElement(collElement);
            int id = writer.AcquireIdForObject(obj);

            writer.WriteAttribute(XmlDictionary.IdAttribute, id.ToString());
            writer.WriteAttribute(XmlDictionary.SizeAttribute, length.ToString());
            Type componentType = GetComponentTypeOfCollection(obj);

            ClassElementHandler.WriteAsAttribute(componentType, writer);
            writer.WriteStartElementEnd();
            if (obj is IList)
            {
                IList list = (IList)obj;
                for (int a = 0, size = list.Count; a < size; a++)
                {
                    Object item = list[a];
                    writer.WriteObject(item);
                }
            }
            else if (obj is IEnumerable)
            {
                IEnumerator enumerator = ((IEnumerable)obj).GetEnumerator();
                while (enumerator.MoveNext())
                {
                    Object item = enumerator.Current;
                    writer.WriteObject(item);
                }
            }
            writer.WriteCloseElement(collElement);
            return(true);
        }
        public virtual bool WritesCustom(Object obj, Type type, IWriter writer)
        {
            if (!typeof(TimeSpan).Equals(type))
            {
                return(false);
            }
            long millis = (long)((TimeSpan)obj).TotalMilliseconds;

            writer.WriteStartElement(XmlDictionary.ObjectElement);
            ClassElementHandler.WriteAsAttribute(typeof(Int64?), writer);
            writer.WriteAttribute(XmlDictionary.ValueAttribute, millis.ToString());
            writer.WriteEndElement();
            return(true);
        }
Beispiel #3
0
        public virtual bool WritesCustom(Object obj, Type type, IWriter writer)
        {
            if (!type.IsEnum)
            {
                return(false);
            }
            writer.WriteStartElement(XmlDictionary.EnumElement);
            int id = writer.AcquireIdForObject(obj);

            writer.WriteAttribute(XmlDictionary.IdAttribute, id.ToString());
            ClassElementHandler.WriteAsAttribute(type, writer);
            writer.WriteAttribute(XmlDictionary.ValueAttribute, obj.ToString());
            writer.WriteEndElement();
            return(true);
        }
Beispiel #4
0
        public virtual Object ReadObject(Type returnType, String elementName, int id, IReader reader)
        {
            if (!XmlDictionary.EnumElement.Equals(elementName))
            {
                throw new Exception("Element '" + elementName + "' not supported");
            }
            Type enumType = ClassElementHandler.ReadFromAttribute(reader);

            String enumValue = reader.GetAttributeValue(XmlDictionary.ValueAttribute);

            if (enumValue == null)
            {
                throw new Exception("Element '" + elementName + "' invalid");
            }
            return(ConversionHelper.ConvertValueToType(enumType, enumValue));
        }
Beispiel #5
0
        public virtual Object ReadObject(Type returnType, IReader reader)
        {
            String elementName = reader.GetElementName();

            if (XmlDictionary.NullElement.Equals(elementName))
            {
                reader.MoveOverElementEnd();
                return(null);
            }
            String idValue = reader.GetAttributeValue(XmlDictionary.IdAttribute);
            int    id      = idValue != null && idValue.Length > 0 ? Int32.Parse(idValue) : 0;

            if (XmlDictionary.RefElement.Equals(elementName))
            {
                reader.MoveOverElementEnd();
                return(reader.GetObjectById(id));
            }
            Object obj;

            if (XmlDictionary.ObjectElement.Equals(elementName))
            {
                Type type = ClassElementHandler.ReadFromAttribute(reader);
                obj = ReadObjectContent(returnType, type, id, reader);
            }
            else
            {
                INameBasedHandler nameBasedElementReader = nameBasedElementReaders.GetExtension(elementName);
                if (nameBasedElementReader == null)
                {
                    throw new Exception("Element name '" + elementName + "' not supported");
                }
                obj = nameBasedElementReader.ReadObject(returnType, elementName, id, reader);
            }
            if (id > 0)
            {
                reader.PutObjectWithId(obj, id);
            }
            reader.MoveOverElementEnd();
            return(obj);
        }
Beispiel #6
0
        public virtual void WriteObject(Object obj, IWriter writer)
        {
            if (obj == null)
            {
                writer.WriteStartElement(XmlDictionary.NullElement);
                writer.WriteEndElement();
                return;
            }
            int id = writer.GetIdOfObject(obj);

            if (id > 0)
            {
                writer.WriteStartElement(XmlDictionary.RefElement);
                writer.WriteAttribute(XmlDictionary.IdAttribute, id.ToString());
                writer.WriteEndElement();
                return;
            }
            Type type = ProxyHelper.GetRealType(obj.GetType());

            for (int a = 0, size = nameBasedElementWriters.Count; a < size; a++)
            {
                INameBasedHandler nameBasedElementWriter = nameBasedElementWriters[a];
                if (nameBasedElementWriter.WritesCustom(obj, type, writer))
                {
                    return;
                }
            }

            id = writer.AcquireIdForObject(obj);
            String objectElement = XmlDictionary.ObjectElement;

            writer.WriteStartElement(objectElement);
            if (id > 0)
            {
                writer.WriteAttribute(XmlDictionary.IdAttribute, id.ToString());
            }
            ClassElementHandler.WriteAsAttribute(type, writer);
            WriteObjectContent(obj, type, writer);
            writer.WriteCloseElement(objectElement);
        }
Beispiel #7
0
        public virtual Object ReadObject(Type returnType, String elementName, int id, IReader reader)
        {
            if (!XmlDictionary.ArrayElement.Equals(elementName))
            {
                throw new Exception("Element '" + elementName + "' not supported");
            }
            int  length        = Int32.Parse(reader.GetAttributeValue(XmlDictionary.SizeAttribute));
            Type componentType = ClassElementHandler.ReadFromAttribute(reader);

            Array targetArray;

            if (!reader.IsEmptyElement())
            {
                reader.NextTag();
            }
            if ("values".Equals(reader.GetElementName()))
            {
                String listOfValuesString = reader.GetAttributeValue("v");

                if (typeof(char).Equals(componentType) || typeof(byte).Equals(componentType) || typeof(sbyte).Equals(componentType) ||
                    typeof(bool).Equals(componentType))
                {
                    targetArray = (Array)ConversionHelper.ConvertValueToType(componentType.MakeArrayType(), listOfValuesString, EncodingInformation.SOURCE_BASE64 | EncodingInformation.TARGET_PLAIN);
                    reader.PutObjectWithId(targetArray, id);
                }
                else
                {
                    targetArray = Array.CreateInstance(componentType, length);
                    reader.PutObjectWithId(targetArray, id);
                    String[] items = splitPattern.Split(listOfValuesString);
                    for (int a = 0, size = items.Length; a < size; a++)
                    {
                        String item = items[a];
                        if (item == null || item.Length == 0)
                        {
                            continue;
                        }
                        Object convertedValue = ConversionHelper.ConvertValueToType(componentType, items[a]);
                        targetArray.SetValue(convertedValue, a);
                    }
                }
                reader.MoveOverElementEnd();
            }
            else
            {
                if (returnType.IsGenericType)
                {
                    componentType = returnType.GetGenericArguments()[0];
                }
                targetArray = Array.CreateInstance(componentType, length);
                reader.PutObjectWithId(targetArray, id);
                ICommandBuilder      commandBuilder      = CommandBuilder;
                ICommandTypeRegistry commandTypeRegistry = reader.CommandTypeRegistry;
                for (int index = 0; index < length; index++)
                {
                    Object item = reader.ReadObject(componentType);
                    if (item is IObjectFuture)
                    {
                        IObjectFuture  objectFuture = (IObjectFuture)item;
                        IObjectCommand command      = commandBuilder.Build(commandTypeRegistry, objectFuture, targetArray, index);
                        reader.AddObjectCommand(command);
                    }
                    else
                    {
                        targetArray.SetValue(item, index);
                    }
                }
            }
            return(targetArray);
        }
Beispiel #8
0
        public virtual bool WritesCustom(Object obj, Type type, IWriter writer)
        {
            if (!type.IsArray)
            {
                return(false);
            }
            Array  array        = (Array)obj;
            String arrayElement = XmlDictionary.ArrayElement;

            writer.WriteStartElement(arrayElement);
            int id = writer.AcquireIdForObject(obj);

            writer.WriteAttribute(XmlDictionary.IdAttribute, id.ToString());
            int length = array.Length;

            writer.WriteAttribute(XmlDictionary.SizeAttribute, length.ToString());
            Type componentType = type.GetElementType();

            ClassElementHandler.WriteAsAttribute(componentType, writer);

            if (length == 0)
            {
                writer.WriteEndElement();
            }
            else
            {
                writer.WriteStartElementEnd();
                if (componentType.IsPrimitive)
                {
                    writer.Write("<values v=\"");
                    if (typeof(char).Equals(componentType) || typeof(byte).Equals(componentType) || typeof(sbyte).Equals(componentType) ||
                        typeof(bool).Equals(componentType))
                    {
                        String value = ConversionHelper.ConvertValueToType <String>(array, EncodingInformation.SOURCE_PLAIN | EncodingInformation.TARGET_BASE64);
                        writer.Write(value);
                    }
                    else
                    {
                        for (int a = 0; a < length; a++)
                        {
                            Object item = array.GetValue(a);
                            if (a > 0)
                            {
                                writer.Write(primitiveValueSeparator);
                            }
                            String value = ConversionHelper.ConvertValueToType <String>(item);
                            writer.WriteEscapedXml(value);
                        }
                    }
                    writer.Write("\"/>");
                }
                else
                {
                    for (int a = 0; a < length; a++)
                    {
                        Object item = array.GetValue(a);
                        writer.WriteObject(item);
                    }
                }
                writer.WriteCloseElement(arrayElement);
            }
            return(true);
        }
        public virtual Object ReadObject(Type returnType, String elementName, int id, IReader reader)
        {
            if (!XmlDictionary.SetElement.Equals(elementName) && !XmlDictionary.ListElement.Equals(elementName))
            {
                throw new Exception("Element '" + elementName + "' not supported");
            }
            String lengthValue = reader.GetAttributeValue(XmlDictionary.SizeAttribute);
            int    length      = lengthValue != null && lengthValue.Length > 0 ? Int32.Parse(lengthValue) : 0;
            // Do not remove although in Java it is not necessary to extract the generic type information of a collection.
            // This code is important for environments like C#
            Type componentType = ClassElementHandler.ReadFromAttribute(reader);

            if (returnType.IsGenericType)
            {
                componentType = returnType.GetGenericArguments()[0];
            }
            MethodInfo addMethod = null;

            Object[]    parameters = new Object[1];
            IEnumerable coll;

            if (XmlDictionary.SetElement.Equals(elementName))
            {
                Type setType = typeof(HashSet <>).MakeGenericType(componentType);
                coll      = (IEnumerable)Activator.CreateInstance(setType);
                addMethod = setType.GetMethod("Add");
            }
            else
            {
                Type listType = typeof(List <>).MakeGenericType(componentType);
                coll      = (IEnumerable)(length > 0 ? Activator.CreateInstance(listType, length) : Activator.CreateInstance(listType));
                addMethod = listType.GetMethod("Add");
            }
            reader.PutObjectWithId(coll, id);
            reader.NextTag();
            bool                 useObjectFuture     = false;
            ICommandBuilder      commandBuilder      = CommandBuilder;
            ICommandTypeRegistry commandTypeRegistry = reader.CommandTypeRegistry;

            while (reader.IsStartTag())
            {
                Object item = reader.ReadObject(componentType);
                if (item is IObjectFuture)
                {
                    IObjectFuture  objectFuture = (IObjectFuture)item;
                    IObjectCommand command      = commandBuilder.Build(commandTypeRegistry, objectFuture, coll, addMethod);
                    reader.AddObjectCommand(command);
                    useObjectFuture = true;
                }
                else if (useObjectFuture)
                {
                    IObjectCommand command = commandBuilder.Build(commandTypeRegistry, null, coll, addMethod, item);
                    reader.AddObjectCommand(command);
                }
                else
                {
                    parameters[0] = item;
                    addMethod.Invoke(coll, parameters);
                }
            }
            return(coll);
        }