Example #1
0
        protected void RemoveCollectionItems(IXmlNode parentNode, XmlReferenceManager references, object value)
        {
            var collection = value as ICollectionProjection;

            if (collection != null)
            {
                collection.Clear();
                return;
            }

            var itemType    = clrType.GetCollectionItemType();
            var accessor    = GetCollectionAccessor(itemType);
            var cursor      = accessor.SelectCollectionItems(parentNode, true);
            var isReference = IsReference;

            var items = value as IEnumerable;

            if (items != null)
            {
                foreach (var item in items)
                {
                    if (!cursor.MoveNext())
                    {
                        break;
                    }
                    if (isReference)
                    {
                        references.OnAssigningNull(cursor, item);
                    }
                }
            }

            cursor.Reset();
            cursor.RemoveAllNext();
        }
        public void UnionWith(XmlReferenceManager other)
        {
            var visited = null as HashSet <Entry>;

            foreach (var otherEntry in other.entriesByValue)
            {
                Entry thisEntry;
                if (entriesByValue.TryGetValue(otherEntry.Key, out thisEntry))
                {
                    if (visited == null)
                    {
                        visited = new HashSet <Entry>(ReferenceEqualityComparer <Entry> .Instance);
                    }
                    else if (visited.Contains(thisEntry))
                    {
                        continue;
                    }
                    visited.Add(thisEntry);

                    foreach (var otherValue in otherEntry.Value.Values)
                    {
                        var otherTarget = otherValue.Value.Target;
                        if (otherTarget == null ||
                            otherTarget == otherEntry.Key ||
                            entriesByValue.ContainsKey(otherTarget))
                        {
                            continue;
                        }
                        AddValueCore(thisEntry, otherValue.Type, otherTarget, false);
                    }
                }
            }
        }
Example #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="XmlAdapter"/> class
        /// that represents a child object in a larger object graph.
        /// </summary>
        /// <param name="node"></param>
        /// <param name="references"></param>
        public XmlAdapter(IXmlNode node, XmlReferenceManager references)
        {
            if (node == null)
            {
                throw Error.ArgumentNull("node");
            }
            if (references == null)
            {
                throw Error.ArgumentNull("references");
            }

            this.node       = node;
            this.references = references;
        }
Example #4
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()));
            }
        }
Example #5
0
        private void InitializePrimary(DictionaryAdapterMeta meta, IDictionaryAdapter dictionaryAdapter)
        {
            RequireXmlMeta(meta);
            primaryXmlMeta = meta.GetXmlMeta();

            if (node == null)
            {
                node = GetBaseNode();
            }
            if (!node.IsReal)
            {
                node.Realized += HandleNodeRealized;
            }

            if (references == null)
            {
                references = new XmlReferenceManager(node, DefaultXmlReferenceFormat.Instance);
            }
            InitializeReference(this);
        }
Example #6
0
        public object GetValue(IXmlNode node, IDictionaryAdapter parentObject, XmlReferenceManager references, bool nodeExists, bool orStub)
        {
            object value;

            if ((nodeExists || orStub) && IsReference)
            {
                value = null;
                object token;

                if (references.OnGetStarting(ref node, ref value, out token))
                {
                    value = GetValueCore(node, parentObject, nodeExists, orStub);
                    references.OnGetCompleted(node, value, token);
                }
            }
            else
            {
                value = GetValueCore(node, parentObject, nodeExists, orStub);
            }
            return(value);
        }
Example #7
0
 public override void SetPropertyValue(IXmlNode parentNode, IDictionaryAdapter parentObject, XmlReferenceManager references, object oldValue, ref object value)
 {
     if (SelectsNodes)
     {
         base.SetPropertyValue(parentNode, parentObject, references, oldValue, ref value);
     }
     else
     {
         throw Error.XPathNotCreatable(path);
     }
 }
Example #8
0
 private object GetDefaultPropertyValue(IXmlNode parentNode, IDictionaryAdapter parentObject, XmlReferenceManager references, bool orStub)
 {
     return(defaultAccessor != null
                         ? defaultAccessor.GetPropertyValue(parentNode, parentObject, references, orStub)
                         : null);
 }
