2004-03-16 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.Security / System.Security.Cryptography.Xml / XmlDsigXsltTransform.cs
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 (spouliot@motus.com)
8 //      Atsushi Enomoto (atsushi@ximian.com)
9 //
10 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
11 // (C) 2004 Novell Inc.
12 //
13
14 using System.IO;
15 using System.Xml;
16 using System.Xml.Xsl;
17
18 namespace System.Security.Cryptography.Xml 
19 {
20
21         public class XmlDsigXsltTransform : Transform 
22         {
23
24                 private Type [] input;
25                 private Type [] output;
26                 private bool comments;
27                 private XmlNodeList xnl;
28                 private XmlDocument inputDoc;
29
30                 public XmlDsigXsltTransform () : this (false)
31                 {
32                 }
33
34                 public XmlDsigXsltTransform (bool includeComments) 
35                 {
36                         comments = includeComments;
37                         Algorithm = "http://www.w3.org/TR/1999/REC-xslt-19991116";
38                 }
39
40                 public override Type [] InputTypes {
41                         get {
42                                 if (input == null) {
43                                         lock (this) {
44                                                 // this way the result is cached if called multiple time
45                                                 input = new Type [3];
46                                                 input [0] = typeof (System.IO.Stream);
47                                                 input [1] = typeof (System.Xml.XmlDocument);
48                                                 input [2] = typeof (System.Xml.XmlNodeList);
49                                         }
50                                 }
51                                 return input;
52                         }
53                 }
54
55                 public override Type [] OutputTypes {
56                         get {
57                                 if (output == null) {
58                                         lock (this) {
59                                                 // this way the result is cached if called multiple time
60                                                 output = new Type [1];
61                                                 output [0] = typeof (System.IO.Stream);
62                                         }
63                                 }
64                                 return output;
65                         }
66                 }
67                         
68                 protected override XmlNodeList GetInnerXml () 
69                 {
70                         return xnl;
71                 }
72
73                 public override object GetOutput () 
74                 {
75                         XslTransform xsl = new XslTransform ();
76                         XmlDocument doc = new XmlDocument ();
77                         doc.XmlResolver = GetResolver ();
78                         foreach (XmlNode n in xnl)
79                                 doc.AppendChild (doc.ImportNode (n, true));
80                         xsl.Load (doc);
81
82                         Stream stream = null;
83
84                         stream = new MemoryStream ();
85                         // only possible output: Stream
86                         xsl.Transform (inputDoc, null, stream);
87
88                         CryptoStream cs = null;
89                         if (stream != null)
90                                 cs = new CryptoStream (stream, new FromBase64Transform (), CryptoStreamMode.Read);
91                         // note: there is no default are other types won't throw an exception
92                         return cs;
93                 }
94
95                 public override object GetOutput (Type type) 
96                 {
97                         if (type != Type.GetType ("System.IO.Stream"))
98                                 throw new ArgumentException ("type");
99                         return GetOutput ();
100                 }
101
102                 public override void LoadInnerXml (XmlNodeList nodeList) 
103                 {
104                         if (nodeList == null)
105                                 throw new CryptographicException ("nodeList");
106                         xnl = nodeList;
107                 }
108
109                 public override void LoadInput (object obj) 
110                 {
111                         // possible input: Stream, XmlDocument, and XmlNodeList
112                         if (obj is Stream) {
113                                 inputDoc = new XmlDocument ();
114                                 inputDoc.Load (obj as Stream);
115                         }
116                         else if (obj is XmlDocument) {
117                                 inputDoc= obj as XmlDocument;
118                         }
119                         else if (obj is XmlNodeList) {
120                                 inputDoc = new XmlDocument ();
121                                 XmlNodeList nl = (XmlNodeList) obj;
122                                 for (int i = 0; i < nl.Count; i++)
123                                         inputDoc.AppendChild (inputDoc.ImportNode (nl [i], true));
124                         }
125                 }
126         }
127 }