XmlDsigXPathTransform.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //
  2. // XmlDsigXPathTransform.cs -
  3. // XmlDsigXPathTransform implementation for XML Signature
  4. // http://www.w3.org/TR/1999/REC-xpath-19991116
  5. //
  6. // Author:
  7. // Sebastien Pouliot ([email protected])
  8. //
  9. // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
  10. //
  11. using System.IO;
  12. using System.Security.Cryptography;
  13. using System.Text;
  14. using System.Xml;
  15. namespace System.Security.Cryptography.Xml {
  16. // www.w3.org/TR/xmldsig-core/
  17. // see Section 6.6.3 of the XMLDSIG specification
  18. public class XmlDsigXPathTransform : Transform {
  19. private XmlNodeList xnl;
  20. private XmlNodeList xpathNodes;
  21. public XmlDsigXPathTransform ()
  22. {
  23. }
  24. public override Type[] InputTypes {
  25. get {
  26. if (input == null) {
  27. lock (this) {
  28. // this way the result is cached if called multiple time
  29. input = new Type [3];
  30. input[0] = typeof (System.IO.Stream);
  31. input[1] = typeof (System.Xml.XmlDocument);
  32. input[2] = typeof (System.Xml.XmlNodeList);
  33. }
  34. }
  35. return input;
  36. }
  37. }
  38. public override Type[] OutputTypes {
  39. get {
  40. if (output == null) {
  41. lock (this) {
  42. // this way the result is cached if called multiple time
  43. output = new Type [1];
  44. output[0] = typeof (System.IO.Stream);
  45. }
  46. }
  47. return output;
  48. }
  49. }
  50. protected override XmlNodeList GetInnerXml ()
  51. {
  52. return xnl;
  53. }
  54. public override object GetOutput ()
  55. {
  56. return xpathNodes;
  57. }
  58. public override object GetOutput (Type type)
  59. {
  60. if (type != typeof (XmlNodeList))
  61. throw new ArgumentException ("type");
  62. return GetOutput ();
  63. }
  64. public override void LoadInnerXml (XmlNodeList nodeList)
  65. {
  66. if (nodeList == null)
  67. throw new CryptographicException ("nodeList");
  68. xnl = nodeList;
  69. }
  70. public override void LoadInput (object obj)
  71. {
  72. XmlNode xn = null;
  73. // possible input: Stream, XmlDocument, and XmlNodeList
  74. if (obj is Stream) {
  75. XmlDocument doc = new XmlDocument ();
  76. doc.Load (obj as Stream);
  77. }
  78. else if (obj is XmlDocument) {
  79. }
  80. else if (obj is XmlNodeList) {
  81. xnl = (XmlNodeList) obj;
  82. }
  83. if (xn != null) {
  84. string xpath = xn.InnerXml;
  85. // only possible output: XmlNodeList
  86. xpathNodes = xnl[0].SelectNodes (xpath);
  87. }
  88. else
  89. xpathNodes = null;
  90. }
  91. }
  92. }