public void Can_Create_Child_Node_With_Name_Not_Matching_Property_Name()
        {
            var serializer = new XomDataConverter();
            var data = new XomNodeData
            {
                NodeType = NodeA.XomNode,
                ChildNodes = new KeyValuePair<string, XomNodeData>[]
                {
                    new KeyValuePair<string, XomNodeData>("C1", new XomNodeData { NodeType = NodeB.XomNode })
                }
            };

            var result = (NodeA)serializer.ConvertToXmlObject(data);
            Assert.IsNotNull(result.Child1, "Child1 was incorrectly null");
        }
Beispiel #2
0
        private void AttachAttributeDataToXmlObject(XomNodeData data, object target)
        {
            if (data.AttributeData == null)
                return;

            var sourceProperties = data.AttributeData
                                       .GetType()
                                       .GetProperties();

            var targetProperties = target.GetType()
                                         .GetProperties();

            foreach (var sourceProperty in sourceProperties)
            {
                // If the name of the property doesn't have a matching XomNodeAttribute
                // we don't care about it
                var xomAttribute = data.NodeType
                                       .Attributes
                                       .FirstOrDefault(x => x.Name == sourceProperty.Name);
                if (xomAttribute == null)
                    continue;

                // If the target type doesn't have the property for the attribute, we don't care about it either
                var targetProperty = targetProperties.FirstOrDefault(x => x.Name == xomAttribute.PropertyName);
                if (targetProperty == null)
                    continue;

                var sourcePropertyType = sourceProperty.PropertyType;
                if (sourcePropertyType.IsGenericType && sourcePropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
                    sourcePropertyType = Nullable.GetUnderlyingType(sourcePropertyType);

                if (targetProperty.PropertyType != sourcePropertyType)
                    throw new XomDataSerializationPropertyTypeMismatchException(sourceProperty.PropertyType, sourceProperty.Name,
                                                                                targetProperty.PropertyType, targetProperty.Name);

                // If the data value is null, don't call setValue() as the property would already be null.
                // If this isn't done, the properties will have default(T) set as its value, and
                // the ValueSpecified values will be triggered to be true.
                if (sourceProperty.GetValue(data.AttributeData) != null)
                    targetProperty.SetValue(target, sourceProperty.GetValue(data.AttributeData));
            }
        }
        public void Can_Serialize_IEnumerable_Data_Nodes_Into_Xml_Object_Collection()
        {
            var serializer = new XomDataConverter();
            var innerData1 = new XomNodeData
            {
                NodeType = NodeB.XomNode
            };

            var data = new XomNodeData
            {
                NodeType = NodeA.XomNode,
                ChildNodes = new KeyValuePair<string, XomNodeData>[]
                {
                    new KeyValuePair<string, XomNodeData>("CollectionChildren", innerData1),
                    new KeyValuePair<string, XomNodeData>("CollectionChildren", innerData1)
                }
            };

            var result = (NodeA)serializer.ConvertToXmlObject(data);
            Assert.AreEqual(2, result.CollectionChildren.Count, "Incorrect number of elements in the collection children node");
        }
Beispiel #4
0
        public static UiNode FromXomNodeData(XomNodeData nodeData, string name)
        {
            if (nodeData == null)
                throw new ArgumentNullException("nodeData");

            var node = new UiNode(nodeData.NodeType, name);
            node.AttributeData = nodeData.AttributeData;
            node.UpdateNodeName();
            foreach (var child in nodeData.ChildNodes)
            {
                var childNodeName = nodeData.NodeType
                                            .Children
                                            .Where(x => x.PropertyName == child.Key)
                                            .SelectMany(x => x.AvailableNodes)
                                            .Where(x => x.Value.Type == child.Value.NodeType.Type)
                                            .Select(x => x.Key)
                                            .First();

                var childNode = UiNode.FromXomNodeData(child.Value, childNodeName);
                node._children.Add(childNode);
            }

            return node;
        }
Beispiel #5
0
 public object ConvertToXmlObject(XomNodeData data)
 {
     return GenerateXmlObjectForNodeData(data, null);
 }
Beispiel #6
0
        private XomNodeData GenerateXomNodeDataObject(object xmlObject, IEnumerable<XomNode> nodeTypes)
        {
            var nodeType = nodeTypes.First(x => x.Type == xmlObject.GetType());
            var attributeType = XomAttributeTypeGenerator.GenerateType(nodeType.Attributes);
            var attributeObject = Activator.CreateInstance(attributeType);

            // Migrate the attribute data from the xmlObject to the XomNodeData.AttributeData
            foreach (var attribute in nodeType.Attributes)
            {
                var sourceValue = xmlObject.GetType()
                                           .GetProperties()
                                           .Where(x => x.Name == attribute.PropertyName)
                                           .Select(x => x.GetValue(xmlObject, null))
                                           .First();

                var targetPropertyInfo = attributeType.GetProperties()
                                                      .Where(x => x.Name == attribute.Name)
                                                      .First();

                targetPropertyInfo.SetValue(attributeObject, sourceValue);
            }

            var result = new XomNodeData
            {
                NodeType = nodeType,
                AttributeData = attributeObject,
            };

            var childNodes = new List<KeyValuePair<string, XomNodeData>>();

            // Add child elements
            if (nodeType.Children != null)
            {
                foreach (var child in nodeType.Children)
                {
                    var propertyInfo = xmlObject.GetType()
                                                .GetProperties()
                                                .Where(x => x.Name == child.PropertyName)
                                                .First();

                    var propertyValue = propertyInfo.GetValue(xmlObject, null);
                    if (propertyValue != null)
                    {
                        // If the property's value is an enumerable, create XomNodeData objects
                        // for the objects in the collection
                        if (propertyValue.GetType().IsGenericType)
                        {
                            var enumerable = propertyValue as IEnumerable;
                            if (enumerable != null)
                            {
                                foreach (object item in enumerable)
                                {
                                    var nodeData = GenerateXomNodeDataObject(item, nodeTypes);
                                    var pair = new KeyValuePair<string, XomNodeData>(child.PropertyName, nodeData);
                                    childNodes.Add(pair);
                                }
                            }
                        }
                        else
                        {
                            var nodeData = GenerateXomNodeDataObject(propertyValue, nodeTypes);
                            var pair = new KeyValuePair<string, XomNodeData>(child.PropertyName, nodeData);
                            childNodes.Add(pair);
                        }
                    }
                }
            }

            result.ChildNodes = childNodes.ToArray();

            return result;
        }
Beispiel #7
0
        private object GenerateXmlObjectForNodeData(XomNodeData data, DataSerializerParentDetails parentDetails)
        {
            if (data == null)
                throw new ArgumentNullException("data");

            if (data.NodeType == null)
                throw new ArgumentException("Data object has a null xom node type");

            if (data.NodeType.Type == null)
                throw new ArgumentException("Data object's xom node type has a null Type value");

            var instance = Activator.CreateInstance(data.NodeType.Type);
            AttachAttributeDataToXmlObject(data, instance);

            // Attach the instance to the parent
            AttachNodeToParent(parentDetails, instance);

            // Serialize children
            if (data.ChildNodes != null)
            {
                foreach (var childDataPair in data.ChildNodes)
                {
                    var details = new DataSerializerParentDetails
                    {
                        ParentObject = instance,
                        ParentXomNode = data.NodeType,
                        ChildNodeName = childDataPair.Key
                    };

                    GenerateXmlObjectForNodeData(childDataPair.Value, details);
                }
            }

            return instance;
        }
        public void Converts_Attribute_By_Xml_Name()
        {
            var serializer = new XomDataConverter();
            var data = new XomNodeData
            {
                NodeType = NodeA.XomNode,
                AttributeData = new { Attribute2 = "Test" }
            };

            var result = (NodeA)serializer.ConvertToXmlObject(data);
            Assert.AreEqual("Test", result.Attr2, "Attr2's value was incorrect");
        }
        public void Can_Set_Nullable_Attribute_Source_To_Non_Nullable_Target()
        {
            const int testValue = 5;
            var serializer = new XomDataConverter();
            var data = new XomNodeData
            {
                NodeType = NodeA.XomNode,
                AttributeData = new { Attribute3 = (int?)testValue }
            };

            var result = (NodeA)serializer.ConvertToXmlObject(data);
            Assert.AreEqual(testValue, result.Attribute3, "Attribute3 had an incorrect value");
        }
        public void Nullable_Attribute_Source_That_Has_Null_Value_Is_Ignored()
        {
            var serializer = new XomDataConverter();
            var data = new XomNodeData
            {
                NodeType = NodeA.XomNode,
                AttributeData = new { Attribute3 = (int?)null }
            };

            var result = (NodeA)serializer.ConvertToXmlObject(data);
            Assert.AreEqual(0, result.Attribute3, "Attribute3 had an incorrect value");
            Assert.IsFalse(result.Attribute3Set, "Attribute3's value was set when it shouldn't have been");
        }
 public void Exception_Thrown_When_Data_Object_Has_Null_XomNode()
 {
     var data = new XomNodeData();
     var serializer = new XomDataConverter();
     serializer.ConvertToXmlObject(data);
 }
        public void Exception_Thrown_When_Attribute_Types_Dont_Match()
        {
            var serializer = new XomDataConverter();
            var data = new XomNodeData
            {
                NodeType = NodeA.XomNode,
                AttributeData = new { Attribute2 = 1 }
            };

            var result = (NodeA)serializer.ConvertToXmlObject(data);
        }
        public void Creates_Correct_XML_Serialization_Object()
        {
            var data = new XomNodeData
            {
                NodeType = new XomNode { Type = typeof(NodeA) }
            };
            var serializer = new XomDataConverter();

            object result = serializer.ConvertToXmlObject(data);

            Assert.IsNotNull(result, "Resulting object was null");
            Assert.IsInstanceOfType(result, typeof(NodeA), "Resulting object was an incorrect type");
        }