XmlDsigXsltTransform.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //
  2. // XmlDsigEnvelopedSignatureTransform.cs -
  3. // Enveloped Signature Transform implementation for XML Signature
  4. // http://www.w3.org/TR/1999/REC-xslt-19991116
  5. //
  6. // Author:
  7. // Sebastien Pouliot ([email protected])
  8. // Atsushi Enomoto ([email protected])
  9. //
  10. // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
  11. // (C) 2004 Novell Inc.
  12. //
  13. using System.IO;
  14. using System.Xml;
  15. using System.Xml.Xsl;
  16. namespace System.Security.Cryptography.Xml
  17. {
  18. public class XmlDsigXsltTransform : Transform
  19. {
  20. private Type [] input;
  21. private Type [] output;
  22. private bool comments;
  23. private XmlNodeList xnl;
  24. private XmlDocument inputDoc;
  25. public XmlDsigXsltTransform () : this (false)
  26. {
  27. }
  28. public XmlDsigXsltTransform (bool includeComments)
  29. {
  30. comments = includeComments;
  31. Algorithm = "http://www.w3.org/TR/1999/REC-xslt-19991116";
  32. }
  33. public override Type [] InputTypes {
  34. get {
  35. if (input == null) {
  36. lock (this) {
  37. // this way the result is cached if called multiple time
  38. input = new Type [3];
  39. input [0] = typeof (System.IO.Stream);
  40. input [1] = typeof (System.Xml.XmlDocument);
  41. input [2] = typeof (System.Xml.XmlNodeList);
  42. }
  43. }
  44. return input;
  45. }
  46. }
  47. public override Type [] OutputTypes {
  48. get {
  49. if (output == null) {
  50. lock (this) {
  51. // this way the result is cached if called multiple time
  52. output = new Type [1];
  53. output [0] = typeof (System.IO.Stream);
  54. }
  55. }
  56. return output;
  57. }
  58. }
  59. protected override XmlNodeList GetInnerXml ()
  60. {
  61. return xnl;
  62. }
  63. public override object GetOutput ()
  64. {
  65. XslTransform xsl = new XslTransform ();
  66. XmlDocument doc = new XmlDocument ();
  67. #if ! NET_1_0
  68. doc.XmlResolver = GetResolver ();
  69. #endif
  70. foreach (XmlNode n in xnl)
  71. doc.AppendChild (doc.ImportNode (n, true));
  72. xsl.Load (doc);
  73. MemoryStream stream = null;
  74. stream = new MemoryStream ();
  75. // only possible output: Stream
  76. xsl.Transform (inputDoc, null, stream);
  77. stream.Seek (0, SeekOrigin.Begin);
  78. return stream;
  79. }
  80. public override object GetOutput (Type type)
  81. {
  82. if (type != Type.GetType ("System.IO.Stream"))
  83. throw new ArgumentException ("type");
  84. return GetOutput ();
  85. }
  86. public override void LoadInnerXml (XmlNodeList nodeList)
  87. {
  88. if (nodeList == null)
  89. throw new CryptographicException ("nodeList");
  90. xnl = nodeList;
  91. }
  92. public override void LoadInput (object obj)
  93. {
  94. // possible input: Stream, XmlDocument, and XmlNodeList
  95. if (obj is Stream) {
  96. inputDoc = new XmlDocument ();
  97. inputDoc.Load (obj as Stream);
  98. }
  99. else if (obj is XmlDocument) {
  100. inputDoc= obj as XmlDocument;
  101. }
  102. else if (obj is XmlNodeList) {
  103. inputDoc = new XmlDocument ();
  104. XmlNodeList nl = (XmlNodeList) obj;
  105. for (int i = 0; i < nl.Count; i++)
  106. inputDoc.AppendChild (inputDoc.ImportNode (nl [i], true));
  107. }
  108. }
  109. }
  110. }