Beispiel #1
0
        public override void Save(TextWriter writer)
        {
            XmlWriter xmlWriter = null;

            try
            {
                if (PreserveWhitespace)
                {
                    XmlFormatter.Format(this);
                    xmlWriter = new XmlAttributePreservingWriter(writer);
                }
                else
                {
                    xmlWriter = XmlWriter.Create(writer);
                    xmlWriter.Settings.Encoding = TextEncoding;
                    xmlWriter.Settings.Indent   = true;
                }
                WriteTo(xmlWriter);
            }
            finally
            {
                if (xmlWriter != null)
                {
                    xmlWriter.Flush();
                    xmlWriter.Dispose();
                }
            }
        }
Beispiel #2
0
        public override void Save(string filename)
        {
            XmlWriter xmlWriter = null;

            using (Stream file = File.Create(filename))
            {
                try
                {
                    if (PreserveWhitespace)
                    {
                        XmlFormatter.Format(this);
                        xmlWriter = new XmlAttributePreservingWriter(file, TextEncoding);
                    }
                    else
                    {
                        XmlTextWriter textWriter = new XmlTextWriter(file, TextEncoding);
                        textWriter.Formatting = Formatting.Indented;
                        xmlWriter             = textWriter;
                    }
                    WriteTo(xmlWriter);
                }
                finally
                {
                    if (xmlWriter != null)
                    {
                        xmlWriter.Flush();
                        xmlWriter.Close();
                    }
                }
            }
        }
Beispiel #3
0
        public void Save(string filename)
        {
            XmlWriter  xmlWriter = null;
            FileStream stream    = null;

            try
            {
                if (PreserveWhitespace)
                {
                    XmlFormatter.Format(this);
                    stream    = File.OpenWrite(filename);
                    xmlWriter = new XmlAttributePreservingWriter(stream, TextEncoding);
                }
                else
                {
                    stream = File.OpenWrite(filename);
                    XmlTextWriter textWriter = new XmlTextWriter(stream, TextEncoding);
                    textWriter.Formatting = Formatting.Indented;
                    xmlWriter             = textWriter;
                }
                WriteTo(xmlWriter);
            }
            finally
            {
                if (xmlWriter != null)
                {
                    xmlWriter.Flush();
                    xmlWriter.Dispose();
                }
                if (stream != null)
                {
                    stream.Dispose();
                }
            }
        }
Beispiel #4
0
 public static void Format(XmlDocument document) {
     XmlFileInfoDocument errorInfoDocument = document as XmlFileInfoDocument;
     if (errorInfoDocument != null) {
         XmlFormatter formatter = new XmlFormatter(errorInfoDocument);
         formatter.FormatLoop(errorInfoDocument);
     }
 }
Beispiel #5
0
        public override void Save(Stream w)
        {
            XmlWriter writer = null;

            try
            {
                if (base.PreserveWhitespace)
                {
                    XmlFormatter.Format(this);
                    writer = new XmlAttributePreservingWriter(w, this.TextEncoding);
                }
                else
                {
                    XmlTextWriter writer2 = new XmlTextWriter(w, this.TextEncoding)
                    {
                        Formatting = Formatting.Indented
                    };
                    writer = writer2;
                }
                this.WriteTo(writer);
            }
            finally
            {
                if (writer != null)
                {
                    writer.Flush();
                }
            }
        }
Beispiel #6
0
        public override void Save(string filename)
        {
            XmlWriter w = null;

            try
            {
                if (base.PreserveWhitespace)
                {
                    XmlFormatter.Format(this);
                    w = new XmlAttributePreservingWriter(filename, this.TextEncoding);
                }
                else
                {
                    XmlTextWriter writer2 = new XmlTextWriter(filename, this.TextEncoding)
                    {
                        Formatting = Formatting.Indented
                    };
                    w = writer2;
                }
                this.WriteTo(w);
            }
            finally
            {
                if (w != null)
                {
                    w.Flush();
                    w.Close();
                }
            }
        }
