Example #1
0
        private static TocNode InitCui(XmlReader reader)
        {
            var elementNames = new Stack <string>();

            elementNames.Push(reader.GetAttribute("type"));
            var curr = new TocNode()
            {
                _type = reader.GetAttribute("type"),
                _id   = reader.GetAttribute("id")
            };
            var allNodes = new Dictionary <string, TocNode>();

            allNodes[curr._id] = curr;

            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                case XmlNodeType.Element:
                    if (reader.LocalName == "Item")
                    {
                        elementNames.Push(reader.GetAttribute("type"));
                    }
                    else
                    {
                        elementNames.Push(reader.LocalName);
                    }
                    if (reader.LocalName == "Item")
                    {
                        curr = new TocNode()
                        {
                            _type = reader.GetAttribute("type"),
                            _id   = reader.GetAttribute("id")
                        };
                        allNodes[curr._id] = curr;
                    }
                    break;

                case XmlNodeType.EndElement:
                    if (elementNames.Count > 0)
                    {
                        elementNames.Pop();
                    }
                    break;

                case XmlNodeType.Text:
                case XmlNodeType.CDATA:
                    if (!string.IsNullOrEmpty(reader.Value))
                    {
                        switch (elementNames.Peek())
                        {
                        case "id":
                        case "itemtype":
                        case "section":
                            break; // Do nothing

                        case "image":
                            curr.Image = reader.Value;
                            break;

                        case "name":
                            curr.Name = reader.Value;
                            break;

                        case "label":
                            curr.Label = reader.Value;
                            break;

                        case "sort_order":
                            curr.SortOrder = int.Parse(reader.Value);
                            break;

                        case "additional_data":
                            using (var json = new Json.Embed.JsonTextReader(reader.Value))
                            {
                                foreach (var kvp in json.Flatten())
                                {
                                    switch (kvp.Key)
                                    {
                                    case "$.itemTypeId":
                                        curr._references.Add(new ItemRef("ItemType", kvp.Value?.ToString()));
                                        break;

                                    case "$.tocViewId":
                                        curr._references.Add(new ItemRef("TOC View", kvp.Value?.ToString()));
                                        break;

                                    case "$.tocAccessId":
                                    case "$.relatedTocAccessId":
                                        curr._references.Add(new ItemRef("TOC Access", kvp.Value?.ToString()));
                                        break;

                                    case "$.formId":
                                        curr._references.Add(new ItemRef("Form", kvp.Value?.ToString()));
                                        break;

                                    default:
                                        if (kvp.Value != null)
                                        {
                                            curr.AdditionalData[kvp.Key.Substring(2)] = kvp.Value;
                                        }
                                        break;
                                    }
                                }
                            }
                            break;

                        default:
                            curr.AdditionalData[elementNames.Peek()] = reader.Value;
                            break;
                        }
                    }
                    break;
                }
            }

            var root = new TocNode();

            foreach (var node in allNodes.Values)
            {
                if (node.AdditionalData.TryGetValue("parent_menu", out var parentId) &&
                    !string.IsNullOrEmpty(parentId?.ToString()) &&
                    allNodes.TryGetValue(parentId?.ToString(), out var parent))
                {
                    parent._children.Add(node);
                    node.AdditionalData.Remove("parent_menu");
                }
                else
                {
                    root._children.Add(node);
                }
            }

            return(root);
        }
Example #2
0
        private static TocNode InitMainItems(XmlReader reader)
        {
            var stack = new Stack <TocNode>();

            stack.Push(new TocNode());
            var elementNames = new Stack <string>();

            elementNames.Push(reader.GetAttribute("type"));

            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                case XmlNodeType.Element:
                    if (!reader.IsEmptyElement)
                    {
                        if (reader.LocalName == "Item")
                        {
                            elementNames.Push(reader.GetAttribute("type"));
                        }
                        else
                        {
                            elementNames.Push(reader.LocalName);
                        }
                    }
                    if (string.Equals(reader.GetAttribute("type"), "Tree Node", StringComparison.OrdinalIgnoreCase))
                    {
                        var newNode = new TocNode()
                        {
                            _type     = reader.GetAttribute("type"),
                            SortOrder = (stack.Peek()._children.Count + 1) * 10
                        };
                        stack.Peek()._children.Add(newNode);
                        stack.Push(newNode);
                    }
                    break;

                case XmlNodeType.EndElement:
                    if (elementNames.Count > 0)
                    {
                        var lastElem = elementNames.Pop();
                        if (string.Equals(lastElem, "Tree Node", StringComparison.OrdinalIgnoreCase))
                        {
                            if (stack.Count == 1)
                            {
                                return(stack.Pop());
                            }
                            else
                            {
                                stack.Pop();
                            }
                        }
                    }
                    break;

                case XmlNodeType.Text:
                case XmlNodeType.CDATA:
                    if (!string.IsNullOrEmpty(reader.Value))
                    {
                        switch (elementNames.Peek())
                        {
                        case "itemtype_id":
                            stack.Peek()._references.Add(new ItemRef("ItemType", reader.Value));
                            break;

                        case "saved_search_id":
                            stack.Peek()._references.Add(new ItemRef("SavedSearch", reader.Value));
                            break;

                        case "name":
                            stack.Peek().Name = reader.Value;
                            break;

                        case "label":
                            stack.Peek().Label = reader.Value;
                            break;

                        case "open_icon":
                            stack.Peek().Image = reader.Value;
                            break;

                        default:
                            stack.Peek().AdditionalData[elementNames.Peek()] = reader.Value;
                            break;
                        }
                    }
                    break;
                }
            }

            if (stack.Count == 1)
            {
                return(stack.Pop());
            }
            else
            {
                return(null);
            }
        }