Author: alanmc
Date: 2007-06-25 17:27:40 -0400 (Mon, 25 Jun 2007)
New Revision: 80729

Added:
   trunk/lunareclipse/Serialization/
   trunk/lunareclipse/Serialization/Serializer.cs
Modified:
   trunk/lunareclipse/DesignerMoon.mdp
   trunk/lunareclipse/View/MainWindow.cs
Log:
Commited initial Serializer (note: contains hacks)

Modified: trunk/lunareclipse/DesignerMoon.mdp
===================================================================
--- trunk/lunareclipse/DesignerMoon.mdp 2007-06-25 21:25:42 UTC (rev 80728)
+++ trunk/lunareclipse/DesignerMoon.mdp 2007-06-25 21:27:40 UTC (rev 80729)
@@ -1,9 +1,6 @@
 <Project name="DesignerMoon" fileversion="2.0" language="C#" 
clr-version="Net_2_0" ctype="DotNetProject">
   <Configurations active="Debug">
     <Configuration name="Debug" ctype="DotNetProjectConfiguration">
-      <CustomCommands>
-        <Command type="Custom" command="export MONO_GAC_PREFIX=/usr/local" />
-      </CustomCommands>
       <Output directory="./bin/Debug" assembly="DesignerMoon" />
       <Build debugmode="True" target="Exe" />
       <Execution runwithwarnings="True" consolepause="False" runtime="MsNet" 
clr-version="Net_2_0" />
@@ -38,6 +35,8 @@
     <File name="./Model/Draw/EllipseDraw.cs" subtype="Code" 
buildaction="Compile" />
     <File name="./Model/Draw/CircleDraw.cs" subtype="Code" 
buildaction="Compile" />
     <File name="./Model/Draw/PenDraw.cs" subtype="Code" buildaction="Compile" 
/>
+    <File name="./Serialization" subtype="Directory" buildaction="Compile" />
+    <File name="./Serialization/Serializer.cs" subtype="Code" 
buildaction="Compile" />
   </Contents>
   <References>
     <ProjectReference type="Gac" localcopy="True" refto="System, 
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />

