AppendIndent() private method

private AppendIndent ( int linePosition, int indent, int depth, bool newLine ) : int
linePosition int
indent int
depth int
newLine bool
return int
Esempio n. 1
0
        // Format an Xml element to be written to the config file.
        // Params:
        //   xmlElement      - the element
        //   linePosition    - start position of the element
        //   indent          - indent for each depth
        //   skipFirstIndent - skip indent for the first element?
        //
        static internal string FormatXmlElement(string xmlElement, int linePosition, int indent, bool skipFirstIndent) {

            XmlParserContext context = new XmlParserContext(null, null, null, XmlSpace.Default, Encoding.Unicode);
            XmlTextReader reader = new XmlTextReader(xmlElement, XmlNodeType.Element, context);

            StringWriter stringWriter = new StringWriter(new StringBuilder(64), CultureInfo.InvariantCulture);
            XmlUtilWriter utilWriter = new XmlUtilWriter(stringWriter, false);

            // append newline before indent?
            bool newLine = false;

            // last node visited was text?
            bool lastWasText = false;

            // width of line from end of indentation
            int lineWidth;

            // length of the stringbuilder after last indent with newline
            int sbLengthLastNewLine = 0;

            while (reader.Read()) {
                XmlNodeType nodeType = reader.NodeType;

                if (lastWasText) {
                    utilWriter.Flush();
                    lineWidth = sbLengthLastNewLine - ((StringWriter)utilWriter.Writer).GetStringBuilder().Length;
                }
                else {
                    lineWidth = 0;
                }

                switch (nodeType) {
                    case XmlNodeType.CDATA:
                    case XmlNodeType.Element:
                    case XmlNodeType.EndElement:
                    case XmlNodeType.Comment:
                        // Do not indent if the last node was text - doing so would add whitespace
                        // that is included as part of the text.
                        if (!skipFirstIndent && !lastWasText) {
                            utilWriter.AppendIndent(linePosition, indent, reader.Depth, newLine);

                            if (newLine) {
                                utilWriter.Flush();
                                sbLengthLastNewLine = ((StringWriter)utilWriter.Writer).GetStringBuilder().Length;
                            }
                        }
                        break;

                    default:
                        break;
                }

                lastWasText = false;
                switch (nodeType) {
                    case XmlNodeType.Whitespace:
                        break;

                    case XmlNodeType.SignificantWhitespace:
                        utilWriter.Write(reader.Value);
                        break;

                    case XmlNodeType.CDATA:
                        utilWriter.AppendCData(reader.Value);
                        break;

                    case XmlNodeType.ProcessingInstruction:
                        utilWriter.AppendProcessingInstruction(reader.Name, reader.Value);
                        break;

                    case XmlNodeType.Comment:
                        utilWriter.AppendComment(reader.Value);
                        break;

                    case XmlNodeType.Text:
                        utilWriter.AppendEscapeTextString(reader.Value);
                        lastWasText = true;
                        break;

                    case XmlNodeType.Element:
                        {
                            // Write "<elem"
                            utilWriter.Write('<');
                            utilWriter.Write(reader.Name);

                            lineWidth += reader.Name.Length + 2;

                            int c = reader.AttributeCount;
                            for (int i = 0; i < c; i++) {
                                // Add new line if we've exceeded the line width
                                bool writeSpace;
                                if (lineWidth > MAX_LINE_WIDTH) {
                                    utilWriter.AppendIndent(linePosition, indent, reader.Depth - 1, true);
                                    lineWidth = indent;
                                    writeSpace = false;
                                    utilWriter.Flush();
                                    sbLengthLastNewLine = ((StringWriter)utilWriter.Writer).GetStringBuilder().Length;
                                }
                                else {
                                    writeSpace = true;
                                }

                                // Write the attribute
                                reader.MoveToNextAttribute();
                                utilWriter.Flush();
                                int startLength = ((StringWriter)utilWriter.Writer).GetStringBuilder().Length;
                                if (writeSpace) {
                                    utilWriter.AppendSpace();
                                }

                                utilWriter.Write(reader.Name);
                                utilWriter.Write('=');
                                utilWriter.AppendAttributeValue(reader);
                                utilWriter.Flush();
                                lineWidth += ((StringWriter)utilWriter.Writer).GetStringBuilder().Length - startLength;
                            }
                        }

                        // position reader back on element
                        reader.MoveToElement();

                        // write closing tag
                        if (reader.IsEmptyElement) {
                            utilWriter.Write(" />");
                        }
                        else {
                            utilWriter.Write('>');
                        }

                        break;

                    case XmlNodeType.EndElement:
                        utilWriter.Write("</");
                        utilWriter.Write(reader.Name);
                        utilWriter.Write('>');
                        break;

                    case XmlNodeType.EntityReference:
                        utilWriter.AppendEntityRef(reader.Name);
                        break;

                    // Ignore <?xml and <!DOCTYPE nodes
                    case XmlNodeType.XmlDeclaration:
                    case XmlNodeType.DocumentType:
                    default:
                        break;
                }

                // put each new element on a new line
                newLine = true;

                // do not skip any more indents
                skipFirstIndent = false;
            }

            utilWriter.Flush();
            string s = ((StringWriter)utilWriter.Writer).ToString();
            return s;
        }
        internal static string FormatXmlElement(string xmlElement, int linePosition, int indent, bool skipFirstIndent)
        {
            XmlParserContext context = new XmlParserContext(null, null, null, XmlSpace.Default, Encoding.Unicode);
            XmlTextReader reader = new XmlTextReader(xmlElement, XmlNodeType.Element, context);
            StringWriter writer = new StringWriter(new StringBuilder(0x40), CultureInfo.InvariantCulture);
            XmlUtilWriter writer2 = new XmlUtilWriter(writer, false);
            bool newLine = false;
            bool flag2 = false;
            int num2 = 0;
            while (reader.Read())
            {
                int num;
                int attributeCount;
                int num4;
                bool flag3;
                XmlNodeType nodeType = reader.NodeType;
                if (flag2)
                {
                    writer2.Flush();
                    num = num2 - ((StringWriter) writer2.Writer).GetStringBuilder().Length;
                }
                else
                {
                    num = 0;
                }
                XmlNodeType type2 = nodeType;
                if (type2 <= XmlNodeType.CDATA)
                {
                    switch (type2)
                    {
                        case XmlNodeType.Element:
                        case XmlNodeType.CDATA:
                            goto Label_0091;
                    }
                    goto Label_00CA;
                }
                if ((type2 != XmlNodeType.Comment) && (type2 != XmlNodeType.EndElement))
                {
                    goto Label_00CA;
                }
            Label_0091:
                if (!skipFirstIndent && !flag2)
                {
                    writer2.AppendIndent(linePosition, indent, reader.Depth, newLine);
                    if (newLine)
                    {
                        writer2.Flush();
                        num2 = ((StringWriter) writer2.Writer).GetStringBuilder().Length;
                    }
                }
            Label_00CA:
                flag2 = false;
                switch (nodeType)
                {
                    case XmlNodeType.Element:
                        writer2.Write('<');
                        writer2.Write(reader.Name);
                        num += reader.Name.Length + 2;
                        attributeCount = reader.AttributeCount;
                        num4 = 0;
                        goto Label_0274;

                    case XmlNodeType.Text:
                        writer2.AppendEscapeTextString(reader.Value);
                        flag2 = true;
                        goto Label_02D6;

                    case XmlNodeType.CDATA:
                        writer2.AppendCData(reader.Value);
                        goto Label_02D6;

                    case XmlNodeType.EntityReference:
                        writer2.AppendEntityRef(reader.Name);
                        goto Label_02D6;

                    case XmlNodeType.ProcessingInstruction:
                        writer2.AppendProcessingInstruction(reader.Name, reader.Value);
                        goto Label_02D6;

                    case XmlNodeType.Comment:
                        writer2.AppendComment(reader.Value);
                        goto Label_02D6;

                    case XmlNodeType.SignificantWhitespace:
                        writer2.Write(reader.Value);
                        goto Label_02D6;

                    case XmlNodeType.EndElement:
                        writer2.Write("</");
                        writer2.Write(reader.Name);
                        writer2.Write('>');
                        goto Label_02D6;

                    default:
                        goto Label_02D6;
                }
            Label_01BE:
                if (num > 60)
                {
                    writer2.AppendIndent(linePosition, indent, reader.Depth - 1, true);
                    num = indent;
                    flag3 = false;
                    writer2.Flush();
                    num2 = ((StringWriter) writer2.Writer).GetStringBuilder().Length;
                }
                else
                {
                    flag3 = true;
                }
                reader.MoveToNextAttribute();
                writer2.Flush();
                int length = ((StringWriter) writer2.Writer).GetStringBuilder().Length;
                if (flag3)
                {
                    writer2.AppendSpace();
                }
                writer2.Write(reader.Name);
                writer2.Write('=');
                writer2.AppendAttributeValue(reader);
                writer2.Flush();
                num += ((StringWriter) writer2.Writer).GetStringBuilder().Length - length;
                num4++;
            Label_0274:
                if (num4 < attributeCount)
                {
                    goto Label_01BE;
                }
                reader.MoveToElement();
                if (reader.IsEmptyElement)
                {
                    writer2.Write(" />");
                }
                else
                {
                    writer2.Write('>');
                }
            Label_02D6:
                newLine = true;
                skipFirstIndent = false;
            }
            writer2.Flush();
            return ((StringWriter) writer2.Writer).ToString();
        }
