Add MIT license to System.Security.DLL
[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 //      Atsushi Enomoto (atsushi@ximian.com)
8 //
9 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
10 // (C) 2004 Novell Inc.
11 //
12
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 using System.Xml;
35
36 namespace System.Security.Cryptography.Xml {
37
38         // XmlElement part of the signature
39         // Note: Looks like KeyInfoNode (but the later is XmlElement inside KeyInfo)
40         // required for "enveloping signatures"
41         public class DataObject {
42
43                 private string id;
44                 private string mimeType;
45                 private string encoding;
46                 private XmlElement element;
47                 private bool propertyModified;
48
49                 public DataObject ()
50                 {
51                         Build (null, null, null, null);
52                 }
53
54                 public DataObject (string id, string mimeType, string encoding, XmlElement data) 
55                 {
56                         if (data == null)
57                                 throw new ArgumentNullException ("data");
58
59                         Build (id, mimeType, encoding, data);
60                 }
61
62                 // this one accept a null "data" parameter
63                 private void Build (string id, string mimeType, string encoding, XmlElement data) 
64                 {
65                         XmlDocument document = new XmlDocument ();
66                         XmlElement xel = document.CreateElement (XmlSignature.ElementNames.Object, XmlSignature.NamespaceURI);
67                         if (id != null) {
68                                 this.id = id;
69                                 xel.SetAttribute (XmlSignature.AttributeNames.Id, id);
70                         }
71                         if (mimeType != null) {
72                                 this.mimeType = mimeType;
73                                 xel.SetAttribute (XmlSignature.AttributeNames.MimeType, mimeType);
74                         }
75                         if (encoding != null) {
76                                 this.encoding = encoding;
77                                 xel.SetAttribute (XmlSignature.AttributeNames.Encoding, encoding);
78                         }
79                         if (data != null) {
80                                 XmlNode newNode = document.ImportNode (data, true);
81                                 xel.AppendChild (newNode);
82                         }
83                         document.AppendChild (xel); // FIXME: it should not be appended
84                         
85                         element = document.DocumentElement;
86                 }
87
88                 // why is data a XmlNodeList instead of a XmlElement ?
89                 public XmlNodeList Data {
90                         get { 
91                                 return element.ChildNodes;
92                         }
93                         set {
94                                 if (value == null)
95                                         throw new ArgumentNullException ("value");
96                                 XmlDocument doc = new XmlDocument ();
97                                 XmlElement el = (XmlElement) doc.ImportNode (element, true);
98                                 doc.AppendChild (el); // FIXME: it should not be appended
99                                 el.RemoveAll ();
100                                 foreach (XmlNode n in value)
101                                         el.AppendChild (doc.ImportNode (n, true));
102                                 element = el;
103                                 propertyModified = true;
104                         }
105                 }
106
107                 // default to null - no encoding
108                 public string Encoding {
109                         get { return GetField (XmlSignature.AttributeNames.Encoding); }
110                         set { SetField (XmlSignature.AttributeNames.Encoding, value); }
111                 }
112
113                 // default to null
114                 public string Id {
115                         get { return GetField (XmlSignature.AttributeNames.Id); }
116                         set { SetField (XmlSignature.AttributeNames.Id, value); }
117                 }
118
119                 // default to null
120                 public string MimeType {
121                         get { return GetField (XmlSignature.AttributeNames.MimeType); }
122                         set { SetField (XmlSignature.AttributeNames.MimeType, value); }
123                 }
124                 
125                 private string GetField (string attribute)
126                 {
127                         XmlNode attr = element.Attributes [attribute];
128                         return attr != null ? attr.Value : null;
129                 }
130
131                 private void SetField (string attribute, string value)
132                 {
133                         // MS-BUGS: it never cleans attribute value up.
134                         if (value == null)
135                                 return;
136
137                         if (propertyModified)
138                                 element.SetAttribute (attribute, value);
139                         else {
140                                 XmlDocument document = new XmlDocument ();
141                                 XmlElement el = document.ImportNode (element, true) as XmlElement;
142                                 el.Attributes.RemoveAll ();
143                                 document.AppendChild (el); // FIXME: it should not be appended
144                                 el.SetAttribute (attribute, value);
145                                 element = el;
146                                 propertyModified = true;
147                         }
148                 }
149
150                 public XmlElement GetXml () 
151                 {
152                         if (propertyModified) {
153                                 // It looks MS.NET returns element which comes from new XmlDocument every time
154                                 XmlElement oldElement = element;
155                                 XmlDocument doc = new XmlDocument ();
156                                 element = doc.CreateElement (XmlSignature.ElementNames.Object, XmlSignature.NamespaceURI);
157                                 doc.AppendChild (element); // FIXME: it should not be appended
158                                 foreach (XmlAttribute attribute in oldElement.Attributes) {
159                                         switch (attribute.Name) {
160                                         case XmlSignature.AttributeNames.Id:
161                                         case XmlSignature.AttributeNames.Encoding:
162                                         case XmlSignature.AttributeNames.MimeType:
163                                                 element.SetAttribute (attribute.Name, attribute.Value);
164                                                 break;
165                                         }
166                                 }
167                                 foreach (XmlNode n in oldElement.ChildNodes)
168                                         element.AppendChild (doc.ImportNode (n, true));
169                         }
170                         return element;
171                 }
172
173                 public void LoadXml (XmlElement value) 
174                 {
175                         if (value == null)
176                                 throw new ArgumentNullException ("value");
177                         element = value;
178                         propertyModified = false;
179                 }
180         }
181 }