X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mcs%2Fclass%2FSystem.XML%2FSystem.Xml%2FXmlDeclaration.cs;h=c34df121227c358b30325be58be625753af47d47;hb=ea63c7d9e2a5f5808af6679a0ee2ae1af124a77a;hp=91b9e5d5a66558b0f0f8430986cd9cd5e00371d4;hpb=bf7e29a3c2d32d16482537d5fc06fa144b8acee6;p=mono.git
diff --git a/mcs/class/System.XML/System.Xml/XmlDeclaration.cs b/mcs/class/System.XML/System.Xml/XmlDeclaration.cs
index 91b9e5d5a66..c34df121227 100644
--- a/mcs/class/System.XML/System.Xml/XmlDeclaration.cs
+++ b/mcs/class/System.XML/System.Xml/XmlDeclaration.cs
@@ -1,165 +1,136 @@
-using System;
-
-namespace System.Xml
-{
- ///
- ///
- ///
- public class XmlDeclaration : XmlNode
- {
- // Private data members
- private string Fencoding = "UTF-8";
- bool Fstandalone;
-
- // public properties
- ///
- /// Get/Set the encoding used for the document
- /// Typical values are "UTF-8", "UTF-16", "ISO-8859-nn" (where n is 0-9).
- /// If not defined, "UTF-8" is assumed.
- /// encoding is not case sensitive.
- ///
- public string Encoding
- {
- get
- {
- return Fencoding;
- }
-
- set
- {
- string val = value.ToUpper();
-
- if ( (val == "UTF-8") | ( val == "UTF-16") )
- {
- Fencoding = value;
- return;
- }
- else
- {
- if ( ( val.StartsWith( "ISO-8859-" ) ) & (val.Length == 10) )
- {
- try
- {
- int code = System.Convert.ToInt32( val.Substring(9,1) );
- Fencoding = value;
- }
- catch
- {
- throw new NotImplementedException("Encoding " + value + " is not supported");
- }
-
- }
-
-
- }
- }
- }
-
- ///
- /// Get the local name of the declaration. Returns "xml"
- ///
- public override string LocalName
- {
- get
- {
- return "xml";
- }
- }
-
- ///
- /// Get the name of the node. For XmlDeclaration, returns "xml".
- ///
- public override string Name
- {
- get
- {
- return "xml";
- }
- }
-
- ///
- /// Return the node type. For XmlDeclaration, returns XmlNodeType.XmlDeclaration.
- ///
- public override XmlNodeType NodeType
- {
- get
- {
- return XmlNodeType.XmlDeclaration;
- }
- }
-
- ///
- /// Get/Set the value of the standalone attribute.
- /// "yes" => no external DTD required.
- /// "no" => external data sources required.
- /// Silently fails if Set to invalid value.
- /// Not case sensitive.
- ///
- public string Standalone {
- get
- {
- if (Fstandalone)
- return "yes";
- else
- return "no";
- }
-
- set
- {
- if (value.ToUpper() == "YES")
- Fstandalone = true;
- if (value.ToUpper() == "NO")
- Fstandalone = false;
- }
- }
-
- ///
- /// Get the xml version of the file. Returns "1.0"
- ///
- public string Version
- {
- get
- {
- return "1.0";
- }
- }
-
- // Public Methods
- ///
- /// Overriden. Returns a cloned version of this node.
- /// Serves as a copy constructor. Duplicate node has no parent.
- ///
- /// Create deep copy. N/A for XmlDeclaration.
- /// Cloned node.
- public override XmlNode CloneNode(bool deep)
- {
- // TODO - implement XmlDeclration.CloneNode()
- throw new NotImplementedException("XmlDeclration.CloneNode() not implemented");
- }
-
- ///
- /// Save the children of this node to the passed XmlWriter. Since an XmlDeclaration has
- /// no children, this call has no effect.
- ///
- ///
- public override void WriteContentTo(XmlWriter w)
- {
- // Nothing to do - no children.
- }
-
- ///
- /// Saves the node to the specified XmlWriter
- ///
- /// XmlWriter to writ to.
- public override void WriteTo(XmlWriter w)
- {
- // TODO - implement XmlDeclration.WriteTo()
- throw new NotImplementedException("XmlDeclaration.WriteTo() not implemented");
- }
-
- // Constructors
- internal XmlDeclaration( XmlDocument aOwnerDoc) : base(aOwnerDoc)
- {
- }
- }
-}
+//
+// System.Xml.XmlDeclaration
+//
+// Author:
+// Duncan Mak (duncan@ximian.com)
+//
+// (C) Ximian, Inc.
+
+using System;
+using System.Xml;
+using System.Text.RegularExpressions;
+
+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 != null)
+ {
+ if (value.ToUpper() == "YES")
+ standalone = "yes";
+ if (value.ToUpper() == "NO")
+ standalone = "no";
+ }
+ else
+ standalone = String.Empty;
+ }
+ }
+
+ 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 ("", 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];
+ Match m = XmlDeclRegex.Match(input);
+ if(!m.Success)
+ throw new XmlException("illegal XML declaration format.");
+// Version = m.Result("${ver}");
+ Encoding = m.Result("${enc}");
+ Standalone = m.Result("${sta}");
+ }
+
+ // This regular expression matches XMLDecl of XML specification BNF[23]
+ static Regex xmlDeclRegex;
+ Regex XmlDeclRegex
+ {
+ get
+ {
+ if(xmlDeclRegex == null) xmlDeclRegex = new Regex(allMatch, RegexOptions.Compiled);
+ return xmlDeclRegex;
+ }
+ }
+
+ // This code makes some loss, but you may understand a bit easily.
+ const string verMatch = "\\s*version\\s*=\\s*(\\'(?.*?)\\'|\\\"(?.*?)\")";
+ const string encMatch = "\\s*encoding\\s*=\\s*(\\'(?.*?)\\'|\\\"(?.*?)\")";
+ const string staMatch = "\\s*standalone\\s*=\\s*(\\'(?.*?)\\'|\\\"(?.*?)\")";
+ const string allMatch = verMatch + "(" + encMatch + ")?(" + staMatch + ")?";
+ }
+}