Example #1
0
        internal override ProjectElement LoadChildElement(string name)
        {
            var item = ContainingProject.CreateItemElement(name);

            AppendChild(item);
            return(item);
        }
        internal override ProjectElement LoadChildElement(XmlReader reader)
        {
            var item = ContainingProject.CreateItemElement(reader.LocalName);

            AppendChild(item);
            return(item);
        }
        /// <summary>
        /// Convenience method that picks a location based on a heuristic:
        /// Adds a new item ordered by include.
        /// Metadata may be null, indicating no metadata.
        /// </summary>
        public ProjectItemElement AddItem(string itemType, string include, IEnumerable <KeyValuePair <string, string> > metadata)
        {
            ErrorUtilities.VerifyThrowArgumentLength(itemType, "itemType");
            ErrorUtilities.VerifyThrowArgumentLength(include, "include");

            // If there are no items, or it turns out that there are only items with
            // item types that sort earlier, then we should go after the last child
            ProjectElement reference = LastChild;

            foreach (ProjectItemElement item in Items)
            {
                // If it's the same item type, and
                if (MSBuildNameIgnoreCaseComparer.Default.Equals(itemType, item.ItemType))
                {
                    // the include sorts after us,
                    if (String.Compare(include, item.Include, StringComparison.OrdinalIgnoreCase) < 0)
                    {
                        // then insert before it (ie. after the previous sibling)
                        reference = item.PreviousSibling;
                        break;
                    }

                    // Otherwise go to the next item
                    continue;
                }

                // If it's an item type that sorts after us,
                if (String.Compare(itemType, item.ItemType, StringComparison.OrdinalIgnoreCase) < 0)
                {
                    // then insert before it (ie. after the previous sibling)
                    reference = item.PreviousSibling;
                    break;
                }
            }

            ProjectItemElement newItem = ContainingProject.CreateItemElement(itemType, include);

            // If reference is null, this will prepend
            InsertAfterChild(newItem, reference);

            if (metadata != null)
            {
                foreach (KeyValuePair <string, string> metadatum in metadata)
                {
                    newItem.AddMetadata(metadatum.Key, metadatum.Value);
                }
            }

            return(newItem);
        }
Example #4
0
        public ProjectItemElement AddItem(string itemType, string include,
                                          IEnumerable <KeyValuePair <string, string> > metadata)
        {
            var item = ContainingProject.CreateItemElement(itemType, include);

            if (metadata != null)
            {
                foreach (var data in metadata)
                {
                    item.AddMetadata(data.Key, data.Value);
                }
            }
            var lastChild = LastChild;

            foreach (var existingItem in Items)
            {
                var compare = string.Compare(item.ItemType, existingItem.ItemType,
                                             StringComparison.OrdinalIgnoreCase);

                if (compare == 0)
                {
                    if (string.Compare(item.Include, existingItem.Include,
                                       StringComparison.OrdinalIgnoreCase) >= 0)
                    {
                        continue;
                    }
                    lastChild = existingItem.PreviousSibling;
                    break;
                }

                if (compare < 0)
                {
                    lastChild = existingItem.PreviousSibling;
                    break;
                }
            }
            InsertAfterChild(item, lastChild);
            return(item);
        }