Exemplo n.º 1
0
        /// <summary>
        /// As GetItem, but creates the item if it does not exist
        /// </summary>
        public XDom GetOrCreateItem(string aTitle)
        {
            XDom r = GetItem(aTitle);

            if (r == null)
            {
                r        = new XDom();
                r.Name   = aTitle;
                r.Parent = this;
                this.Contents.Add(r);
            }
            return(r);
        }
Exemplo n.º 2
0
        public XDom this[string title]
        {
            get
            {
                XDom result = GetItem(title);

                if (result == null)
                {
                    throw new InvalidOperationException($"Expected to find {title} in {Name}.");
                }

                return(result);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Creates a new CDom object from a heirarchial-INI file
        /// </summary>
        private static XDom NewFromIndentedHierarchy(string aFileName)
        {
            XDom rootNode = new XDom();

            string[]    fc          = System.IO.File.ReadAllLines(aFileName);
            Stack <int> indentStack = new Stack <int>();

            indentStack.Push(0);
            int  currentIndent = 0;
            XDom previousNode  = null;
            XDom levelNode     = rootNode;

            foreach (string ll in fc)
            {
                string l = "";
                int    i = FindIndent(ll, ref l);
                if (l.Length != 0)
                {
                    if (l.StartsWith("="))
                    {
                        // Append
                        previousNode.Value += "\n" + l.Substring(1);
                    }
                    else if (i > currentIndent)
                    {
                        // Indent
                        indentStack.Push(i);
                        levelNode = previousNode;
                    }
                    else if (i < currentIndent)
                    {
                        // Unindent
                        while (i < indentStack.Peek())
                        {
                            indentStack.Pop();
                            levelNode = levelNode.Parent;
                        }
                    }

                    previousNode  = NewFromIniLine(l, levelNode);
                    currentIndent = i;
                }
            }

            return(rootNode);
        }
Exemplo n.º 4
0
        public void Read(string fileName)
        {
            XDom root = XDom.LoadFile(fileName, XDom.EDomType.Xml);

            if (root.Name != "doc")
            {
                throw new FormatException($"Expected the XML documentation file {{{fileName}}} to have a root named doc, but it is named {{{root.Name}}}.");
            }

            XDom members = root["members"];

            foreach (XDom member in members.GetItems("member"))
            {
                XDom name    = member["name"];
                XDom summary = member["summary"];

                _documentation.Add(name.Value, summary.Value);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Creates a new CDom object from an INI-style line ("x=y")
        /// </summary>
        private static XDom NewFromIniLine(string aLine, XDom aParent)
        {
            XDom r = new XDom();

            r.Parent = aParent;
            if (r.Parent != null)
            {
                r.Parent.Contents.Add(r);
            }
            if (aLine.Contains("="))
            {
                r.Name  = aLine.Substring(0, aLine.IndexOf('=')).Trim();
                r.Value = aLine.Substring(aLine.IndexOf('=') + 1).Trim();
            }
            else
            {
                r.Name  = aLine.Trim();
                r.Value = "";
            }
            return(r);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Creates a new CDom object from an XML file
        /// </summary>
        private static XDom NewFromXml(string aFileName)
        {
            // Settings
            XmlReaderSettings xrs = new XmlReaderSettings();

            xrs.IgnoreComments   = true;
            xrs.IgnoreWhitespace = true;
            xrs.CloseInput       = true;
            xrs.CheckCharacters  = true;
            xrs.ProhibitDtd      = false;

            // Open file
            XmlReader reader = XmlReader.Create(aFileName, xrs);

            // Create root node
            XDom rootNode    = null;
            XDom currentNode = null;

            // Read nodes
            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                case XmlNodeType.Element:
                {
                    XDom newNode = new XDom();
                    newNode.Name = reader.Name;
                    if (rootNode == null)
                    {
                        rootNode = newNode;
                    }
                    else
                    {
                        currentNode.Contents.Add(newNode);
                    }
                    newNode.Parent = currentNode;

                    if (reader.HasAttributes)
                    {
                        while (reader.MoveToNextAttribute())
                        {
                            XDom newAttrNode = new XDom();
                            newAttrNode.Name       = reader.Name;
                            newAttrNode.Value      = reader.Value;
                            newAttrNode.Attributes = EDomAttributes.Attribute;
                            newNode.Contents.Add(newAttrNode);
                        }

                        reader.MoveToElement();
                    }

                    if (!reader.IsEmptyElement)
                    {
                        currentNode = newNode;
                    }
                }

                break;

                case XmlNodeType.EndElement:
                    currentNode = currentNode.Parent;
                    break;

                case XmlNodeType.Text:
                    currentNode.Value += RemoveWhiteSpace(reader.Value);
                    break;

                case XmlNodeType.Attribute:
                {
                    XDom newNode = new XDom();
                    newNode.Name       = reader.Name;
                    newNode.Value      = reader.Value;
                    newNode.Attributes = EDomAttributes.Attribute;
                    currentNode.Contents.Add(newNode);
                }
                break;

                case XmlNodeType.XmlDeclaration:
                    break;

                default:
                    throw new Exception("Unknown XML node: " + reader.NodeType.ToString());
                } // switch
            }     // while

            reader.Close();

            return(rootNode);
        }