Beispiel #7
0
        public override void Save(Stream w)
        {
            XmlWriter xmlWriter = null;

            try {
                if (PreserveWhitespace)
                {
                    XmlFormatter.Format(this);
                    xmlWriter = new XmlAttributePreservingWriter(w, TextEncoding);
                }
                else
                {
                    XmlTextWriter textWriter = new XmlTextWriter(w, TextEncoding);
                    textWriter.Formatting = Formatting.Indented;
                    xmlWriter             = textWriter;
                }
                WriteTo(xmlWriter);
            }
            finally {
                if (xmlWriter != null)
                {
                    xmlWriter.Flush();
                }
            }
        }
 public string GetAttributeNewLineString(XmlFormatter formatter)
 {
     if (attributeNewLineString == null)
     {
         attributeNewLineString = ComputeAttributeNewLineString(formatter);
     }
     return(attributeNewLineString);
 }
Beispiel #9
0
 public string GetAttributeNewLineString(XmlFormatter formatter)
 {
     if (this.attributeNewLineString == null)
     {
         this.attributeNewLineString = this.ComputeAttributeNewLineString(formatter);
     }
     return(this.attributeNewLineString);
 }
Beispiel #10
0
        public static void Format(XmlDocument document)
        {
            XmlFileInfoDocument errorInfoDocument = document as XmlFileInfoDocument;

            if (errorInfoDocument != null)
            {
                XmlFormatter formatter = new XmlFormatter(errorInfoDocument);
                formatter.FormatLoop(errorInfoDocument);
            }
        }
        private string ComputeAttributeNewLineString(XmlFormatter formatter)
        {
            string lookAheadString = LookAheadForNewLineString();

            if (lookAheadString != null)
            {
                return(lookAheadString);
            }
            else if (formatter != null)
            {
                return(formatter.CurrentAttributeIndent);
            }
            else
            {
                return(null);
            }
        }
 private void EnsureAttributeNewLineString(XmlFormatter formatter)
 {
     GetAttributeNewLineString(formatter);
 }
        internal void UpdatePreservationInfo(XmlAttributeCollection updatedAttributes, XmlFormatter formatter)
        {
            if (updatedAttributes.Count == 0)
            {
                if (orderedAttributes.Count > 0)
                {
                    // All attributes were removed, clear preservation info
                    leadingSpaces.Clear();
                    orderedAttributes.Clear();
                }
            }
            else
            {
                Dictionary <string, bool> attributeExists = new Dictionary <string, bool>();

                // Prepopulate the list with attributes that existed before
                foreach (string attributeName in orderedAttributes)
                {
                    attributeExists[attributeName] = false;
                }

                // Update the list with attributes that exist now
                foreach (XmlAttribute attribute in updatedAttributes)
                {
                    if (!attributeExists.ContainsKey(attribute.Name))
                    {
                        orderedAttributes.Add(attribute.Name);
                    }
                    attributeExists[attribute.Name] = true;
                }

                bool   firstAttribute        = true;
                string keepLeadingWhitespace = null;
                foreach (string key in orderedAttributes)
                {
                    bool exists = attributeExists[key];

                    // Handle removal
                    if (!exists)
                    {
                        // We need to figure out whether to keep the leading
                        // or trailing whitespace. The logic is:
                        //   1. The whitespace before the first attribute is
                        //      always kept, so remove trailing space.
                        //   2. We want to keep newlines, so if the leading
                        //      space contains a newline then remove trailing
                        //      space. If multiple newlines are being removed,
                        //      keep the last one.
                        //   3. Otherwise, remove leading space.
                        //
                        // In order to remove trailing space, we have to
                        // remove the leading space of the next attribute, so
                        // we store this leading space to replace the next.
                        if (leadingSpaces.ContainsKey(key))
                        {
                            string leadingSpace = leadingSpaces[key];
                            if (firstAttribute)
                            {
                                if (keepLeadingWhitespace == null)
                                {
                                    keepLeadingWhitespace = leadingSpace;
                                }
                            }
                            else if (ContainsNewLine(leadingSpace))
                            {
                                keepLeadingWhitespace = leadingSpace;
                            }

                            leadingSpaces.Remove(key);
                        }
                    }

                    else if (keepLeadingWhitespace != null)
                    {
                        // Exception to rule #2 above: Don't replace an existing
                        // newline with one that was removed
                        if (firstAttribute || !leadingSpaces.ContainsKey(key) || !ContainsNewLine(leadingSpaces[key]))
                        {
                            leadingSpaces[key] = keepLeadingWhitespace;
                        }
                        keepLeadingWhitespace = null;
                    }

                    // Handle addition
                    else if (!leadingSpaces.ContainsKey(key))
                    {
                        if (firstAttribute)
                        {
                            // This will prevent the textwriter from writing a
                            // newline before the first attribute
                            leadingSpaces[key] = " ";
                        }
                        else if (OneAttributePerLine)
                        {
                            // Add the indent space between each attribute
                            leadingSpaces[key] = GetAttributeNewLineString(formatter);
                        }
                        else
                        {
                            // Don't add any hard-coded spaces. All new attributes
                            // should be at the end, so they'll be formatted while
                            // writing. Make sure we have the right indent string,
                            // though.
                            EnsureAttributeNewLineString(formatter);
                        }
                    }

                    // firstAttribute remains true until we find the first
                    // attribute that actually exists
                    firstAttribute = firstAttribute && !exists;
                }
            }
        }
