diff --git a/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/ICSharpCode.Reporting.csproj b/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/ICSharpCode.Reporting.csproj index 9d135c5203..a6feaaf15d 100644 --- a/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/ICSharpCode.Reporting.csproj +++ b/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/ICSharpCode.Reporting.csproj @@ -107,11 +107,13 @@ + + diff --git a/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/Exporter/Visitors/ExpressionVisitor.cs b/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/Exporter/Visitors/ExpressionVisitor.cs index c06c606953..19a3b721d9 100644 --- a/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/Exporter/Visitors/ExpressionVisitor.cs +++ b/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/Exporter/Visitors/ExpressionVisitor.cs @@ -2,6 +2,7 @@ // This code is distributed under the GNU LGPL (for details please see \doc\license.txt) using System; using System.Collections.ObjectModel; +using ICSharpCode.Reporting.Expressions; using ICSharpCode.Reporting.PageBuilder.ExportColumns; using Irony.Interpreter.Evaluator; @@ -23,6 +24,11 @@ namespace ICSharpCode.Reporting.Exporter.Visitors evaluator = new ExpressionEvaluator(grammar); } + internal ExpressionVisitor() { + grammar = new ExpressionEvaluatorGrammar(); + evaluator = new ExpressionEvaluator(grammar); + } + public override void Visit(ExportPage page) { var result = evaluator.Evaluate("5 * 10"); @@ -49,8 +55,16 @@ namespace ICSharpCode.Reporting.Exporter.Visitors public override void Visit(ExportText exportColumn) { - var result = evaluator.Evaluate("2 + 3"); - Console.WriteLine("\t\tExpressionVisitor <{0}> - {1}",exportColumn.Name,result); + if (exportColumn.Text.StartsWith("=")) { + try { + var str = ExpressionHelper.ExtractExpressionPart(exportColumn.Text); + var result = evaluator.Evaluate(str); + exportColumn.Text = result.ToString(); + } catch (Exception) { + + throw; + } + } } } } diff --git a/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/Expressions/ExpressionHelper.cs b/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/Expressions/ExpressionHelper.cs new file mode 100644 index 0000000000..bd1f9fc491 --- /dev/null +++ b/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/Expressions/ExpressionHelper.cs @@ -0,0 +1,24 @@ +// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) +// This code is distributed under the GNU LGPL (for details please see \doc\license.txt) +using System; +using System.Globalization; +using ICSharpCode.Reporting.Globals; + +namespace ICSharpCode.Reporting.Expressions +{ + /// + /// Description of ExpressionHelper. + /// + class ExpressionHelper + { + public ExpressionHelper() + { + } + + public static string ExtractExpressionPart (string src) + { + char v = Convert.ToChar("=",CultureInfo.CurrentCulture ); + return StringHelper.RightOf(src,v).Trim(); + } + } +} diff --git a/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/Expressions/ExpressionRunner.cs b/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/Expressions/ExpressionRunner.cs index c2dcdf0b1c..af6f957774 100644 --- a/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/Expressions/ExpressionRunner.cs +++ b/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/Expressions/ExpressionRunner.cs @@ -32,113 +32,9 @@ namespace ICSharpCode.Reporting.Expressions foreach (var page in Pages) { var acceptor = page as IAcceptor; acceptor.Accept(visitor); -// RunInternal(page); } Console.WriteLine("Finish ExpressionVisitor"); Console.WriteLine(); } - -// "Visitor" -// http://irony.codeplex.com/discussions/213938 -// -// http://irony.codeplex.com/discussions/35310 - - /* - void RunInternal(IExportContainer container) - { -// Console.WriteLine(); -// Console.WriteLine("{0}{1}",leading,container.Name); - foreach (var item in container.ExportedItems) { - var exportContainer = item as IExportContainer; - var acceptor = item as IAcceptor; - if (exportContainer != null) { - if (exportContainer.ExportedItems.Count > 0) { -// acceptor.Accept(visitor); - bRunInternal(exportContainer.ExportedItems); - acceptor.Accept(visitor); -// ShowDebug(leading = leading + "--",exportContainer); - - } - } -// acceptor.Accept(visitor); - } - } - */ - /* - - void bRunInternal(List list) - { -// Console.WriteLine(); -// Console.WriteLine("{0}{1}",leading,container.Name); - foreach (var item in list) { - var exportContainer = item as IExportContainer; - var acceptor = item as IAcceptor; - if (exportContainer != null) { - if (exportContainer.ExportedItems.Count > 0) { - acceptor.Accept(visitor); - RunInternal(exportContainer); - -// ShowDebug(leading = leading + "--",exportContainer); - - } - } - acceptor.Accept(visitor); - } - } - */ - /* - void bRunInternal(IExportContainer container) - { -// Console.WriteLine(); -// Console.WriteLine("{0}{1}",leading,container.Name); - foreach (var item in container.ExportedItems) { - var exportContainer = item as IExportContainer; - var acceptor = item as IAcceptor; - if (exportContainer != null) { - if (exportContainer.ExportedItems.Count > 0) { - acceptor.Accept(visitor); - RunInternal(exportContainer); - -// ShowDebug(leading = leading + "--",exportContainer); - - } - } - acceptor.Accept(visitor); - } - } - */ - - /* - //Items first, then container - void aRunInternal(IExportContainer container) - { -// Console.WriteLine(); -// Console.WriteLine("{0}{1}",leading,container.Name); - foreach (var item in container.ExportedItems) { - var exportContainer = item as IExportContainer; - var acceptor = item as IAcceptor; - if (exportContainer != null) { - if (exportContainer.ExportedItems.Count > 0) { - RunInternal(exportContainer); - acceptor.Accept(visitor); -// ShowDebug(leading = leading + "--",exportContainer); - - } - } - acceptor.Accept(visitor); - } - } - */ - /* - void InternalRun(ExportPage page) - { - page.Accept(visitor); - foreach (var item in page.ExportedItems) { -// ShowContainerRecursive(null,item); - var acceptor = item as IAcceptor; - acceptor.Accept(visitor); - } - } - */ } } diff --git a/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/Globals/StringHelper.cs b/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/Globals/StringHelper.cs new file mode 100644 index 0000000000..57dee6b267 --- /dev/null +++ b/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/Globals/StringHelper.cs @@ -0,0 +1,139 @@ +// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) +// This code is distributed under the GNU LGPL (for details please see \doc\license.txt) +using System; + +namespace ICSharpCode.Reporting.Globals +{ + /// + /// Description of StringHelper. + /// + class StringHelper + { + /// + /// Left of the first occurance of c + /// + /// + /// + /// + public static string LeftOf(string src, char c) + { + int idx=src.IndexOf(c); + if (idx==-1) + { + return src; + } + + return src.Substring(0, idx); + } + + /// + /// Left of the n'th occurance of c + /// + /// + /// + /// + /// + public static string LeftOf(string src, char c, int n) + { + int idx=-1; + while (n != 0) + { + idx=src.IndexOf(c, idx+1); + if (idx==-1) + { + return src; + } + --n; + } + return src.Substring(0, idx); + } + + /// + /// Right of the first occurance of c + /// + /// + /// + /// + public static string RightOf(string src, char c) + { + int idx=src.IndexOf(c); + if (idx==-1) + { + return ""; + } + + return src.Substring(idx+1); + } + + /// + /// Right of the n'th occurance of c + /// + /// + /// + /// + public static string RightOf(string src, char c, int n) + { + int idx=-1; + while (n != 0) + { + idx=src.IndexOf(c, idx+1); + if (idx==-1) + { + return ""; + } + --n; + } + + return src.Substring(idx+1); + } + + public static string LeftOfRightmostOf(string src, char c) + { + int idx=src.LastIndexOf(c); + if (idx==-1) + { + return src; + } + return src.Substring(0, idx); + } + + public static string RightOfRightmostOf(string src, char c) + { + int idx=src.LastIndexOf(c); + if (idx==-1) + { + return src; + } + return src.Substring(idx+1); + } + + public static string Between(string src, char start, char end) + { + string res=String.Empty; + int idxStart=src.IndexOf(start); + if (idxStart != -1) + { + ++idxStart; + int idxEnd=src.IndexOf(end, idxStart); + if (idxEnd != -1) + { + res=src.Substring(idxStart, idxEnd-idxStart); + } + } + return res; + } + + public static int Count(string src, char find) + { + int ret=0; + foreach(char s in src) + { + if (s==find) + { + ++ret; + } + } + return ret; + } + } +} diff --git a/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/PageBuilder/ExportColumns/ExportColumn.cs b/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/PageBuilder/ExportColumns/ExportColumn.cs index 28c00c5862..c0b9e3fc08 100644 --- a/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/PageBuilder/ExportColumns/ExportColumn.cs +++ b/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/PageBuilder/ExportColumns/ExportColumn.cs @@ -42,10 +42,8 @@ namespace ICSharpCode.Reporting.PageBuilder.ExportColumns public bool CanGrow {get;set;} - public Rectangle DisplayRectangle { get { -// return new Rectangle(Location,Size); return new Rectangle(Location,DesiredSize); } } diff --git a/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Test/ICSharpCode.Reporting.Test/ICSharpCode.Reporting.Test.csproj b/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Test/ICSharpCode.Reporting.Test/ICSharpCode.Reporting.Test.csproj index 7993496e54..aebc9fcdc6 100644 --- a/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Test/ICSharpCode.Reporting.Test/ICSharpCode.Reporting.Test.csproj +++ b/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Test/ICSharpCode.Reporting.Test/ICSharpCode.Reporting.Test.csproj @@ -66,6 +66,7 @@ + diff --git a/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Test/ICSharpCode.Reporting.Test/src/Expressions/IntegrationTests.cs b/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Test/ICSharpCode.Reporting.Test/src/Expressions/IntegrationTests.cs new file mode 100644 index 0000000000..2dfddda071 --- /dev/null +++ b/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Test/ICSharpCode.Reporting.Test/src/Expressions/IntegrationTests.cs @@ -0,0 +1,99 @@ +// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) +// This code is distributed under the GNU LGPL (for details please see \doc\license.txt) +using System; +using System.Collections.ObjectModel; +using ICSharpCode.Reporting.Exporter.Visitors; +using ICSharpCode.Reporting.PageBuilder.ExportColumns; +using NUnit.Framework; + +namespace ICSharpCode.Reporting.Test.Expressions +{ + [TestFixture] + public class IntegrationTests + { + Collection collection; + ExpressionVisitor expressionVisitor; + + [Test] + public void ExpressionMustStartWithEqualChar() + { + var result = collection[0]; + expressionVisitor.Visit(collection[0]); + Assert.That(result.Text,Is.EqualTo(collection[0].Text)); + } + + + [Test] + public void SimpleMath() { + expressionVisitor.Visit(collection[1]); + + Assert.That(collection[1].Text,Is.EqualTo("8")); + var res = Convert.ToInt32(collection[1].Text); + Assert.That(res is int); + } + + + [Test] + public void SimpleStringHandling () { + var script = "='Sharpdevelop' + ' is great'"; + collection.Add(new ExportText() + { + Text = script + }); + expressionVisitor.Visit(collection[2]); + Assert.That(collection[2].Text,Is.EqualTo("Sharpdevelop is great")); + } + + #region System.Environment + + [Test] + [Ignore] + public void CanUserSystemEnvironment() { + /* + //Using methods imported from System.Environment + var script = @"report = '#{MachineName}-#{OSVersion}-#{UserName}'"; + var result = evaluator.Evaluate(script); + var expected = string.Format("{0}-{1}-{2}", Environment.MachineName, Environment.OSVersion, Environment.UserName); + Assert.AreEqual(expected, result, "Unexpected computation result"); + */ + } + + #endregion + + + #region System.Math + + [Test] + public void CanRunSystemMath () { + //Using methods imported from System.Math class + var script = @"=abs(-1.0) + Log10(100.0) + sqrt(9) + floor(4.5) + sin(PI/2)"; + collection[1].Text = script; + expressionVisitor.Visit(collection[1]); + var res = Convert.ToDouble(collection[1].Text); + Assert.That(collection[1].Text,Is.EqualTo("11")); + } + + #endregion + + [SetUp] + public void CreateExportlist() { + collection = new Collection(); + collection.Add(new ExportText() + { + Text = "myExporttextColumn" + }); + collection.Add(new ExportText() + { + Text ="= 3 + 5" + + }); + + } + + [TestFixtureSetUp] + public void Setup() { + expressionVisitor = new ExpressionVisitor(); + } + + } +} diff --git a/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Test/ICSharpCode.Reporting.Test/src/TestReports/FromList.srd b/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Test/ICSharpCode.Reporting.Test/src/TestReports/FromList.srd index 1a5a2404c9..cefeb25109 100644 --- a/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Test/ICSharpCode.Reporting.Test/src/TestReports/FromList.srd +++ b/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Test/ICSharpCode.Reporting.Test/src/TestReports/FromList.srd @@ -120,7 +120,7 @@ False System.String No - =Globals!PageNumber + Globals!PageNumber False Black Black diff --git a/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Test/ICSharpCode.Reporting.Test/src/TestReports/ReportWithTwoItems.srd b/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Test/ICSharpCode.Reporting.Test/src/TestReports/ReportWithTwoItems.srd index 76739e303c..0bff69b2e1 100644 --- a/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Test/ICSharpCode.Reporting.Test/src/TestReports/ReportWithTwoItems.srd +++ b/src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Test/ICSharpCode.Reporting.Test/src/TestReports/ReportWithTwoItems.srd @@ -118,7 +118,7 @@ False System.String No - =Globals!PageNumber + Globals!PageNumber False Black Black