Exemple #1
0
        public XmlCollectionAdapter(
            IXmlNode parentNode,
            IDictionaryAdapter parentObject,
            IXmlCollectionAccessor accessor)
        {
            items = new List <XmlCollectionItem <T> >();

            this.accessor     = accessor;
            this.cursor       = accessor.SelectCollectionItems(parentNode, true);
            this.parentNode   = parentNode;
            this.parentObject = parentObject;
            this.references   = XmlAdapter.For(parentObject).References;

            while (cursor.MoveNext())
            {
                items.Add(new XmlCollectionItem <T>(cursor.Save()));
            }
        }
Exemple #2
0
        public bool OnAssigningValue(IXmlNode node, object oldValue, ref object newValue, out object token)
        {
            if (newValue == oldValue && newValue != null)
            {
                token = null; return(false);
            }

            var oldEntry = OnReplacingValue(node, oldValue);

            if (newValue == null)
            {
                return(ShouldAssignmentProceed(oldEntry, null, token = null));
            }

            var type = newValue.GetComponentType();

            if (ShouldExclude(type))
            {
                return(ShouldAssignmentProceed(oldEntry, null, token = null));
            }

            var xmlAdapter = XmlAdapter.For(newValue, false);

            Entry newEntry;

            if (entriesByValue.TryGetValue(xmlAdapter ?? newValue, out newEntry))
            {
                // Value already present in graph; add reference
                TryGetCompatibleValue(newEntry, type, ref newValue);
                AddReference(node, newEntry);
                token = null;
            }
            else
            {
                // Value not present in graph; add as primary
                newEntry = oldEntry ?? new Entry(node);
                AddValue(newEntry, type, newValue, xmlAdapter);
                format.ClearIdentity(node);
                format.ClearReference(node);
                token = newEntry;
            }
            return(ShouldAssignmentProceed(oldEntry, newEntry, token));
        }
Exemple #3
0
        public static object CreateChildAdapter(
            this IDictionaryAdapter parent,
            Type type,
            XmlAdapter adapter,
            IDictionary dictionary
            )
        {
            if (null == dictionary)
            {
                dictionary = new Hashtable();
            }

            var descriptor = parent.Meta.CreateDescriptor();

            parent.This.Descriptor.CopyBehaviors(descriptor);
            descriptor.AddBehavior(adapter);

            return(parent.This.Factory.GetAdapter(type, dictionary, descriptor));
        }
Exemple #4
0
        public override void SetValue(
            IXmlNode node,
            IDictionaryAdapter parent,
            IXmlAccessor accessor,
            object oldValue,
            ref object value
            )
        {
            // Require a dictionary adapter
            var source = value as IDictionaryAdapter;

            if (source == null)
            {
                throw Error.NotDictionaryAdapter(nameof(value));
            }

            // Detect assignment of own value
            var sourceAdapter = XmlAdapter.For(source, false);

            if (sourceAdapter != null && node.PositionEquals(sourceAdapter.Node))
            {
                return;
            }

            // Create a fresh component
            var targetAdapter = new XmlAdapter(node.Save(), XmlAdapter.For(parent).References);

            if (sourceAdapter != null)
            {
                targetAdapter.References.UnionWith(sourceAdapter.References);
            }
            var component = (IDictionaryAdapter)parent.CreateChildAdapter(
                node.ClrType,
                targetAdapter
                );

            // Copy value onto fresh component
            source.CopyTo(component);
            value = component;
        }
Exemple #5
0
        public override void SetValue(IXmlNode node, IDictionaryAdapter parent, IXmlAccessor accessor, object oldValue, ref object value)
        {
            var source      = (Array)value;
            var target      = (Array)null;
            var originals   = (Array)oldValue;
            var itemType    = source.GetType().GetElementType();
            var subaccessor = accessor.GetCollectionAccessor(itemType);
            var cursor      = subaccessor.SelectCollectionItems(node, true);
            var serializer  = subaccessor.Serializer;
            var references  = XmlAdapter.For(parent).References;

            for (var i = 0; i < source.Length; i++)
            {
                var originalItem = GetItemSafe(originals, i);
                var providedItem = source.GetValue(i);
                var assignedItem = providedItem;

                subaccessor.SetValue(cursor, parent, references, cursor.MoveNext(), originalItem, ref assignedItem);

                if (target != null)
                {
                    target.SetValue(assignedItem, i);
                }
                else if (!Equals(assignedItem, providedItem))
                {
                    target = Array.CreateInstance(itemType, source.Length);
                    Array.Copy(source, target, i);
                    target.SetValue(assignedItem, i);
                }
            }

            cursor.RemoveAllNext();

            if (target != null)
            {
                value = target;
            }
        }
Exemple #6
0
        object IDictionaryCreateStrategy.Create(IDictionaryAdapter parent, Type type, IDictionary dictionary)
        {
            var adapter = new XmlAdapter(new XmlDocument());

            return(parent.CreateChildAdapter(type, adapter, dictionary));
        }
Exemple #7
0
        public override object GetValue(IXmlNode node, IDictionaryAdapter parent, IXmlAccessor accessor)
        {
            var adapter = new XmlAdapter(node.Save(), XmlAdapter.For(parent).References);

            return(parent.CreateChildAdapter(node.ClrType, adapter));
        }
Exemple #8
0
 public static object CreateChildAdapter(this IDictionaryAdapter parent, Type type, XmlAdapter adapter)
 {
     return(CreateChildAdapter(parent, type, adapter, null));
 }