protected internal override T?Deserialize(IntermediateReader input, ContentSerializerAttribute format, T?existingInstance)
        {
            var list = new List <T>();

            _serializer.Deserialize(input, list);
            return(list.First());
        }
        protected internal override ExternalReference <T> Deserialize(IntermediateReader input, ContentSerializerAttribute format, ExternalReference <T> existingInstance)
        {
            var result = existingInstance ?? new ExternalReference <T>();

            input.ReadExternalReference(result);
            return(result);
        }
        protected internal override List <T> Deserialize(IntermediateReader input, ContentSerializerAttribute format, List <T> existingInstance)
        {
            var result = existingInstance ?? new List <T>();

            var elementSerializer = _itemSerializer as ElementSerializer <T>;

            if (elementSerializer != null)
            {
                elementSerializer.Deserialize(input, result);
            }
            else
            {
                // Create the item serializer attribute.
                var itemFormat = new ContentSerializerAttribute();
                itemFormat.ElementName = format.CollectionItemName;

                // Read all the items.
                while (input.MoveToElement(itemFormat.ElementName))
                {
                    var value = input.ReadObject <T>(itemFormat, _itemSerializer);
                    result.Add(value);
                }
            }

            return(result);
        }
        protected internal override object Deserialize(IntermediateReader input, ContentSerializerAttribute format, object existingInstance)
        {
            var result = existingInstance;

            if (result == null)
            {
                try
                {
                    result = Activator.CreateInstance(TargetType, true);
                }
                catch (MissingMethodException e)
                {
                    throw new Exception(string.Format("Couldn't create object of type {0}: {1}", TargetType.Name, e.Message), e);
                }
            }

            // First deserialize the base type.
            if (_baseSerializer != null)
            {
                _baseSerializer.Deserialize(input, format, result);
            }

            // Now deserialize our own elements.
            foreach (var info in _elements)
            {
                if (!info.Attribute.FlattenContent)
                {
                    if (!input.MoveToElement(info.Attribute.ElementName))
                    {
                        // If the the element was optional then we can
                        // safely skip it and continue.
                        if (info.Attribute.Optional)
                        {
                            continue;
                        }

                        // We failed to find a required element.
                        throw new InvalidContentException(string.Format("The Xml element `{0}` is required!", info.Attribute.ElementName));
                    }
                }

                if (info.Attribute.SharedResource)
                {
                    Action <object> fixup = (o) => info.Setter(result, o);
                    input.ReadSharedResource(info.Attribute, fixup);
                }
                else if (info.Setter == null)
                {
                    var value = info.Getter(result);
                    input.ReadObject(info.Attribute, info.Serializer, value);
                }
                else
                {
                    var value = input.ReadObject <object>(info.Attribute, info.Serializer);
                    info.Setter(result, value);
                }
            }

            return(result);
        }
Example #5
0
        public static T Deserialize <T>(XmlReader input, string referenceRelocationPath)
        {
            var serializer = new IntermediateSerializer();
            var reader     = new IntermediateReader(serializer, input, referenceRelocationPath);

            if (!reader.MoveToElement("XnaContent"))
            {
                throw new InvalidContentException(string.Format("Could not find XnaContent element in '{0}'.", referenceRelocationPath));
            }

            // Initialize the namespace lookups from
            // the attributes on the XnaContent element.
            serializer.CreateNamespaceLookup(input);

            // Move past the XnaContent.
            input.ReadStartElement();

            // Read the asset.
            var format = new ContentSerializerAttribute {
                ElementName = "Asset"
            };
            var asset = reader.ReadObject <T>(format);

            // TODO: Read the shared resources and external
            // references here!

            // Move past the closing XnaContent element.
            input.ReadEndElement();

            return(asset);
        }