Esempio n. 3
0
        // Format an Xml element to be written to the config file.
        // Params:
        //   xmlElement      - the element
        //   linePosition    - start position of the element
        //   indent          - indent for each depth
        //   skipFirstIndent - skip indent for the first element?
        //
        static internal string FormatXmlElement(string xmlElement, int linePosition, int indent, bool skipFirstIndent)
        {
            XmlParserContext context = new XmlParserContext(null, null, null, XmlSpace.Default, Encoding.Unicode);
            XmlTextReader    reader  = new XmlTextReader(xmlElement, XmlNodeType.Element, context);

            StringWriter  stringWriter = new StringWriter(new StringBuilder(64), CultureInfo.InvariantCulture);
            XmlUtilWriter utilWriter   = new XmlUtilWriter(stringWriter, false);

            // append newline before indent?
            bool newLine = false;

            // last node visited was text?
            bool lastWasText = false;

            // width of line from end of indentation
            int lineWidth;

            // length of the stringbuilder after last indent with newline
            int sbLengthLastNewLine = 0;

            while (reader.Read())
            {
                XmlNodeType nodeType = reader.NodeType;

                if (lastWasText)
                {
                    utilWriter.Flush();
                    lineWidth = sbLengthLastNewLine - ((StringWriter)utilWriter.Writer).GetStringBuilder().Length;
                }
                else
                {
                    lineWidth = 0;
                }

                switch (nodeType)
                {
                case XmlNodeType.CDATA:
                case XmlNodeType.Element:
                case XmlNodeType.EndElement:
                case XmlNodeType.Comment:
                    // Do not indent if the last node was text - doing so would add whitespace
                    // that is included as part of the text.
                    if (!skipFirstIndent && !lastWasText)
                    {
                        utilWriter.AppendIndent(linePosition, indent, reader.Depth, newLine);

                        if (newLine)
                        {
                            utilWriter.Flush();
                            sbLengthLastNewLine = ((StringWriter)utilWriter.Writer).GetStringBuilder().Length;
                        }
                    }
                    break;

                default:
                    break;
                }

                lastWasText = false;
                switch (nodeType)
                {
                case XmlNodeType.Whitespace:
                    break;

                case XmlNodeType.SignificantWhitespace:
                    utilWriter.Write(reader.Value);
                    break;

                case XmlNodeType.CDATA:
                    utilWriter.AppendCData(reader.Value);
                    break;

                case XmlNodeType.ProcessingInstruction:
                    utilWriter.AppendProcessingInstruction(reader.Name, reader.Value);
                    break;

                case XmlNodeType.Comment:
                    utilWriter.AppendComment(reader.Value);
                    break;

                case XmlNodeType.Text:
                    utilWriter.AppendEscapeTextString(reader.Value);
                    lastWasText = true;
                    break;

                case XmlNodeType.Element:
                {
                    // Write "<elem"
                    utilWriter.Write('<');
                    utilWriter.Write(reader.Name);

                    lineWidth += reader.Name.Length + 2;

                    int c = reader.AttributeCount;
                    for (int i = 0; i < c; i++)
                    {
                        // Add new line if we've exceeded the line width
                        bool writeSpace;
                        if (lineWidth > MAX_LINE_WIDTH)
                        {
                            utilWriter.AppendIndent(linePosition, indent, reader.Depth - 1, true);
                            lineWidth  = indent;
                            writeSpace = false;
                            utilWriter.Flush();
                            sbLengthLastNewLine = ((StringWriter)utilWriter.Writer).GetStringBuilder().Length;
                        }
                        else
                        {
                            writeSpace = true;
                        }

                        // Write the attribute
                        reader.MoveToNextAttribute();
                        utilWriter.Flush();
                        int startLength = ((StringWriter)utilWriter.Writer).GetStringBuilder().Length;
                        if (writeSpace)
                        {
                            utilWriter.AppendSpace();
                        }

                        utilWriter.Write(reader.Name);
                        utilWriter.Write('=');
                        utilWriter.AppendAttributeValue(reader);
                        utilWriter.Flush();
                        lineWidth += ((StringWriter)utilWriter.Writer).GetStringBuilder().Length - startLength;
                    }
                }

                    // position reader back on element
                    reader.MoveToElement();

                    // write closing tag
                    if (reader.IsEmptyElement)
                    {
                        utilWriter.Write(" />");
                    }
                    else
                    {
                        utilWriter.Write('>');
                    }

                    break;

                case XmlNodeType.EndElement:
                    utilWriter.Write("</");
                    utilWriter.Write(reader.Name);
                    utilWriter.Write('>');
                    break;

                case XmlNodeType.EntityReference:
                    utilWriter.AppendEntityRef(reader.Name);
                    break;

                // Ignore <?xml and <!DOCTYPE nodes
                case XmlNodeType.XmlDeclaration:
                case XmlNodeType.DocumentType:
                default:
                    break;
                }

                // put each new element on a new line
                newLine = true;

                // do not skip any more indents
                skipFirstIndent = false;
            }

            utilWriter.Flush();
            string s = ((StringWriter)utilWriter.Writer).ToString();

            return(s);
        }
