public void Process(
            XmlReader xmlReader,
            StringBuilder output,
            ElementProcessContext elementProcessContext)
        {
            if (elementProcessContext.Current.IsPreservingSpace)
            {
                output.Append("</").Append(xmlReader.Name).Append(">");
            }
            else if (elementProcessContext.Current.IsSignificantWhiteSpace && !output.IsNewLine())
            {
                output.Append("</").Append(xmlReader.Name).Append(">");
            }
            else if ((elementProcessContext.Current.ContentType == ContentTypeEnum.None) &&
                     this.options.RemoveEndingTagOfEmptyElement)
            {
                // Shrink the current element, if it has no content.
                // E.g., <Element>  </Element> => <Element />
                output = output.TrimEnd(' ', '\t', '\r', '\n');

                int bracketIndex = output.LastIndexOf('>');
                output.Insert(bracketIndex, '/');

                if ((output[bracketIndex - 1] != '\t') &&
                    (output[bracketIndex - 1] != ' ') &&
                    this.options.SpaceBeforeClosingSlash)
                {
                    output.Insert(bracketIndex, ' ');
                }
            }
            else if ((elementProcessContext.Current.ContentType == ContentTypeEnum.SingleLineTextOnly) &&
                     !elementProcessContext.Current.IsMultlineStartTag)
            {
                int bracketIndex = output.LastIndexOf('>');

                string text = output.Substring((bracketIndex + 1), (output.Length - bracketIndex - 1)).Trim();

                output.Length = (bracketIndex + 1);
                output.Append(text).Append("</").Append(xmlReader.Name).Append(">");
            }
            else
            {
                string currentIndentString = this.indentService.GetIndentString(xmlReader.Depth);

                if (!output.IsNewLine())
                {
                    output.Append(Environment.NewLine);
                }

                output.Append(currentIndentString).Append("</").Append(xmlReader.Name).Append(">");
            }

            elementProcessContext.Pop();
        }
        public void Process(
            XmlReader xmlReader, 
            StringBuilder output,
            ElementProcessContext elementProcessContext)
        {
            if (elementProcessContext.Current.IsPreservingSpace)
            {
                output.Append("</").Append(xmlReader.Name).Append(">");
            }
            else if (elementProcessContext.Current.IsSignificantWhiteSpace && !output.IsNewLine())
            {
                output.Append("</").Append(xmlReader.Name).Append(">");
            }
            else if ((elementProcessContext.Current.ContentType == ContentTypeEnum.None)
                && this.options.RemoveEndingTagOfEmptyElement)
            {
                // Shrink the current element, if it has no content.
                // E.g., <Element>  </Element> => <Element />
                output = output.TrimEnd(' ', '\t', '\r', '\n');

                int bracketIndex = output.LastIndexOf('>');
                output.Insert(bracketIndex, '/');

                if ((output[bracketIndex - 1] != '\t') 
                    && (output[bracketIndex - 1] != ' ')
                    && this.options.SpaceBeforeClosingSlash)
                {
                    output.Insert(bracketIndex, ' ');
                }
            }
            else if ((elementProcessContext.Current.ContentType == ContentTypeEnum.SingleLineTextOnly)
                && !elementProcessContext.Current.IsMultlineStartTag)
            {
                int bracketIndex = output.LastIndexOf('>');

                string text = output.Substring((bracketIndex + 1), (output.Length - bracketIndex - 1)).Trim();

                output.Length = (bracketIndex + 1);
                output.Append(text).Append("</").Append(xmlReader.Name).Append(">");
            }
            else
            {
                string currentIndentString = this.indentService.GetIndentString(xmlReader.Depth);

                if (!output.IsNewLine())
                {
                    output.Append(Environment.NewLine);
                }

                output.Append(currentIndentString).Append("</").Append(xmlReader.Name).Append(">");
            }

            elementProcessContext.Pop();
        }
Beispiel #3
0
        public void Process(XmlReader xmlReader, StringBuilder output, ElementProcessContext elementProcessContext)
        {
            elementProcessContext.UpdateParentElementProcessStatus(ContentTypeEnum.Mixed);

            var elementName = xmlReader.Name;

            elementProcessContext.Push(
                new ElementProcessStatus
            {
                Parent             = elementProcessContext.Current,
                Name               = elementName,
                ContentType        = ContentTypeEnum.None,
                IsMultlineStartTag = false,
                IsPreservingSpace  = elementProcessContext.Current.IsPreservingSpace
            });

            var currentIndentString       = this.indentService.GetIndentString(xmlReader.Depth);
            var attributeIndetationString = this.GetAttributeIndetationString(xmlReader);

            // Calculate how element should be indented
            if (!elementProcessContext.Current.IsPreservingSpace)
            {
                // "Run" get special treatment to try to preserve spacing. Use xml:space='preserve' to make sure!
                if (elementName.Equals("Run"))
                {
                    elementProcessContext.Current.Parent.IsSignificantWhiteSpace = true;
                    if ((output.Length == 0) || output.IsNewLine())
                    {
                        output.Append(currentIndentString);
                    }
                }
                else
                {
                    elementProcessContext.Current.Parent.IsSignificantWhiteSpace = false;
                    if ((output.Length == 0) || output.IsNewLine())
                    {
                        output.Append(currentIndentString);
                    }
                    else
                    {
                        output.Append(Environment.NewLine).Append(currentIndentString);
                    }
                }
            }

            // Output the element itself.
            output.Append('<').Append(elementName);

            bool isEmptyElement = xmlReader.IsEmptyElement;

            if (xmlReader.HasAttributes)
            {
                bool isNoLineBreakElement = this.IsNoLineBreakElement(elementName);
                this.ProcessAttributes(
                    xmlReader,
                    output,
                    elementProcessContext,
                    isNoLineBreakElement,
                    attributeIndetationString);
            }

            // Determine if to put ending bracket on new line.
            bool putEndingBracketOnNewLine = (this.options.PutEndingBracketOnNewLine &&
                                              elementProcessContext.Current.IsMultlineStartTag);

            if (putEndingBracketOnNewLine)
            {
                // Indent ending bracket just like an attribute.
                output.Append(Environment.NewLine).Append(attributeIndetationString);
            }

            if (isEmptyElement)
            {
                if (!putEndingBracketOnNewLine && this.options.SpaceBeforeClosingSlash)
                {
                    output.Append(' ');
                }

                output.Append("/>");

                // Self closing element. Remember to pop element context.
                elementProcessContext.Pop();
            }
            else
            {
                output.Append(">");
            }
        }