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