Ejemplo n.º 1
0
        /// <summary>
        /// Parses an AOML string into open, close and content nodes
        /// </summary>
        /// <param name="aoml">The aoml string to be parsed</param>
        public NodeCollection Parse(string aoml)
        {
            NodeCollection nodes = new NodeCollection();

            // Create regular expression instance

            if (this.regex == null)
            {
                this.regex = new Regex(
                    "[<]\\s*(?<closer>[/]?)\\s*" +
                    "(?<name>[a-z]+)" +
                    "(\\s+((?<attribute>[a-z_\\-]+)\\s*" +
                    "(=\\s*(" +
                    /**/ (Mode == ParserMode.Strict ? "" : "(?<value>[^\\s<>'\"]+)|") +
                    /**/ "[\"](?<value>[^\"]*)[\"]|" +
                    /**/ (Mode == ParserMode.Compatibility ? "['](?<value>[^'>]*)[']?)|" : "['](?<value>[^']*)['])|") +
                    /**/ "(?<value>)" +
                    ")?)\\s*)*" +
                    "\\s*(?<closed>[/]?)[>]",
                    RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Multiline);
            }

            // Some setup
            int regexCloser    = this.regex.GroupNumberFromName("closer");
            int regexName      = this.regex.GroupNumberFromName("name");
            int regexAttribute = this.regex.GroupNumberFromName("attribute");
            int regexValue     = this.regex.GroupNumberFromName("value");
            int regexClosed    = this.regex.GroupNumberFromName("closed");

            // Convert to unix line endings
            aoml = aoml.Replace("\r\n", "\n");
            // Parse AOML
            MatchCollection matches = this.regex.Matches(aoml);
            int             offset  = 0;

            foreach (Match match in matches)
            {
                // Extract content
                if (match.Index > offset)
                {
                    string content = aoml.Substring(offset, match.Index - offset);
                    AddContent(nodes, content, content);
                }
                offset = match.Index + match.Length;
                // Extract tag
                string raw  = match.Groups[0].Value;
                string name = match.Groups[regexName].Value;
                if (string.IsNullOrEmpty(name))
                {
                    // No name, let's assume it's just text
                    AddContent(nodes, raw, raw);
                    continue;
                }
                bool closer = !string.IsNullOrEmpty(match.Groups[regexCloser].Value);
                bool closed = !string.IsNullOrEmpty(match.Groups[regexClosed].Value);
                if (closer)
                {
                    // Closing tag
                    nodes.Add(new CloseNode(name, raw));
                }
                else
                {
                    // Opening tag
                    OpenNode node = new OpenNode(name, raw, closed);
                    // Add attributes
                    for (int i = 0; i < match.Groups[regexAttribute].Captures.Count; i++)
                    {
                        node.AddAttribute(
                            match.Groups[regexAttribute].Captures[i].Value,
                            match.Groups[regexValue].Captures[i].Value);
                    }
                    nodes.Add(node);
                }
            }
            // Remainder is content
            if (offset < aoml.Length)
            {
                string content = aoml.Substring(offset);
                AddContent(nodes, content, content);
            }
            return(nodes);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Balances out the stream of nodes to form a valid tree.
        /// This process will always try to resolve conflicts by introducing extra inline elements over block elements.
        /// </summary>
        public void Balance(NodeCollection nodes)
        {
            if (nodes == null)
            {
                throw new ArgumentNullException();
            }
            // Fill in missing elements
            List <string> nameStack = new List <string>();

            nodes.Reset();
            Node node = null;

            while ((node = nodes.Next()) != null)
            {
                switch (node.Type)
                {
                case NodeType.Open:
                    OpenNode openNode = (OpenNode)node;
                    if (openNode.Closed)
                    {
                        continue;
                    }
                    nameStack.Insert(0, openNode.Name);
                    break;

                case NodeType.Close:
                    CloseNode closeNode = (CloseNode)node;
                    if (!nameStack.Contains(closeNode.Name))
                    {
                        // Found CloseNode without OpenNode
                        // Fix by insert an OpenNode before it
                        nodes.InsertBefore(
                            closeNode,
                            new OpenNode(closeNode.Name, "", false));
                    }
                    nameStack.Remove(closeNode.Name);
                    break;
                }
            }
            while (nameStack.Count > 0)
            {
                // Found OpenNode without CloseNode
                // Fix by adding a CloseNode at the end of the stream
                string name = nameStack[0];
                nameStack.RemoveAt(0);
                nodes.Add(new CloseNode(name, ""));
            }
            // Resolve tree structure
            List <OpenNode> nodeStack = new List <OpenNode>();

            nodes.Reset();
            while ((node = nodes.Next()) != null)
            {
                switch (node.Type)
                {
                case NodeType.Open:
                    OpenNode on = (OpenNode)node;
                    if (on.Closed)
                    {
                        continue;
                    }
                    nodeStack.Insert(0, on);
                    break;

                case NodeType.Close:
                    // A bit of setup
                    CloseNode closeNode = (CloseNode)node;
                    bool      block     = blockElements.Contains(closeNode.Name);
                    // Find matching OpenNode
                    OpenNode openNode = null;
                    foreach (OpenNode o in nodeStack)
                    {
                        if (o.Name == closeNode.Name)
                        {
                            openNode = o;
                            break;
                        }
                    }
                    if (openNode == null)
                    {
                        throw new InvalidOperationException("Unable to find matching OpenNode to CloseNode");
                    }
                    // Handle incorrect balance
                    int offset = 0;
                    while (nodeStack[offset].Name != closeNode.Name)
                    {
                        OpenNode n = nodeStack[offset];
                        if (block)
                        {
                            nodes.InsertBefore(node, new CloseNode(n.Name, ""));
                            nodes.InsertAfter(node, n.Clone());
                            nodeStack.RemoveAt(offset);
                            offset--;
                        }
                        else
                        {
                            nodes.InsertBefore(n, closeNode.Clone());
                            nodes.InsertAfter(n, openNode.Clone());
                        }
                        offset++;
                    }
                    // All fixed
                    nodeStack.RemoveAt(offset);
                    break;
                }
            }
            // And we're done!
            nodes.Reset();
        }