XmlDsigXPathTransform.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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, 2003 Motus Technologies Inc. (http://www.motus.com)
  10. // (C) 2004 Novell (http://www.novell.com)
  11. //
  12. using System.IO;
  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. [MonoTODO]
  19. public class XmlDsigXPathTransform : Transform {
  20. private Type[] input;
  21. private Type[] output;
  22. private XmlNodeList xpath;
  23. private XmlDocument doc;
  24. public XmlDsigXPathTransform ()
  25. {
  26. Algorithm = "http://www.w3.org/TR/1999/REC-xpath-19991116";
  27. }
  28. public override Type[] InputTypes {
  29. get {
  30. if (input == null) {
  31. lock (this) {
  32. // this way the result is cached if called multiple time
  33. input = new Type [3];
  34. input[0] = typeof (System.IO.Stream);
  35. input[1] = typeof (System.Xml.XmlDocument);
  36. input[2] = typeof (System.Xml.XmlNodeList);
  37. }
  38. }
  39. return input;
  40. }
  41. }
  42. public override Type[] OutputTypes {
  43. get {
  44. if (output == null) {
  45. lock (this) {
  46. // this way the result is cached if called multiple time
  47. output = new Type [1];
  48. output[0] = typeof (System.Xml.XmlNodeList);
  49. }
  50. }
  51. return output;
  52. }
  53. }
  54. protected override XmlNodeList GetInnerXml ()
  55. {
  56. if (xpath == null) {
  57. // default value
  58. XmlDocument doc = new XmlDocument ();
  59. doc.LoadXml ("<XPath xmlns=\"" + XmlSignature.NamespaceURI + "\"></XPath>");
  60. xpath = doc.ChildNodes;
  61. }
  62. return xpath;
  63. }
  64. public override object GetOutput ()
  65. {
  66. // note: this will throw a NullReferenceException if
  67. // doc is null - just like MS implementation does
  68. if ((xpath == null) || (xpath.Count < 1)) {
  69. // can't create an XmlNodeList
  70. XmlDocument xd = new XmlDocument ();
  71. return xd.ChildNodes;
  72. }
  73. return doc.ChildNodes;
  74. //* I know it doesn't make a lot of sense - but this is what the MS framework
  75. //* returns - I must miss something really bad
  76. //* return doc.DocumentElement.SelectNodes (xpath [0].InnerXml);
  77. }
  78. public override object GetOutput (Type type)
  79. {
  80. if (type != typeof (XmlNodeList))
  81. throw new ArgumentException ("type");
  82. return GetOutput ();
  83. }
  84. public override void LoadInnerXml (XmlNodeList nodeList)
  85. {
  86. if (nodeList == null)
  87. throw new CryptographicException ("nodeList");
  88. xpath = nodeList;
  89. }
  90. public override void LoadInput (object obj)
  91. {
  92. // possible input: Stream, XmlDocument, and XmlNodeList
  93. if (obj is Stream) {
  94. doc = new XmlDocument ();
  95. doc.XmlResolver = GetResolver ();
  96. doc.Load (obj as Stream);
  97. }
  98. else if (obj is XmlDocument) {
  99. doc = (obj as XmlDocument);
  100. }
  101. else if (obj is XmlNodeList) {
  102. doc = new XmlDocument ();
  103. doc.XmlResolver = GetResolver ();
  104. foreach (XmlNode xn in (obj as XmlNodeList)) {
  105. XmlNode importedNode = doc.ImportNode (xn, true);
  106. doc.AppendChild (importedNode);
  107. }
  108. }
  109. }
  110. }
  111. }