Example #6
0
        private static string[] ReadElements(IntermediateReader input)
        {
            if (input.Xml.IsEmptyElement)
            {
                return(new string[0]);
            }

            string str = string.Empty;

            while (input.Xml.NodeType != XmlNodeType.EndElement)
            {
                if (input.Xml.NodeType == XmlNodeType.Comment)
                {
                    input.Xml.Read();
                }
                else
                {
                    str += input.Xml.ReadString();
                }
            }

            var elements = str.Split(_seperators, StringSplitOptions.RemoveEmptyEntries);

            if (elements.Length == 0)
            {
                elements = new[] { str }
            }
            ;

            return(elements);
        }
        public void Deserialize(IntermediateReader input, object collection, ContentSerializerAttribute format)
        {
            var itemFormat = new ContentSerializerAttribute();

            itemFormat.ElementName = format.CollectionItemName;
            while (input.MoveToElement(format.CollectionItemName))
            {
                _addMethod.Invoke(collection, new[] { input.ReadObject <object>(itemFormat, _contentSerializer) });
            }
        }
        protected internal override T[] Deserialize(IntermediateReader input, ContentSerializerAttribute format, T[] existingInstance)
        {
            if (existingInstance != null)
            {
                throw new InvalidOperationException("You cannot deserialize an array into a getter-only property.");
            }
            var result = _listSerializer.Deserialize(input, format, null);

            return(result.ToArray());
        }
        protected internal override object Deserialize(IntermediateReader input, ContentSerializerAttribute format, object existingInstance)
        {
            var result = existingInstance;

            if (result == null)
            {
                try
                {
                    result = Activator.CreateInstance(TargetType, true);
                }
                catch (MissingMethodException e)
                {
                    throw new Exception(string.Format("Couldn't create object of type {0}: {1}", TargetType.Name, e.Message), e);
                }
            }

            var reader = input.Xml;
            var depth  = reader.Depth;

            // Read the next node.
            while (reader.Read())
            {
                // Did we reach the end of this object?
                if (reader.NodeType == XmlNodeType.EndElement)
                {
                    break;
                }

                Debug.Assert(reader.Depth == depth, "We are not at the right depth!");

                if (reader.NodeType == XmlNodeType.Element)
                {
                    var elementName = reader.Name;
                    reader.ReadStartElement();

                    ElementInfo info;
                    if (!_elements.TryGetValue(elementName, out info))
                    {
                        throw new InvalidContentException(string.Format("Element `{0}` was not found in type `{1}`.", elementName, TargetType));
                    }
                    var value = info.Serializer.Deserialize(input, format, null);
                    info.Setter(result, value);
                    reader.ReadEndElement();
                    continue;
                }

                // If we got here then we were not interested
                // in this node... so skip its children.
                reader.Skip();
            }

            return(result);
        }
Example #10
0
        protected internal override T Deserialize(IntermediateReader input, ContentSerializerAttribute format, T existingInstance)
        {
            var elements = ReadElements(input);

            if (elements.Length < _elementCount)
            {
                ThrowElementCountException();
            }

            var index = 0;

            return(Deserialize(elements, ref index));
        }
Example #11
0
        protected internal override T Deserialize(IntermediateReader input, ContentSerializerAttribute format, T existingInstance)
        {
            var str      = input.Xml.ReadString();
            var elements = str.Split(_seperators, StringSplitOptions.RemoveEmptyEntries);

            if (elements.Length < _elementCount)
            {
                ThrowElementCountException();
            }
            var index = 0;

            return(Deserialize(elements, ref index));
        }
Example #12
0
        protected internal override object Deserialize(IntermediateReader input, ContentSerializerAttribute format, object existingInstance)
        {
            var str = input.Xml.ReadString();

            try
            {
                return(Enum.Parse(TargetType, str, true));
            }
            catch (Exception ex)
            {
                throw input.NewInvalidContentException(ex, "Invalid enum value '{0}' for type '{1}'", str, TargetType.Name);
            }
        }
Example #13
0
        protected internal void Deserialize(IntermediateReader input, List <T> results)
        {
            var elements = ReadElements(input);

            for (var index = 0; index < elements.Length;)
            {
                if (elements.Length - index < _elementCount)
                {
                    ThrowElementCountException();
                }

                var elem = Deserialize(elements, ref index);
                results.Add(elem);
            }
        }
Example #14
0
        protected internal void Deserialize(IntermediateReader input, List <T> results)
        {
            var str      = input.Xml.ReadString();
            var elements = str.Split(_seperators, StringSplitOptions.RemoveEmptyEntries);

            for (var index = 0; index < elements.Length;)
            {
                if (elements.Length - index < _elementCount)
                {
                    ThrowElementCountException();
                }

                var elem = Deserialize(elements, ref index);
                results.Add(elem);
            }
        }
