2004-05-13 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / System.Security / System.Security.Cryptography.Xml / Manifest.cs
1 //
2 // Manifest.cs - Manifest implementation for XML Signature
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // (C) 2004 Novell (http://www.novell.com)
8 //
9
10 using System.Collections;
11 using System.Xml;
12
13 namespace System.Security.Cryptography.Xml { 
14
15         internal class Manifest {
16
17                 private ArrayList references;
18                 private string id;
19                 private XmlElement element;
20
21                 public Manifest ()
22                 {
23                         references = new ArrayList ();
24                 }
25
26                 public Manifest (XmlElement xel) : this ()
27                 {
28                         LoadXml (xel);
29                 }
30
31                 public string Id {
32                         get { return id; }
33                         set {
34                                 element = null;
35                                 id = value;
36                         }
37                 }
38
39                 public ArrayList References {
40                         get { return references; }
41                 }
42
43                 public void AddReference (Reference reference) 
44                 {
45                         references.Add (reference);
46                 }
47
48                 public XmlElement GetXml () 
49                 {
50                         if (element != null)
51                                 return element;
52
53                         XmlDocument document = new XmlDocument ();
54                         XmlElement xel = document.CreateElement (XmlSignature.ElementNames.SignedInfo, XmlSignature.NamespaceURI);
55                         if (id != null)
56                                 xel.SetAttribute (XmlSignature.AttributeNames.Id, id);
57
58                         // we add References afterward so we don't end up with extraneous
59                         // xmlns="..." in each reference elements.
60                         foreach (Reference r in references) {
61                                 XmlNode xn = r.GetXml ();
62                                 XmlNode newNode = document.ImportNode (xn, true);
63                                 xel.AppendChild (newNode);
64                         }
65
66                         return xel;
67                 }
68
69                 private string GetAttribute (XmlElement xel, string attribute) 
70                 {
71                         XmlAttribute xa = xel.Attributes [attribute];
72                         return ((xa != null) ? xa.InnerText : null);
73                 }
74
75                 public void LoadXml (XmlElement value) 
76                 {
77                         if (value == null)
78                                 throw new ArgumentNullException ("value");
79
80                         if ((value.LocalName != XmlSignature.ElementNames.Manifest) || (value.NamespaceURI != XmlSignature.NamespaceURI))
81                                 throw new CryptographicException ();
82
83                         id = GetAttribute (value, XmlSignature.AttributeNames.Id);
84
85                         for (int i = 0; i < value.ChildNodes.Count; i++) {
86                                 XmlNode n = value.ChildNodes [i];
87                                 if (n.NodeType == XmlNodeType.Element &&
88                                         n.LocalName == XmlSignature.ElementNames.Reference &&
89                                         n.NamespaceURI == XmlSignature.NamespaceURI) {
90                                         Reference r = new Reference ();
91                                         r.LoadXml ((XmlElement) n);
92                                         AddReference (r);
93                                 }
94                         }
95                         element = value;
96                 }
97         }
98 }