Beispiel #14
0
        private string ComputeAttributeNewLineString(XmlFormatter formatter)
        {
            string str = this.LookAheadForNewLineString();

            return((str == null) ? formatter?.CurrentAttributeIndent : str);
        }
Beispiel #15
0
 internal void UpdatePreservationInfo(XmlAttributeCollection updatedAttributes, XmlFormatter formatter)
 {
     if (updatedAttributes.Count == 0)
     {
         if (this.orderedAttributes.Count > 0)
         {
             this.leadingSpaces.Clear();
             this.orderedAttributes.Clear();
         }
     }
     else
     {
         Dictionary <string, bool> dictionary = new Dictionary <string, bool>();
         foreach (string str in this.orderedAttributes)
         {
             dictionary[str] = false;
         }
         foreach (XmlAttribute attribute in updatedAttributes)
         {
             if (!dictionary.ContainsKey(attribute.Name))
             {
                 this.orderedAttributes.Add(attribute.Name);
             }
             dictionary[attribute.Name] = true;
         }
         bool   flag = true;
         string str2 = null;
         foreach (string str3 in this.orderedAttributes)
         {
             bool flag2 = dictionary[str3];
             if (!flag2)
             {
                 if (this.leadingSpaces.ContainsKey(str3))
                 {
                     string space = this.leadingSpaces[str3];
                     if (flag)
                     {
                         if (str2 == null)
                         {
                             str2 = space;
                         }
                     }
                     else if (this.ContainsNewLine(space))
                     {
                         str2 = space;
                     }
                     this.leadingSpaces.Remove(str3);
                 }
             }
             else if (str2 != null)
             {
                 if (flag || (!this.leadingSpaces.ContainsKey(str3) || !this.ContainsNewLine(this.leadingSpaces[str3])))
                 {
                     this.leadingSpaces[str3] = str2;
                 }
                 str2 = null;
             }
             else if (!this.leadingSpaces.ContainsKey(str3))
             {
                 if (flag)
                 {
                     this.leadingSpaces[str3] = " ";
                 }
                 else if (this.OneAttributePerLine)
                 {
                     this.leadingSpaces[str3] = this.GetAttributeNewLineString(formatter);
                 }
                 else
                 {
                     this.EnsureAttributeNewLineString(formatter);
                 }
             }
             flag = flag && !flag2;
         }
     }
 }