Added: trunk/lunareclipse/Serialization/Serializer.cs
===================================================================
--- trunk/lunareclipse/Serialization/Serializer.cs      2007-06-25 21:25:42 UTC 
(rev 80728)
+++ trunk/lunareclipse/Serialization/Serializer.cs      2007-06-25 21:27:40 UTC 
(rev 80729)
@@ -0,0 +1,216 @@
+// /home/alan/Projects/LunarEclipse/Serialization/Serializer.cs created with 
MonoDevelop
+// User: alan at 2:19 PMĀ 6/25/2007
+//
+// To change standard headers go to Edit->Preferences->Coding->Standard Headers
+//
+
+using System;
+using System.Reflection;
+using System.Windows.Controls;
+using System.Windows;
+using System.Windows.Shapes;
+using System.Xml;
+using System.Text;
+using System.Collections;
+
+
+namespace DesignerMoon
+{
+    public class Serializer
+    {
+        public Serializer()
+        {
+        }
+
+        private static string CleanName(string fieldName)
+        {
+            return fieldName.Substring(0, fieldName.Length - 8);
+        }
+        
+        private static bool IsCollection(object value)
+        {
+            if(value == null)
+                return false;
+
+            // FIXME: Should i just be checking for IEnumerable? That may be 
easier.
+            return 
value.GetType().BaseType.FullName.StartsWith("MS.Internal.Collection");
+        }
+        
+        
+        private void SerialiseCollection(FieldInfo field, object value, 
XmlWriter writer)
+        {
+            //FIXME: Reflection hack
+            
+            int count = 0;
+            Console.WriteLine("Serializing Collection: " + field.Name);
+            
+            
+            Type t = value.GetType();
+            MethodInfo info = t.GetMethod("GetEnumerator");
+            IEnumerator enumerator = (IEnumerator)info.Invoke(value, null);
+            
+            enumerator.Reset();
+            while(enumerator.MoveNext())
+            {
+                if((count++) == 0)
+                    writer.WriteStartElement(CleanName(field.Name));
+                Console.WriteLine("Moving Next: " + (count++));
+                Console.WriteLine("Serializing child: " + 
enumerator.Current.GetType().Name);
+                Serialize((DependencyObject)enumerator.Current, writer);
+            }
+            
+            if(count > 0)
+                writer.WriteEndElement();
+        }
+        
+        public string Serialize(Canvas canvas)
+        {
+            StringBuilder sb = new StringBuilder();
+            XmlWriter writer = XmlWriter.Create(sb);
+            
+            try
+            {
+            Serialize(canvas, writer);
+            }
+            catch(Exception ex)
+            {
+                Console.WriteLine(ex.ToString());
+            }
+            writer.Flush();
+
+            sb.Insert(6, " 
xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"";);
+            return sb.ToString();
+        }
+        
+        private void Serialize(DependencyObject item, XmlWriter writer)
+        {
+            Console.WriteLine("Serializing object: " + item.GetType().Name);
+            //Console.ReadLine();
+            Type currentType = item.GetType();
+            Type dependencyProperty = Canvas.BackgroundProperty.GetType();
+
+            writer.WriteStartElement(currentType.Name);
+
+            while(currentType != null)
+            {
+                FieldInfo[] fields = currentType.GetFields();
+                
+                foreach(FieldInfo field in fields)
+                {
+                    if(!field.FieldType.Equals(dependencyProperty))
+                        continue;
+                    
+                    object dependencyValue = field.GetValue(item);
+                    
+                    if(dependencyValue == null)
+                    {
+                        Console.WriteLine(string.Format("{0} from {1} 
contained a null property", field.Name, field.ReflectedType.ToString()));
+                    }
+                    else
+                    {
+                        object value = 
item.GetValue((DependencyProperty)dependencyValue);
+                        
+
+                        if(!IsCollection(value) && !IsDefaultValue(value))
+                        {
+                            writer.WriteAttributeString(CleanName(field.Name), 
value.ToString());
+                            Console.WriteLine(string.Format("{0} is of value 
{1}", field.Name, value));
+                        }
+                    }
+                }
+                
+                currentType = currentType.BaseType;
+            }
+
+            currentType = item.GetType();
+            while(currentType != null)
+            {
+                FieldInfo[] fields = currentType.GetFields();
+                
+                foreach(FieldInfo field in fields)
+                {
+                    if(!field.FieldType.Equals(dependencyProperty))
+                        continue;
+                    
+                    object dependencyValue = field.GetValue(item);
+                    
+                    if(dependencyValue == null)
+                    {
+                        Console.WriteLine(string.Format("{0} from {1} 
contained a null property", field.Name, field.ReflectedType.ToString()));
+                    }
+                    else
+                    {
+                        object value = 
item.GetValue((DependencyProperty)dependencyValue);
+                        
+                        if(IsCollection(value))
+                        {
+                            SerialiseCollection(field, value, writer);
+                            Console.WriteLine(string.Format("{0} is of value 
{1}", field.Name, value));
+                        }
+                    }
+                }
+                
+                currentType = currentType.BaseType;
+            }
+            
+            writer.WriteEndElement();
+        }
+        
+ /*       private void Serialize(DependencyObject item, XmlWriter writer)
+        {
+            Type currentType = item.GetType();
+            Type dependencyProperty = Canvas.BackgroundProperty.GetType();
+
+            writer.WriteStartElement(currentType.Name);
+
+            while(currentType != null)
+            {
+                FieldInfo[] fields = currentType.GetFields();
+                
+                foreach(FieldInfo field in fields)
+                {
+                    if(!field.FieldType.Equals(dependencyProperty))
+                        continue;
+                    
+                    object dependencyValue = field.GetValue(item);
+                    
+                    if(dependencyValue == null)
+                    {
+                        Console.WriteLine(string.Format("{0} from {1} 
contained a null property", field.Name, field.ReflectedType.ToString()));
+                    }
+                    else
+                    {
+                        object value = 
item.GetValue((DependencyProperty)dependencyValue);
+                        Console.WriteLine(string.Format("{0} is of value {1}", 
field.Name, value));
+                        
+                        if(IsCollection(value))
+                            SerialiseCollection(field, value, writer);
+
+                        else if(!IsDefaultValue(value))
+                            writer.WriteAttributeString(CleanName(field.Name), 
value.ToString());
+                    }
+                }
+                
+                currentType = currentType.BaseType;
+            }
+            
+            writer.WriteEndElement();
+        }
+*/
+        private static bool IsDefaultValue(object value)
+        {
+            if(value == null)
+                return true;
+            
+
+            try
+            {
+                return Convert.ToDouble(value) == 0;
+            }
+            catch
+            {
+               return false;
+            }
+        }
+    }
+}

Modified: trunk/lunareclipse/View/MainWindow.cs
===================================================================
--- trunk/lunareclipse/View/MainWindow.cs       2007-06-25 21:25:42 UTC (rev 
80728)
+++ trunk/lunareclipse/View/MainWindow.cs       2007-06-25 21:27:40 UTC (rev 
80729)
@@ -28,6 +28,32 @@
             Canvas c = new Canvas();
             c.Width = 640;
             c.Height = 480;
+            Rectangle r = new Rectangle();
+            r.Width = 100;
+            r.Height = 50;
+            r.SetValue<double>(Canvas.TopProperty, 100);
+            r.SetValue<double>(Canvas.TopProperty, 200);
+            
+            Ellipse e = new Ellipse();
+            e.Fill = new SolidColorBrush(Colors.Red);
+            LinearGradientBrush brush = new LinearGradientBrush();
+            brush.StartPoint = new Point(55,66);
+            brush.ColorInterpolationMode = 
ColorInterpolationMode.SRgbLinearInterpolation;
+            e.Stroke = brush;
+            e.StrokeThickness = 5;
+            c.Children.Add(e);
+                
+            c.Background = new ImageBrush();
+            c.SetValue<string>(Canvas.NameProperty, "Canvas Name");
+            c.Background.Opacity = 5;
+            
+            c.RenderTransformOrigin = new Point(55, 55);
+            
+            c.Children.Add(r);
+            Serializer s = new Serializer();
+            Console.WriteLine(s.Serialize(c));
+            return;
+
                GtkSilver moonlight = new GtkSilver(640, 480);
             moonlight.Attach(c);
 

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

Reply via email to