Author: mkrueger
Date: 2007-09-05 08:10:30 -0400 (Wed, 05 Sep 2007)
New Revision: 85352

Added:
   
trunk/monodevelop/Core/src/MonoDevelop.Core/MonoDevelop.Core/ICustomXmlSerializer.cs
Modified:
   trunk/monodevelop/Core/src/MonoDevelop.Core/ChangeLog
   trunk/monodevelop/Core/src/MonoDevelop.Core/Makefile.am
   trunk/monodevelop/Core/src/MonoDevelop.Core/MonoDevelop.Core.mdp
   trunk/monodevelop/Core/src/MonoDevelop.Core/MonoDevelop.Core/Properties.cs
Log:
* MonoDevelop.Core/Properties.cs,
  MonoDevelop.Core/ICustomXmlSerializer.cs: Worked on property
  infrastructure - serialization can now be customized using the
  ICustomXmlSerializer interface.

Modified: trunk/monodevelop/Core/src/MonoDevelop.Core/ChangeLog
===================================================================
--- trunk/monodevelop/Core/src/MonoDevelop.Core/ChangeLog       2007-09-05 
11:57:12 UTC (rev 85351)
+++ trunk/monodevelop/Core/src/MonoDevelop.Core/ChangeLog       2007-09-05 
12:10:30 UTC (rev 85352)
@@ -1,3 +1,9 @@
+2007-09-05  Mike Krüger <[EMAIL PROTECTED]> 
+
+       * MonoDevelop.Core/Properties.cs, 
MonoDevelop.Core/ICustomXmlSerializer.cs:
+         Worked on property infrastructure - serialization can now be 
customized
+         using the ICustomXmlSerializer interface.
+
 2007-09-04  Michael Hutchinson <[EMAIL PROTECTED]>
 
        * MonoDevelop.Core/Properties.cs: Fix compiler warnings. Note: one is 

Modified: trunk/monodevelop/Core/src/MonoDevelop.Core/Makefile.am
===================================================================
--- trunk/monodevelop/Core/src/MonoDevelop.Core/Makefile.am     2007-09-05 
11:57:12 UTC (rev 85351)
+++ trunk/monodevelop/Core/src/MonoDevelop.Core/Makefile.am     2007-09-05 
12:10:30 UTC (rev 85352)
@@ -52,6 +52,7 @@
        MonoDevelop.Core/FileEventArgs.cs \
        MonoDevelop.Core/FileService.cs \
        MonoDevelop.Core/IAsyncOperation.cs \
+       MonoDevelop.Core/ICustomXmlSerializer.cs \
        MonoDevelop.Core/ILoggingService.cs \
        MonoDevelop.Core/IProgressMonitor.cs \
        MonoDevelop.Core/IService.cs \

Added: 
trunk/monodevelop/Core/src/MonoDevelop.Core/MonoDevelop.Core/ICustomXmlSerializer.cs
===================================================================
--- 
trunk/monodevelop/Core/src/MonoDevelop.Core/MonoDevelop.Core/ICustomXmlSerializer.cs
        2007-09-05 11:57:12 UTC (rev 85351)
+++ 
trunk/monodevelop/Core/src/MonoDevelop.Core/MonoDevelop.Core/ICustomXmlSerializer.cs
        2007-09-05 12:10:30 UTC (rev 85352)
@@ -0,0 +1,39 @@
+//
+// ICustomXmlSerializer.cs
+//
+// Author:
+//   Mike Krüger <[EMAIL PROTECTED]>
+//
+// Copyright (C) 2007 Novell, Inc (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System;
+using System.Xml;
+
+namespace MonoDevelop.Core
+{
+       public interface ICustomXmlSerializer
+       {
+               void WriteTo (XmlWriter writer);
+               ICustomXmlSerializer ReadFrom (XmlReader reader);
+       }
+}

