예제 #1
0
        /// <summary>
        /// Method ImplementedClosingTag
        /// Implements the logic associated with closing a tag.
        /// </summary>
        private void ImplementedClosingTag()
        {
            var tagWithoutValues = new string(StackWithTags.Peek().TakeWhile(x => x != ' ').ToArray());

            // Determines if the top of the stack matches the closing tag.
            if (parsingElement.ToString() != tagWithoutValues)
            {
                throw new Exception("Incorrectly closed tags.");
            }
            Result.CloseTag();
            parsingElement.Clear();
            // Remove a closed tag from the stack.
            StackWithTags.Pop();
        }
예제 #2
0
        /// <summary>
        /// Method ParsingXml
        /// Parsing a string, if the XML file is compiled
        /// correctly, returns the result of the parsing.
        /// </summary>
        private void ParsingXml()
        {
            Result.XmlResult.Add("{");
            for (var i = 0; i < XmlString.Length; i++)
            {
                SkipComment(ref i);

                // Skip character if it is a space or a line break.
                if (((XmlString[i] == ' ') && (!flagsOfTheState.TagFlag && !flagsOfTheState.ArgumentFlag)) || (XmlString[i] == '\n'))
                {
                    continue;
                }

                if (XmlString[i] == '<')
                {
                    // Check for opening tag one more time.
                    if (flagsOfTheState.TagFlag)
                    {
                        throw new Exception("Incorrect brackets.");
                    }

                    // If there is a ready argument, then write it down.
                    if (parsingElement.ToString() != string.Empty)
                    {
                        Result.CreateArg(parsingElement.ToString());
                    }
                    parsingElement.Clear();
                    GetTypeOfTag(XmlString, flagsOfTheState, ref i);

                    continue;
                }


                if (XmlString[i] == '>')
                {
                    // Check for closing only open tag.
                    if (!flagsOfTheState.TagFlag)
                    {
                        throw new Exception("Incorrect brackets.");
                    }

                    SkipDoctype();

                    // Check for XML declaration at the beginning.
                    if (!flagsOfTheState.XmlFlag)
                    {
                        ImplementDeclarationTag();
                        flagsOfTheState.DisableParsingTag();
                        continue;
                    }

                    // If it is a closing tag, it checks for consistency with the tags in the stack.
                    if (flagsOfTheState.ClosingTagFlag)
                    {
                        ImplementedClosingTag();

                        flagsOfTheState.DisableParsingTag();
                        continue;
                    }

                    // If this is an empty tag. (< ... />)
                    if (XmlString[i - 1] == '/')
                    {
                        Result.CreateEmptyArg(parsingElement.ToString());
                        parsingElement.Clear();
                        flagsOfTheState.DisableParsingTag();
                        continue;
                    }

                    if (parsingElement.ToString() != string.Empty)
                    {
                        StackWithTags.Push(parsingElement.ToString());
                        Result.OpenTag();
                        parsingElement.Clear();
                    }
                    flagsOfTheState.DisableParsingTag();

                    continue;
                }

                if (!flagsOfTheState.TagFlag)
                {
                    flagsOfTheState.ArgumentFlag = true;
                }

                parsingElement.Append(XmlString[i]);
            }

            Result.XmlResult.Add("}");
            if (StackWithTags.Count != 0)
            {
                throw new Exception("Incorrectly closed tags.");
            }
        }