Merge pull request #439 from mono-soc-2012/garyb/iconfix
[mono.git] / mcs / class / System.Xml.Linq / System.Xml.Linq / XDocument.cs
index eac4fa36c91787e7835bfc65bc08a4bc3fa26448..40ac74035a09f210dcdb7136eac8804b93913be9 100644 (file)
@@ -48,9 +48,9 @@ namespace System.Xml.Linq
                        Add (content);
                }
 
-               public XDocument (XDeclaration xmldecl, params object [] content)
+               public XDocument (XDeclaration declaration, params object [] content)
                {
-                       Declaration = xmldecl;
+                       Declaration = declaration;
                        Add (content);
                }
 
@@ -95,22 +95,38 @@ namespace System.Xml.Linq
                public static XDocument Load (string uri, LoadOptions options)
                {
                        XmlReaderSettings s = new XmlReaderSettings ();
+#if !MOONLIGHT
+                       s.ProhibitDtd = false; // see XNodeNavigatorTest.MoveToId().
+#endif
                        s.IgnoreWhitespace = (options & LoadOptions.PreserveWhitespace) == 0;
                        using (XmlReader r = XmlReader.Create (uri, s)) {
                                return LoadCore (r, options);
                        }
                }
 
-               public static XDocument Load (TextReader reader)
+               public static XDocument Load (Stream stream)
                {
-                       return Load (reader, LoadOptions.None);
+                       return Load (new StreamReader (stream), LoadOptions.None);
+               }
+
+               public static XDocument Load (Stream stream, LoadOptions options)
+               {
+                       return Load (new StreamReader (stream), options);
+               }
+
+               public static XDocument Load (TextReader textReader)
+               {
+                       return Load (textReader, LoadOptions.None);
                }
 
