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);
78 // why is data a XmlNodeList instead of a XmlElement ?
79 public XmlNodeList Data {
81 return element.ChildNodes;
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));
93 propertyModified = true;
97 // default to null - no encoding
98 public string Encoding {
99 get { return GetField (XmlSignature.AttributeNames.Encoding); }
100 set { SetField (XmlSignature.AttributeNames.Encoding, value); }
105 get { return GetField (XmlSignature.AttributeNames.Id); }
106 set { SetField (XmlSignature.AttributeNames.Id, value); }
110 public string MimeType {
111 get { return GetField (XmlSignature.AttributeNames.MimeType); }
112 set { SetField (XmlSignature.AttributeNames.MimeType, value); }
115 private string GetField (string attribute)
117 XmlNode attr = element.Attributes [attribute];
118 return attr != null ? attr.Value : null;
121 private void SetField (string attribute, string value)
123 // MS-BUGS: it never cleans attribute value up.
127 if (propertyModified)
128 element.SetAttribute (attribute, value);
130 XmlDocument document = new XmlDocument ();
131 XmlElement el = document.ImportNode (element, true) as XmlElement;
132 el.SetAttribute (attribute, value);
134 propertyModified = true;
138 public XmlElement GetXml ()
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);
154 foreach (XmlNode n in oldElement.ChildNodes)
155 element.AppendChild (doc.ImportNode (n, true));
160 public void LoadXml (XmlElement value)
163 throw new ArgumentNullException ("value");
165 propertyModified = false;