Пример #1
0
        private static int InsertEndElement(TextBuffer buffer, int offset, Stack stack, ref int depth)
        {
            TextIter insertAt, applyStart, applyEnd;
            TagStart tagStart = (TagStart)stack.Pop();
            string   suffix   = '#' + depth.ToString();

                #if DEBUG
            Console.WriteLine("Element: {0}, End: {1}", tagStart.Tag.Name, offset);
                #endif

            if (((DocumentTag)tagStart.Tag).IsEditable)
            {
                if (tagStart.Start + 1 == offset)
                {
                    offset = DocumentUtils.AddStub(buffer, offset, "To be added test", suffix);
                }

                insertAt = buffer.GetIterAtOffset(offset);
                buffer.Insert(ref insertAt, "]");
                offset += 1;
            }
            else if (tagStart.Start == offset)
            {
                offset = DocumentUtils.AddPaddingEmpty(buffer, offset, suffix);
            }

            applyStart = buffer.GetIterAtOffset(tagStart.Start);
            applyEnd   = buffer.GetIterAtOffset(offset);
            buffer.ApplyTag(tagStart.Tag, applyStart, applyEnd);
            offset = FormatEnd(buffer, offset, suffix, tagStart.Name);
            depth--;

                #if DEBUG
            Console.WriteLine("Applied: {0}, Start: {1}, End: {2}", tagStart.Tag.Name, tagStart.Start, offset);
                #endif

            // Padding between tag regions
            suffix = "#" + depth;
            offset = DocumentUtils.AddPadding(buffer, offset, suffix);

            return(offset);
        }
Пример #2
0
        private static int InsertStartElement(TextBuffer buffer, int offset, XmlTextReader xmlReader, Stack stack, ref int depth, ref int count)
        {
            string           elementName = xmlReader.Name;
            string           suffix = String.Empty;
            DocumentTagTable tagTable = (DocumentTagTable)buffer.TagTable;
            bool             emptyElement = xmlReader.IsEmptyElement;
            bool             isDynamic = DocumentTagTable.IsDynamic(elementName);
            TextIter         insertAt, applyStart, applyEnd;

            depth++;

            // We define a suffix so each dynamic tag has an unique name.
            // Suffix has format: #{depth level}
            if (isDynamic)
            {
                suffix = "#" + depth;
            }

            // We add any needed string to give format to the document.
            offset = FormatStart(buffer, offset, suffix, elementName);

            TagStart tagStart = new TagStart();

            tagStart.Start = offset;
            tagStart.Name  = elementName;

            // We first lookup the tag name, if the element is dynamic, we can
            // have three scenarios.
            // 1) The tag is not in the table: So we create it in the spot.
            // 2) Tag is in table but it priority is wrong: We created a new
            // dynamic tag with an extra suffix. Format #{depth level}.{count}
            // 3) Tag is in table with right priority: We reuse it and we don't
            // create a new dymamic tag.
            tagStart.Tag = tagTable.Lookup(elementName + suffix);
            if (isDynamic && tagStart.Tag == null)
            {
                tagStart.Tag = tagTable.CreateDynamicTag(elementName + suffix);
            }
            else if (isDynamic && tagStart.Tag != null && tagStart.Tag.Priority < ((TagStart)stack.Peek()).Tag.Priority)
            {
                suffix      += "." + count;
                tagStart.Tag = tagTable.CreateDynamicTag(elementName + suffix);
                count++;
            }

                #if DEBUG
            try {
                Console.WriteLine("Element: {0} Start: {1}", tagStart.Tag.Name, tagStart.Start);
            } catch (NullReferenceException) {
                Console.WriteLine("Error: Missing {0} element", xmlReader.Name);
                Environment.Exit(1);
            }
                #endif

            // If element has attributes we have to get them and deserialize them.
            if (xmlReader.HasAttributes)
            {
                offset = DeserializeAttributes(buffer, offset, xmlReader, suffix);
            }

            // Special case when an elment is empty.
            // Case A: If element is editable a string stub is inserted to allow edition.
            // Case B: If element is not editable then a padding is inserted to handle
            // TextTag behaviour in which zero length ranges are lost.
            if (emptyElement)
            {
                if (((DocumentTag)tagStart.Tag).IsEditable)
                {
                    insertAt = buffer.GetIterAtOffset(offset);
                    buffer.Insert(ref insertAt, "[");
                    offset += 1;

                    offset = DocumentUtils.AddStub(buffer, offset, "Click to Add Documentation", suffix);

                    insertAt = buffer.GetIterAtOffset(offset);
                    buffer.Insert(ref insertAt, "]");
                    offset += 1;
                }
                else
                {
                    offset = DocumentUtils.AddPaddingEmpty(buffer, offset, suffix);
                }

                applyStart = buffer.GetIterAtOffset(tagStart.Start);
                applyEnd   = buffer.GetIterAtOffset(offset);
                buffer.ApplyTag(tagStart.Tag, applyStart, applyEnd);
                offset = FormatEnd(buffer, offset, suffix, elementName);

                // Padding between tag regions
                offset = DocumentUtils.AddPadding(buffer, offset, suffix);
                depth--;

                        #if DEBUG
                Console.WriteLine("Empty Element: {0}, Start: {1}, End: {2}", tagStart.Tag.Name, tagStart.Start, offset);
                        #endif
            }
            else
            {
                stack.Push(tagStart);

                if (((DocumentTag)tagStart.Tag).IsEditable)
                {
                    insertAt = buffer.GetIterAtOffset(offset);
                    buffer.Insert(ref insertAt, "[");
                    offset += 1;
                }
            }

            return(offset);
        }