]> O.S.I.I.S - jp/crow.git/commitdiff
resolved bug multiple template level read, ensure reader stay on sybling 18/head
authorjpbruyere <jp.bruyere@hotmail.com>
Thu, 11 Aug 2016 00:35:39 +0000 (02:35 +0200)
committerjpbruyere <jp.bruyere@hotmail.com>
Thu, 11 Aug 2016 01:32:23 +0000 (03:32 +0200)
nodes
modifié :         src/IMLReader.cs
create instance from XmlFragment
modifié :         src/Instantiator.cs
update tests
modifié :         Tests/BasicTests.cs
modifié :         Tests/Interfaces/Divers/imlEditor.crow

Tests/BasicTests.cs
Tests/Interfaces/Divers/imlEditor.crow
src/IMLReader.cs
src/Instantiator.cs

index c862ccac462b846c87cc489365ca80cd8b9165c2..658cddd82fe29facdf9571affdce5e6dd1a5d2d8 100644 (file)
@@ -4,6 +4,8 @@ using System.Collections.Generic;
 using System.Linq;
 using System.IO;
 using System.Reflection;
+using System.Text;
+using System.Diagnostics;
 
 
 namespace Tests
@@ -147,17 +149,30 @@ namespace Tests
                }
                void Tv_SelectedItemChanged (object sender, SelectionChangeEventArgs e)
                {
-//                     FileInfo fi = e.NewValue as FileInfo;
-//                     if (fi == null)
-//                             return;
-//                     if (fi.Extension == ".crow" || fi.Extension == ".goml") {
-//                             IMLStream imls = new IMLStream (fi.FullName);
-//                             lock (CrowInterface.UpdateMutex) {
-//                                     (CrowInterface.FindByName ("crowContainer") as Container).SetChild
-//                                     (imls.Instance);
-//                                     CurSources = imls.Source;
-//                             }
-//                     }
+                       FileInfo fi = e.NewValue as FileInfo;
+                       if (fi == null)
+                               return;
+                       if (fi.Extension == ".crow" || fi.Extension == ".goml") {
+                               Instantiator i = new Instantiator(fi.FullName);
+                               lock (CrowInterface.UpdateMutex) {
+                                       (CrowInterface.FindByName ("crowContainer") as Container).SetChild
+                                       (i.CreateInstance());
+                                       CurSources = i.GetImlSourcesCode();
+                               }
+                       }
+               }
+               void onImlSourceChanged(Object sender, TextChangeEventArgs e){
+                       Instantiator i;
+                       try {
+                               i = Instantiator.CreateFromImlFragment (e.Text);
+                       } catch (Exception ex) {
+                               Debug.WriteLine (ex);
+                               return;
+                       }
+                       lock (CrowInterface.UpdateMutex) {
+                               (CrowInterface.FindByName ("crowContainer") as Container).SetChild
+                               (i.CreateInstance());
+                       }
                }
                void onButClick(object send, MouseButtonEventArgs e)
                {
index 527745c17b028e1885530f9a2fa091cda8bff62e..bbad04efd1c1041dcf9384d01017bc3adba5f9f3 100644 (file)
@@ -15,6 +15,7 @@
                                        <TextBox Background="White" Height="Fit"
                                                        VerticalAlignment="Top"
                                                        TextAlignment="TopLeft" Font="mono, 10"
+                                                       TextChanged="onImlSourceChanged"
                                                        Text="{CurSources}" Multiline="true"/>
                                </Scroller>
                                <ScrollBar Name="scrollbar1" Value="{../scroller1.ScrollY}" 
index 87426726bc143cf2010d75dfa79299b835578b1f..f8fd34ed3b62a5e359dc5d28a82b14bfb8ed9908 100644 (file)
@@ -32,12 +32,25 @@ namespace Crow
        {
                Interface.LoaderInvoker loader = null;
 
-               public string ImlPath;
-               public Stream ImlStream;
-               public Type RootType = null;
+               string ImlPath;
+               Stream ImlStream;
 
                DynamicMethod dm = null;
+
                public ILGenerator il = null;
+               public Type RootType = null;
+
+               /// <summary>
+               /// Finalize instatiator MSIL and return LoaderInvoker delegate
+               /// </summary>
+               public Interface.LoaderInvoker GetLoader(){
+                       if (loader != null)
+                               return loader;
+
+                       il.Emit(OpCodes.Ret);
+                       loader = (Interface.LoaderInvoker)dm.CreateDelegate (typeof(Interface.LoaderInvoker));
+                       return loader;
+               }
 
                #region CTOR
                public IMLReader (string path)
@@ -72,17 +85,6 @@ namespace Crow
                        Read();//close tag
                }
                /// <summary>
-               /// Finalize instatiator MSIL and return LoaderInvoker delegate
-               /// </summary>
-               public Interface.LoaderInvoker GetLoader(){
-                       if (loader != null)
-                               return loader;
-
-                       il.Emit(OpCodes.Ret);
-                       loader = (Interface.LoaderInvoker)dm.CreateDelegate (typeof(Interface.LoaderInvoker));
-                       return loader;
-               }
-               /// <summary>
                /// Inits il generator, RootType must have been read first
                /// </summary>
                void InitEmitter(){
@@ -114,16 +116,15 @@ namespace Crow
                                        List<string[]> itemTemplateIds = new List<string[]> ();
                                        bool inlineTemplate = false;
                                        reader.Read ();
-
+                                       int depth = reader.Depth + 1;
                                        while (reader.Read ()) {
-                                               if (!reader.IsStartElement ())
+                                               if (!reader.IsStartElement () || reader.Depth > depth)
                                                        continue;
                                                if (reader.Name == "Template") {
                                                        inlineTemplate = true;
                                                        reader.Read ();
 
                                                        readChildren (reader, crowType,true);
-                                                       continue;
                                                } else if (reader.Name == "ItemTemplate") {
                                                        string dataType = "default", datas = "", path = "";
                                                        while (reader.MoveToNextAttribute ()) {
@@ -142,8 +143,6 @@ namespace Crow
                                                                
                                                                itemTemplateIds.Add (new string[] { dataType, uid, datas });
                                                        }
-
-                                                       continue;
                                                }
                                        }
 
index 23871ef28923763ffaf27168db425fabd4fde3a1..e79988fbec660e9568914923d0d2b9f9fb3f2bea 100644 (file)
@@ -20,6 +20,8 @@
 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 using System;
 using System.Threading;
+using System.IO;
+using System.Text;
 
 namespace Crow
 {
@@ -27,11 +29,12 @@ namespace Crow
        {
                public Type RootType;
                Interface.LoaderInvoker loader;
+               string imlPath;
+
 
                #region CTOR
                public Instantiator (string path){
-                       System.Globalization.CultureInfo savedCulture = Thread.CurrentThread.CurrentCulture;
-                       Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
+                       imlPath = path;
 
                        #if DEBUG_LOAD
                        Stopwatch loadingTime = new Stopwatch ();
@@ -43,7 +46,7 @@ namespace Crow
                                        RootType = itr.RootType;
                                }
                        } catch (Exception ex) {
-                               throw new Exception ("Error loading <" + path + ">:", ex);
+                               throw new Exception ("Error loading <" + path + ">:\n", ex);
                        }
 
                        #if DEBUG_LOAD
@@ -51,8 +54,30 @@ namespace Crow
                        Debug.WriteLine ("IML Instantiator creation '{2}' : {0} ticks, {1} ms",
                        loadingTime.ElapsedTicks, loadingTime.ElapsedMilliseconds, path);
                        #endif
-
-                       Thread.CurrentThread.CurrentCulture = savedCulture;                     
+               }
+               public static Instantiator CreateFromImlFragment(string fragment){
+                       try {
+                               using (Stream s = new MemoryStream(Encoding.UTF8.GetBytes(fragment))){
+                                       return new Instantiator(s);
+                               }
+                       } catch (Exception ex) {
+                               throw new Exception ("Error loading fragment:\n" + fragment + "\n", ex);
+                       }
+               }
+               public Instantiator (Stream stream){
+                       #if DEBUG_LOAD
+                       Stopwatch loadingTime = new Stopwatch ();
+                       loadingTime.Start ();
+                       #endif
+                       using (IMLReader itr = new IMLReader (stream)){
+                               loader = itr.GetLoader ();
+                               RootType = itr.RootType;
+                       }
+                       #if DEBUG_LOAD
+                       loadingTime.Stop ();
+                       Debug.WriteLine ("IML Instantiator creation '{2}' : {0} ticks, {1} ms",
+                       loadingTime.ElapsedTicks, loadingTime.ElapsedMilliseconds, path);
+                       #endif
                }
                public Instantiator (Type _root, Interface.LoaderInvoker _loader)
                {
@@ -66,6 +91,14 @@ namespace Crow
                        loader (tmp);
                        return tmp;
                }
+               public string GetImlSourcesCode(){
+                       try {
+                               using (StreamReader sr = new StreamReader (imlPath))
+                                       return sr.ReadToEnd();
+                       } catch (Exception ex) {
+                               throw new Exception ("Error getting sources for <" + imlPath + ">:", ex);
+                       }
+               }
        }
 }