XmlDeclaration.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.ToUpper() == "YES")
  51. standalone = "yes";
  52. if (value.ToUpper() == "NO")
  53. standalone = "no";
  54. }
  55. }
  56. public override string Value {
  57. get {
  58. string formatEncoding = "";
  59. string formatStandalone = "";
  60. if (encoding != String.Empty)
  61. formatEncoding = String.Format (" encoding=\"{0}\"", encoding);
  62. if (standalone != String.Empty)
  63. formatStandalone = String.Format (" standalone=\"{0}\"", standalone);
  64. return String.Format ("version=\"{0}\"{1}{2}", Version, formatEncoding, formatStandalone);
  65. }
  66. set { ParseInput (value); }
  67. }
  68. public string Version {
  69. get { return version; }
  70. }
  71. public override XmlNode CloneNode (bool deep)
  72. {
  73. return new XmlDeclaration (Version, Encoding, standalone, OwnerDocument);
  74. }
  75. public override void WriteContentTo (XmlWriter w) {}
  76. public override void WriteTo (XmlWriter w)
  77. {
  78. // This doesn't seem to match up very well with w.WriteStartDocument()
  79. // so writing out custom here.
  80. w.WriteRaw (String.Format ("<?xml {0}?>", Value));
  81. }
  82. void ParseInput (string input)
  83. {
  84. // Encoding = input.Split (new char [] { ' ' }) [1].Split (new char [] { '=' }) [1];
  85. // Standalone = input.Split (new char [] { ' ' }) [2].Split (new char [] { '=' }) [1];
  86. Match m = XmlDeclRegex.Match(input);
  87. if(!m.Success)
  88. throw new XmlException("illegal XML declaration format.");
  89. // Version = m.Result("${ver}");
  90. Encoding = m.Result("${enc}");
  91. Standalone = m.Result("${sta}");
  92. }
  93. // This regular expression matches XMLDecl of XML specification BNF[23]
  94. static Regex xmlDeclRegex;
  95. Regex XmlDeclRegex
  96. {
  97. get
  98. {
  99. if(xmlDeclRegex == null) xmlDeclRegex = new Regex(allMatch, RegexOptions.Compiled);
  100. return xmlDeclRegex;
  101. }
  102. }
  103. // This code makes some loss, but you may understand a bit easily.
  104. const string verMatch = "\\s*version\\s*=\\s*(\\'(?<ver>.*?)\\'|\\\"(?<ver>.*)\")";
  105. const string encMatch = "\\s*encoding\\s*=\\s*(\\'(?<enc>.*?)\\'|\\\"(?<enc>.*)\")";
  106. const string staMatch = "\\s*standalone\\s*=\\s*(\\'(?<sta>.*?)\\'|\\\"(?<sta>.*)\")";
  107. const string allMatch = verMatch + "(" + encMatch + ")?(" + staMatch + ")?";
  108. }
  109. }