Merge pull request #1319 from directhex/systemwide-per-arch-aot-cache
[mono.git] / mcs / class / System.Xml.Linq / System.Xml.Linq / XStreamingElement.cs
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
27 using System;
28 using System.IO;
29 using System.Collections.Generic;
30
31 namespace System.Xml.Linq
32 {
33         public class XStreamingElement
34         {
35                 public XStreamingElement (XName name)
36                 {
37                         Name = name;
38                 }
39
40                 public XStreamingElement (XName name, object content)
41                         : this (name)
42                 {
43                         Add (content);
44                 }
45
46                 public XStreamingElement (XName name, params object [] content)
47                         : this (name)
48                 {
49                         Add (content);
50                 }
51
52                 XName name;
53                 List<object> contents;
54
55                 public XName Name {
56                         get { return name; }
57                         set { name = value; }
58                 }
59
60                 internal IEnumerable<object> Contents {
61                         get { return contents; }
62                 }
63
64                 public void Add (object content)
65                 {
66                         if (contents == null)
67                                 contents = new List<object> ();
68                         contents.Add (content);
69                 }
70
71                 public void Add (params object [] content)
72                 {
73                         if (contents == null)
74                                 contents = new List<object> ();
75                         contents.Add (content);
76                 }
77
78                 public void Save (string fileName)
79                 {
80                         using (TextWriter w = File.CreateText (fileName))
81                                 Save (w, SaveOptions.None);
82                 }
83
84                 public void Save (TextWriter textWriter)
85                 {
86                         Save (textWriter, SaveOptions.None);
87                 }
88
89                 public void Save (XmlWriter writer)
90                 {
91                         WriteTo (writer);
92                 }
93
94                 public void Save (string fileName, SaveOptions options)
95                 {
96                         using (TextWriter w = File.CreateText (fileName))
97                                 Save (w, options);
98                 }
99
100                 public void Save (TextWriter textWriter, SaveOptions options)
101                 {
102                         XmlWriterSettings s = new XmlWriterSettings ();
103                         s.OmitXmlDeclaration = true;
104                         
105                         if ((options & SaveOptions.DisableFormatting) == SaveOptions.None)
106                                 s.Indent = true;
107 #if NET_4_0
108                         if ((options & SaveOptions.OmitDuplicateNamespaces) == SaveOptions.OmitDuplicateNamespaces)
109                                 s.NamespaceHandling |= NamespaceHandling.OmitDuplicates;
110 #endif
111                         using (XmlWriter w = XmlWriter.Create (textWriter, s))
112                                 WriteTo (w);
113                 }
114
115 #if NET_4_0
116                 public void Save (Stream stream)
117                 {
118                         Save (stream, SaveOptions.None);
119                 }
120
121                 public void Save (Stream stream, SaveOptions options)
122                 {
123                         XmlWriterSettings s = new XmlWriterSettings ();
124                         if ((options & SaveOptions.DisableFormatting) == SaveOptions.None)
125                                 s.Indent = true;
126                         if ((options & SaveOptions.OmitDuplicateNamespaces) == SaveOptions.OmitDuplicateNamespaces)
127                                 s.NamespaceHandling |= NamespaceHandling.OmitDuplicates;
128                         
129                         using (var writer = XmlWriter.Create (stream, s)){
130                                 WriteTo (writer);
131                         }
132                 }
133 #endif
134
135                 public override string ToString ()
136                 {
137                         return ToString (SaveOptions.None);
138                 }
139
140                 public string ToString (SaveOptions options)
141                 {
142                         StringWriter sw = new StringWriter ();
143                         Save (sw, options);
144                         return sw.ToString ();
145                 }
146
147                 public void WriteTo (XmlWriter writer)
148                 {
149                         writer.WriteStartElement (name.LocalName, name.Namespace.NamespaceName);
150                         WriteContents (contents, writer);
151                         writer.WriteEndElement ();
152                 }
153
154                 void WriteContents (IEnumerable<object> items, XmlWriter w)
155                 {
156                         foreach (object o in XUtil.ExpandArray (items)) {
157                                 if (o == null)
158                                         continue;
159                                 else if (o is XStreamingElement)
160                                         ((XStreamingElement) o).WriteTo (w);
161                                 else if (o is XNode)
162                                         ((XNode) o).WriteTo (w);
163                                 else if (o is object [])
164                                         WriteContents ((object []) o, w);
165                                 else if (o is XAttribute)
166                                         WriteAttribute ((XAttribute) o, w);
167                                 else
168                                         new XText (o.ToString ()).WriteTo (w);
169                         }
170                 }
171
172                 void WriteAttribute (XAttribute a, XmlWriter w)
173                 {
174                         if (a.IsNamespaceDeclaration) {
175                                 if (a.Name.Namespace == XNamespace.Xmlns)
176                                         w.WriteAttributeString ("xmlns", a.Name.LocalName, XNamespace.Xmlns.NamespaceName, a.Value);
177                                 else
178                                         w.WriteAttributeString ("xmlns", a.Value);
179                         }
180                         else
181                                 w.WriteAttributeString (a.Name.LocalName, a.Name.Namespace.NamespaceName, a.Value);
182                 }
183         }
184 }