2004-03-12 Sebastien Pouliot <sebastien@ximian.com>
[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 // Authors:
6 //      Sebastien Pouliot <sebastien@ximian.com>
7 //      Aleksey Sanin (aleksey@aleksey.com)
8 //
9 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
10 // (C) 2003 Aleksey Sanin (aleksey@aleksey.com)
11 // (C) 2004 Novell (http://www.novell.com)
12 //
13
14 using System.Collections;
15 using System.IO;
16 using System.Text;
17 using System.Xml;
18
19 using Mono.Xml;
20
21 namespace System.Security.Cryptography.Xml { 
22
23         public class XmlDsigC14NTransform : Transform {
24                 private Type[] input;
25                 private Type[] output;
26                 private XmlCanonicalizer canonicalizer;
27                 private Stream s;
28                 
29                 public XmlDsigC14NTransform () 
30                 {
31                         Algorithm = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315";
32                         canonicalizer = new XmlCanonicalizer (false, false);
33                 }
34
35                 public XmlDsigC14NTransform (bool includeComments) 
36                 {
37                         canonicalizer = new XmlCanonicalizer (includeComments, false);
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 null; // THIS IS DOCUMENTED AS SUCH
71                 }
72
73                 public override object GetOutput () 
74                 {
75                         return (object) s;
76                 }
77
78                 public override object GetOutput (Type type) 
79                 {
80                         if (type == Type.GetType ("Stream"))
81                                 return GetOutput ();
82                         throw new ArgumentException ("type");
83                 }
84
85                 public override void LoadInnerXml (XmlNodeList nodeList) 
86                 {
87                         // documented as not changing the state of the transform
88                 }
89
90                 public override void LoadInput (object obj) 
91                 {
92                         if (obj is Stream) {
93                                 s = (obj as Stream);
94                                 XmlDocument doc = new XmlDocument ();
95                                 doc.PreserveWhitespace = true;  // REALLY IMPORTANT
96                                 doc.Load (obj as Stream);
97                                 s = canonicalizer.Canonicalize (doc);
98                         } else if (obj is XmlDocument)
99                                 s = canonicalizer.Canonicalize ((obj as XmlDocument));
100                         else if (obj is XmlNodeList)
101                                 s = canonicalizer.Canonicalize ((obj as XmlNodeList));
102                         // note: there is no default are other types won't throw an exception
103                 }
104         }
105 }
106