19 changed files with 533 additions and 139 deletions
@ -0,0 +1,35 @@
@@ -0,0 +1,35 @@
|
||||
// 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.Reflection; |
||||
|
||||
namespace ICSharpCode.Reporting.DataSource |
||||
{ |
||||
/// <summary>
|
||||
/// Description of FieldMemberAccessor.
|
||||
/// </summary>
|
||||
public class FieldMemberAccessor : IMemberAccessor |
||||
{ |
||||
private readonly FieldInfo _field; |
||||
|
||||
public FieldMemberAccessor(FieldInfo field) |
||||
{ |
||||
_field = field; |
||||
} |
||||
|
||||
public object GetValue(object target) |
||||
{ |
||||
return _field.GetValue(target); |
||||
} |
||||
|
||||
public bool IsStatic |
||||
{ |
||||
get { return _field.IsStatic; } |
||||
} |
||||
|
||||
public Type MemberType |
||||
{ |
||||
get { return _field.FieldType;} |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,42 @@
@@ -0,0 +1,42 @@
|
||||
// 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.Reflection; |
||||
|
||||
namespace ICSharpCode.Reporting.DataSource |
||||
{ |
||||
/// <summary>
|
||||
/// Description of PropertyMemberAccessore.
|
||||
/// </summary>
|
||||
public interface IMemberAccessor |
||||
{ |
||||
object GetValue(object target); |
||||
bool IsStatic { get; } |
||||
Type MemberType { get; } |
||||
} |
||||
|
||||
public class PropertyMemberAccessor : IMemberAccessor |
||||
{ |
||||
private readonly PropertyInfo _prop; |
||||
|
||||
public PropertyMemberAccessor(PropertyInfo prop) |
||||
{ |
||||
_prop = prop; |
||||
} |
||||
|
||||
public object GetValue(object target) |
||||
{ |
||||
return _prop.GetValue(target, null); |
||||
} |
||||
|
||||
public bool IsStatic |
||||
{ |
||||
get { return _prop.GetGetMethod().IsStatic; } |
||||
} |
||||
|
||||
public Type MemberType |
||||
{ |
||||
get { return _prop.PropertyType;} |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,88 @@
@@ -0,0 +1,88 @@
|
||||
// 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.Text; |
||||
|
||||
namespace ICSharpCode.Reporting.DataSource |
||||
{ |
||||
public class PropertyPath |
||||
{ |
||||
private readonly Type _rootType; |
||||
private readonly IMemberAccessor[] _properties; |
||||
private readonly bool _rootIsStatic; |
||||
|
||||
private PropertyPath(Type rootType,IMemberAccessor[] props) |
||||
{ |
||||
_properties = props; |
||||
_rootType = rootType; |
||||
|
||||
if (_properties[0].IsStatic) |
||||
_rootIsStatic = true; |
||||
} |
||||
|
||||
public object Evaluate(object target) |
||||
{ |
||||
if (target == null && _rootIsStatic == false) |
||||
return null; |
||||
|
||||
// Type s = target.GetType();
|
||||
|
||||
if (target != null && _rootType.IsAssignableFrom(target.GetType()) == false) |
||||
return null; |
||||
|
||||
|
||||
object current = target; |
||||
foreach (IMemberAccessor prop in _properties) |
||||
{ |
||||
current = prop.GetValue(current); |
||||
if (current == null) |
||||
return null; |
||||
} |
||||
return current; |
||||
} |
||||
|
||||
public static PropertyPath Parse(Type targetType,string propPath) |
||||
{ |
||||
if (String.IsNullOrEmpty(propPath)) |
||||
return null; |
||||
|
||||
string[] parts = propPath.Split('.'); |
||||
return Compile(targetType, parts); |
||||
|
||||
} |
||||
|
||||
public static PropertyPath Compile(Type targetType,string[] pathParts) |
||||
{ |
||||
var accessors = new IMemberAccessor[pathParts.Length]; |
||||
|
||||
Type currentType = targetType; |
||||
for (int i = 0; i < pathParts.Length; i++) |
||||
{ |
||||
string part = pathParts[i]; |
||||
|
||||
IMemberAccessor accessor = currentType.FindAccessor(part); |
||||
if (accessor == null) |
||||
return null; |
||||
|
||||
|
||||
accessors[i] = accessor; |
||||
currentType = accessor.MemberType; |
||||
} |
||||
|
||||
return new PropertyPath(targetType, accessors); |
||||
} |
||||
|
||||
public static string GetCacheKey(Type targetType,string[] name) |
||||
{ |
||||
var key = new StringBuilder(); |
||||
key.Append(targetType.FullName); |
||||
foreach (string namePart in name) |
||||
key.Append(namePart); |
||||
return key.ToString(); |
||||
} |
||||
|
||||
|
||||
|
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,53 @@
@@ -0,0 +1,53 @@
|
||||
// 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.Reflection; |
||||
|
||||
|
||||
namespace ICSharpCode.Reporting.DataSource |
||||
{ |
||||
/// <summary>
|
||||
/// Description of ReflectionExtension.
|
||||
/// </summary>
|
||||
///
|
||||
|
||||
|
||||
|
||||
public static class ReflectionExtensions |
||||
{ |
||||
public static PropertyPath ParsePropertyPath(this object target,string propertyPath) |
||||
{ |
||||
if (target == null || String.IsNullOrEmpty(propertyPath)) |
||||
return null; |
||||
|
||||
return PropertyPath.Parse(target.GetType(), propertyPath); |
||||
} |
||||
|
||||
|
||||
public static object EvaluatePropertyPath(this object target,string propertyPath) |
||||
{ |
||||
PropertyPath path = ParsePropertyPath(target, propertyPath); |
||||
if (path != null) |
||||
return path.Evaluate(target); |
||||
return null; |
||||
} |
||||
|
||||
|
||||
public static IMemberAccessor FindAccessor(this Type type, string accessorName) |
||||
{ |
||||
PropertyInfo prop = type.GetProperty(accessorName, |
||||
BindingFlags.IgnoreCase | BindingFlags.NonPublic | |
||||
BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance); |
||||
if (prop != null) |
||||
return new PropertyMemberAccessor(prop); |
||||
|
||||
FieldInfo field = type.GetField(accessorName, |
||||
BindingFlags.IgnoreCase | BindingFlags.NonPublic | |
||||
BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance); |
||||
if (field != null) |
||||
return new FieldMemberAccessor(field); |
||||
|
||||
return null; |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,58 @@
@@ -0,0 +1,58 @@
|
||||
// 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; |
||||
using System.Collections.Generic; |
||||
using System.Globalization; |
||||
using System.Linq; |
||||
using System.Text; |
||||
|
||||
using ICSharpCode.Reporting.DataManager.Listhandling; |
||||
using ICSharpCode.Reporting.DataSource; |
||||
using Irony.Interpreter; |
||||
using Irony.Interpreter.Ast; |
||||
|
||||
|
||||
namespace ICSharpCode.Reporting.Expressions.Irony.Imports |
||||
{ |
||||
/// <summary>
|
||||
/// Description of ImportAggregates.
|
||||
/// </summary>
|
||||
public static class ImportAggregates |
||||
{ |
||||
public static object Sum(ScriptThread thread, AstNode[] childNodes) { |
||||
double sum = 0; |
||||
var fieldName = childNodes[0].Evaluate(thread).ToString(); |
||||
|
||||
var dataSource = (CollectionSource)thread.App.Globals["Current"]; |
||||
|
||||
var curpos = dataSource.CurrentPosition; |
||||
|
||||
dataSource.CurrentPosition = 0; |
||||
|
||||
if (FieldExist(dataSource.Current,fieldName)) { |
||||
do { |
||||
var current = dataSource.Current; |
||||
var property = current.ParsePropertyPath(fieldName); |
||||
var val = property.Evaluate(current); |
||||
var nextVal = TypeNormalizer.EnsureType<double>(val); |
||||
sum = sum + nextVal; |
||||
} |
||||
while (dataSource.MoveNext()); |
||||
} |
||||
|
||||
dataSource.CurrentPosition = curpos; |
||||
return sum; |
||||
} |
||||
|
||||
|
||||
static bool FieldExist (object current,string fieldName) { |
||||
var property1 = current.ParsePropertyPath(fieldName); |
||||
if (property1 == null) { |
||||
Console.WriteLine(String.Format("Aggregate <Sum> Field '{0}' not found",fieldName)); |
||||
return false; |
||||
} |
||||
return true; |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,106 @@
@@ -0,0 +1,106 @@
|
||||
// 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.Expressions |
||||
{ |
||||
/// <summary>
|
||||
/// Description of TypeNormalizer.
|
||||
/// </summary>
|
||||
public class TypeNormalizer |
||||
{ |
||||
|
||||
public static void NormalizeTypes(ref object left,ref object right) |
||||
{ |
||||
NormalizeTypes(ref left,ref right,0); |
||||
} |
||||
|
||||
public static void NormalizeTypes(ref object left,ref object right,object nullValue) |
||||
{ |
||||
if (left == null) |
||||
left = 0; |
||||
if (right == null) |
||||
right = 0; |
||||
|
||||
if (left.GetType() == right.GetType()) |
||||
return; |
||||
|
||||
try |
||||
{ |
||||
right = Convert.ChangeType(right,left.GetType()); |
||||
} |
||||
catch |
||||
{ |
||||
try |
||||
{ |
||||
left = Convert.ChangeType(left, right.GetType()); |
||||
} |
||||
catch |
||||
{ |
||||
throw new Exception(String.Format("Error converting from {0} type to {1}", left.GetType().FullName, right.GetType().FullName)); |
||||
} |
||||
} |
||||
} |
||||
|
||||
public static void EnsureTypes(ref object[] values,Type targetType) |
||||
{ |
||||
object nullValue = null; |
||||
if (targetType.IsValueType) |
||||
nullValue = Activator.CreateInstance(targetType); |
||||
EnsureTypes(ref values,targetType,nullValue); |
||||
|
||||
} |
||||
|
||||
public static void EnsureTypes(ref object[] values,Type targetType,object nullValue) |
||||
{ |
||||
for (int i=0;i<values.Length;i++) |
||||
{ |
||||
values[i] = EnsureType(values[i],targetType,nullValue); |
||||
} |
||||
} |
||||
|
||||
public static T EnsureType<T>(object value) |
||||
{ |
||||
return EnsureType<T>(value, default(T)); |
||||
} |
||||
|
||||
public static T EnsureType<T>(object value,object nullValue) |
||||
{ |
||||
return (T) EnsureType(value, typeof (T), nullValue); |
||||
} |
||||
|
||||
public static object EnsureType(object value, Type targetType) |
||||
{ |
||||
if (value != null && value.GetType() == targetType) |
||||
return value; |
||||
|
||||
object defaultValue = null; |
||||
if (targetType.IsValueType) |
||||
defaultValue = Activator.CreateInstance(targetType); |
||||
return EnsureType(value, targetType, defaultValue); |
||||
} |
||||
|
||||
public static object EnsureType(object value,Type targetType,object nullValue) |
||||
{ |
||||
if (value == null) |
||||
return nullValue; |
||||
|
||||
if (targetType == typeof(object)) |
||||
return value; |
||||
|
||||
if (value.GetType() == targetType) |
||||
return value; |
||||
|
||||
try { |
||||
return Convert.ChangeType(value, targetType); |
||||
} catch (Exception e) { |
||||
|
||||
Console.WriteLine("TypeNormalizer {0} - {1}",value.ToString(),e.Message); |
||||
return value.ToString(); |
||||
//throw new Exception()String.Format("TypeNormalizer for <{0}> - {1}",value.ToString(),e.Message));
|
||||
} |
||||
} |
||||
|
||||
} |
||||
} |
||||
|
||||
@ -0,0 +1,46 @@
@@ -0,0 +1,46 @@
|
||||
// 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.Generic; |
||||
|
||||
namespace ICSharpCode.Reporting.Test.Expressions.Aggregates |
||||
{ |
||||
/// <summary>
|
||||
/// Description of AggregateFuctionHelper.
|
||||
/// </summary>
|
||||
internal class AggregateFuctionHelper |
||||
{ |
||||
|
||||
public AggregateFuctionHelper() |
||||
{ |
||||
this.AggregateCollection = new AggregateCollection(); |
||||
this.AggregateCollection.Add (new Aggregate("Value1",1,1.5)); |
||||
this.AggregateCollection.Add (new Aggregate("Value2",2,2.5)); |
||||
this.AggregateCollection.Add (new Aggregate("Value3",3,3.5)); |
||||
this.AggregateCollection.Add (new Aggregate("Value400",400,400.75)); |
||||
} |
||||
|
||||
|
||||
public AggregateCollection AggregateCollection {get; private set;} |
||||
|
||||
} |
||||
|
||||
|
||||
class AggregateCollection: List<Aggregate> |
||||
{ |
||||
} |
||||
|
||||
|
||||
class Aggregate { |
||||
public Aggregate (string name,int intValue, double amount) |
||||
{ |
||||
this.Name = name; |
||||
this.IntValue = intValue; |
||||
this.DoubleValue = amount; |
||||
} |
||||
|
||||
public string Name {get;set;} |
||||
public int IntValue {get;set;} |
||||
public double DoubleValue {get;set;} |
||||
} |
||||
} |
||||
@ -0,0 +1,72 @@
@@ -0,0 +1,72 @@
|
||||
// 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.DataManager.Listhandling; |
||||
using ICSharpCode.Reporting.Exporter.Visitors; |
||||
using ICSharpCode.Reporting.Items; |
||||
using ICSharpCode.Reporting.PageBuilder.ExportColumns; |
||||
using NUnit.Framework; |
||||
|
||||
namespace ICSharpCode.Reporting.Test.Expressions.Aggregates |
||||
{ |
||||
[TestFixture] |
||||
public class SumAggregate |
||||
{ |
||||
|
||||
Collection<ExportText> collection; |
||||
ExpressionVisitor expressionVisitor; |
||||
AggregateCollection agc; |
||||
AggregateFuctionHelper helper; |
||||
CollectionSource cs; |
||||
|
||||
[Test] |
||||
public void CanSum_Int_WholeCollection() |
||||
{ |
||||
var reportSettings = new ReportSettings(); |
||||
var visitor = new ExpressionVisitor(reportSettings); |
||||
var script = "= sum('intValue')"; |
||||
collection[0].Text = script; |
||||
visitor.Evaluator.Globals.Add("Current",cs); |
||||
visitor.Visit(collection[0]); |
||||
Assert.That (collection[0].Text,Is.EqualTo("406")); |
||||
Assert.That(Convert.ToInt32(collection[0].Text),Is.TypeOf(typeof(int))); |
||||
} |
||||
|
||||
|
||||
[Test] |
||||
public void CanSum_Double_WholeCollection() |
||||
{ |
||||
var reportSettings = new ReportSettings(); |
||||
var visitor = new ExpressionVisitor(reportSettings); |
||||
var script = "= sum('doubleValue')"; |
||||
collection[0].Text = script; |
||||
visitor.Evaluator.Globals.Add("Current",cs); |
||||
visitor.Visit(collection[0]); |
||||
Assert.That (collection[0].Text,Is.EqualTo("408,25")); |
||||
Assert.That(Convert.ToDouble(collection[0].Text),Is.TypeOf(typeof(double))); |
||||
} |
||||
|
||||
|
||||
|
||||
[SetUp] |
||||
public void CreateExportlist() { |
||||
collection = new Collection<ExportText>(); |
||||
collection.Add(new ExportText() |
||||
{ |
||||
Text = String.Empty |
||||
}); |
||||
|
||||
helper = new AggregateFuctionHelper(); |
||||
agc = helper.AggregateCollection; |
||||
cs = new CollectionSource(agc,typeof(Aggregate),new ReportSettings()); |
||||
cs.Bind(); |
||||
} |
||||
|
||||
|
||||
[TestFixtureSetUp] |
||||
public void Setup() { |
||||
expressionVisitor = new ExpressionVisitor(new ReportSettings()); |
||||
} |
||||
} |
||||
} |
||||
@ -1,118 +0,0 @@
@@ -1,118 +0,0 @@
|
||||
// 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 ICSharpCode.Reporting.BaseClasses; |
||||
using ICSharpCode.Reporting.Expressions.Irony; |
||||
using ICSharpCode.Reporting.Expressions.Irony.Ast; |
||||
using ICSharpCode.Reporting.Items; |
||||
using NUnit.Framework; |
||||
|
||||
namespace ICSharpCode.Reporting.Test.Expressions |
||||
{ |
||||
[TestFixture] |
||||
public class ParametersHandlingFixture |
||||
{ |
||||
ReportingLanguageGrammer grammar; |
||||
ReportingExpressionEvaluator evaluator; |
||||
|
||||
[Test] |
||||
public void CanEvaluateOneParameter() |
||||
{ |
||||
var resultValue = "Hi from param1"; |
||||
var parameters = new ParameterCollection(); |
||||
parameters.Add(new BasicParameter() { |
||||
ParameterName = "param1", |
||||
ParameterValue = resultValue |
||||
} |
||||
); |
||||
AddToGlobals(parameters); |
||||
var script = "Parameters!param1"; |
||||
var result = evaluator.Evaluate(script); |
||||
Assert.That (result,Is.EqualTo(resultValue)); |
||||
} |
||||
|
||||
|
||||
[Test] |
||||
public void CanFindParameter () { |
||||
var resultValue = "Hi from param2"; |
||||
var parameters = new ParameterCollection(); |
||||
parameters.Add(new BasicParameter() { |
||||
ParameterName = "param1", |
||||
ParameterValue = "Value for parameter1" |
||||
} |
||||
); |
||||
|
||||
parameters.Add(new BasicParameter() { |
||||
ParameterName = "param2", |
||||
ParameterValue = resultValue |
||||
} |
||||
); |
||||
parameters.Add(new BasicParameter() { |
||||
ParameterName = "param3", |
||||
ParameterValue = "Value for parameter2" |
||||
} |
||||
); |
||||
|
||||
|
||||
AddToGlobals(parameters); |
||||
|
||||
var script = "Parameters!param2"; |
||||
var result = evaluator.Evaluate(script); |
||||
Assert.That (result,Is.EqualTo(resultValue)); |
||||
} |
||||
|
||||
|
||||
[Test] |
||||
public void CanConcatParameter () { |
||||
var parameters = new ParameterCollection(); |
||||
parameters.Add(new BasicParameter() { |
||||
ParameterName = "param1", |
||||
ParameterValue = "SharpDevelop" |
||||
} |
||||
); |
||||
|
||||
parameters.Add(new BasicParameter() { |
||||
ParameterName = "param2", |
||||
ParameterValue = " is " |
||||
} |
||||
); |
||||
parameters.Add(new BasicParameter() { |
||||
ParameterName = "param3", |
||||
ParameterValue = "great" |
||||
} |
||||
); |
||||
AddToGlobals(parameters); |
||||
var script = "Parameters!param1 + Parameters!param2 + Parameters!param3"; |
||||
var result = evaluator.Evaluate(script); |
||||
Assert.That (result,Is.EqualTo("SharpDevelop is great")); |
||||
} |
||||
|
||||
|
||||
void AddToGlobals(ParameterCollection parameters) |
||||
{ |
||||
var reportSettings = new ReportSettings(); |
||||
reportSettings.ParameterCollection.AddRange(parameters); |
||||
evaluator.AddReportSettings(reportSettings); |
||||
} |
||||
|
||||
|
||||
[SetUp] |
||||
public void Initialize() { |
||||
grammar = new ReportingLanguageGrammer(); |
||||
evaluator = new ReportingExpressionEvaluator(grammar); |
||||
} |
||||
|
||||
|
||||
[TestFixtureSetUp] |
||||
public void Init() |
||||
{ |
||||
// TODO: Add Init code.
|
||||
} |
||||
|
||||
[TestFixtureTearDown] |
||||
public void Dispose() |
||||
{ |
||||
// TODO: Add tear down code.
|
||||
} |
||||
} |
||||
} |
||||
Loading…
Reference in new issue