XmlDeclaration.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // System.Xml.XmlDeclaration
  3. //
  4. // Author:
  5. // Duncan Mak ([email protected])
  6. //
  7. // (C) Ximian, Inc.
  8. using System;
  9. using System.Xml;
  10. using System.Text.RegularExpressions;
  11. namespace System.Xml
  12. {
  13. public class XmlDeclaration : XmlLinkedNode
  14. {
  15. string encoding = "UTF-8"; // defaults to UTF-8
  16. string standalone;
  17. string version;
  18. protected internal XmlDeclaration (string version, string encoding,
  19. string standalone, XmlDocument doc)
  20. : base (doc)
  21. {
  22. if (encoding == null)
  23. encoding = "";
  24. if (standalone == null)
  25. standalone = "";
  26. this.version = version;
  27. this.encoding = encoding;
  28. this.standalone = standalone;
  29. }
  30. public string Encoding {
  31. get { return encoding; }
  32. set { encoding = (value == null) ? String.Empty : value; }
  33. }
  34. public override string InnerText {
  35. get { return Value; }
  36. set { ParseInput (value); }
  37. }
  38. public override string LocalName {
  39. get { return "xml"; }
  40. }
  41. public override string Name {
  42. get { return "xml"; }
  43. }
  44. public override XmlNodeType NodeType {
  45. get { return XmlNodeType.XmlDeclaration; }
  46. }
  47. public string Standalone {
  48. get { return standalone; }
  49. set {
  50. if(value != null)
  51. {
  52. if (value.ToUpper() == "YES")
  53. standalone = "yes";
  54. if (value.ToUpper() == "NO")
  55. standalone = "no";
  56. }
  57. else
  58. standalone = String.Empty;
  59. }
  60. }
  61. public override string Value {
  62. get {
  63. string formatEncoding = "";
  64. string formatStandalone = "";
  65. if (encoding != String.Empty)
  66. formatEncoding = String.Format (" encoding=\"{0}\"", encoding);
  67. if (standalone != String.Empty)
  68. formatStandalone = String.Format (" standalone=\"{0}\"", standalone);
  69. return String.Format ("version=\"{0}\"{1}{2}", Version, formatEncoding, formatStandalone);
  70. }
  71. set { ParseInput (value); }
  72. }
  73. public string Version {
  74. get { return version; }
  75. }
  76. public override XmlNode CloneNode (bool deep)
  77. {
  78. return new XmlDeclaration (Version, Encoding, standalone, OwnerDocument);
  79. }
  80. public override void WriteContentTo (XmlWriter w) {}
  81. public override void WriteTo (XmlWriter w)
  82. {
  83. // This doesn't seem to match up very well with w.WriteStartDocument()
  84. // so writing out custom here.
  85. w.WriteRaw (String.Format ("<?xml {0}?>", Value));
  86. }
  87. void ParseInput (string input)
  88. {
  89. // Encoding = input.Split (new char [] { ' ' }) [1].Split (new char [] { '=' }) [1];
  90. // Standalone = input.Split (new char [] { ' ' }) [2].Split (new char [] { '=' }) [1];
  91. Match m = XmlDeclRegex.Match(input);
  92. if(!m.Success)
  93. throw new XmlException("illegal XML declaration format.");
  94. // Version = m.Result("${ver}");
  95. Encoding = m.Result("${enc}");
  96. Standalone = m.Result("${sta}");
  97. }
  98. // This regular expression matches XMLDecl of XML specification BNF[23]
  99. static Regex xmlDeclRegex;
  100. Regex XmlDeclRegex
  101. {
  102. get
  103. {
  104. if(xmlDeclRegex == null) xmlDeclRegex = new Regex(allMatch, RegexOptions.Compiled);
  105. return xmlDeclRegex;
  106. }
  107. }
  108. // This code makes some loss, but you may understand a bit easily.
  109. const string verMatch = "\\s*version\\s*=\\s*(\\'(?<ver>.*?)\\'|\\\"(?<ver>.*?)\")";
  110. const string encMatch = "\\s*encoding\\s*=\\s*(\\'(?<enc>.*?)\\'|\\\"(?<enc>.*?)\")";
  111. const string staMatch = "\\s*standalone\\s*=\\s*(\\'(?<sta>.*?)\\'|\\\"(?<sta>.*?)\")";
  112. const string allMatch = verMatch + "(" + encMatch + ")?(" + staMatch + ")?";
  113. }
  114. }