/// <summary> /// Tries to get the <see cref="L20nCore.Objects.Entity"/> instance based on the stored reference (name) /// and will return the evaluation result of the looked up object if possible. /// Returns <c>null</c> in case the object could not be found or the evaluation /// of the looked up object returned <c>null</c> itself. /// </summary> public override L20nObject Eval(LocaleContext ctx, params L20nObject[] argv) { var entity = ctx.GetEntity(m_Identifier); if (entity == null) { return entity; } return entity.Eval(ctx, argv); }
/// <summary> /// Evaluate all the expressions contained in this <see cref="L20nCore.Objects.StringValue"/> /// and use the results of these evaluations as the variables used in the evaluation /// of the format string to compute the final result value as a <see cref="L20nCore.Objects.StringOutput"/>. /// </summary> public override L20nObject Eval(LocaleContext ctx, params L20nObject[] argv) { // Evaluate each expression and store them in the cached list for (int i = 0; i < m_EvaluatedExpressions.Length; ++i) { var e = m_Expressions [i].Eval(ctx); // if one cannot evaluate, we can't know the real final result // and thus we return early if (e == null) { return null; } // Identifiers get evaluated until we get a non-reference value Identifier id; Entity entity; while ((id = e as Identifier) != null) { entity = ctx.GetEntity(id.Value); if (entity != null) e = entity.Eval(ctx); } var primitive = e as Primitive; // in the end we expect to be left with a primitive value, // if not there is something wrong in the global evaluation logic. if (primitive == null) { Logger.WarningFormat("<StringValue>: couldn't evaluate expression #{0}", i); return null; } // we expect a primitive value so that we can turn it into a string var stringOutput = primitive.ToString(ctx); if (stringOutput == null) { Logger.WarningFormat("<StringValue>: couldn't evaluate expression #{0} to <StringOutput>", i); return null; } m_EvaluatedExpressions [i] = stringOutput; } // we update the cached StringOutput value and return it as // the result of this evaluation. m_Output.Value = FormatString(m_Value, m_EvaluatedExpressions); return m_Output; }
/// <summary> /// A help function to get the entity based on the first variable. /// The given identifier is either an Identifier, Variable or Global, /// which will define the action to be taken in order to get and return the Root Entity. /// </summary> private Entity GetEntity(LocaleContext ctx) { // is it an identifier? var identifier = m_Root as Identifier; if (identifier != null) return ctx.GetEntity(identifier.Value); // is it a string? var str = m_Root.Eval(ctx) as StringOutput; if (str != null) return ctx.GetEntity(str.Value); // is it a variable? var variable = m_Root as Variable; if (variable != null) return ctx.GetVariable(variable.Identifier) as Entity; // is it a global? var global = m_Root as Global; if (global != null) return ctx.GetGlobal(global.Identifier) as Entity; return null; }
/// <summary> /// A help function to get the root entity based on the first variable. /// The given identifier is either an Identifier, Variable or Global, /// which will define the action to be taken in order to get and return the Root Entity. /// </summary> private Entity GetEntity(LocaleContext ctx, L20nObject key) { // is it an identifier? var identifier = key as Identifier; if (identifier != null) return ctx.GetEntity(identifier.Value); // is it a variable? var variable = key as Variable; if (variable != null) { var obj = ctx.GetVariable(variable.Identifier); // a variable can also be a string-reference to an entity var reference = obj as StringOutput; if (reference != null) { return ctx.GetEntity(reference.Value); } // otherwise it's simply an entity (or at least it should be) return obj as Entity; } // is it a global? var global = key as Global; if (global != null) return ctx.GetGlobal(global.Identifier) as Entity; // is it a string? var str = key as StringOutput; if (str != null) return ctx.GetEntity(str.Value); return null; }