2004-03-23 Atsushi Enomoto <atsushi@ximian.com>
authorAtsushi Eno <atsushieno@gmail.com>
Tue, 23 Mar 2004 13:05:03 +0000 (13:05 -0000)
committerAtsushi Eno <atsushieno@gmail.com>
Tue, 23 Mar 2004 13:05:03 +0000 (13:05 -0000)
* DataObject.cs : It now hold element instead of each parameters.
  Avoid loading element content to different document unless its
  properties were modified (to keep namespace node context).

svn path=/trunk/mcs/; revision=24459

mcs/class/System.Security/System.Security.Cryptography.Xml/ChangeLog
mcs/class/System.Security/System.Security.Cryptography.Xml/DataObject.cs

index b3a6da2500cf4580e1f7189681517735f671e0b4..7023bee61e13775216338c7abbc9cb102b5b5ed4 100644 (file)
@@ -1,3 +1,9 @@
+2004-03-23  Atsushi Enomoto <atsushi@ximian.com>
+
+       * DataObject.cs : It now hold element instead of each parameters.
+         Avoid loading element content to different document unless its
+         properties were modified (to keep namespace node context).
+
 2004-03-20 Sebastien Pouliot  <sebastien@ximian.com>
 
        * SignedXml.cs: Removed unused (and commented) ComputeHash method.
index bb78685e87561ec088761a6c4b9ff368ac1dfac5..23ef0a434937e03b74b86eaf2ab08945867e0a35 100644 (file)
@@ -4,8 +4,10 @@
 //
 // Author:
 //     Sebastien Pouliot (spouliot@motus.com)
+//     Atsushi Enomoto (atsushi@ximian.com)
 //
 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
+// (C) 2004 Novell Inc.
 //
 
 using System.Xml;
