Ejemplo n.º 1
0
        /****************************************************************************/
        internal static CompiledTransform Compile(string source, IDictionary <string, string> includeSource = null)
        {
            var transform = new CompiledTransform(includeSource);

            transform.Compile(transform, JObject.Parse(source));

            return(transform);
        }
Ejemplo n.º 2
0
        /****************************************************************************/
        internal TThrow(string name, JValue val)
        {
            var parms = CompiledTransform.ParseElementParams("throw", name, new List <bool> {
                false, true
            });

            _code = parms.Count > 0 ? parms[0] : null;

            _message = CreateValue(val);
        }
Ejemplo n.º 3
0
        /****************************************************************************/
        internal TBind(string name, JObject val)
        {
            var parms = CompiledTransform.ParseElementParams("bind", name, new List <bool> {
                false
            });

            _expression = parms[0];

            // Compile children
            Compile(null, val);
        }
Ejemplo n.º 4
0
        /****************************************************************************/
        internal TCatch(string name, JObject val)
        {
            var parms = CompiledTransform.ParseElementParams("catch", name, new List <bool> {
                false, true
            });

            _expression = parms.Count > 0 ? parms[0] : null;

            // Compile children
            Compile(null, val);
        }
Ejemplo n.º 5
0
        /****************************************************************************/
        internal protected void Compile(CompiledTransform transform, JObject source)
        {
            foreach (var child in source)
            {
                var newToken = CreateToken(transform, child.Key, child.Value);

                if (newToken != null)
                {
                    this.Children.Add(newToken);
                }

                _previous = newToken;
            }
        }
Ejemplo n.º 6
0
        /****************************************************************************/
        internal TCopyOf(string name, string val)
        {
            _name = CreateValue(name);

            var parms = CompiledTransform.ParseElementParams("copyof", val, new List <bool> {
                false
            });

            if (parms.Count == 0)
            {
                throw new Transformer.SyntaxException("Missing expression for #copyof");
            }

            _expression = parms[0];
        }
Ejemplo n.º 7
0
        /****************************************************************************/
        internal TFunction(string name, JObject val)
        {
            var parms = CompiledTransform.ParseElementParams("function", name, new List <bool> {
                true
            });
            var context = new ExpressionContext(new {});

            this.Name = parms[0].Evaluate(context).ToString().ToLower().Trim();

            this.Parameters.AddRange(parms.Select(s => s.Evaluate(context).ToString().Trim()));
            this.Parameters.RemoveAt(0);

            // Compile children
            Compile(null, val);
        }
Ejemplo n.º 8
0
        /****************************************************************************/
        internal TForEach(string name, JObject val)
        {
            var parms = CompiledTransform.ParseElementParams("foreach", name, new List <bool> {
                false, true
            });

            if (parms.Count < 1)
            {
                throw new Transformer.SyntaxException("Missing expression for #foreach");
            }

            _expression = parms[0];
            _name       = parms.Count > 1 ? parms[1] : null;

            // Compile children
            Compile(null, val);
        }
Ejemplo n.º 9
0
        /****************************************************************************/
        private TToken CreateToken(CompiledTransform transform, string name, JToken child)
        {
            var t = child.GetType().ToString();

            if (child is JValue val)
            {
                if (name.StartsWith("#include"))
                {
                    transform.LoadInclude(val.Value.ToString());
                    return(null);
                }

                if (name.StartsWith("#break"))
                {
                    return(new TBreak());
                }

                if (name.StartsWith("#variable"))
                {
                    return(new TVariable(name, val));
                }

                if (name.StartsWith("#message"))
                {
                    return(new TMessage(val));
                }

                if (name.StartsWith("#throw"))
                {
                    return(new TThrow(name, val));
                }

                var sval = val.ToString();

                if (name.StartsWith("#arrayitem"))
                {
                    return(new TSimpleArrayItem(val));
                }

                if (sval.StartsWith("#copyof"))
                {
                    return(new TCopyOf(name, sval));
                }

                return(new TProperty(name, val));
            }

            if (child is JObject obj)
            {
                if (name.StartsWith("#template"))
                {
                    var template = new TTemplate(name, obj);

                    this.Templates.Add(template.Name, template);
                    return(null);
                }

                if (name.StartsWith("#function"))
                {
                    var function = new TFunction(name, obj);

                    this.Functions.Add(function.Name, function);
                    return(null);
                }

                if (name.StartsWith("#variable"))
                {
                    return(new TVariableObject(name, obj));
                }

                if (name.StartsWith("#calltemplate"))
                {
                    return(new TCallTemplate(name, obj));
                }

                if (name.StartsWith("#bind"))
                {
                    return(new TBind(name, obj));
                }

                if (name.StartsWith("#foreachgroup"))
                {
                    return(new TForEachGroup(name, obj));
                }

                if (name.StartsWith("#foreach"))
                {
                    return(new TForEach(name, obj));
                }

                if (name.StartsWith("#arrayitem"))
                {
                    return(new TObject(name, obj));
                }

                if (name.StartsWith("#array"))
                {
                    return(new TArray(name, obj));
                }

                if (name.StartsWith("#if"))
                {
                    return(new TIf(name, obj));
                }

                if (name.StartsWith("#try"))
                {
                    return(new TTry(obj));
                }

                if (name.StartsWith("#catch"))
                {
                    if (_previous is TTry || _previous is TCatch)
                    {
                        return(new TCatch(name, obj));
                    }

                    throw new Transformer.SyntaxException("#catch must follow a #try or another #catch");
                }

                if (name.StartsWith("#if"))
                {
                    return(new TIf(name, obj));
                }

                if (name.StartsWith("#elseif"))
                {
                    if (_previous is TIf || _previous is TElseIf)
                    {
                        return(new TElseIf(name, obj));
                    }

                    throw new Transformer.SyntaxException("#elseif must follow an #if or another #elseif");
                }

                if (name.StartsWith("#else"))
                {
                    if (_previous is TIf || _previous is TElseIf)
                    {
                        return(new TElse(name, obj));
                    }

                    throw new Transformer.SyntaxException("#elseif must follow an #if or an #elseif");
                }

                return(new TObject(name, obj));
            }

            if (child is JArray array)
            {
                return(new TExplicitArray(name, array));
            }

            return(null);
        }
Ejemplo n.º 10
0
 /****************************************************************************/
 /// <summary>
 /// Construct a new Transformer
 /// </summary>
 /// <param name="transform">The JSON that defines the transform</param>
 public Transformer(string transform, IEnumerable extensionFunctions = null, IDictionary <string, string> includeSource = null)
 {
     _transform          = CompiledTransform.Compile(transform, includeSource);
     _extensionFunctions = CompileFunctions(extensionFunctions);
 }