XmlDeclaration.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. //
  2. // System.Xml.XmlDeclaration
  3. //
  4. // Author:
  5. // Duncan Mak ([email protected])
  6. // Atsushi Enomotot ([email protected])
  7. //
  8. // (C) Ximian, Inc.
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Globalization;
  31. using System.Text;
  32. using System.Xml;
  33. namespace System.Xml
  34. {
  35. public class XmlDeclaration : XmlLinkedNode
  36. {
  37. string encoding = "UTF-8"; // defaults to UTF-8
  38. string standalone;
  39. string version;
  40. protected internal XmlDeclaration (string version, string encoding,
  41. string standalone, XmlDocument doc)
  42. : base (doc)
  43. {
  44. if (encoding == null)
  45. encoding = "";
  46. if (standalone == null)
  47. standalone = "";
  48. this.version = version;
  49. this.encoding = encoding;
  50. this.standalone = standalone;
  51. }
  52. public string Encoding {
  53. get { return encoding; }
  54. set { encoding = (value == null) ? String.Empty : value; }
  55. }
  56. public override string InnerText {
  57. get { return Value; }
  58. set { ParseInput (value); }
  59. }
  60. public override string LocalName {
  61. get { return "xml"; }
  62. }
  63. public override string Name {
  64. get { return "xml"; }
  65. }
  66. public override XmlNodeType NodeType {
  67. get { return XmlNodeType.XmlDeclaration; }
  68. }
  69. public string Standalone {
  70. get { return standalone; }
  71. set {
  72. if(value != null)
  73. {
  74. if (String.Compare (value, "YES", true, CultureInfo.InvariantCulture) == 0)
  75. standalone = "yes";
  76. if (String.Compare (value, "NO", true, CultureInfo.InvariantCulture) == 0)
  77. standalone = "no";
  78. }
  79. else
  80. standalone = String.Empty;
  81. }
  82. }
  83. public override string Value {
  84. get {
  85. string formatEncoding = "";
  86. string formatStandalone = "";
  87. if (encoding != String.Empty)
  88. formatEncoding = String.Format (" encoding=\"{0}\"", encoding);
  89. if (standalone != String.Empty)
  90. formatStandalone = String.Format (" standalone=\"{0}\"", standalone);
  91. return String.Format ("version=\"{0}\"{1}{2}", Version, formatEncoding, formatStandalone);
  92. }
  93. set { ParseInput (value); }
  94. }
  95. public string Version {
  96. get { return version; }
  97. }
  98. public override XmlNode CloneNode (bool deep)
  99. {
  100. return new XmlDeclaration (Version, Encoding, standalone, OwnerDocument);
  101. }
  102. public override void WriteContentTo (XmlWriter w) {}
  103. public override void WriteTo (XmlWriter w)
  104. {
  105. // This doesn't seem to match up very well with w.WriteStartDocument()
  106. // so writing out custom here.
  107. w.WriteRaw (String.Format ("<?xml {0}?>", Value));
  108. }
  109. private int SkipWhitespace (string input, int index)
  110. {
  111. while (index < input.Length) {
  112. if (XmlChar.IsWhitespace (input [index]))
  113. index++;
  114. else
  115. break;
  116. }
  117. return index;
  118. }
  119. void ParseInput (string input)
  120. {
  121. int index = SkipWhitespace (input, 0);
  122. if (index + 7 > input.Length || input.IndexOf ("version", index, 7) != index)
  123. throw new XmlException ("Missing 'version' specification.");
  124. index = SkipWhitespace (input, index + 7);
  125. char c = input [index];
  126. if (c != '=')
  127. throw new XmlException ("Invalid 'version' specification.");
  128. index++;
  129. index = SkipWhitespace (input, index);
  130. c = input [index];
  131. if (c != '"' && c != '\'')
  132. throw new XmlException ("Invalid 'version' specification.");
  133. index++;
  134. int end = input.IndexOf (c, index);
  135. if (end < 0 || input.IndexOf ("1.0", index, 3) != index)
  136. throw new XmlException ("Invalid 'version' specification.");
  137. index += 4;
  138. if (index == input.Length)
  139. return;
  140. if (!XmlChar.IsWhitespace (input [index]))
  141. throw new XmlException ("Invalid XML declaration.");
  142. index = SkipWhitespace (input, index + 1);
  143. if (index == input.Length)
  144. return;
  145. if (input.Length > index + 8 && input.IndexOf ("encoding", index, 8) > 0) {
  146. index = SkipWhitespace (input, index + 8);
  147. c = input [index];
  148. if (c != '=')
  149. throw new XmlException ("Invalid 'version' specification.");
  150. index++;
  151. index = SkipWhitespace (input, index);
  152. c = input [index];
  153. if (c != '"' && c != '\'')
  154. throw new XmlException ("Invalid 'encoding' specification.");
  155. end = input.IndexOf (c, index + 1);
  156. if (end < 0)
  157. throw new XmlException ("Invalid 'encoding' specification.");
  158. Encoding = input.Substring (index + 1, end - index - 1);
  159. index = end + 1;
  160. if (index == input.Length)
  161. return;
  162. if (!XmlChar.IsWhitespace (input [index]))
  163. throw new XmlException ("Invalid XML declaration.");
  164. index = SkipWhitespace (input, index + 1);
  165. return;
  166. }
  167. if (input.Length > index + 10 && input.IndexOf ("standalone", index, 10) > 0) {
  168. index = SkipWhitespace (input, index + 10);
  169. c = input [index];
  170. if (c != '=')
  171. throw new XmlException ("Invalid 'version' specification.");
  172. index++;
  173. index = SkipWhitespace (input, index);
  174. c = input [index];
  175. if (c != '"' && c != '\'')
  176. throw new XmlException ("Invalid 'standalone' specification.");
  177. end = input.IndexOf (c, index + 1);
  178. if (end < 0)
  179. throw new XmlException ("Invalid 'standalone' specification.");
  180. string tmp = input.Substring (index + 1, end - index - 1);
  181. switch (tmp) {
  182. case "yes":
  183. case "no":
  184. break;
  185. default:
  186. throw new XmlException ("Invalid standalone specification.");
  187. }
  188. Standalone = tmp;
  189. index = end + 1;
  190. index = SkipWhitespace (input, index);
  191. }
  192. if (index != input.Length)
  193. throw new XmlException ("Invalid XML declaration.");
  194. }
  195. }
  196. }