aboutsummaryrefslogtreecommitdiffstatsci
path: root/src
diff options
context:
space:
mode:
authorjpbruyere <jp.bruyere@hotmail.com>2016-08-11 02:35:39 +0200
committerjpbruyere <jp.bruyere@hotmail.com>2016-08-11 03:32:23 +0200
commit7560c0bb4a769210e289e2633ddf13d2617f8e6e (patch)
tree3225277358a2bba61c4496923ba39353c942098b /src
parent13b1a9755a9e036122dd2f3e29714bbb5141118c (diff)
resolved bug multiple template level read, ensure reader stay on sybling
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
Diffstat (limited to 'src')
-rw-r--r--src/IMLReader.cs37
-rw-r--r--src/Instantiator.cs43
2 files changed, 56 insertions, 24 deletions
diff --git a/src/IMLReader.cs b/src/IMLReader.cs
index 87426726..f8fd34ed 100644
--- a/src/IMLReader.cs
+++ b/src/IMLReader.cs
@@ -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;
}
}
diff --git a/src/Instantiator.cs b/src/Instantiator.cs
index 23871ef2..e79988fb 100644
--- a/src/Instantiator.cs
+++ b/src/Instantiator.cs
@@ -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);
+ }
+ }
}
}