2 // DataObject.cs - DataObject implementation for XML Signature
3 // http://www.w3.org/2000/09/xmldsig#Object
6 // Sebastien Pouliot <sebastien@ximian.com>
7 // Atsushi Enomoto (atsushi@ximian.com)
9 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
10 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
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.
34 namespace System.Security.Cryptography.Xml {
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 {
41 private XmlElement element;
42 private bool propertyModified;
46 Build (null, null, null, null);
49 public DataObject (string id, string mimeType, string encoding, XmlElement data)
52 throw new ArgumentNullException ("data");
54 Build (id, mimeType, encoding, data);
57 // this one accept a null "data" parameter
58 private void Build (string id, string mimeType, string encoding, XmlElement data)
60 XmlDocument document = new XmlDocument ();
61 XmlElement xel = document.CreateElement (XmlSignature.ElementNames.Object, XmlSignature.NamespaceURI);
63 xel.SetAttribute (XmlSignature.AttributeNames.Id, id);
65 if (mimeType != null) {
66 xel.SetAttribute (XmlSignature.AttributeNames.MimeType, mimeType);
68 if (encoding != null) {
69 xel.SetAttribute (XmlSignature.AttributeNames.Encoding, encoding);
72 XmlNode newNode = document.ImportNode (data, true);
73 xel.AppendChild (newNode);
75 document.AppendChild (xel); // FIXME: it should not be appended
77 element = document.DocumentElement;
80 // why is data a XmlNodeList instead of a XmlElement ?
81 public XmlNodeList Data {
83 return element.ChildNodes;
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
92 foreach (XmlNode n in value)
93 el.AppendChild (doc.ImportNode (n, true));
95 propertyModified = true;
99 // default to null - no encoding
100 public string Encoding {
101 get { return GetField (XmlSignature.AttributeNames.Encoding); }
102 set { SetField (XmlSignature.AttributeNames.Encoding, value); }
107 get { return GetField (XmlSignature.AttributeNames.Id); }
108 set { SetField (XmlSignature.AttributeNames.Id, value); }
112 public string MimeType {
113 get { return GetField (XmlSignature.AttributeNames.MimeType); }
114 set { SetField (XmlSignature.AttributeNames.MimeType, value); }
117 private string GetField (string attribute)
119 XmlNode attr = element.Attributes [attribute];
120 return attr != null ? attr.Value : null;
123 private void SetField (string attribute, string value)
125 // MS-BUGS: it never cleans attribute value up.
129 if (propertyModified)
130 element.SetAttribute (attribute, value);
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);
138 propertyModified = true;
142 public XmlElement GetXml ()
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);
159 foreach (XmlNode n in oldElement.ChildNodes)
160 element.AppendChild (doc.ImportNode (n, true));
165 public void LoadXml (XmlElement value)
168 throw new ArgumentNullException ("value");
170 propertyModified = false;