Example #15
0
        protected internal override Dictionary <TKey, TValue> Deserialize(IntermediateReader input, ContentSerializerAttribute format, Dictionary <TKey, TValue> existingInstance)
        {
            var result = existingInstance ?? new Dictionary <TKey, TValue>();

            while (input.MoveToElement(format.CollectionItemName))
            {
                input.Xml.ReadStartElement();

                var key   = input.ReadObject <TKey>(_keyFormat, _keySerializer);
                var value = input.ReadObject <TValue>(_valueFormat, _valueSerializer);
                result.Add(key, value);

                input.Xml.ReadEndElement();
            }

            return(result);
        }
        protected internal override CurveKeyCollection Deserialize(
            IntermediateReader input,
            ContentSerializerAttribute format,
            CurveKeyCollection existingInstance)
        {
            var result = existingInstance ?? new CurveKeyCollection();

            if (input.Xml.HasValue)
            {
                var elements = PackedElementsHelper.ReadElements(input);
                if (elements.Length > 0)
                {
                    // Each CurveKey consists of 5 elements
                    if (elements.Length % 5 != 0)
                    {
                        throw new InvalidContentException(
                                  "Elements count in CurveKeyCollection is inncorect!");
                    }
                    try
                    {
                        // Parse all CurveKeys
                        for (int i = 0; i < elements.Length; i += 5)
                        {
                            // Order: Position, Value, TangentIn, TangentOut and Continuity
                            var curveKey = new CurveKey
                                               (XmlConvert.ToSingle(elements[i]),
                                               XmlConvert.ToSingle(elements[i + 1]),
                                               XmlConvert.ToSingle(elements[i + 2]),
                                               XmlConvert.ToSingle(elements[i + 3]),
                                               (CurveContinuity)Enum.Parse(
                                                   typeof(CurveContinuity),
                                                   elements[i + 4],
                                                   true));
                            result.Add(curveKey);
                        }
                    }
                    catch (Exception e)
                    {
                        throw new InvalidContentException
                                  ("Error parsing CurveKey", e);
                    }
                }
            }
            return(result);
        }
        protected internal override object Deserialize(IntermediateReader input, ContentSerializerAttribute format, object existingInstance)
        {
            var result = (IList)(existingInstance ?? Activator.CreateInstance(TargetType));

            // Create the item serializer attribute.
            var itemFormat = new ContentSerializerAttribute();

            itemFormat.ElementName = format.CollectionItemName;

            // Read all the items.
            while (input.MoveToElement(itemFormat.ElementName))
            {
                var value = input.ReadObject <object>(itemFormat);
                result.Add(value);
            }

            return(result);
        }
Example #18
0
        public static T Deserialize <T>(XmlReader input, string referenceRelocationPath)
        {
            var serializer = new IntermediateSerializer();
            var reader     = new IntermediateReader(serializer, input, referenceRelocationPath);
            var asset      = default(T);

            try
            {
                if (!reader.MoveToElement("XnaContent"))
                {
                    throw new InvalidContentException(string.Format("Could not find XnaContent element in '{0}'.",
                                                                    referenceRelocationPath));
                }

                // Initialize the namespace lookups from
                // the attributes on the XnaContent element.
                serializer.CreateNamespaceLookup(input);

                // Move past the XnaContent.
                input.ReadStartElement();

                // Read the asset.
                var format = new ContentSerializerAttribute {
                    ElementName = "Asset"
                };
                asset = reader.ReadObject <T>(format);

                // Process the shared resources and external references.
                reader.ReadSharedResources();
                reader.ReadExternalReferences();

                // Move past the closing XnaContent element.
                input.ReadEndElement();
            }
            catch (XmlException xmlException)
            {
                throw reader.NewInvalidContentException(xmlException, "An error occured parsing.");
            }

            return(asset);
        }
Example #19
0
        private static string[] ReadElements(IntermediateReader input)
        {
            if (input.Xml.IsEmptyElement)
            {
                return(new string[0]);
            }

            string str = string.Empty;

            while (input.Xml.NodeType != XmlNodeType.EndElement)
            {
                if (input.Xml.NodeType == XmlNodeType.Comment)
                {
                    input.Xml.Read();
                }
                else
                {
                    str += input.Xml.ReadString();
                }
            }

            // Special case for char ' '
            if (str.Length > 0 && str.Trim() == string.Empty)
            {
                return new string[] { str }
            }
            ;

            var elements = str.Split(_seperators, StringSplitOptions.RemoveEmptyEntries);

            if (elements.Length == 1 && string.IsNullOrEmpty(elements[0]))
            {
                return(new string[0]);
            }

            return(elements);
        }
Example #20
0
 protected internal abstract T Deserialize(IntermediateReader input, ContentSerializerAttribute format, T existingInstance);
Example #21
0
 protected internal override Object Deserialize(IntermediateReader input, ContentSerializerAttribute format, Object existingInstance)
 {
     return(Deserialize(input, format, (T)existingInstance));
 }
 protected internal override object Deserialize(IntermediateReader input, ContentSerializerAttribute format, object existingInstance)
 {
     throw new System.NotImplementedException();
 }
Example #23
0
        protected internal override T[] Deserialize(IntermediateReader input, ContentSerializerAttribute format, T[] existingInstance)
        {
            var result = _listSerializer.Deserialize(input, format, null);

            return(result.ToArray());
        }
Example #24
0
        protected internal override int Deserialize(IntermediateReader input, ContentSerializerAttribute format, int existingInstance)
        {
            var str = input.Xml.ReadString();

            return(XmlConvert.ToInt32(str));
        }
        protected internal override object Deserialize(IntermediateReader input, ContentSerializerAttribute format, object existingInstance)
        {
            var cast = existingInstance == null ? default(T) : (T)existingInstance;

            return(Deserialize(input, format, cast));
        }
Example #26
0
 protected internal override T?Deserialize(IntermediateReader input, ContentSerializerAttribute format, T?existingInstance)
 {
     return(input.ReadRawObject <T>(_format, _serializer));
 }