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