Example #9
0
 private object GetPropertyValueCore(IXmlNode parentNode, IDictionaryAdapter parentObject, XmlReferenceManager references, bool orStub)
 {
     return(SelectsNodes
                         ? base.GetPropertyValue(parentNode, parentObject, references, orStub)
                         : Evaluate(parentNode));
 }
Example #10
0
 public override object GetPropertyValue(IXmlNode parentNode, IDictionaryAdapter parentObject, XmlReferenceManager references, bool orStub)
 {
     return(GetPropertyValueCore(parentNode, parentObject, references, orStub)
            ?? GetDefaultPropertyValue(parentNode, parentObject, references, orStub));
 }
 public override void SetValue(IXmlCursor cursor, IDictionaryAdapter parentObject, XmlReferenceManager references,
                               bool hasCurrent, object oldValue, ref object newValue)
 {
     if (newValue == null && IsCollection)
     {
         base.RemoveCollectionItems(cursor, references, oldValue);
     }
     else
     {
         base.SetValue(cursor, parentObject, references, hasCurrent, oldValue, ref newValue);
     }
 }
Example #12
0
        public void GetCollectionItems(IXmlNode parentNode, IDictionaryAdapter parentObject, XmlReferenceManager references, IList values)
        {
            var cursor = SelectCollectionItems(parentNode, false);

            while (cursor.MoveNext())
            {
                object value;

                if (IsReference)
                {
                    IXmlNode node = cursor;
                    value = null;
                    object token;

                    if (references.OnGetStarting(ref node, ref value, out token))
                    {
                        value = serializer.GetValue(node, parentObject, this);
                        references.OnGetCompleted(node, value, token);
                    }
                }
                else
                {
                    value = serializer.GetValue(cursor, parentObject, this);
                }
                values.Add(value);
            }
        }
Example #13
0
        public virtual void SetValue(IXmlCursor cursor, IDictionaryAdapter parentObject, XmlReferenceManager references,
                                     bool hasCurrent, object oldValue, ref object newValue)
        {
            var hasValue    = null != newValue;
            var isNillable  = this.IsNillable;
            var isReference = this.IsReference;

            var clrType = hasValue
                                ? newValue.GetComponentType()
                                : this.clrType;

            if (hasValue || isNillable)
            {
                if (hasCurrent)
                {
                    Coerce(cursor, clrType, !hasValue && cursor.IsAttribute);                     // TODO: Refactor. (NB: && isNillable is emplied)
                }
                else
                {
                    cursor.Create(clrType);
                }
            }
            else if (!hasCurrent)
            {
                // No node exists + no value to assign + and not nillable = no work to do
                return;
            }

            object token = null;

            if (isReference)
            {
                if (!references.OnAssigningValue(cursor, oldValue, ref newValue, out token))
                {
                    return;
                }
            }

            var givenValue = newValue;

            if (hasValue)
            {
                serializer.SetValue(cursor, parentObject, this, oldValue, ref newValue);
            }
            else if (isNillable)
            {
                cursor.IsNil = true;
            }
            else
            {
                cursor.Remove(); cursor.RemoveAllNext();
            }

            if (isReference)
            {
                references.OnAssignedValue(cursor, givenValue, newValue, token);
            }
        }
Example #14
0
        public virtual void SetPropertyValue(IXmlNode parentNode, IDictionaryAdapter parentObject, XmlReferenceManager references,
                                             object oldValue, ref object value)
        {
            var cursor = IsCollection
                                ? SelectCollectionNode(parentNode, true)
                                : SelectPropertyNode(parentNode, true);

            SetValue(cursor, parentObject, references, cursor.MoveNext(), oldValue, ref value);
        }
Example #15
0
        public virtual object GetPropertyValue(IXmlNode parentNode, IDictionaryAdapter parentObject, XmlReferenceManager references, bool orStub)
        {
            if (orStub)
            {
                orStub &= serializer.CanGetStub;
            }

            var cursor = IsCollection
                                ? SelectCollectionNode(parentNode, orStub)
                                : SelectPropertyNode(parentNode, orStub);

            return(GetValue(cursor, parentObject, references, cursor.MoveNext(), orStub));
        }