-               public static XDocument Load (TextReader reader, LoadOptions options)
+               public static XDocument Load (TextReader textReader, LoadOptions options)
                {
                        XmlReaderSettings s = new XmlReaderSettings ();
+#if !MOONLIGHT
+                       s.ProhibitDtd = false; // see XNodeNavigatorTest.MoveToId().
+#endif
                        s.IgnoreWhitespace = (options & LoadOptions.PreserveWhitespace) == 0;
-                       using (XmlReader r = XmlReader.Create (reader, s)) {
+                       using (XmlReader r = XmlReader.Create (textReader, s)) {
                                return LoadCore (r, options);
                        }
                }
@@ -122,7 +138,7 @@ namespace System.Xml.Linq
 
                public static XDocument Load (XmlReader reader, LoadOptions options)
                {
-                       XmlReaderSettings s = reader.Settings.Clone ();
+                       XmlReaderSettings s = reader.Settings != null ? reader.Settings.Clone () : new XmlReaderSettings ();
                        s.IgnoreWhitespace = (options & LoadOptions.PreserveWhitespace) == 0;
                        using (XmlReader r = XmlReader.Create (reader, s)) {
                                return LoadCore (r, options);
@@ -140,6 +156,7 @@ namespace System.Xml.Linq
                {
                        if (reader.ReadState == ReadState.Initial)
                                reader.Read ();
+                       this.FillLineInfoAndBaseUri (reader, options);
                        if (reader.NodeType == XmlNodeType.XmlDeclaration) {
                                Declaration = new XDeclaration (
                                        reader.GetAttribute ("version"),
@@ -163,69 +180,68 @@ namespace System.Xml.Linq
                                }
                }
 
-               public static XDocument Parse (string s)
+               public static XDocument Parse (string text)
                {
-                       return Parse (s, LoadOptions.None);
+                       return Parse (text, LoadOptions.None);
                }
 
-               public static XDocument Parse (string s, LoadOptions options)
+               public static XDocument Parse (string text, LoadOptions options)
                {
-                       return Load (new StringReader (s), options);
+                       return Load (new StringReader (text), options);
                }
 
-               public void Save (string filename)
+               public void Save (string fileName)
                {
-                       Save (filename, SaveOptions.None);
+                       Save (fileName, SaveOptions.None);
                }
 
-               public void Save (string filename, SaveOptions options)
+               public void Save (string fileName, SaveOptions options)
                {
                        XmlWriterSettings s = new XmlWriterSettings ();
-                       if ((options & SaveOptions.DisableFormatting) == 0) {
-                               // hacky!
+                       if ((options & SaveOptions.DisableFormatting) == SaveOptions.None)
                                s.Indent = true;
-                               s.IndentChars = String.Empty;
-                               s.NewLineChars = String.Empty;
-                       }
-                       using (XmlWriter w = XmlWriter.Create (filename)) {
+#if NET_4_0
+                       if ((options & SaveOptions.OmitDuplicateNamespaces) == SaveOptions.OmitDuplicateNamespaces)
+                               s.NamespaceHandling |= NamespaceHandling.OmitDuplicates;
+#endif
+                       
+                       using (XmlWriter w = XmlWriter.Create (fileName, s)) {
                                Save (w);
                        }
                }
 
-               public void Save (TextWriter tw)
+               public void Save (TextWriter textWriter)
                {
-                       Save (tw, SaveOptions.None);
+                       Save (textWriter, SaveOptions.None);
                }
 
-               public void Save (TextWriter tw, SaveOptions options)
+               public void Save (TextWriter textWriter, SaveOptions options)
                {
                        XmlWriterSettings s = new XmlWriterSettings ();
-                       if ((options & SaveOptions.DisableFormatting) == 0) {
-                               // hacky!
+                       if ((options & SaveOptions.DisableFormatting) == SaveOptions.None)
                                s.Indent = true;
-                               s.IndentChars = String.Empty;
-                               s.NewLineChars = String.Empty;
-                       }
-                       using (XmlWriter w = XmlWriter.Create (tw)) {
+#if NET_4_0
+                       if ((options & SaveOptions.OmitDuplicateNamespaces) == SaveOptions.OmitDuplicateNamespaces)
+                               s.NamespaceHandling |= NamespaceHandling.OmitDuplicates;
+#endif
+                       using (XmlWriter w = XmlWriter.Create (textWriter, s)) {
                                Save (w);
                        }
                }
 
-               public void Save (XmlWriter w)
+               public void Save (XmlWriter writer)
                {
-                       WriteTo (w);
+                       WriteTo (writer);
                }
 
-               public override void WriteTo (XmlWriter w)
+               public override void WriteTo (XmlWriter writer)
                {
-                       if (xmldecl != null) {
-                               if (xmldecl.Standalone != null)
-                                       w.WriteStartDocument (xmldecl.Standalone == "yes");
-                               else
-                                       w.WriteStartDocument ();
-                       }
+                       if (xmldecl != null && xmldecl.Standalone != null)
+                               writer.WriteStartDocument (xmldecl.Standalone == "yes");
+                       else
+                               writer.WriteStartDocument ();
                        foreach (XNode node in Nodes ())
-                               node.WriteTo (w);
+                               node.WriteTo (writer);
                }
 
                internal override bool OnAddingObject (object obj, bool rejectAttribute, XNode refNode, bool addFirst)
@@ -256,5 +272,25 @@ namespace System.Xml.Linq
                                        throw new InvalidOperationException ("An element cannot be added before the document type declaration");
                        }
                }
+#if NET_4_0
+               public void Save (Stream stream)
+               {
+                       Save (stream, SaveOptions.None);
+               }
+
+               public void Save (Stream stream, SaveOptions options)
+               {
+                       XmlWriterSettings s = new XmlWriterSettings ();
+                       if ((options & SaveOptions.DisableFormatting) == SaveOptions.None)
+                               s.Indent = true;
+                       if ((options & SaveOptions.OmitDuplicateNamespaces) == SaveOptions.OmitDuplicateNamespaces)
+                               s.NamespaceHandling |= NamespaceHandling.OmitDuplicates;
+       
+                       using (var writer = XmlWriter.Create (stream, s)){
+                               Save (writer);
+                       }
+               }
+
+#endif
        }
 }