Modified: 
trunk/monodevelop/Core/src/MonoDevelop.Core/MonoDevelop.Core/Properties.cs
===================================================================
--- trunk/monodevelop/Core/src/MonoDevelop.Core/MonoDevelop.Core/Properties.cs  
2007-09-05 11:57:12 UTC (rev 85351)
+++ trunk/monodevelop/Core/src/MonoDevelop.Core/MonoDevelop.Core/Properties.cs  
2007-09-05 12:10:30 UTC (rev 85352)
@@ -40,7 +40,7 @@
 
 namespace MonoDevelop.Core
 {
-       public class Properties
+       public class Properties : ICustomXmlSerializer
        {
                Dictionary<string, object> properties    = new 
Dictionary<string, object> ();
                Dictionary<string, object> defaultValues = new 
Dictionary<string, object> ();
@@ -57,12 +57,12 @@
                
                T Convert<T> (object o)
                {
-                       if (o is T) {
+                       if (o is T) 
                                return (T)o;
-                       }
-                       if (o is LazyXmlDeserializer) {
-                               return (T)((LazyXmlDeserializer)o).Deserialize 
();
-                       }
+                       
+                       if (o is LazyXmlDeserializer) 
+                               return 
(T)((LazyXmlDeserializer)o).Deserialize<T> ();
+                       
                        TypeConverter converter = TypeDescriptor.GetConverter 
(typeof(T));
                        if (o is string) {
                                try {
@@ -124,40 +124,43 @@
                public const string KeyAttribute   = "key";
                public const string ValueAttribute = "value";
                public const string XmlSerialized  = "XmlSerialized";
-               public const string TypeAttribute  = "type";
                
                public const string PropertiesRootNode = 
"MonoDevelopProperties";
                public const string PropertiesVersionAttribute  = "version";
                public const string PropertiesVersion  = "2.0";
-               
+
+               void ICustomXmlSerializer.WriteTo (XmlWriter writer)
+               {
+                       Write (writer, false);
+               }
+                       
+               ICustomXmlSerializer ICustomXmlSerializer.ReadFrom (XmlReader 
reader)
+               {
+                       return Read (reader);
+               }
+                       
                public void Write (XmlWriter writer)
                {
-                               Write (writer, true);
+                       Write (writer, true);
                }
                
                public void Write (XmlWriter writer, bool createPropertyParent)
                {
                        if (createPropertyParent)
-                                       writer.WriteStartElement (Node);
+                               writer.WriteStartElement (Node);
 
                        foreach (KeyValuePair<string, object> property in 
this.properties) {
                                writer.WriteStartElement (PropertyNode);
                                writer.WriteAttributeString (KeyAttribute, 
property.Key);
                                
                                if (property.Value is LazyXmlDeserializer) {
-                                       writer.WriteStartElement 
(XmlSerialized);
-                                       writer.WriteAttributeString 
(TypeAttribute, ((LazyXmlDeserializer)property.Value).Type);
                                        writer.WriteRaw 
(((LazyXmlDeserializer)property.Value).Xml);
-                                       writer.WriteEndElement (); // 
XmlSerialized
-                               } else if (property.Value is Properties) {
-                                       ((Properties)property.Value).Write 
(writer);
+                               } else if (property.Value is 
ICustomXmlSerializer) {
+                                       
((ICustomXmlSerializer)property.Value).WriteTo (writer);
                                } else {
                                        if (property.Value.GetType () != 
typeof(string) && property.Value.GetType ().IsClass) {
-                                               writer.WriteStartElement 
(XmlSerialized);
-                                               writer.WriteAttributeString 
(TypeAttribute, property.Value.GetType ().FullName);
                                                XmlSerializer serializer = new 
XmlSerializer (property.Value.GetType ());
                                                serializer.Serialize (writer, 
property.Value);
-                                               writer.WriteEndElement (); // 
XmlSerialized
                                        } else {
                                                writer.WriteAttributeString 
(ValueAttribute, ConvertToString (property.Value));
                                        }
@@ -185,45 +188,39 @@
                
                class LazyXmlDeserializer
                {
-                       string type;
                        string xml;
                        
-                       public string Type {
-                               get {
-                                       return type;
-                               }
-                       }
-                               
                        public string Xml {
                                get {
                                        return xml;
                                }
                        }
                        
-                       public LazyXmlDeserializer (string type, string xml)
+                       public LazyXmlDeserializer (string xml)
                        {
-                               this.type = type;
                                this.xml  = xml;
                        }
                        
-                       static System.Type Lookup (string type)
+                       public T Deserialize<T> ()
                        {
-                               System.Type result = System.Type.GetType (type);
-                               if (result == null) {
-                                       foreach (Assembly asm in 
AppDomain.CurrentDomain.GetAssemblies ()) {
-                                               result = asm.GetType (type);
-                                               if (result != null)
-                                                       break;
+                               try {
+                                       if 
(typeof(ICustomXmlSerializer).IsAssignableFrom (typeof(T))) {
+                                               XmlReader reader = new 
XmlTextReader (new MemoryStream (System.Text.Encoding.UTF8.GetBytes (xml)));
+                                               // set cursor on the first 
element.
+                                               while (reader.Read () && 
reader.NodeType != XmlNodeType.Element)
+                                                       ;
+                                               object result = 
((ICustomXmlSerializer)typeof(T).Assembly.CreateInstance 
(typeof(T).FullName)).ReadFrom (reader);
+                                               reader.Close ();
+                                               return (T)result;
                                        }
+                                                   
+                                       XmlSerializer serializer = new 
XmlSerializer (typeof(T));
+                                       return (T)serializer.Deserialize (new 
StreamReader(new MemoryStream(System.Text.Encoding.UTF8.GetBytes (xml))));
+                               } catch (Exception e) {
+                                       Runtime.LoggingService.Warn 
((object)("Caught exception while deserializing:" + typeof(T)), e);
+                                       return default(T);
                                }
-                               return result;
                        }
-                       
-                       public object Deserialize ()
-                       {
-                               XmlSerializer serializer = new XmlSerializer 
(Lookup (type));
-                               return serializer.Deserialize (new 
StreamReader(new MemoryStream(System.Text.Encoding.UTF8.GetBytes (xml))));
-                       }
                }
                
                public static Properties Read (XmlReader reader)
@@ -234,16 +231,7 @@
                                case PropertyNode:
                                        string key = reader.GetAttribute 
(KeyAttribute);
                                        if (!reader.IsEmptyElement) {
-                                               while (reader.Read () && 
reader.NodeType != XmlNodeType.Element)
-                                                       ;  
-                                               switch (reader.LocalName) {
-                                               case Node:
-                                                       result.Set (key, Read 
(reader));
-                                                       break;
-                                               case XmlSerialized:
-                                                       result.Set (key, new 
LazyXmlDeserializer(reader.GetAttribute (TypeAttribute), reader.ReadInnerXml 
()));
-                                                       break;
-                                               }
+                                               result.Set (key, new 
LazyXmlDeserializer (reader.ReadInnerXml ()));
                                        } else {
                                                result.Set (key, 
reader.GetAttribute (ValueAttribute));
                                        }

Modified: trunk/monodevelop/Core/src/MonoDevelop.Core/MonoDevelop.Core.mdp
===================================================================
--- trunk/monodevelop/Core/src/MonoDevelop.Core/MonoDevelop.Core.mdp    
2007-09-05 11:57:12 UTC (rev 85351)
+++ trunk/monodevelop/Core/src/MonoDevelop.Core/MonoDevelop.Core.mdp    
2007-09-05 12:10:30 UTC (rev 85352)
@@ -83,6 +83,7 @@
     <File name="./MonoDevelop.Core/PropertyChangedEventArgs.cs" subtype="Code" 
buildaction="Compile" />
     <File name="./MonoDevelop.Core/PropertyService.cs" subtype="Code" 
buildaction="Compile" />
     <File name="./MonoDevelop.Core/XmlReadHelper.cs" subtype="Code" 
buildaction="Compile" />
+    <File name="./MonoDevelop.Core/ICustomXmlSerializer.cs" subtype="Code" 
buildaction="Compile" />
   </Contents>
   <References>
     <ProjectReference type="Gac" localcopy="False" refto="System, 
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to