Esempio n. 4
0
        internal static string FormatXmlElement(string xmlElement, int linePosition, int indent, bool skipFirstIndent)
        {
            XmlParserContext context = new XmlParserContext(null, null, null, XmlSpace.Default, Encoding.Unicode);
            XmlTextReader    reader  = new XmlTextReader(xmlElement, XmlNodeType.Element, context);
            StringWriter     writer  = new StringWriter(new StringBuilder(0x40), CultureInfo.InvariantCulture);
            XmlUtilWriter    writer2 = new XmlUtilWriter(writer, false);
            bool             newLine = false;
            bool             flag2   = false;
            int num2 = 0;

            while (reader.Read())
            {
                int         num;
                int         attributeCount;
                int         num4;
                bool        flag3;
                XmlNodeType nodeType = reader.NodeType;
                if (flag2)
                {
                    writer2.Flush();
                    num = num2 - ((StringWriter)writer2.Writer).GetStringBuilder().Length;
                }
                else
                {
                    num = 0;
                }
                XmlNodeType type2 = nodeType;
                if (type2 <= XmlNodeType.CDATA)
                {
                    switch (type2)
                    {
                    case XmlNodeType.Element:
                    case XmlNodeType.CDATA:
                        goto Label_0091;
                    }
                    goto Label_00CA;
                }
                if ((type2 != XmlNodeType.Comment) && (type2 != XmlNodeType.EndElement))
                {
                    goto Label_00CA;
                }
Label_0091:
                if (!skipFirstIndent && !flag2)
                {
                    writer2.AppendIndent(linePosition, indent, reader.Depth, newLine);
                    if (newLine)
                    {
                        writer2.Flush();
                        num2 = ((StringWriter)writer2.Writer).GetStringBuilder().Length;
                    }
                }
Label_00CA:
                flag2 = false;
                switch (nodeType)
                {
                case XmlNodeType.Element:
                    writer2.Write('<');
                    writer2.Write(reader.Name);
                    num           += reader.Name.Length + 2;
                    attributeCount = reader.AttributeCount;
                    num4           = 0;
                    goto Label_0274;

                case XmlNodeType.Text:
                    writer2.AppendEscapeTextString(reader.Value);
                    flag2 = true;
                    goto Label_02D6;

                case XmlNodeType.CDATA:
                    writer2.AppendCData(reader.Value);
                    goto Label_02D6;

                case XmlNodeType.EntityReference:
                    writer2.AppendEntityRef(reader.Name);
                    goto Label_02D6;

                case XmlNodeType.ProcessingInstruction:
                    writer2.AppendProcessingInstruction(reader.Name, reader.Value);
                    goto Label_02D6;

                case XmlNodeType.Comment:
                    writer2.AppendComment(reader.Value);
                    goto Label_02D6;

                case XmlNodeType.SignificantWhitespace:
                    writer2.Write(reader.Value);
                    goto Label_02D6;

                case XmlNodeType.EndElement:
                    writer2.Write("</");
                    writer2.Write(reader.Name);
                    writer2.Write('>');
                    goto Label_02D6;

                default:
                    goto Label_02D6;
                }
Label_01BE:
                if (num > 60)
                {
                    writer2.AppendIndent(linePosition, indent, reader.Depth - 1, true);
                    num   = indent;
                    flag3 = false;
                    writer2.Flush();
                    num2 = ((StringWriter)writer2.Writer).GetStringBuilder().Length;
                }
                else
                {
                    flag3 = true;
                }
                reader.MoveToNextAttribute();
                writer2.Flush();
                int length = ((StringWriter)writer2.Writer).GetStringBuilder().Length;
                if (flag3)
                {
                    writer2.AppendSpace();
                }
                writer2.Write(reader.Name);
                writer2.Write('=');
                writer2.AppendAttributeValue(reader);
                writer2.Flush();
                num += ((StringWriter)writer2.Writer).GetStringBuilder().Length - length;
                num4++;
Label_0274:
                if (num4 < attributeCount)
                {
                    goto Label_01BE;
                }
                reader.MoveToElement();
                if (reader.IsEmptyElement)
                {
                    writer2.Write(" />");
                }
                else
                {
                    writer2.Write('>');
                }
Label_02D6:
                newLine         = true;
                skipFirstIndent = false;
            }
            writer2.Flush();
            return(((StringWriter)writer2.Writer).ToString());
        }