XmlDsigXsltTransform.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. XmlResolver resolver = GetResolver ();
  66. XslTransform xsl = new XslTransform ();
  67. XmlDocument doc = new XmlDocument ();
  68. #if NET_1_1
  69. doc.XmlResolver = resolver;
  70. #endif
  71. foreach (XmlNode n in xnl)
  72. doc.AppendChild (doc.ImportNode (n, true));
  73. #if NET_1_1
  74. xsl.Load (doc, resolver);
  75. #else
  76. xsl.Load (doc);
  77. #endif
  78. if (inputDoc == null)
  79. throw new NullReferenceException ("Load input document before transformation.");
  80. MemoryStream stream = new MemoryStream ();
  81. // only possible output: Stream
  82. #if NET_1_1
  83. xsl.XmlResolver = resolver;
  84. #endif
  85. xsl.Transform (inputDoc, null, stream);
  86. stream.Seek (0, SeekOrigin.Begin);
  87. return stream;
  88. }
  89. public override object GetOutput (Type type)
  90. {
  91. if (type != Type.GetType ("System.IO.Stream"))
  92. throw new ArgumentException ("type");
  93. return GetOutput ();
  94. }
  95. public override void LoadInnerXml (XmlNodeList nodeList)
  96. {
  97. if (nodeList == null)
  98. throw new CryptographicException ("nodeList");
  99. xnl = nodeList;
  100. }
  101. public override void LoadInput (object obj)
  102. {
  103. // possible input: Stream, XmlDocument, and XmlNodeList
  104. if (obj is Stream) {
  105. inputDoc = new XmlDocument ();
  106. #if NET_1_1
  107. inputDoc.XmlResolver = GetResolver ();
  108. #endif
  109. inputDoc.Load (obj as Stream);
  110. }
  111. else if (obj is XmlDocument) {
  112. inputDoc= obj as XmlDocument;
  113. }
  114. else if (obj is XmlNodeList) {
  115. inputDoc = new XmlDocument ();
  116. #if NET_1_1
  117. inputDoc.XmlResolver = GetResolver ();
  118. #endif
  119. XmlNodeList nl = (XmlNodeList) obj;
  120. for (int i = 0; i < nl.Count; i++)
  121. inputDoc.AppendChild (inputDoc.ImportNode (nl [i], true));
  122. }
  123. }
  124. }
  125. }