Example #1
0
        private int MarkupProcess(int startOffset, int cutOffIndex)
        {
            int count     = 0;
            int offset    = startOffset;
            int character = 0;

            characterList.Clear();
            PageData pageData = new PageData();

            pageData.StartOffset = offset;

            // If the markup was previously open, the markup tag should be attached to the front.
            string tag = "";

            foreach (TagData data in tagList)
            {
                tag += "<" + data.TagName + data.AttributeName + ">";
            }
            pageData.PreviousTag = tag;


            bool isTag = false;

            while ((character = stream.Read()) != -1)
            {
                offset++;
                characterList.Add((char)character);

                TagData tagData = new TagData();
                isTag = false;
                if (LESS_THAN == character) // '<'
                {
                    isTag = IsTag(tagData, ref offset);
                }

                if (isTag)
                {
                    if (tagData.IsEndTag)
                    {
                        int lastIndex = tagList.Count;
                        tagList.RemoveAt(lastIndex - 1);
                    }
                    else
                    {
                        tagList.Add(tagData);
                    }
                }
                else
                {
                    count++;
                }
                if (count >= cutOffIndex)
                {
                    break;
                }
            }

            // If the markup was previously open, you should attach the label tag.
            tag = "";
            foreach (TagData data in tagList)
            {
                tag = "</" + data.TagName + ">" + tag;
            }
            pageData.EndTag = tag;

            pageData.EndOffset = offset;
            pageList.Add(pageData);

            if (character == -1)
            {
                offset = -1;
            }
            return(offset);
        }
Example #2
0
        private bool IsTag(TagData tag, ref int offset)
        {
            List <char> tagChaList = new List <char>();

            bool isTag           = false;
            bool isQuotationOpen = false;
            bool attributesFound = false;

            tag.IsEndTag = false;
            bool isPreviousLessThan = true;
            bool isPreviousSlash    = false;

            int character;

            tag.TagName       = "";
            tag.AttributeName = "";
            // SkipWhiteSpace(ref offset);
            while ((!isTag) && ((character = stream.Read()) != -1))
            {
                offset++;
                characterList.Add((char)character);
                if (!isQuotationOpen && (SLASH == character)) // '/'
                {
                    if (isPreviousLessThan)
                    {
                        tag.IsEndTag = true;
                    }
                    else
                    {
                        // if the tag has a '/' it may be an end tag.
                        isPreviousSlash = true;
                    }
                    isPreviousLessThan = false;
                    // SkipWhiteSpace(ref offset);
                }
                else if (GREATER_THAN == character) // '>'
                {
                    isTag = true;
                    if (isPreviousSlash)
                    {
                        tag.IsEndTag = true;
                    }

                    if (!attributesFound)
                    {
                        tag.TagName = new String(tagChaList.ToArray());
                    }
                    else
                    {
                        tag.AttributeName = new String(tagChaList.ToArray());
                    }

                    isPreviousSlash    = false;
                    isPreviousLessThan = false;
                }
                else if (QUOTATION_MARK == character)
                {
                    tagChaList.Add((char)character);
                    isQuotationOpen = !isQuotationOpen;

                    isPreviousSlash    = false;
                    isPreviousLessThan = false;
                }
                else if (WHITE_SPACE >= character || EQUAL == character) // ' ', '='
                {
                    // Let's save tag name.
                    if (!attributesFound)
                    {
                        tag.TagName = new String(tagChaList.ToArray());
                        tagChaList.Clear();
                    }
                    tagChaList.Add((char)character);
                    // If the tag contains white spaces then it may have attributes.
                    if (!isQuotationOpen)
                    {
                        attributesFound = true;
                    }
                }
                else
                {
                    tagChaList.Add((char)character);
                    isPreviousSlash    = false;
                    isPreviousLessThan = false;
                }
            }
            return(isTag);
        }