BindingFlags.Public needed here as Exception.HResult is now public in .NET 4.5. This...
[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 //
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
22 // 
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 // 
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 //
34
35 using System.IO;
36 using System.Xml;
37 using System.Xml.Xsl;
38
39 namespace System.Security.Cryptography.Xml 
40 {
41
42         public class XmlDsigXsltTransform : Transform 
43         {
44
45                 private Type [] input;
46                 private Type [] output;
47                 private bool comments;
48                 private XmlNodeList xnl;
49                 private XmlDocument inputDoc;
50
51                 public XmlDsigXsltTransform () : this (false)
52                 {
53                 }
54
55                 public XmlDsigXsltTransform (bool includeComments) 
56                 {
57                         comments = includeComments;
58                         Algorithm = XmlSignature.AlgorithmNamespaces.XmlDsigXsltTransform;
59                 }
60
61                 public override Type [] InputTypes {
62                         get {
63                                 if (input == null) {
64                                         input = new Type [3];
65                                         input [0] = typeof (System.IO.Stream);
66                                         input [1] = typeof (System.Xml.XmlDocument);
67                                         input [2] = typeof (System.Xml.XmlNodeList);
68                                 }
69                                 return input;
70                         }
71                 }
72
73                 public override Type [] OutputTypes {
74                         get {
75                                 if (output == null) {
76                                         output = new Type [1];
77                                         output [0] = typeof (System.IO.Stream);
78                                 }
79                                 return output;
80                         }
81                 }
82                         
83                 protected override XmlNodeList GetInnerXml () 
84                 {
85                         return xnl;
86                 }
87
88                 public override object GetOutput () 
89                 {
90 #if NET_2_0
91                         if (xnl == null)
92                                 throw new ArgumentNullException ("LoadInnerXml before transformation.");
93 #endif
94                         XmlResolver resolver = GetResolver ();
95
96                         XslTransform xsl = new XslTransform ();
97                         XmlDocument doc = new XmlDocument ();
98 #if NET_1_1
99                         doc.XmlResolver = resolver;
100 #endif
101                         foreach (XmlNode n in xnl)
102                                 doc.AppendChild (doc.ImportNode (n, true));
103 #if NET_1_1
104                         xsl.Load (doc, resolver);
105 #else
106                         xsl.Load (doc);
107 #endif
108
109                         if (inputDoc == null)
110 #if NET_2_0
111                                 throw new ArgumentNullException ("LoadInput before transformation.");
112 #else
113                                 throw new NullReferenceException ("LoadInput before transformation.");
114 #endif
115
116                         MemoryStream stream = new MemoryStream ();
117                         // only possible output: Stream
118 #if NET_1_1
119                         xsl.XmlResolver = resolver;
120 #endif
121                         xsl.Transform (inputDoc, null, stream);
122
123                         stream.Seek (0, SeekOrigin.Begin);
124                         return stream;
125                 }
126
127                 public override object GetOutput (Type type) 
128                 {
129                         if (type != typeof (Stream))
130                                 throw new ArgumentException ("type");
131                         return GetOutput ();
132                 }
133
134                 public override void LoadInnerXml (XmlNodeList nodeList) 
135                 {
136                         if (nodeList == null)
137                                 throw new CryptographicException ("nodeList");
138                         xnl = nodeList;
139                 }
140
141                 public override void LoadInput (object obj) 
142                 {
143                         // possible input: Stream, XmlDocument, and XmlNodeList
144                         Stream s = (obj as Stream);
145                         if (s != null) {
146                                 inputDoc = new XmlDocument ();
147 #if NET_1_1
148                                 inputDoc.XmlResolver = GetResolver ();
149 #endif
150 //                              inputDoc.Load (obj as Stream);
151                                 inputDoc.Load (new XmlSignatureStreamReader (new StreamReader (s)));
152                                 return;
153                         }
154
155                         XmlDocument xd = (obj as XmlDocument);
156                         if (xd != null) {
157                                 inputDoc = xd;
158                                 return;
159                         }
160
161                         XmlNodeList nl = (obj as XmlNodeList);
162                         if (nl != null) {
163                                 inputDoc = new XmlDocument ();
164 #if NET_1_1
165                                 inputDoc.XmlResolver = GetResolver ();
166 #endif
167                                 for (int i = 0; i < nl.Count; i++)
168                                         inputDoc.AppendChild (inputDoc.ImportNode (nl [i], true));
169                         }
170                 }
171         }
172 }