PageRenderTime 287ms CodeModel.GetById 283ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Otis/CodeGen/DefaultAggregateFunctionCodeGenerator.cs

http://otis-lib.googlecode.com/
C# | 81 lines | 65 code | 16 blank | 0 comment | 7 complexity | f5ce46866986b1a6dd2c53ad432071e3 MD5 | raw file
 1using System;
 2using System.CodeDom;
 3using System.Collections.Generic;
 4using System.Text;
 5using Otis.Parsing;
 6
 7namespace Otis.CodeGen
 8{
 9	public class DefaultAggregateFunctionCodeGenerator : IAggregateFunctionCodeGenerator 
10	{
11		virtual public IEnumerable<CodeStatement> GetInitializationStatements(AggregateFunctionContext context)
12		{
13			CodeExpression[] parameters = new CodeExpression[0];
14			CodeStatement st = new CodeVariableDeclarationStatement(context.ImplementationType, context.FunctionObjectName,
15							new CodeObjectCreateExpression(context.ImplementationType, parameters));
16
17			return new CodeStatement[] {st};
18		}
19
20		virtual public IEnumerable<string> GetIterationStatements(AggregateFunctionContext context, IList<AggregateExpressionPathItem> pathItems)
21		{
22			AggregateExpressionPathItem lastPathItem = pathItems[pathItems.Count - 1];
23			string finalTarget = "";
24			foreach (AggregateExpressionPathItem pathItem in pathItems)
25				if (pathItem.IsCollection)
26					finalTarget = pathItem.Object;
27
28			string finalExpression = context.Member.AggregateMappingDescription.FinalExpression;
29
30			string processedExpression = "";
31			if ((string.IsNullOrEmpty(finalExpression) || lastPathItem.IsCollection))
32				processedExpression = finalTarget;
33			else
34				processedExpression = string.Format("{0}.{1}", finalTarget, finalExpression);
35
36			string fullFormat = GetFormatForExecutedExpression(context);
37			fullFormat = fullFormat.Replace("FN_OBJ", "{0}");
38			fullFormat = fullFormat.Replace("CURR_ITEM", "{1}");
39
40			return new string[] { string.Format(fullFormat, context.FunctionObjectName, processedExpression) };
41		}
42
43		protected virtual string GetFormatForExecutedExpression(AggregateFunctionContext context) 
44		{
45			string processedExpressionFormat = GetProcessedExpressionFormat(context);
46			return "{0}.ProcessValue(" + processedExpressionFormat + ")";
47		}
48
49		virtual public CodeStatement GetAssignmentStatement(AggregateFunctionContext context)
50		{
51			string resultExpression = context.FunctionObjectName + ".Result";
52			if (context.Member.HasFormatting)
53				resultExpression = string.Format("string.Format(\"{0}\", {1}.Result)", context.Member.Format, context.FunctionObjectName);
54
55			return new CodeAssignStatement(
56					new CodeVariableReferenceExpression("target." + context.Member.Member),
57					new CodeArgumentReferenceExpression(resultExpression));	
58		}
59
60
61		private string GetProcessedExpressionFormat(AggregateFunctionContext context)
62		{
63			IExpressionFormatProvider fmtProvider = context.Generator as IExpressionFormatProvider;
64
65			if (fmtProvider == null)
66			{
67				Type realImplementationType = context.ImplementationType.IsGenericTypeDefinition ?
68				                              context.ImplementationType.MakeGenericType(typeof (int)) :
69				                              context.ImplementationType;
70
71				fmtProvider = (IExpressionFormatProvider) Activator.CreateInstance(realImplementationType, true);
72			}
73
74			if (string.IsNullOrEmpty(fmtProvider.ExpressionFormat))
75				return "{1}";
76			else
77				return fmtProvider.ExpressionFormat;
78		}
79
80	}
81}