Beispiel #16
0
 void IXmlFormattableAttributes.FormatAttributes(XmlFormatter formatter)
 {
     preservationDict.UpdatePreservationInfo(Attributes, formatter);
 }
 void IXmlFormattableAttributes.FormatAttributes(XmlFormatter formatter) {
     preservationDict.UpdatePreservationInfo(Attributes, formatter);
 }
 private void EnsureAttributeNewLineString(XmlFormatter formatter) {
     GetAttributeNewLineString(formatter);
 }
 private string ComputeAttributeNewLineString(XmlFormatter formatter) {
     string lookAheadString = LookAheadForNewLineString();
     if (lookAheadString != null) {
         return lookAheadString;
     }
     else if (formatter != null) {
         return formatter.CurrentAttributeIndent;
     }
     else {
         return null;
     }
 }
 public string GetAttributeNewLineString(XmlFormatter formatter) {
     if (attributeNewLineString == null) {
         attributeNewLineString = ComputeAttributeNewLineString(formatter);
     }
     return attributeNewLineString;
 }
        internal void UpdatePreservationInfo(XmlAttributeCollection updatedAttributes, XmlFormatter formatter) {
            if (updatedAttributes.Count == 0) {
                if (orderedAttributes.Count > 0) {
                    // All attributes were removed, clear preservation info
                    leadingSpaces.Clear();
                    orderedAttributes.Clear();
                }
            }
            else {
                Dictionary<string, bool> attributeExists = new Dictionary<string, bool>();

                // Prepopulate the list with attributes that existed before
                foreach (string attributeName in orderedAttributes) {
                    attributeExists[attributeName] = false;
                }

                // Update the list with attributes that exist now
                foreach (XmlAttribute attribute in updatedAttributes) {
                    if (!attributeExists.ContainsKey(attribute.Name)) {
                        orderedAttributes.Add(attribute.Name);
                    }
                    attributeExists[attribute.Name] = true;
                }

                bool firstAttribute = true;
                string keepLeadingWhitespace = null;
                foreach (string key in orderedAttributes) {
                    bool exists = attributeExists[key];

                    // Handle removal
                    if (!exists) {
                        // We need to figure out whether to keep the leading
                        // or trailing whitespace. The logic is:
                        //   1. The whitespace before the first attribute is
                        //      always kept, so remove trailing space.
                        //   2. We want to keep newlines, so if the leading
                        //      space contains a newline then remove trailing
                        //      space. If multiple newlines are being removed,
                        //      keep the last one.
                        //   3. Otherwise, remove leading space.
                        //
                        // In order to remove trailing space, we have to 
                        // remove the leading space of the next attribute, so
                        // we store this leading space to replace the next.
                        if (leadingSpaces.ContainsKey(key)) {
                            string leadingSpace = leadingSpaces[key];
                            if (firstAttribute) {
                                if (keepLeadingWhitespace == null) {
                                    keepLeadingWhitespace = leadingSpace;
                                }
                            }
                            else if (ContainsNewLine(leadingSpace)) {
                                keepLeadingWhitespace = leadingSpace;
                            }

                            leadingSpaces.Remove(key);
                        }
                    }

                    else if (keepLeadingWhitespace != null) {
                        // Exception to rule #2 above: Don't replace an existing
                        // newline with one that was removed
                        if (firstAttribute || !leadingSpaces.ContainsKey(key) || !ContainsNewLine(leadingSpaces[key])) {
                            leadingSpaces[key] = keepLeadingWhitespace;
                        }
                        keepLeadingWhitespace = null;
                    }

                    // Handle addition
                    else if (!leadingSpaces.ContainsKey(key)) {
                        if (firstAttribute) {
                            // This will prevent the textwriter from writing a
                            // newline before the first attribute
                            leadingSpaces[key] = " ";
                        }
                        else if (OneAttributePerLine) {
                            // Add the indent space between each attribute
                            leadingSpaces[key] = GetAttributeNewLineString(formatter);
                        }
                        else {
                            // Don't add any hard-coded spaces. All new attributes
                            // should be at the end, so they'll be formatted while
                            // writing. Make sure we have the right indent string,
                            // though.
                            EnsureAttributeNewLineString(formatter);
                        }
                    }

                    // firstAttribute remains true until we find the first
                    // attribute that actually exists
                    firstAttribute = firstAttribute && !exists;
                }
            }
        }