2003-06-19 Nick Drochak <ndrochak@gol.com>
[mono.git] / mcs / class / System.Security / System.Security.Cryptography.Xml / KeyInfo.cs
1 //
2 // KeyInfo.cs - Xml Signature KeyInfo implementation
3 //
4 // Author:
5 //      Sebastien Pouliot (spouliot@motus.com)
6 //
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 //
9
10 using System.Collections;
11 using System.Xml;
12
13 namespace System.Security.Cryptography.Xml {
14
15         public class KeyInfo : IEnumerable {
16
17                 private ArrayList Info;
18                 private string id;
19
20                 public KeyInfo() 
21                 {
22                         Info = new ArrayList ();
23                 }
24
25                 public int Count {
26                         get { return Info.Count; }
27                 }
28                 
29                 public string Id {
30                         get { return id; }
31                         set { id = value; }
32                 }
33
34                 public void AddClause (KeyInfoClause clause) 
35                 {
36                         Info.Add (clause);
37                 }
38
39                 public IEnumerator GetEnumerator () 
40                 {
41                         return Info.GetEnumerator ();
42                 }
43
44                 public IEnumerator GetEnumerator (Type requestedObjectType)
45                 {
46                         // Build a new ArrayList...
47                         ArrayList TypeList = new ArrayList ();
48                         IEnumerator e = Info.GetEnumerator ();
49                         while (true) {
50                                 // ...with all object of specified type...
51                                 if ((e.Current).GetType().Equals (requestedObjectType))
52                                         TypeList.Add (e.Current);
53                                 if (!e.MoveNext ())
54                                         break;
55                         }
56                         // ...and return its enumerator
57                         return TypeList.GetEnumerator ();
58                 }
59
60                 public XmlElement GetXml () 
61                 {
62                         XmlDocument document = new XmlDocument ();
63                         XmlElement xel = document.CreateElement (XmlSignature.ElementNames.KeyInfo, XmlSignature.NamespaceURI);
64                         // we add References afterward so we don't end up with extraneous
65                         // xmlns="..." in each reference elements.
66                         foreach (KeyInfoClause kic in Info) {
67                                 XmlNode xn = kic.GetXml ();
68                                 XmlNode newNode = document.ImportNode (xn, true);
69                                 xel.AppendChild (newNode);
70                         }
71                         return xel;
72                 }
73
74                 public void LoadXml (XmlElement value) 
75                 {
76                         if (value == null)
77                                 throw new ArgumentNullException ("value");
78
79                         if ((value.LocalName == XmlSignature.ElementNames.KeyInfo) && (value.NamespaceURI == XmlSignature.NamespaceURI)) {
80                                 foreach (XmlNode n in value.ChildNodes) {
81                                         KeyInfoClause kic = null;
82                                         if (n is XmlWhitespace)
83                                                 continue;
84
85                                         switch (n.LocalName) {
86                                         case XmlSignature.ElementNames.KeyValue:
87                                                 XmlNodeList xnl = n.ChildNodes;
88                                                 if (xnl.Count > 0) {
89                                                         // we must now treat the whitespace !
90                                                         foreach (XmlNode m in xnl) {
91                                                                 switch (m.LocalName) {
92                                                                 case XmlSignature.ElementNames.DSAKeyValue:
93                                                                         kic = (KeyInfoClause) new DSAKeyValue ();
94                                                                         break;
95                                                                 case XmlSignature.ElementNames.RSAKeyValue:
96                                                                         kic = (KeyInfoClause) new RSAKeyValue ();
97                                                                         break;
98                                                                 }
99                                                         }
100                                                 }
101                                                 break;
102                                         case XmlSignature.ElementNames.KeyName:
103                                                 kic = (KeyInfoClause) new KeyInfoName ();
104                                                 break;
105                                         case XmlSignature.ElementNames.RetrievalMethod:
106                                                 kic = (KeyInfoClause) new KeyInfoRetrievalMethod ();
107                                                 break;
108                                         case XmlSignature.ElementNames.X509Data:
109                                                 kic = (KeyInfoClause) new KeyInfoX509Data ();
110                                                 break;
111 /*                                      case XmlSignature.ElementNames.RSAKeyValue:
112                                                 kic = (KeyInfoClause) new RSAKeyValue ();
113                                                 break;*/
114                                         default:
115                                                 kic = (KeyInfoClause) new KeyInfoNode ();
116                                                 break;
117                                         }
118
119                                         if (kic != null) {
120                                                 kic.LoadXml ((XmlElement) n);
121                                                 AddClause (kic);
122                                         }
123                                 }
124                         }
125                 }
126         }
127 }