InitializeFromText() public method

Reads the tag at the specified position in the specified word and separator array.
public InitializeFromText ( ParserBase parser, string text, int position, TagBase parentTag ) : int
parser ParserBase
text string
position int
parentTag TagBase /// The parent tag of this tag. This argument is used to determine /// the end of the tag (it can be the end of the parent tag). ///
return int
Exemplo n.º 1
0
        /// <summary>
        /// Parses the specified block of a text.
        /// </summary>
        /// <returns>
        /// Returns the end position of the parsed block.
        /// </returns>
        protected int ParseBlock(XmlNode parentNode, TagBase parentTag, string text, int position)
        {
            #region Check the arguments

            if (parentNode == null)
            {
                throw new ArgumentNullException("parentNode");
            }

            CheckTextAndPositionArguments(text, position);

            #endregion

            while (position < text.Length)
            {
                if (IsSkipWhiteSpace)
                {
                    SkipWhiteSpace(text, ref position);
                }

                if (position == text.Length)
                {
                    break;
                }

                #region Read the parent tag ending

                if (parentTag != null)
                {
                    int myParentTagEndingEndPosition = parentTag.MatchEnd(text, position);
                    if (myParentTagEndingEndPosition >= 0)
                    {
                        position = myParentTagEndingEndPosition;
                        return(position);
                    }
                }

                #endregion

                Type myTagType = IsTag(text, position);
                if (myTagType != null)
                {
                    #region Read a tag

                    #region Create the tag class instance

                    TagBase myTag = Activator.CreateInstance(myTagType) as TagBase;
                    position = myTag.InitializeFromText(this, text, position, parentTag);

                    #endregion

                    #region Create an xml node for the tag

                    XmlNode myTagXmlNode = CreateTagXmlNode(myTag);
                    parentNode.AppendChild(myTagXmlNode);

                    #endregion

                    if (myTag.HasContents)
                    {
                        position = ParseBlock(myTagXmlNode, myTag, text, position);
                    }

                    #endregion
                }
                else
                {
                    #region Read text

                    string myText = ReadWordOrSeparator(text, ref position, !IsSkipWhiteSpace);
                    parentNode.AppendChild(CreateTextXmlNode(myText));

                    #endregion
                }
            }

            if (parentTag != null && !parentTag.CanTerminateByStringEnd)
            {
                throw new Exception("Invalid format");
            }

            return(position);
        }