DataObject.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.Xml;
  11. namespace System.Security.Cryptography.Xml {
  12. // XmlElement part of the signature
  13. // Note: Looks like KeyInfoNode (but the later is XmlElement inside KeyInfo)
  14. // required for "enveloping signatures"
  15. public class DataObject {
  16. private string id;
  17. private string mimeType;
  18. private string encoding;
  19. private XmlDocument document;
  20. public DataObject ()
  21. {
  22. Build (null, null, null, null);
  23. }
  24. public DataObject (string id, string mimeType, string encoding, XmlElement data)
  25. {
  26. if (data == null)
  27. throw new ArgumentNullException ("data");
  28. Build (id, mimeType, encoding, data);
  29. }
  30. // this one accept a null "data" parameter
  31. private void Build (string id, string mimeType, string encoding, XmlElement data)
  32. {
  33. document = new XmlDocument ();
  34. XmlElement xel = document.CreateElement (XmlSignature.ElementNames.Object, XmlSignature.NamespaceURI);
  35. if (id != null) {
  36. this.id = id;
  37. xel.SetAttribute (XmlSignature.AttributeNames.Id, id);
  38. }
  39. if (mimeType != null) {
  40. this.mimeType = mimeType;
  41. xel.SetAttribute (XmlSignature.AttributeNames.MimeType, mimeType);
  42. }
  43. if (encoding != null) {
  44. this.encoding = encoding;
  45. xel.SetAttribute (XmlSignature.AttributeNames.Encoding, encoding);
  46. }
  47. if (data != null) {
  48. XmlNode newNode = document.ImportNode (data, true);
  49. xel.AppendChild (newNode);
  50. }
  51. document.AppendChild (xel);
  52. }
  53. // why is data a XmlNodeList instead of a XmlElement ?
  54. public XmlNodeList Data {
  55. get {
  56. XmlNodeList xnl = document.GetElementsByTagName (XmlSignature.ElementNames.Object);
  57. return xnl[0].ChildNodes;
  58. }
  59. set {
  60. if (value == null)
  61. throw new ArgumentNullException ("value");
  62. Build (id, mimeType, encoding, null);
  63. XmlNodeList xnl = document.GetElementsByTagName (XmlSignature.ElementNames.Object);
  64. if ((xnl != null) && (xnl.Count > 0)) {
  65. foreach (XmlNode xn in value) {
  66. XmlNode newNode = document.ImportNode (xn, true);
  67. xnl [0].AppendChild (newNode);
  68. }
  69. }
  70. }
  71. }
  72. // default to null - no encoding
  73. public string Encoding {
  74. get { return encoding; }
  75. set { encoding = value; }
  76. }
  77. // default to null
  78. public string Id {
  79. get { return id; }
  80. set { id = value; }
  81. }
  82. // default to null
  83. public string MimeType {
  84. get { return mimeType; }
  85. set { mimeType = value; }
  86. }
  87. public XmlElement GetXml ()
  88. {
  89. if ((document.DocumentElement.LocalName == XmlSignature.ElementNames.Object) && (document.DocumentElement.NamespaceURI == XmlSignature.NamespaceURI)) {
  90. // recreate all attributes in order
  91. XmlAttribute xa = null;
  92. document.DocumentElement.Attributes.RemoveAll ();
  93. if (id != null) {
  94. xa = document.CreateAttribute (XmlSignature.AttributeNames.Id);
  95. xa.Value = id;
  96. document.DocumentElement.Attributes.Append (xa);
  97. }
  98. if (mimeType != null) {
  99. xa = document.CreateAttribute (XmlSignature.AttributeNames.MimeType);
  100. xa.Value = mimeType;
  101. document.DocumentElement.Attributes.Append (xa);
  102. }
  103. if (encoding != null) {
  104. xa = document.CreateAttribute (XmlSignature.AttributeNames.Encoding);
  105. xa.Value = encoding;
  106. document.DocumentElement.Attributes.Append (xa);
  107. }
  108. xa = document.CreateAttribute ("xmlns");
  109. xa.Value = XmlSignature.NamespaceURI;
  110. document.DocumentElement.Attributes.Append (xa);
  111. }
  112. return document.DocumentElement;
  113. }
  114. public void LoadXml (XmlElement value)
  115. {
  116. if (value == null)
  117. throw new ArgumentNullException ("value");
  118. if ((value.LocalName != XmlSignature.ElementNames.Object) || (value.NamespaceURI != XmlSignature.NamespaceURI)) {
  119. document.LoadXml (value.OuterXml);
  120. }
  121. else {
  122. document.LoadXml (value.OuterXml);
  123. XmlAttribute xa = value.Attributes [XmlSignature.AttributeNames.Id];
  124. id = ((xa != null) ? xa.InnerText : null);
  125. xa = value.Attributes [XmlSignature.AttributeNames.MimeType];
  126. mimeType = ((xa != null) ? xa.InnerText : null);
  127. xa = value.Attributes [XmlSignature.AttributeNames.Encoding];
  128. encoding = ((xa != null) ? xa.InnerText : null);
  129. }
  130. }
  131. }
  132. }