2
0

XmlDeclaration.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. //
  2. // System.Xml.XmlDeclaration
  3. //
  4. // Author:
  5. // Duncan Mak ([email protected])
  6. // Atsushi Enomotot ([email protected])
  7. //
  8. // (C) Ximian, Inc.
  9. using System;
  10. using System.Globalization;
  11. using System.Text;
  12. using System.Xml;
  13. namespace System.Xml
  14. {
  15. public class XmlDeclaration : XmlLinkedNode
  16. {
  17. string encoding = "UTF-8"; // defaults to UTF-8
  18. string standalone;
  19. string version;
  20. protected internal XmlDeclaration (string version, string encoding,
  21. string standalone, XmlDocument doc)
  22. : base (doc)
  23. {
  24. if (encoding == null)
  25. encoding = "";
  26. if (standalone == null)
  27. standalone = "";
  28. this.version = version;
  29. this.encoding = encoding;
  30. this.standalone = standalone;
  31. }
  32. public string Encoding {
  33. get { return encoding; }
  34. set { encoding = (value == null) ? String.Empty : value; }
  35. }
  36. public override string InnerText {
  37. get { return Value; }
  38. set { ParseInput (value); }
  39. }
  40. public override string LocalName {
  41. get { return "xml"; }
  42. }
  43. public override string Name {
  44. get { return "xml"; }
  45. }
  46. public override XmlNodeType NodeType {
  47. get { return XmlNodeType.XmlDeclaration; }
  48. }
  49. public string Standalone {
  50. get { return standalone; }
  51. set {
  52. if(value != null)
  53. {
  54. if (String.Compare (value, "YES", true, CultureInfo.InvariantCulture) == 0)
  55. standalone = "yes";
  56. if (String.Compare (value, "NO", true, CultureInfo.InvariantCulture) == 0)
  57. standalone = "no";
  58. }
  59. else
  60. standalone = String.Empty;
  61. }
  62. }
  63. public override string Value {
  64. get {
  65. string formatEncoding = "";
  66. string formatStandalone = "";
  67. if (encoding != String.Empty)
  68. formatEncoding = String.Format (" encoding=\"{0}\"", encoding);
  69. if (standalone != String.Empty)
  70. formatStandalone = String.Format (" standalone=\"{0}\"", standalone);
  71. return String.Format ("version=\"{0}\"{1}{2}", Version, formatEncoding, formatStandalone);
  72. }
  73. set { ParseInput (value); }
  74. }
  75. public string Version {
  76. get { return version; }
  77. }
  78. public override XmlNode CloneNode (bool deep)
  79. {
  80. return new XmlDeclaration (Version, Encoding, standalone, OwnerDocument);
  81. }
  82. public override void WriteContentTo (XmlWriter w) {}
  83. public override void WriteTo (XmlWriter w)
  84. {
  85. // This doesn't seem to match up very well with w.WriteStartDocument()
  86. // so writing out custom here.
  87. w.WriteRaw (String.Format ("<?xml {0}?>", Value));
  88. }
  89. private int SkipWhitespace (string input, int index)
  90. {
  91. while (index < input.Length) {
  92. if (XmlChar.IsWhitespace (input [index]))
  93. index++;
  94. else
  95. break;
  96. }
  97. return index;
  98. }
  99. void ParseInput (string input)
  100. {
  101. int index = SkipWhitespace (input, 0);
  102. if (index + 7 > input.Length || input.IndexOf ("version", index, 7) != index)
  103. throw new XmlException ("Missing 'version' specification.");
  104. index = SkipWhitespace (input, index + 7);
  105. char c = input [index];
  106. if (c != '=')
  107. throw new XmlException ("Invalid 'version' specification.");
  108. index++;
  109. index = SkipWhitespace (input, index);
  110. c = input [index];
  111. if (c != '"' && c != '\'')
  112. throw new XmlException ("Invalid 'version' specification.");
  113. index++;
  114. int end = input.IndexOf (c, index);
  115. if (end < 0 || input.IndexOf ("1.0", index, 3) != index)
  116. throw new XmlException ("Invalid 'version' specification.");
  117. index += 4;
  118. if (index == input.Length)
  119. return;
  120. if (!XmlChar.IsWhitespace (input [index]))
  121. throw new XmlException ("Invalid XML declaration.");
  122. index = SkipWhitespace (input, index + 1);
  123. if (index == input.Length)
  124. return;
  125. if (input.Length > index + 8 && input.IndexOf ("encoding", index, 8) > 0) {
  126. index = SkipWhitespace (input, index + 8);
  127. c = input [index];
  128. if (c != '=')
  129. throw new XmlException ("Invalid 'version' specification.");
  130. index++;
  131. index = SkipWhitespace (input, index);
  132. c = input [index];
  133. if (c != '"' && c != '\'')
  134. throw new XmlException ("Invalid 'encoding' specification.");
  135. end = input.IndexOf (c, index + 1);
  136. if (end < 0)
  137. throw new XmlException ("Invalid 'encoding' specification.");
  138. Encoding = input.Substring (index + 1, end - index - 1);
  139. index = end + 1;
  140. if (index == input.Length)
  141. return;
  142. if (!XmlChar.IsWhitespace (input [index]))
  143. throw new XmlException ("Invalid XML declaration.");
  144. index = SkipWhitespace (input, index + 1);
  145. return;
  146. }
  147. if (input.Length > index + 10 && input.IndexOf ("standalone", index, 10) > 0) {
  148. index = SkipWhitespace (input, index + 10);
  149. c = input [index];
  150. if (c != '=')
  151. throw new XmlException ("Invalid 'version' specification.");
  152. index++;
  153. index = SkipWhitespace (input, index);
  154. c = input [index];
  155. if (c != '"' && c != '\'')
  156. throw new XmlException ("Invalid 'standalone' specification.");
  157. end = input.IndexOf (c, index + 1);
  158. if (end < 0)
  159. throw new XmlException ("Invalid 'standalone' specification.");
  160. string tmp = input.Substring (index + 1, end - index - 1);
  161. switch (tmp) {
  162. case "yes":
  163. case "no":
  164. break;
  165. default:
  166. throw new XmlException ("Invalid standalone specification.");
  167. }
  168. Standalone = tmp;
  169. index = end + 1;
  170. index = SkipWhitespace (input, index);
  171. }
  172. if (index != input.Length)
  173. throw new XmlException ("Invalid XML declaration.");
  174. }
  175. }
  176. }