DataObject.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //
  2. // DataObject.cs - DataObject implementation for XML Signature
  3. // http://www.w3.org/2000/09/xmldsig#Object
  4. //
  5. // Author:
  6. // Sebastien Pouliot ([email protected])
  7. //
  8. // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
  9. //
  10. using System.Text;
  11. using System.Xml;
  12. namespace System.Security.Cryptography.Xml {
  13. // XmlElement part of the signature
  14. // Note: Looks like KeyInfoNode (but the later is XmlElement inside KeyInfo)
  15. // required for "enveloping signatures"
  16. public class DataObject {
  17. private string id;
  18. private string mimeType;
  19. private string encoding;
  20. private XmlDocument doc;
  21. static private string xmldsig = "http://www.w3.org/2000/09/xmldsig#";
  22. public DataObject ()
  23. {
  24. Build (null, null, null, null);
  25. }
  26. public DataObject (string id, string mimeType, string encoding, XmlElement data)
  27. {
  28. if (data == null)
  29. throw new ArgumentNullException ("data");
  30. Build (id, mimeType, encoding, data);
  31. }
  32. private void Build (string id, string mimeType, string encoding, XmlElement data)
  33. {
  34. StringBuilder sb = new StringBuilder ();
  35. sb.Append ("<Object ");
  36. if (id != null) {
  37. this.id = id;
  38. sb.Append ("Id=\"");
  39. sb.Append (id);
  40. sb.Append ("\" ");
  41. }
  42. if (mimeType != null) {
  43. this.mimeType = mimeType;
  44. sb.Append ("MimeType=\"");
  45. sb.Append (mimeType);
  46. sb.Append ("\" ");
  47. }
  48. if (encoding != null) {
  49. this.encoding = encoding;
  50. sb.Append ("Encoding=\"");
  51. sb.Append (encoding);
  52. sb.Append ("\" ");
  53. }
  54. sb.Append ("xmlns=\"http://www.w3.org/2000/09/xmldsig#\" />");
  55. doc = new XmlDocument ();
  56. doc.LoadXml (sb.ToString ());
  57. if (data != null) {
  58. XmlNodeList xnl = doc.GetElementsByTagName ("Object");
  59. XmlNode newNode = doc.ImportNode (data, true);
  60. xnl[0].AppendChild (newNode);
  61. }
  62. }
  63. // why is data a XmlNodeList instead of a XmlElement ?
  64. public XmlNodeList Data {
  65. get {
  66. XmlNodeList xnl = doc.GetElementsByTagName ("Object");
  67. return xnl[0].ChildNodes;
  68. }
  69. set {
  70. if (value == null)
  71. throw new ArgumentNullException ("value");
  72. Build (id, mimeType, encoding, null);
  73. XmlNodeList xnl = doc.GetElementsByTagName ("Object");
  74. if ((xnl != null) && (xnl.Count > 0)) {
  75. foreach (XmlNode xn in value) {
  76. XmlNode newNode = doc.ImportNode (xn, true);
  77. xnl[0].AppendChild (newNode);
  78. }
  79. }
  80. }
  81. }
  82. // default to null - no encoding
  83. public string Encoding {
  84. get { return encoding; }
  85. set { encoding = value; }
  86. }
  87. // default to null
  88. public string Id {
  89. get { return id; }
  90. set { id = value; }
  91. }
  92. // default to null
  93. public string MimeType {
  94. get { return mimeType; }
  95. set { mimeType = value; }
  96. }
  97. public XmlElement GetXml ()
  98. {
  99. if ((doc.DocumentElement.LocalName == "Object") && (doc.DocumentElement.NamespaceURI == xmldsig)) {
  100. // recreate all attributes in order
  101. XmlAttribute xa = null;
  102. doc.DocumentElement.Attributes.RemoveAll ();
  103. if (id != null) {
  104. xa = doc.CreateAttribute ("Id");
  105. xa.Value = id;
  106. doc.DocumentElement.Attributes.Append (xa);
  107. }
  108. if (mimeType != null) {
  109. xa = doc.CreateAttribute ("MimeType");
  110. xa.Value = mimeType;
  111. doc.DocumentElement.Attributes.Append (xa);
  112. }
  113. if (encoding != null) {
  114. xa = doc.CreateAttribute ("Encoding");
  115. xa.Value = encoding;
  116. doc.DocumentElement.Attributes.Append (xa);
  117. }
  118. xa = doc.CreateAttribute ("xmlns");
  119. xa.Value = xmldsig;
  120. doc.DocumentElement.Attributes.Append (xa);
  121. }
  122. return doc.DocumentElement;
  123. }
  124. public void LoadXml (XmlElement value)
  125. {
  126. if (value == null)
  127. throw new ArgumentNullException ("value");
  128. if ((value.LocalName == "Object") && (value.NamespaceURI == xmldsig)) {
  129. doc.LoadXml (value.OuterXml);
  130. XmlAttribute xa = value.Attributes ["Id"];
  131. id = ((xa != null) ? xa.InnerText : null);
  132. xa = value.Attributes ["MimeType"];
  133. mimeType = ((xa != null) ? xa.InnerText : null);
  134. xa = value.Attributes ["Encoding"];
  135. encoding = ((xa != null) ? xa.InnerText : null);
  136. }
  137. else
  138. doc.LoadXml (value.OuterXml);
  139. }
  140. }
  141. }