2002-10-18 Duncan Mak <duncan@ximian.com>
[mono.git] / mcs / class / System.XML / System.Xml / XmlDeclaration.cs
index 69d5fc83c4602a9c04fb888128938670111639b9..8f1974e4d9aef7fd34764e2427267795bcf1f5fd 100644 (file)
-using System;\r
-\r
-namespace System.Xml\r
-{\r
-       /// <summary>\r
-       /// \r
-       /// </summary>\r
-       public class XmlDeclaration\r
-       {\r
-               // Private data members\r
-\r
-               // public properties\r
-               \r
-\r
-               // Public Methods\r
-       }\r
-}\r
+//
+// System.Xml.XmlDeclaration
+//
+// Author:
+//     Duncan Mak  (duncan@ximian.com)
+//
+// (C) Ximian, Inc.
+
+using System;
+using System.Xml;
+
+namespace System.Xml
+{
+       public class XmlDeclaration : XmlLinkedNode
+       {
+               string encoding = "UTF-8"; // defaults to UTF-8
+               string standalone;
+               string version;
+
+               protected internal XmlDeclaration (string version, string encoding,
+                                                  string standalone, XmlDocument doc)
+                       : base (doc)
+               {
+                       if (encoding == null)
+                               encoding = "";
+
+                       if (standalone == null)
+                               standalone = "";
+
+                       this.version = version;
+                       this.encoding = encoding;
+                       this.standalone = standalone;
+               }
+
+               public string Encoding  {
+                       get { return encoding; } 
+                       set { encoding = (value == null) ? String.Empty : value; }
+               }
+
+               public override string InnerText {
+                       get { return Value; }
+                       set { ParseInput (value); }
+               }
+               
+               public override string LocalName {
+                       get { return "xml"; }
+               }
+
+               public override string Name {
+                       get { return "xml"; }
+               }
+
+               public override XmlNodeType NodeType {
+                       get { return XmlNodeType.XmlDeclaration; }
+               }
+
+               public string Standalone {
+                       get { return standalone; }
+                       set {
+                               if (value.ToUpper() == "YES")
+                                       standalone = "yes";
+                               if (value.ToUpper() == "NO")
+                                       standalone = "no";
+                       }
+               }
+
+               public override string Value {
+                       get {
+                               string formatEncoding = "";
+                               string formatStandalone = "";
+
+                               if (encoding != String.Empty)
+                                       formatEncoding = String.Format (" encoding=\"{0}\"", encoding);
+
+                               if (standalone != String.Empty)
+                                       formatStandalone = String.Format (" standalone=\"{0}\"", standalone);
+
+                               return String.Format ("version=\"{0}\"{1}{2}", Version, formatEncoding, formatStandalone);
+                       }
+                       set { ParseInput (value); }
+               }
+
+               public string Version {
+                       get { return version; }
+               }
+
+               public override XmlNode CloneNode (bool deep)
+               {
+                       return new XmlDeclaration (Version, Encoding, standalone, OwnerDocument);
+               }
+
+               public override void WriteContentTo (XmlWriter w) {}
+
+               public override void WriteTo (XmlWriter w)
+               {
+                       // This doesn't seem to match up very well with w.WriteStartDocument()
+                       // so writing out custom here.
+                       w.WriteRaw (String.Format ("<?xml {0}?>", Value));
+               }
+
+               void ParseInput (string input)
+               {                       
+                       Encoding = input.Split (new char [] { ' ' }) [1].Split (new char [] { '=' }) [1];
+                       Standalone = input.Split (new char [] { ' ' }) [2].Split (new char [] { '=' }) [1];
+               }
+       }
+}