* roottypes.cs: Rename from tree.cs.
[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                         document.AppendChild (xel); // FIXME: it should not be appended
76                         
77                         element = document.DocumentElement;
78                 }
79
80                 // why is data a XmlNodeList instead of a XmlElement ?
81                 public XmlNodeList Data {
82                         get { 
83                                 return element.ChildNodes;
84                         }
85                         set {
86                                 if (value == null)
87                                         throw new ArgumentNullException ("value");
88                                 XmlDocument doc = new XmlDocument ();
89                                 XmlElement el = (XmlElement) doc.ImportNode (element, true);
90                                 doc.AppendChild (el); // FIXME: it should not be appended
91                                 el.RemoveAll ();
92                                 foreach (XmlNode n in value)
93                                         el.AppendChild (doc.ImportNode (n, true));
94                                 element = el;
95                                 propertyModified = true;
96                         }
97                 }
98
99                 // default to null - no encoding
100                 public string Encoding {
101                         get { return GetField (XmlSignature.AttributeNames.Encoding); }
102                         set { SetField (XmlSignature.AttributeNames.Encoding, value); }
103                 }
104
105                 // default to null
106                 public string Id {
107                         get { return GetField (XmlSignature.AttributeNames.Id); }
108                         set { SetField (XmlSignature.AttributeNames.Id, value); }
109                 }
110
111                 // default to null
112                 public string MimeType {
113                         get { return GetField (XmlSignature.AttributeNames.MimeType); }
114                         set { SetField (XmlSignature.AttributeNames.MimeType, value); }
115                 }
116                 
117                 private string GetField (string attribute)
118                 {
119                         XmlNode attr = element.Attributes [attribute];
120                         return attr != null ? attr.Value : null;
121                 }
122
123                 private void SetField (string attribute, string value)
124                 {
125                         // MS-BUGS: it never cleans attribute value up.
126                         if (value == null)
127                                 return;
128
129                         if (propertyModified)
130                                 element.SetAttribute (attribute, value);
131                         else {
132                                 XmlDocument document = new XmlDocument ();
133                                 XmlElement el = document.ImportNode (element, true) as XmlElement;
134                                 el.Attributes.RemoveAll ();
135                                 document.AppendChild (el); // FIXME: it should not be appended
136                                 el.SetAttribute (attribute, value);
137                                 element = el;
138                                 propertyModified = true;
139                         }
140                 }
141
142                 public XmlElement GetXml () 
143                 {
144                         if (propertyModified) {
145                                 // It looks MS.NET returns element which comes from new XmlDocument every time
146                                 XmlElement oldElement = element;
147                                 XmlDocument doc = new XmlDocument ();
148                                 element = doc.CreateElement (XmlSignature.ElementNames.Object, XmlSignature.NamespaceURI);
149                                 doc.AppendChild (element); // FIXME: it should not be appended
150                                 foreach (XmlAttribute attribute in oldElement.Attributes) {
151                                         switch (attribute.Name) {
152                                         case XmlSignature.AttributeNames.Id:
153                                         case XmlSignature.AttributeNames.Encoding:
154                                         case XmlSignature.AttributeNames.MimeType:
155                                                 element.SetAttribute (attribute.Name, attribute.Value);
156                                                 break;
157                                         }
158                                 }
159                                 foreach (XmlNode n in oldElement.ChildNodes)
160                                         element.AppendChild (doc.ImportNode (n, true));
161                         }
162                         return element;
163                 }
164
165                 public void LoadXml (XmlElement value) 
166                 {
167                         if (value == null)
168                                 throw new ArgumentNullException ("value");
169                         element = value;
170                         propertyModified = false;
171                 }
172         }
173 }