PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System.Xml.Linq/System.Xml.Linq/XStreamingElement.cs

https://bitbucket.org/danipen/mono
C# | 184 lines | 136 code | 23 blank | 25 comment | 25 complexity | 23888a0f7820b7975ec43da886688d4b MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. //
  2. // Authors:
  3. // Atsushi Enomoto
  4. //
  5. // Copyright 2007 Novell (http://www.novell.com)
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining
  8. // a copy of this software and associated documentation files (the
  9. // "Software"), to deal in the Software without restriction, including
  10. // without limitation the rights to use, copy, modify, merge, publish,
  11. // distribute, sublicense, and/or sell copies of the Software, and to
  12. // permit persons to whom the Software is furnished to do so, subject to
  13. // the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be
  16. // included in all copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  22. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  23. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  24. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. //
  26. using System;
  27. using System.IO;
  28. using System.Collections.Generic;
  29. namespace System.Xml.Linq
  30. {
  31. public class XStreamingElement
  32. {
  33. public XStreamingElement (XName name)
  34. {
  35. Name = name;
  36. }
  37. public XStreamingElement (XName name, object content)
  38. : this (name)
  39. {
  40. Add (content);
  41. }
  42. public XStreamingElement (XName name, params object [] content)
  43. : this (name)
  44. {
  45. Add (content);
  46. }
  47. XName name;
  48. List<object> contents;
  49. public XName Name {
  50. get { return name; }
  51. set { name = value; }
  52. }
  53. internal IEnumerable<object> Contents {
  54. get { return contents; }
  55. }
  56. public void Add (object content)
  57. {
  58. if (contents == null)
  59. contents = new List<object> ();
  60. contents.Add (content);
  61. }
  62. public void Add (params object [] content)
  63. {
  64. if (contents == null)
  65. contents = new List<object> ();
  66. contents.Add (content);
  67. }
  68. public void Save (string fileName)
  69. {
  70. using (TextWriter w = File.CreateText (fileName))
  71. Save (w, SaveOptions.None);
  72. }
  73. public void Save (TextWriter textWriter)
  74. {
  75. Save (textWriter, SaveOptions.None);
  76. }
  77. public void Save (XmlWriter writer)
  78. {
  79. WriteTo (writer);
  80. }
  81. public void Save (string fileName, SaveOptions options)
  82. {
  83. using (TextWriter w = File.CreateText (fileName))
  84. Save (w, options);
  85. }
  86. public void Save (TextWriter textWriter, SaveOptions options)
  87. {
  88. XmlWriterSettings s = new XmlWriterSettings ();
  89. s.OmitXmlDeclaration = true;
  90. if ((options & SaveOptions.DisableFormatting) == SaveOptions.None)
  91. s.Indent = true;
  92. #if NET_4_0
  93. if ((options & SaveOptions.OmitDuplicateNamespaces) == SaveOptions.OmitDuplicateNamespaces)
  94. s.NamespaceHandling |= NamespaceHandling.OmitDuplicates;
  95. #endif
  96. using (XmlWriter w = XmlWriter.Create (textWriter, s))
  97. WriteTo (w);
  98. }
  99. #if NET_4_0
  100. public void Save (Stream stream)
  101. {
  102. Save (stream, SaveOptions.None);
  103. }
  104. public void Save (Stream stream, SaveOptions options)
  105. {
  106. XmlWriterSettings s = new XmlWriterSettings ();
  107. if ((options & SaveOptions.DisableFormatting) == SaveOptions.None)
  108. s.Indent = true;
  109. if ((options & SaveOptions.OmitDuplicateNamespaces) == SaveOptions.OmitDuplicateNamespaces)
  110. s.NamespaceHandling |= NamespaceHandling.OmitDuplicates;
  111. using (var writer = XmlWriter.Create (stream, s)){
  112. WriteTo (writer);
  113. }
  114. }
  115. #endif
  116. public override string ToString ()
  117. {
  118. return ToString (SaveOptions.None);
  119. }
  120. public string ToString (SaveOptions options)
  121. {
  122. StringWriter sw = new StringWriter ();
  123. Save (sw, options);
  124. return sw.ToString ();
  125. }
  126. public void WriteTo (XmlWriter writer)
  127. {
  128. writer.WriteStartElement (name.LocalName, name.Namespace.NamespaceName);
  129. WriteContents (contents, writer);
  130. writer.WriteEndElement ();
  131. }
  132. void WriteContents (IEnumerable<object> items, XmlWriter w)
  133. {
  134. foreach (object o in XUtil.ExpandArray (items)) {
  135. if (o == null)
  136. continue;
  137. else if (o is XStreamingElement)
  138. ((XStreamingElement) o).WriteTo (w);
  139. else if (o is XNode)
  140. ((XNode) o).WriteTo (w);
  141. else if (o is object [])
  142. WriteContents ((object []) o, w);
  143. else if (o is XAttribute)
  144. WriteAttribute ((XAttribute) o, w);
  145. else
  146. new XText (o.ToString ()).WriteTo (w);
  147. }
  148. }
  149. void WriteAttribute (XAttribute a, XmlWriter w)
  150. {
  151. if (a.IsNamespaceDeclaration) {
  152. if (a.Name.Namespace == XNamespace.Xmlns)
  153. w.WriteAttributeString ("xmlns", a.Name.LocalName, XNamespace.Xmlns.NamespaceName, a.Value);
  154. else
  155. w.WriteAttributeString ("xmlns", a.Value);
  156. }
  157. else
  158. w.WriteAttributeString (a.Name.LocalName, a.Name.Namespace.NamespaceName, a.Value);
  159. }
  160. }
  161. }