2003-01-17 Sebastien Pouliot <spouliot@videotron.ca>
[mono.git] / mcs / class / System.Security / System.Security.Cryptography.Xml / XmlDsigC14NTransform.cs
1 //
2 // XmlDsigC14NTransform.cs - C14N Transform implementation for XML Signature
3 // http://www.w3.org/TR/xml-c14n
4 //
5 // Author:
6 //      Sebastien Pouliot (spouliot@motus.com)
7 //
8 // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
9 //
10
11 using System.IO;
12 using System.Text;
13 using System.Xml;
14
15 namespace System.Security.Cryptography.Xml { 
16
17 public class XmlDsigC14NTransform : Transform {
18
19         private Type[] input;
20         private Type[] output;
21         private bool comments;
22         private Stream s;
23
24         public XmlDsigC14NTransform () 
25         {
26                 Algorithm = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315";
27                 comments = false;
28         }
29
30         public XmlDsigC14NTransform (bool includeComments) 
31         {
32                 comments = includeComments;
33         }
34
35         public override Type[] InputTypes {
36                 get {
37                         if (input == null) {
38                                 lock (this) {
39                                         // this way the result is cached if called multiple time
40                                         input = new Type [3];
41                                         input[0] = typeof (System.IO.Stream);
42                                         input[1] = typeof (System.Xml.XmlDocument);
43                                         input[2] = typeof (System.Xml.XmlNodeList);
44                                 }
45                         }
46                         return input;
47                 }
48         }
49
50         public override Type[] OutputTypes {
51                 get {
52                         if (output == null) {
53                                 lock (this) {
54                                         // this way the result is cached if called multiple time
55                                         output = new Type [1];
56                                         output[0] = typeof (System.IO.Stream);
57                                 }
58                         }
59                         return output;
60                 }
61         }
62
63         protected override XmlNodeList GetInnerXml () 
64         {
65                 return null; // THIS IS DOCUMENTED AS SUCH
66         }
67
68         public override object GetOutput () 
69         {
70                 return (object) s;
71         }
72
73         public override object GetOutput (Type type) 
74         {
75                 if (type == Type.GetType ("Stream"))
76                         return GetOutput ();
77                 throw new ArgumentException ("type");
78         }
79
80         public override void LoadInnerXml (XmlNodeList nodeList) 
81         {
82                 // documented as not changing the state of the transform
83         }
84
85         public override void LoadInput (object obj) 
86         {
87                 XmlNodeList xnl = null;
88
89                 if (obj is Stream) 
90                         s = (obj as Stream);
91                 else if (obj is XmlDocument)
92                         xnl = (obj as XmlDocument).ChildNodes;
93                 else if (obj is XmlNodeList)
94                         xnl = (XmlNodeList) obj;
95
96                 if (xnl != null) {
97                         StringBuilder sb = new StringBuilder ();
98                         foreach (XmlNode xn in xnl)
99                                 sb.Append (xn.InnerText);
100
101                         UTF8Encoding utf8 = new UTF8Encoding ();
102                         byte[] data = utf8.GetBytes (sb.ToString ());
103                         s = new MemoryStream (data);
104                 }
105
106                 // note: there is no default are other types won't throw an exception
107         }
108 }
109
110 }