Example #1
0
        /// <summary>
        /// Merges the current value set with that of the supplied object.
        /// </summary>
        /// <remarks>The supplied object is considered the parent, and values in the
        /// callee's value set must override those of the supplied object.
        /// </remarks>
        /// <param name="parent">The parent object to merge with</param>
        /// <returns>The result of the merge operation</returns>
        /// <exception cref="ArgumentNullException">If the supplied parent is <code>null</code></exception>
        /// <exception cref="InvalidOperationException">If merging is not enabled for this instance,
        /// (i.e. <code>MergeEnabled</code> equals <code>false</code>.</exception>
        public object Merge(object parent)
        {
            if (!this.mergeEnabled)
            {
                throw new InvalidOperationException(
                          "Not allowed to merge when the 'MergeEnabled' property is set to 'false'");
            }
            if (parent == null)
            {
                return(this);
            }
            NameValueCollection pDict = parent as NameValueCollection;

            if (pDict == null)
            {
                throw new InvalidOperationException("Cannot merge with object of type [" + parent.GetType() + "]");
            }
            NameValueCollection merged = new ManagedNameValueCollection();

            foreach (string s in pDict.AllKeys)
            {
                merged[s] = pDict.Get(s);
            }
            foreach (string s in this.AllKeys)
            {
                merged[s] = this.Get(s);
            }
            return(merged);
        }
 public void MergeEmptyChild()
 {
     ManagedNameValueCollection parent = new ManagedNameValueCollection();
     parent.Add("one", "one");
     parent.Add("two", "two");
     ManagedNameValueCollection child = new ManagedNameValueCollection();
     child.MergeEnabled = true;
     NameValueCollection mergedMap = (NameValueCollection)child.Merge(parent);
     Assert.AreEqual(2, mergedMap.Count);
 }
 public void MergeSunnyDay()
 {
     ManagedNameValueCollection parent = new ManagedNameValueCollection();
     parent.Add("one", "one");
     parent.Add("two", "two");
     ManagedNameValueCollection child = new ManagedNameValueCollection();
     child.Add("three", "three");
     child.MergeEnabled = true;
     NameValueCollection mergedList = (NameValueCollection)child.Merge(parent);
     Assert.AreEqual(3, mergedList.Count);
 }
        public object GetObject(IObjectDefinitionService objectDefinitionService)
        {
            ManagedNameValueCollection nameValueCollection = new ManagedNameValueCollection();

            foreach (var nameValueCollectionAction in _nameValueCollectionActions)
            {
                nameValueCollectionAction(nameValueCollection, objectDefinitionService);
            }

            return nameValueCollection;
        }
 public void MergeChildValueOverrideTheParents()
 {
     ManagedNameValueCollection parent = new ManagedNameValueCollection();
     parent.Add("one", "one");
     parent.Add("two", "two");
     ManagedNameValueCollection child = new ManagedNameValueCollection();
     child.Add("one", "fork");
     child.MergeEnabled = true;
     NameValueCollection mergedMap = (NameValueCollection)child.Merge(parent);
     Assert.AreEqual(2, mergedMap.Count);
     Assert.AreEqual("fork", mergedMap["one"]);
 }
        /// <summary>
        /// Gets a name value collection mapping definition.
        /// </summary>
        /// <param name="nameValueEle">
        /// The element describing the name value collection mapping definition.
        /// </param>
        /// <param name="name">
        /// The name of the object (definition) associated with the
        /// name value collection mapping definition.
        /// </param>
        /// <param name="parserContext">the context carrying parsing state information</param>
        /// <returns>The name value collection definition.</returns>
        protected NameValueCollection ParseNameValueCollectionElement(XmlElement nameValueEle, string name, ParserContext parserContext)
        {
            ManagedNameValueCollection nvc = new ManagedNameValueCollection();
            nvc.MergeEnabled = ParseMergeAttribute(nameValueEle, parserContext.ParserHelper);

            XmlNodeList addElements = nameValueEle.GetElementsByTagName(ObjectDefinitionConstants.AddElement);
            foreach (XmlElement addElement in addElements)
            {
                string key = GetAttributeValue(addElement, ObjectDefinitionConstants.KeyAttribute);
                string value = GetAttributeValue(addElement, ObjectDefinitionConstants.ValueAttribute);
                string delimiters = GetAttributeValue(addElement, ObjectDefinitionConstants.DelimitersAttribute);

                if (StringUtils.HasText(delimiters))
                {
                    string[] values = value.Split(delimiters.ToCharArray());
                    foreach (string v in values)
                    {
                        nvc.Add(key, v);
                    }
                }
                else
                {
                    nvc.Add(key, value);
                }
            }
            return nvc;
        }
 public void MergeWithNonCompatibleParentType()
 {
     ManagedNameValueCollection child = new ManagedNameValueCollection();
     child.MergeEnabled = true;
     Assert.Throws<InvalidOperationException>(() => child.Merge("hello"));
 }
 public void MergeNotAllowedWhenMergeNotEnabled()
 {
     ManagedNameValueCollection child = new ManagedNameValueCollection();
     Assert.Throws<InvalidOperationException>(() => child.Merge(null), "Not allowed to merge when the 'MergeEnabled' property is set to 'false'");
 }
 public void MergeWithNullParent()
 {
     ManagedNameValueCollection child = new ManagedNameValueCollection();
     child.MergeEnabled = true;
     Assert.AreSame(child, child.Merge(null));
 }
 public void MergeWithNonCompatibleParentType()
 {
     ManagedNameValueCollection child = new ManagedNameValueCollection();
     child.MergeEnabled = true;
     child.Merge("hello");
 }
 public void MergeNotAllowedWhenMergeNotEnabled()
 {
     ManagedNameValueCollection child = new ManagedNameValueCollection();
     child.Merge(null);
 }
 /// <summary>
 /// Merges the current value set with that of the supplied object.
 /// </summary>
 /// <remarks>The supplied object is considered the parent, and values in the 
 /// callee's value set must override those of the supplied object.
 /// </remarks>
 /// <param name="parent">The parent object to merge with</param>
 /// <returns>The result of the merge operation</returns>
 /// <exception cref="ArgumentNullException">If the supplied parent is <code>null</code></exception>
 /// <exception cref="InvalidOperationException">If merging is not enabled for this instance,
 /// (i.e. <code>MergeEnabled</code> equals <code>false</code>.</exception>
 public object Merge(object parent)
 {
     if (!this.mergeEnabled)
     {
         throw new InvalidOperationException(
             "Not allowed to merge when the 'MergeEnabled' property is set to 'false'");
     }
     if (parent == null)
     {
         return this;
     }
     NameValueCollection pDict = parent as NameValueCollection;
     if (pDict == null)
     {
         throw new InvalidOperationException("Cannot merge with object of type [" + parent.GetType() + "]");
     }
     NameValueCollection merged = new ManagedNameValueCollection();
     foreach (string s in pDict.AllKeys)
     {
         merged[s] = pDict.Get(s);
     }
     foreach (string s in this.AllKeys)
     {
         merged[s] = this.Get(s);
     }
     return merged;
 }