Пример #1
0
        public override void Parse(XElement definition)
        {
            ChildrenAreUnique = TryParseBool(definition, "ChildrenAreUnique");
            Collapse          = TryParseBool(definition, "Collapse");
            Seperator         = definition.Attribute("Seperator")?.Value;
            if (Seperator == null)
            {
                Seperator = ",";
            }

            MinCount = TryParseInt(definition, "MinCount", 0);
            MaxCount = TryParseInt(definition, "MaxCount", int.MaxValue);

            var currentGroup = "Items";

            var childDefs = definition.Nodes();

            foreach (var childDef in childDefs)
            {
                if (childDef is XComment)
                {
                    currentGroup = (childDef as XComment).Value;
                }
                else if (childDef is XElement)
                {
                    var xel = childDef as XElement;
                    if (xel.Name == "Attributes" || xel.Name == "AdditionalDefs")
                    {
                        continue;
                    }

                    var cdef = new CollectionChildDefinition();
                    cdef.Parse(xel);

                    ChildDefinitions.Add(cdef);
                    Keys.Add(new Tuple <CollectionChildDefinition, string>(cdef, currentGroup));
                }
            }

            if (ChildDefinitions.Count == 0)
            {
                throw new Exception("No child definitions in collection '" + Name + "'!");
            }

            var addEls = definition.Element("AdditionalDefs");

            if (addEls != null)
            {
                foreach (var addEl in addEls.Elements())
                {
                    var addDef = LoadDefinition(addEl);
                    AdditionalDefs.Add(addDef);
                }
            }
        }
Пример #2
0
        public override DataItem LoadData(XElement element, UndoRedoManager undoRedo)
        {
            var item = new CollectionItem(this, undoRedo);

            if (Collapse && ChildDefinitions.Count == 1 && ChildDefinitions[0].WrappedDefinition is PrimitiveDataDefinition)
            {
                var primDef = ChildDefinitions[0].WrappedDefinition as PrimitiveDataDefinition;
                var split   = element.Value.Split(new string[] { Seperator }, StringSplitOptions.None);
                foreach (var s in split)
                {
                    var child = primDef.LoadFromString(s, undoRedo);
                    var citem = ChildDefinitions[0].CreateData(undoRedo) as CollectionChildItem;
                    citem.WrappedItem = child;

                    item.Children.Add(citem);

                    if (item.Children.Count == MaxCount)
                    {
                        break;
                    }
                }
            }
            else
            {
                var uncreatedAdds = AdditionalDefs.ToList();

                foreach (var el in element.Elements())
                {
                    var prev = el.PreviousNode as XComment;
                    if (prev != null)
                    {
                        var comment = new CommentDefinition().LoadData(prev, undoRedo);
                        item.Children.Add(comment);
                    }

                    var cdef = ChildDefinitions.FirstOrDefault(e => e.Name == el.Name);
                    if (cdef != null)
                    {
                        var child = cdef.LoadData(el, undoRedo);
                        item.Children.Add(child);
                    }
                    else
                    {
                        var def = AdditionalDefs.FirstOrDefault(e => e.Name == el.Name);
                        if (def != null)
                        {
                            var child = def.LoadData(el, undoRedo);
                            item.Children.Insert(0, child);

                            uncreatedAdds.Remove(def);
                        }
                        else if (ChildDefinitions.Count == 1)
                        {
                            var child = ChildDefinitions[0].LoadData(el, undoRedo);
                            item.Children.Add(child);
                        }
                        else
                        {
                            throw new Exception("Unable to find def for '" + el.Name + "' in collection '" + Name + "'!");
                        }
                    }
                }

                foreach (var def in uncreatedAdds)
                {
                    var child = def.CreateData(undoRedo);
                    item.Children.Insert(0, child);
                }

                if (element.LastNode is XComment)
                {
                    var comment = new CommentDefinition().LoadData(element.LastNode as XComment, undoRedo);
                    item.Children.Add(comment);
                }
            }

            foreach (var att in Attributes)
            {
                var      el      = element.Attribute(att.Name);
                DataItem attItem = null;

                if (el != null)
                {
                    attItem = att.LoadData(new XElement(el.Name, el.Value.ToString()), undoRedo);
                }
                else
                {
                    attItem = att.CreateData(undoRedo);
                }
                item.Attributes.Add(attItem);
            }

            return(item);
        }
Пример #3
0
        public override void Parse(XElement definition)
        {
            ChildrenAreUnique = TryParseBool(definition, "ChildrenAreUnique");
            Collapse          = TryParseBool(definition, "Collapse");
            Seperator         = definition.Attribute("Seperator")?.Value;
            if (Seperator == null)
            {
                Seperator = ",";
            }

            MinCount = TryParseInt(definition, "MinCount", 0);
            MaxCount = TryParseInt(definition, "MaxCount", int.MaxValue);

            DefKey = definition.Attribute("DefKey")?.Value?.ToString();
            var keyString = definition.Attribute("Keys")?.Value?.ToString();

            if (!string.IsNullOrWhiteSpace(keyString))
            {
                if (!keyString.Contains('('))
                {
                    DefKeys.AddRange(keyString.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(e => new Tuple <string, string>(e.Trim(), "Type")));
                }
                else
                {
                    var categories = keyString.Split(new char[] { ')' }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (var categoryString in categories)
                    {
                        var split    = categoryString.Split('(');
                        var category = split[0].Trim();
                        if (category.StartsWith(","))
                        {
                            category = category.Substring(1);
                        }
                        DefKeys.AddRange(split[1].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(e => new Tuple <string, string>(e.Trim(), category)));
                    }
                }
            }

            var currentGroup = "Items";

            var childDefs = definition.Nodes();

            foreach (var childDef in childDefs)
            {
                if (childDef is XComment)
                {
                    currentGroup = (childDef as XComment).Value;
                }
                else if (childDef is XElement)
                {
                    var xel = childDef as XElement;
                    if (xel.Name == "Attributes" || xel.Name == "AdditionalDefs")
                    {
                        continue;
                    }

                    var cdef = new CollectionChildDefinition();
                    cdef.Parse(xel);

                    ChildDefinitions.Add(cdef);
                    Keys.Add(new Tuple <CollectionChildDefinition, string>(cdef, currentGroup));
                }
            }

            if (ChildDefinitions.Count == 0 && DefKey == null && DefKeys.Count == 0)
            {
                throw new Exception("No child definitions in collection '" + Name + "'!");
            }

            var addEls = definition.Element("AdditionalDefs");

            if (addEls != null)
            {
                foreach (var addEl in addEls.Elements())
                {
                    var addDef = LoadDefinition(addEl);
                    AdditionalDefs.Add(addDef);
                }
            }
        }