Example #1
0
        /// <summary>
        /// <see cref="SerializedData.LoadFields(List{SerializerError}, PrototypeParserState)"/>
        /// </summary>
        /// <param name="errors"></param>
        /// <param name="filename">The filename from where data will be parsed. Only used for error reporting.</param>
        public void ParseAndLoadData(string filename, List <SerializerError> errors, XMLSerializerParams parameters)
        {
            var elementNodes = xElement.Nodes().ToList();
            var collection   = GetCollectionInstance(this.collectionType, elementNodes.Count);

            Type   elementType      = GetElementType(this.collectionType);
            var    elementTypeCache = SerializerCache.GetSerializableTypeCacheFor(elementType);
            string elementTypeName  = elementType.Name;

            foreach (var node in elementNodes)
            {
                var xElementNode = node as XElement;
                if (ReferenceEquals(xElementNode, null))                 // Malformed XML
                {
                    errors.Add(new SerializerError(SerializerErrorSeverity.ERROR, filename, (node as IXmlLineInfo).LineNumber, "Unable to cast node to element for " + node + "! Skipping element!"));
                    continue;
                }

                IXMLDataSerializer serializer = SerializerCache.GetBestSerializerFor(elementType);
                if (!ReferenceEquals(serializer, null))
                {
                    try
                    {
                        if (!SerializerValidation.SerializerWasFound(parameters, null, serializer, "Collection", null, elementType, "SERIALIZE", errors))
                        {
                            continue;
                        }

                        this.elements.Add(serializer.Deserialize(elementType, xElementNode, parameters));
                    }
                    catch (Exception ex)
                    {
                        errors.Add(new SerializerError(SerializerErrorSeverity.ERROR, "SERIALIZE", -1, "Serializer threw exception on collection of type " + elementType + ":\n\n" + ex.ToString() + "\n\nSkipping element!"));
                    }
                }
                else if (typeof(ISerializableRoot).IsAssignableFrom(elementType))
                {
                    // object ref
                    this.elements.Add(new SerializedRootObjectReference()
                    {
                        identifier = xElementNode.Value
                    });
                }
                else
                {
                    // Determine type
                    var    serializableTypeCache = elementTypeCache;
                    string typeName = elementTypeName;

                    // Try to read class attrib
                    var classAttrib = xElementNode.Attribute(XMLSerializer.AttributeType);
                    if (!ReferenceEquals(classAttrib, null))
                    {
                        serializableTypeCache = SerializerCache.GetSerializableTypeCacheFor(classAttrib.Value, parameters.standardNamespace);
                        typeName = classAttrib.Value;
                    }

                    // Validity checks
                    // Field not serializable?
                    if (ReferenceEquals(serializableTypeCache, null))
                    {
                        // TODO: Line number, better reporting as to why this type is unserializable!
                        errors.Add(new SerializerError(SerializerErrorSeverity.ERROR, filename, -1, "Collection element with unknown type " + typeName + " unserializable! Skipping field!"));
                        continue;
                    }

                    // Add element
                    this.elements.Add(new SerializedData(serializableTypeCache, xElementNode));
                }
            }

            foreach (var element in this.elements)
            {
                var sElement = element as SerializedData;
                if (!ReferenceEquals(sElement, null))
                {
                    sElement.LoadFieldsFromXML(filename, errors, parameters);
                }
            }
        }
Example #2
0
        /// <summary>
        /// It will for every field with string data deserialize this data using <see cref="IXMLDataSerializer"/>.
        ///
        /// For every sub-data field a <see cref="SerializedData"/> object is being written to <see cref="fields"/>.
        /// </summary>
        /// <param name="filename">Only used for error reporting</param>
        public void LoadFieldsFromXML(string filename, List <SerializerError> errors, XMLSerializerParams parameters)
        {
            foreach (var xNode in xElement.Nodes())
            {
                if (!SerializerValidation.NodeIsElement(parameters, xNode, filename, errors))                 // Malformed XML
                {
                    continue;
                }

                var xElement    = xNode as XElement;
                var elementName = xElement.Name.LocalName;

                // Field unknown?
                if (!SerializerValidation.FieldKnown(parameters, xElement, targetType, elementName, filename, errors))
                {
                    continue;
                }

                debug.Add(elementName, xElement);
                var _fieldData = targetType.GetFieldData(elementName);
                var fieldData  = _fieldData.Value;
                var fieldType  = fieldData.serializableTypeCache;

                if (xElement.Value == XMLSerializer.TokenNull)                 // Null special case
                {
                    fields.Add(elementName, null);
                    continue;
                }

                bool isCollection             = SerializedCollectionData.IsCollection(fieldData.fieldInfo.FieldType);
                IXMLDataSerializer serializer = SerializerCache.GetBestSerializerFor(fieldData.fieldInfo.FieldType);

                if (!ReferenceEquals(serializer, null))
                {
                    try
                    {
                        if (!SerializerValidation.SerializerWasFound(parameters, xElement, serializer, elementName, targetType == null ? null : targetType.type, fieldType == null ? null : fieldType.type, filename, errors))
                        {
                            continue;
                        }

                        fields.Add(elementName, serializer.Deserialize(fieldData.fieldInfo.FieldType, xElement, parameters));
                    }
                    catch (Exception ex)
                    {
                        errors.Add(new SerializerError(SerializerErrorSeverity.ERROR, filename, -1, "Serializer threw exception on field " + elementName + " on type " + targetType.type + ":\n\n" + ex.ToString() + "\n\nSkipping field!"));
                    }
                }
                else if (isCollection)
                {
                    var col = new SerializedCollectionData(fieldData.fieldInfo.FieldType, xElement);
                    col.ParseAndLoadData(filename, errors, parameters);
                    fields.Add(elementName, col);

                    // Collection override action?
                    var collectionOverrideAttrib = xElement.Attribute(XMLSerializer.AttributeCollectionOverrideAction);
                    if (!ReferenceEquals(collectionOverrideAttrib, null))
                    {
                        collectionOverrideActions.Set(elementName, (CollectionOverrideAction)Enum.Parse(typeof(CollectionOverrideAction), collectionOverrideAttrib.Value));
                    }
                }
                else
                {
                    // root reference?
                    if (fieldData.isSerializableRoot)
                    {
                        fields.Add(elementName, new SerializedRootObjectReference()
                        {
                            identifier = xElement.Value as string
                        });
                    }
                    else
                    {
                        // Determine which type to serialize
                        var    targetType = fieldData.serializableTypeCache;
                        string typeName   = fieldData.fieldInfo.Name;

                        // Check if element explicitly overwrites the type to support polymorphism
                        // The field type might be some base class type and the xml overwrites this type with a class extending from the base
                        var classAttrib = xElement.Attribute(XMLSerializer.AttributeType);
                        if (!ReferenceEquals(classAttrib, null))
                        {
                            targetType = SerializerCache.GetSerializableTypeCacheFor(classAttrib.Value, parameters.standardNamespace);
                            typeName   = classAttrib.Value;
                        }

                        // Field not serializable?
                        if (!SerializerValidation.DataFieldSerializerValid(parameters, xElement, targetType, typeName, elementName, filename, errors))
                        {
                            continue;
                        }

                        // Resolve field name type
                        var d = new SerializedData(targetType, xElement as XElement);
                        d.LoadFieldsFromXML(filename, errors, parameters);
                        fields.Add(elementName, d);
                    }
                }
            }
        }