コード例 #1
0
ファイル: PropertyExpression.cs プロジェクト: GlenDC/L20n.cs
			/// <summary>
			/// Evaluates to the final value, found based on the stored (and evaluated) property identifiers,
			/// and optionally the given parameters.
			/// Returns null inc ase something went wrong.
			/// </summary>
			public override L20nObject Eval(LocaleContext ctx, params L20nObject[] argv)
			{
				L20nObject maybe;
				int i = 0;

				// The root entity is either given as a parameter
				// or we need to get it using the first identifier.
				// See this.GetEntity for more information.
				if (argv == null || argv.Length == 0 || argv [0] as Dummy != null)
				{
					maybe = GetEntity(ctx, m_Identifiers [i]);
					i += 1;
				} else
				{
					maybe = argv [0];
				}

				// return null in case we have no root entity at all
				if (maybe == null)
				{
					Logger.Warning("<PropertyExpression>: couldn't evaluate first expression");
					return maybe;
				}

				L20nObject obj = maybe;

				// in case this property is pointing to a root entity,
				// directly requested by the L20n-user, we need to
				// evaluate it using the current identifier, such that
				// we can return null in case the Entity is private
				// and should thus not be requested.
				if (argv.Length == 1 && i < m_Identifiers.Length && (argv [0] as Dummy) != null)
				{
					obj = obj.Eval(ctx, argv [0], m_Identifiers [i]);
					++i;
				}

				// go through each identifier and replace the current Entity,
				// such that we can go deeper until we went through the
				// entire identifier list.
				for (; i < m_Identifiers.Length; ++i)
				{
					if (obj == null)
					{
						Logger.WarningFormat(
							"<PropertyExpression>: couldn't evaluate expression #{0}", i);
						return obj;
					}

					// check if we are dealing with a string
					// in which case we want to make it into an identifierExpression
					var stringObject = obj as StringOutput;
					if (stringObject != null)
					{
						// we do want to fall back to it being just a regular string,
						// in case that was the actual intention of the user
						L20nObject alt = new IdentifierExpression(stringObject.Value);
						alt = alt.Eval(ctx, m_Identifiers [i]);
						if (alt != null)
						{
							obj = alt;
							continue;
						}
					}

					obj = obj.Eval(ctx, m_Identifiers [i]);
				}

				if (obj == null)
				{
					Logger.Warning(
						"<PropertyExpression>: couldn't evaluate the final expression");
					return obj;
				}

				// if all is fine, we can now return the evaluation
				// of the last found value.
				return obj.Eval(ctx);
			}