2004-03-20 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / System.Security / System.Security.Cryptography.Xml / DataObject.cs
1 //
2 // DataObject.cs - DataObject implementation for XML Signature
3 // http://www.w3.org/2000/09/xmldsig#Object
4 //
5 // Author:
6 //      Sebastien Pouliot (spouliot@motus.com)
7 //
8 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
9 //
10
11 using System.Xml;
12
13 namespace System.Security.Cryptography.Xml {
14
15         // XmlElement part of the signature
16         // Note: Looks like KeyInfoNode (but the later is XmlElement inside KeyInfo)
17         // required for "enveloping signatures"
18         public class DataObject {
19
20                 private string id;
21                 private string mimeType;
22                 private string encoding;
23                 private XmlDocument document;
24
25                 public DataObject ()
26                 {
27                         Build (null, null, null, null);
28                 }
29
30                 public DataObject (string id, string mimeType, string encoding, XmlElement data) 
31                 {
32                         if (data == null)
33                                 throw new ArgumentNullException ("data");
34
35                         Build (id, mimeType, encoding, data);
36                 }
37
38                 // this one accept a null "data" parameter
39                 private void Build (string id, string mimeType, string encoding, XmlElement data) 
40                 {
41                         document = new XmlDocument ();
42                         XmlElement xel = document.CreateElement (XmlSignature.ElementNames.Object, XmlSignature.NamespaceURI);
43                         if (id != null) {
44                                 this.id = id;
45                                 xel.SetAttribute (XmlSignature.AttributeNames.Id, id);
46                         }
47                         if (mimeType != null) {
48                                 this.mimeType = mimeType;
49                                 xel.SetAttribute (XmlSignature.AttributeNames.MimeType, mimeType);
50                         }
51                         if (encoding != null) {
52                                 this.encoding = encoding;
53                                 xel.SetAttribute (XmlSignature.AttributeNames.Encoding, encoding);
54                         }
55                         if (data != null) {
56                                 XmlNode newNode = document.ImportNode (data, true);
57                                 xel.AppendChild (newNode);
58                         }
59                         document.AppendChild (xel);
60                 }
61
62                 // why is data a XmlNodeList instead of a XmlElement ?
63                 public XmlNodeList Data {
64                         get { 
65                                 XmlNodeList xnl = document.GetElementsByTagName (XmlSignature.ElementNames.Object);
66                                 return xnl[0].ChildNodes;
67                         }
68                         set {
69                                 if (value == null)
70                                         throw new ArgumentNullException ("value");
71
72                                 Build (id, mimeType, encoding, null);
73                                 XmlNodeList xnl = document.GetElementsByTagName (XmlSignature.ElementNames.Object);
74                                 if ((xnl != null) && (xnl.Count > 0)) {
75                                         foreach (XmlNode xn in value) {
76                                                 XmlNode newNode = document.ImportNode (xn, true);
77                                                 xnl [0].AppendChild (newNode);
78                                         }
79                                 }
80                         }
81                 }
82
83                 // default to null - no encoding
84                 public string Encoding {
85                         get { return encoding; }
86                         set { encoding = value; }
87                 }
88
89                 // default to null
90                 public string Id {
91                         get { return id; }
92                         set { id = value; }
93                 }
94
95                 // default to null
96                 public string MimeType {
97                         get { return mimeType; }
98                         set { mimeType = value; }
99                 }
100
101                 public XmlElement GetXml () 
102                 {
103                         if ((document.DocumentElement.LocalName == XmlSignature.ElementNames.Object) && (document.DocumentElement.NamespaceURI == XmlSignature.NamespaceURI)) {
104                                 // recreate all attributes in order
105                                 XmlAttribute xa = null;
106                                 document.DocumentElement.Attributes.RemoveAll ();
107                                 if (id != null) {
108                                         xa = document.CreateAttribute (XmlSignature.AttributeNames.Id);
109                                         xa.Value = id;
110                                         document.DocumentElement.Attributes.Append (xa);
111                                 }
112                                 if (mimeType != null) {
113                                         xa = document.CreateAttribute (XmlSignature.AttributeNames.MimeType);
114                                         xa.Value = mimeType;
115                                         document.DocumentElement.Attributes.Append (xa);
116                                 }
117                                 if (encoding != null) {
118                                         xa = document.CreateAttribute (XmlSignature.AttributeNames.Encoding);
119                                         xa.Value = encoding;
120                                         document.DocumentElement.Attributes.Append (xa);
121                                 }
122                                 xa = document.CreateAttribute ("xmlns");
123                                 xa.Value = XmlSignature.NamespaceURI;
124                                 document.DocumentElement.Attributes.Append (xa);
125                         }
126                         return document.DocumentElement;
127                 }
128
129                 public void LoadXml (XmlElement value) 
130                 {
131                         if (value == null)
132                                 throw new ArgumentNullException ("value");
133
134                         if ((value.LocalName != XmlSignature.ElementNames.Object) || (value.NamespaceURI != XmlSignature.NamespaceURI)) {
135                                 document.LoadXml (value.OuterXml);
136                         }
137                         else {
138                                 document.LoadXml (value.OuterXml);
139                                 XmlAttribute xa = value.Attributes [XmlSignature.AttributeNames.Id];
140                                 id = ((xa != null) ? xa.InnerText : null);
141                                 xa = value.Attributes [XmlSignature.AttributeNames.MimeType];
142                                 mimeType = ((xa != null) ? xa.InnerText : null);
143                                 xa = value.Attributes [XmlSignature.AttributeNames.Encoding];
144                                 encoding = ((xa != null) ? xa.InnerText : null);
145                         }
146                 }
147         }
148 }