@@ -20,7 +22,8 @@ namespace System.Security.Cryptography.Xml {
                private string id;
                private string mimeType;
                private string encoding;
-               private XmlDocument document;
+               private XmlElement element;
+               private bool propertyModified;
 
                public DataObject ()
                {
@@ -38,7 +41,7 @@ namespace System.Security.Cryptography.Xml {
                // this one accept a null "data" parameter
                private void Build (string id, string mimeType, string encoding, XmlElement data) 
                {
-                       document = new XmlDocument ();
+                       XmlDocument document = new XmlDocument ();
                        XmlElement xel = document.CreateElement (XmlSignature.ElementNames.Object, XmlSignature.NamespaceURI);
                        if (id != null) {
                                this.id = id;
@@ -56,93 +59,102 @@ namespace System.Security.Cryptography.Xml {
                                XmlNode newNode = document.ImportNode (data, true);
                                xel.AppendChild (newNode);
                        }
-                       document.AppendChild (xel);
+                       document.AppendChild (xel); // FIXME: it should not be appended
+                       
+                       element = document.DocumentElement;
                }
 
                // why is data a XmlNodeList instead of a XmlElement ?
                public XmlNodeList Data {
                        get { 
-                               XmlNodeList xnl = document.GetElementsByTagName (XmlSignature.ElementNames.Object);
-                               return xnl[0].ChildNodes;
+                               return element.ChildNodes;
                        }
                        set {
                                if (value == null)
                                        throw new ArgumentNullException ("value");
-
-                               Build (id, mimeType, encoding, null);
-                               XmlNodeList xnl = document.GetElementsByTagName (XmlSignature.ElementNames.Object);
-                               if ((xnl != null) && (xnl.Count > 0)) {
-                                       foreach (XmlNode xn in value) {
-                                               XmlNode newNode = document.ImportNode (xn, true);
-                                               xnl [0].AppendChild (newNode);
-                                       }
-                               }
+                               XmlDocument doc = new XmlDocument ();
+                               XmlElement el = (XmlElement) doc.ImportNode (element, true);
+                               doc.AppendChild (el); // FIXME: it should not be appended
+                               el.RemoveAll ();
+                               foreach (XmlNode n in value)
+                                       el.AppendChild (doc.ImportNode (n, true));
+                               element = el;
+                               propertyModified = true;
                        }
                }
 
                // default to null - no encoding
                public string Encoding {
-                       get { return encoding; }
-                       set { encoding = value; }
+                       get { return GetField (XmlSignature.AttributeNames.Encoding); }
+                       set { SetField (XmlSignature.AttributeNames.Encoding, value); }
                }
 
                // default to null
                public string Id {
-                       get { return id; }
-                       set { id = value; }
+                       get { return GetField (XmlSignature.AttributeNames.Id); }
+                       set { SetField (XmlSignature.AttributeNames.Id, value); }
                }
 
                // default to null
                public string MimeType {
-                       get { return mimeType; }
-                       set { mimeType = value; }
+                       get { return GetField (XmlSignature.AttributeNames.MimeType); }
+                       set { SetField (XmlSignature.AttributeNames.MimeType, value); }
+               }
+               
+               private string GetField (string attribute)
+               {
+                       XmlNode attr = element.Attributes [attribute];
+                       return attr != null ? attr.Value : null;
+               }
+
+               private void SetField (string attribute, string value)
+               {
+                       // MS-BUGS: it never cleans attribute value up.
+                       if (value == null)
+                               return;
+
+                       if (propertyModified)
+                               element.SetAttribute (attribute, value);
+                       else {
+                               XmlDocument document = new XmlDocument ();
+                               XmlElement el = document.ImportNode (element, true) as XmlElement;
+                               el.Attributes.RemoveAll ();
+                               document.AppendChild (el); // FIXME: it should not be appended
+                               el.SetAttribute (attribute, value);
+                               element = el;
+                               propertyModified = true;
+                       }
                }
 
                public XmlElement GetXml () 
                {
-                       if ((document.DocumentElement.LocalName == XmlSignature.ElementNames.Object) && (document.DocumentElement.NamespaceURI == XmlSignature.NamespaceURI)) {
-                               // recreate all attributes in order
-                               XmlAttribute xa = null;
-                               document.DocumentElement.Attributes.RemoveAll ();
-                               if (id != null) {
-                                       xa = document.CreateAttribute (XmlSignature.AttributeNames.Id);
-                                       xa.Value = id;
-                                       document.DocumentElement.Attributes.Append (xa);
-                               }
-                               if (mimeType != null) {
-                                       xa = document.CreateAttribute (XmlSignature.AttributeNames.MimeType);
-                                       xa.Value = mimeType;
-                                       document.DocumentElement.Attributes.Append (xa);
-                               }
-                               if (encoding != null) {
-                                       xa = document.CreateAttribute (XmlSignature.AttributeNames.Encoding);
-                                       xa.Value = encoding;
-                                       document.DocumentElement.Attributes.Append (xa);
+                       if (propertyModified) {
+                               // It looks MS.NET returns element which comes from new XmlDocument every time
+                               XmlElement oldElement = element;
+                               XmlDocument doc = new XmlDocument ();
+                               element = doc.CreateElement (XmlSignature.ElementNames.Object, XmlSignature.NamespaceURI);
+                               doc.AppendChild (element); // FIXME: it should not be appended
+                               foreach (XmlAttribute attribute in oldElement.Attributes) {
+                                       switch (attribute.Name) {
+                                       case XmlSignature.AttributeNames.Id:
+                                       case XmlSignature.AttributeNames.Encoding:
+                                       case XmlSignature.AttributeNames.MimeType:
+                                               element.SetAttribute (attribute.Name, attribute.Value);
+                                               break;
+                                       }
                                }
-                               xa = document.CreateAttribute ("xmlns");
-                               xa.Value = XmlSignature.NamespaceURI;
-                               document.DocumentElement.Attributes.Append (xa);
+                               foreach (XmlNode n in oldElement.ChildNodes)
+                                       element.AppendChild (doc.ImportNode (n, true));
                        }
-                       return document.DocumentElement;
+                       return element;
                }
 
                public void LoadXml (XmlElement value) 
                {
                        if (value == null)
                                throw new ArgumentNullException ("value");
-
-                       if ((value.LocalName != XmlSignature.ElementNames.Object) || (value.NamespaceURI != XmlSignature.NamespaceURI)) {
-                               document.LoadXml (value.OuterXml);
-                       }
-                       else {
-                               document.LoadXml (value.OuterXml);
-                               XmlAttribute xa = value.Attributes [XmlSignature.AttributeNames.Id];
-                               id = ((xa != null) ? xa.InnerText : null);
-                               xa = value.Attributes [XmlSignature.AttributeNames.MimeType];
-                               mimeType = ((xa != null) ? xa.InnerText : null);
-                               xa = value.Attributes [XmlSignature.AttributeNames.Encoding];
-                               encoding = ((xa != null) ? xa.InnerText : null);
-                       }
+                       element = value;
+                       propertyModified = false;
                }
        }
-}
\ No newline at end of file
+}