**** Merged from MCS ****
[mono-project.git] / mcs / class / System.Security / System.Security.Cryptography.Xml / XmlDsigXsltTransform.cs
blob713a5b7bdd180c43b3c4973410c9f6d86e11bf46
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.
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.
35 using System.IO;
36 using System.Xml;
37 using System.Xml.Xsl;
39 namespace System.Security.Cryptography.Xml
42 public class XmlDsigXsltTransform : Transform
45 private Type [] input;
46 private Type [] output;
47 private bool comments;
48 private XmlNodeList xnl;
49 private XmlDocument inputDoc;
51 public XmlDsigXsltTransform () : this (false)
55 public XmlDsigXsltTransform (bool includeComments)
57 comments = includeComments;
58 Algorithm = "http://www.w3.org/TR/1999/REC-xslt-19991116";
61 public override Type [] InputTypes {
62 get {
63 if (input == null) {
64 lock (this) {
65 // this way the result is cached if called multiple time
66 input = new Type [3];
67 input [0] = typeof (System.IO.Stream);
68 input [1] = typeof (System.Xml.XmlDocument);
69 input [2] = typeof (System.Xml.XmlNodeList);
72 return input;
76 public override Type [] OutputTypes {
77 get {
78 if (output == null) {
79 lock (this) {
80 // this way the result is cached if called multiple time
81 output = new Type [1];
82 output [0] = typeof (System.IO.Stream);
85 return output;
89 protected override XmlNodeList GetInnerXml ()
91 return xnl;
94 public override object GetOutput ()
96 XmlResolver resolver = GetResolver ();
98 XslTransform xsl = new XslTransform ();
99 XmlDocument doc = new XmlDocument ();
100 #if NET_1_1
101 doc.XmlResolver = resolver;
102 #endif
103 foreach (XmlNode n in xnl)
104 doc.AppendChild (doc.ImportNode (n, true));
105 #if NET_1_1
106 xsl.Load (doc, resolver);
107 #else
108 xsl.Load (doc);
109 #endif
111 if (inputDoc == null)
112 throw new NullReferenceException ("Load input document before transformation.");
114 MemoryStream stream = new MemoryStream ();
115 // only possible output: Stream
116 #if NET_1_1
117 xsl.XmlResolver = resolver;
118 #endif
119 xsl.Transform (inputDoc, null, stream);
121 stream.Seek (0, SeekOrigin.Begin);
122 return stream;
125 public override object GetOutput (Type type)
127 if (type != Type.GetType ("System.IO.Stream"))
128 throw new ArgumentException ("type");
129 return GetOutput ();
132 public override void LoadInnerXml (XmlNodeList nodeList)
134 if (nodeList == null)
135 throw new CryptographicException ("nodeList");
136 xnl = nodeList;
139 public override void LoadInput (object obj)
141 // possible input: Stream, XmlDocument, and XmlNodeList
142 if (obj is Stream) {
143 inputDoc = new XmlDocument ();
144 #if NET_1_1
145 inputDoc.XmlResolver = GetResolver ();
146 #endif
147 inputDoc.Load (obj as Stream);
149 else if (obj is XmlDocument) {
150 inputDoc= obj as XmlDocument;
152 else if (obj is XmlNodeList) {
153 inputDoc = new XmlDocument ();
154 #if NET_1_1
155 inputDoc.XmlResolver = GetResolver ();
156 #endif
157 XmlNodeList nl = (XmlNodeList) obj;
158 for (int i = 0; i < nl.Count; i++)
159 inputDoc.AppendChild (inputDoc.ImportNode (nl [i], true));