Наследование: Parrot.Nodes.AbstractNode
Пример #1
0
        public override void Render(IParrotWriter writer, IRendererFactory rendererFactory, Statement statement, IDictionary<string, object> documentHost, object model)
        {
            //get the parameter
            List<object> parameters = new List<object>()
                {
                    writer, 
                    documentHost
                };

            if (statement.Parameters != null && statement.Parameters.Any())
            {
                //assume only the first is the path
                //second is the argument (model)
                //TODO: fix this
                parameters.AddRange((List<object>)GetLocalModel(documentHost, statement, model));
            }
            else
            {
                //layout = "_layout";
                throw new InvalidOperationException("Missing parameters");
            }

            var call = _renderAction.SingleOrDefault(r => r.GetParameters().Count() == parameters.Count);

            if (call != null)
            {
                call.Invoke(this, parameters.ToArray());
            }
            else
            {
                //throw an exception
            }
        }
Пример #2
0
        public void Render(IParrotWriter writer, IRendererFactory rendererFactory, Statement statement, IDictionary<string, object> documentHost, object model)
        {
            var modelValueProviderFactory = _host.ModelValueProviderFactory;

            string result = "";
            if (statement is StringLiteral)
            {
                foreach (var value in (statement as StringLiteral).Values)
                {
                    result += GetModelValue(
                        modelValueProviderFactory,
                        documentHost,
                        model,
                        value.Type,
                        value.Data
                        );
                }
            }
            else
            {
                result = GetModelValue(modelValueProviderFactory, documentHost, model, StringLiteralPartType.Encoded, statement.Name);
            }

            writer.Write(result);
        }
Пример #3
0
        public override void Render(IParrotWriter writer, IRendererFactory rendererFactory, Statement statement, IDictionary<string, object> documentHost, object model)
        {
            var localModel = GetLocalModel(documentHost, statement, model);

            //Assert that we're looping over something
            IEnumerable loop = localModel as IEnumerable;
            if (loop == null)
            {
                throw new InvalidCastException("model is not IEnumerable");
            }

            //create a local copy
            IList<object> items = ToList(loop);

            //create locals object to handle local values to the method
            Locals locals = new Locals(documentHost);

            for (int i = 0; i < items.Count; i++)
            {
                var item = items[i];

                foreach (var child in statement.Children)
                {
                    var renderer = rendererFactory.GetRenderer(child.Name);
                    locals.Push(IteratorItem(i, items));

                    renderer.Render(writer, rendererFactory, child, documentHost, item);

                    locals.Pop();
                }
            }
        }
Пример #4
0
        protected object GetLocalModel(IDictionary<string, object> documentHost, Statement statement, object model)
        {
            Type modelType = model != null ? model.GetType() : null;
            var modelValueProvider = Host.ModelValueProviderFactory.Get(modelType);

            return GetLocalModelValue(documentHost, statement, modelValueProvider, model);
        }
Пример #5
0
        public void Render(IParrotWriter writer, IRendererFactory rendererFactory, Statement statement, IDictionary<string, object> documentHost, object model)
        {
            var value = "html"; //default value of "html"

            var parameter = statement.Parameters.FirstOrDefault();
            if (parameter != null)
            {
                Type modelType = model != null ? model.GetType() : null;
                var modelValueProvider = Host.ModelValueProviderFactory.Get(modelType);

                object result;
                if (modelValueProvider.GetValue(documentHost, model, statement.Parameters[0].Value, out result))
                {
                    value = result.ToString();
                }
            }

            if (_docTypes.ContainsKey(value))
            {
                value = _docTypes[value]();
            }

            documentHost.Add("doctype", value);

            writer.Write(string.Format("<!DOCTYPE {0}>", value));
        }
Пример #6
0
        public override void Render(IParrotWriter writer, IRendererFactory rendererFactory, Statement statement, IDictionary<string, object> documentHost, object model)
        {
            //get the parameter
            var partial = GetLocalModel(documentHost, statement, model) as string;
            if (string.IsNullOrEmpty(partial))
            {
                throw new ArgumentNullException("partial");
            }
            //ok...we need to load the layoutpage
            //then pass the node's children into the layout page
            //then return the result
            var engine = (Host as AspNetHost).ViewEngine;
            var result = engine.FindView(null, partial, null, false);
            if (result != null)
            {
                var parrotView = (result.View as ParrotView);
                using (var stream = parrotView.LoadStream())
                {
                    var document = parrotView.LoadDocument(stream);

                    DocumentView view = new DocumentView(Host, rendererFactory, documentHost, document);

                    view.Render(writer);
                }
            }
        }
Пример #7
0
        protected override void CreateTag(IParrotWriter writer, IRendererFactory rendererFactory, IDictionary<string, object> documentHost, object model, Statement statement)
        {
            string tagName = string.IsNullOrWhiteSpace(statement.Name) ? DefaultChildTag : statement.Name;

            TagBuilder builder = new TagBuilder(tagName);
            //add attributes
            RenderAttributes(rendererFactory, documentHost, model, statement, builder);

            writer.Write(builder.ToString(TagRenderMode.SelfClosing));
        }
Пример #8
0
        private string GetType(Statement statement, IRendererFactory rendererFactory, IDictionary<string, object> documentHost, object model)
        {
            for (int i = 0; i < statement.Attributes.Count; i++)
            {
                if (statement.Attributes[i].Key.Equals("type", StringComparison.OrdinalIgnoreCase))
                {
                    return RenderAttribute(statement.Attributes[i], rendererFactory, documentHost, model);
                }
            }

            return "hidden";
        }
Пример #9
0
        public override void Render(IParrotWriter writer, IRendererFactory rendererFactory, Statement statement, IDictionary<string, object> documentHost, object model)
        {
            var childrenQueue = documentHost.GetValueOrDefault(LayoutRenderer.LayoutChildren) as Queue<StatementList>;
            if (childrenQueue == null)
            {
                //TODO: replace this with a real exception
                throw new Exception("Children elements empty");
            }

            var children = childrenQueue.Dequeue();

            RenderChildren(writer, children, rendererFactory, documentHost, DefaultChildTag, model);
        }
Пример #10
0
        public override void Render(IParrotWriter writer, IRendererFactory rendererFactory, Statement statement, IDictionary<string, object> documentHost, object model)
        {
            //get the parameter
            List<string> parameters = new List<string>();
            if (statement.Parameters != null && statement.Parameters.Any())
            {
                //assume only the first is the path
                //second is the argument (model)
                //TODO: fix this

                //statement.Parameters[0].Value;

                parameters.AddRange(statement.Parameters.Select(p => p.CalculatedValue));
                documentHost.Add(parameters[0], parameters[1]);
            }
        }
Пример #11
0
        public void Render(StringWriter writer, Statement statement, object documentHost)
        {
            var outputNode = statement as EncodedOutput;
            if (outputNode == null)
            {
                throw new ArgumentNullException("statement");

            }

            Type documentHostType = documentHost != null ? documentHost.GetType() : null;
            var modelValueProvider = ModelValueProviderFactory.Get(documentHostType);

            var value = modelValueProvider.GetValue(documentHost, ValueType.Property, statement);

            //html encode this!
            writer.Write(System.Net.WebUtility.HtmlEncode(value.ToString()));
        }
Пример #12
0
        public override void Render(IParrotWriter writer, IRendererFactory rendererFactory, Statement statement, IDictionary<string, object> documentHost, object model)
        {
            string layout = "";
            if (statement.Parameters != null && statement.Parameters.Any())
            {
                Type modelType = model != null ? model.GetType() : null;
                var modelValueProvider = Host.ModelValueProviderFactory.Get(modelType);

                //var parameterLayout = GetLocalModelValue(documentHost, statement, modelValueProvider, model) ?? "_layout";
                var parameterLayout = GetLocalModel(documentHost, statement, model) ?? "_layout";

                //assume only the first is the path
                //second is the argument (model)
                layout = parameterLayout.ToString();
            }


            //ok...we need to load the view
            //then pass the model to it and
            //then return the result

            var result = _parrotViewLocator.LocateView(layout);

            if (result != null)
            {
                var parrotView = new ParrotView(_host, rendererFactory, _parrotViewLocator, result.Contents, writer);
                string contents = result.Contents().ReadToEnd();

                var document = parrotView.LoadDocument(contents);

                //Create a new DocumentView and DocumentHost
                if (!documentHost.ContainsKey("_LayoutChildren_"))
                {
                    documentHost.Add("_LayoutChildren_", new Queue<StatementList>());
                }
                (documentHost["_LayoutChildren_"] as Queue<StatementList>).Enqueue(statement.Children);

                DocumentView view = new DocumentView(_host, rendererFactory, documentHost, document);

                view.Render(writer);
            }
            else
            {
                throw new Exception(string.Format("Layout {0} could not be found", layout));
            }
        }
Пример #13
0
        public override void Render(IParrotWriter writer, IRendererFactory rendererFactory, Statement statement, IDictionary<string, object> documentHost, object model)
        {
            var localModel = GetLocalModel(documentHost, statement, model);

            string statementToOutput = "default";

            if (localModel != null)
            {
                statementToOutput = localModel.ToString();
            }

            //check that there is a parameter

            foreach (var child in statement.Children)
            {
                string value;
                var valueWriter = Host.CreateWriter();

                //get string value
                var renderer = rendererFactory.GetRenderer(child.Name);
                if (renderer is StringLiteralRenderer)
                {
                    renderer.Render(valueWriter, rendererFactory, child, documentHost, model);
                    value = valueWriter.Result();
                }
                else
                {
                    value = child.Name;
                }

                if (value.Equals(statementToOutput, StringComparison.OrdinalIgnoreCase))
                {
                    //render only the child
                    RenderChildren(writer, child, rendererFactory, documentHost, model);
                    return;
                }
            }

            var defaultChild = statement.Children.SingleOrDefault(s => s.Name.Equals("default", StringComparison.OrdinalIgnoreCase));
            if (defaultChild != null)
            {
                RenderChildren(writer, defaultChild, rendererFactory, documentHost, model);
            }
        }
Пример #14
0
        public virtual string RenderChildren(Statement statement, object localModel, string defaultTag = null)
        {
            var factory = Host.DependencyResolver.Resolve<IRendererFactory>();

            if (string.IsNullOrEmpty(defaultTag))
            {
                defaultTag = DefaultChildTag;
            }

            Func<string, string> tagName = s => string.IsNullOrEmpty(s) ? defaultTag : s;

            var sb = new StringBuilder();
            if (localModel is IEnumerable && statement.Parameters != null && statement.Parameters.Any())
            {
                foreach (object item in localModel as IEnumerable)
                {
                    var localItem = item;

                    foreach (var child in statement.Children)
                    {
                        child.Name = tagName(child.Name);
                        var renderer = factory.GetRenderer(child.Name);

                        sb.AppendLine(renderer.Render(child, localItem));
                    }
                }
            }
            else
            {
                foreach (var child in statement.Children)
                {
                    if (child != null)
                    {
                        child.Name = tagName(child.Name);
                        var renderer = factory.GetRenderer(child.Name);

                        sb.Append(renderer.Render(child, localModel));
                    }
                }
            }

            return sb.ToString();
        }
Пример #15
0
        protected virtual void CreateTag(IParrotWriter writer, IRendererFactory rendererFactory, IDictionary<string, object> documentHost, object model, Statement statement)
        {
            string tagName = string.IsNullOrWhiteSpace(statement.Name) ? DefaultChildTag : statement.Name;

            TagBuilder builder = new TagBuilder(tagName);
            //add attributes
            RenderAttributes(rendererFactory, documentHost, model, statement, builder);
            //AppendAttributes(builder, statement.Attributes, documentHost, modelValueProvider);

            writer.Write(builder.ToString(TagRenderMode.StartTag));
            //render children

            if (statement.Children.Count > 0)
            {
                RenderChildren(writer, statement, rendererFactory, documentHost, model);
            }

            writer.Write(builder.ToString(TagRenderMode.EndTag));
        }
Пример #16
0
        public override void Render(IParrotWriter writer, IRendererFactory rendererFactory, Statement statement, IDictionary<string, object> documentHost, object model)
        {
            string layout = "";
            if (statement.Parameters != null && statement.Parameters.Any())
            {
                Type modelType = model != null ? model.GetType() : null;
                var modelValueProvider = Host.ModelValueProviderFactory.Get(modelType);

                var parameterLayout = GetLocalModel(documentHost, statement, model) ?? "_layout";

                //assume only the first is the path
                //second is the argument (model)
                layout = parameterLayout.ToString();
            }


            //ok...we need to load the view
            //then pass the model to it and
            //then return the result
            var engine = (Host as AspNetHost).ViewEngine;
            var result = engine.FindView(null, layout, null, false);
            if (result != null)
            {
                var parrotView = (result.View as ParrotView);
                using (var stream = parrotView.LoadStream())
                {
                    var document = parrotView.LoadDocument(stream);

                    //Create a new DocumentView and DocumentHost
                    
                    if (!documentHost.ContainsKey(LayoutChildren))
                    {
                        documentHost.Add(LayoutChildren, new Queue<StatementList>());
                    }
                    (documentHost[LayoutChildren] as Queue<StatementList>).Enqueue(statement.Children);

                    DocumentView view = new DocumentView(Host, rendererFactory, documentHost, document);

                    view.Render(writer);
                }
            }
        }
Пример #17
0
        public void BeforeRender(IParrotWriter writer, IRendererFactory rendererFactory, Statement statement, IDictionary<string, object> documentHost, object model)
        {
            //process parameters
            if (statement.Parameters != null && statement.Parameters.Any())
            {
                foreach (var parameter in statement.Parameters)
                {
                    if (parameter.Value != null && ((parameter.Value.StartsWith("\"") && parameter.Value.EndsWith("\"")) || (parameter.Value.StartsWith("'") && parameter.Value.EndsWith("'"))))
                    {
                        var stringLiteral = new StringLiteral(parameter.Value);
                        var renderer = rendererFactory.GetRenderer("string");
                        var w = new StandardWriter();
                        {
                            renderer.Render(w, rendererFactory, stringLiteral, documentHost, model);
                        }

                        parameter.CalculatedValue = w.Result();
                    }
                }
            }
        }
Пример #18
0
        public virtual void RenderChildren(IParrotWriter writer, Statement statement, IRendererFactory rendererFactory, IDictionary<string, object> documentHost, object model, string defaultTag = null)
        {
            if (string.IsNullOrEmpty(defaultTag))
            {
                defaultTag = DefaultChildTag;
            }

            if (model is IEnumerable && statement.Parameters.Any())
            {
                foreach (object item in model as IEnumerable)
                {
                    var localItem = item;

                    RenderChildren(writer, statement.Children, rendererFactory, documentHost, defaultTag, localItem);
                }
            }
            else
            {
                RenderChildren(writer, statement.Children, rendererFactory, documentHost, defaultTag, model);
            }
        }
Пример #19
0
        public override void Render(IParrotWriter writer, IRendererFactory rendererFactory, Statement statement, IDictionary<string, object> documentHost, object model)
        {
            //get the input type
            string type = GetType(statement, rendererFactory, documentHost, model);
            switch (type)
            {
                case "checkbox":
                case "radio":
                    //special handling for "checked"
                    //is there a checked attribute
                    for (int i = 0; i < statement.Attributes.Count; i++)
                    {
                        if (statement.Attributes[i].Key == "checked")
                        {
                            string attributeValue = RenderAttribute(statement.Attributes[i], rendererFactory, documentHost, model);

                            switch (attributeValue)
                            {
                                case "true":
                                    statement.Attributes[i] = new Nodes.Attribute(statement.Attributes[i].Key, new StringLiteral("\"checked\""));
                                    //.Value = "checked";
                                    break;
                                case "false":
                                case "null":
                                    //remove this attribute
                                    statement.Attributes.RemoveAt(i);
                                    i -= 1;
                                    break;
                            }
                        }
                    }

                    break;
            }

            base.Render(writer, rendererFactory, statement, documentHost, model);
        }
Пример #20
0
        private object GetLocalModelValue(IDictionary<string, object> documentHost, Statement statement, IModelValueProvider modelValueProvider, object model)
        {
            object value;
            if (statement.Parameters.Count > 0)
            {
                if (statement.Parameters.Count == 1)
                {
                    //check here first
                    if (modelValueProvider.GetValue(documentHost, model, statement.Parameters[0].Value, out value))
                    {
                        return value;
                    }
                }

                List<object> parameters = new List<object>();
                foreach (var parameter in statement.Parameters)
                {
                    if (modelValueProvider.GetValue(documentHost, model, parameter.Value, out value))
                    {
                        parameters.Add(value);
                    }
                }

                return parameters;
            }

            if (model != null)
            {
                //check DocumentHost next
                //if (modelValueProvider.GetValue(documentHost, model, null, out value))
                if (modelValueProvider.GetValue(documentHost, model, Parrot.Infrastructure.ValueType.Property, null, out value))
                {
                    return value;
                }
            }
            return model;
        }
Пример #21
0
 public override void Render(IParrotWriter writer, IRendererFactory rendererFactory, Statement statement, IDictionary<string, object> documentHost, object model)
 {
     var localModel = GetLocalModel(documentHost, statement, model);
     CreateTag(writer, rendererFactory, documentHost, localModel, statement);
 }
Пример #22
0
        protected virtual void RenderAttributes(IRendererFactory rendererFactory, IDictionary<string, object> documentHost, object model, Statement statement, TagBuilder builder)
        {
            foreach (var attribute in statement.Attributes)
            {
                if (attribute.Value == null)
                {
                    builder.MergeAttribute(attribute.Key, attribute.Key, true);
                }
                else
                {
                    object attributeValue = RenderAttribute(attribute, rendererFactory, documentHost, model);

                    if (attribute.Key == "class")
                    {
                        builder.AddCssClass((string) attributeValue);
                    }
                    else
                    {
                        builder.MergeAttribute(attribute.Key, (string) attributeValue, true);
                    }
                }
            }
        }
Пример #23
0
 public void AfterRender(IParrotWriter writer, IRendererFactory rendererFactory, Statement statement, IDictionary<string, object> documentHost, object model)
 {
 }
Пример #24
0
        protected TagBuilder CreateTag(object model, Statement statement)
        {
            Type modelType = model != null ? model.GetType() : null;

            object localModel = model;
            var modelValueProviderFactory = Host.DependencyResolver.Resolve<IModelValueProviderFactory>();

            if (statement.Parameters.Count > 0)
            {
                var modelValueProvider = modelValueProviderFactory.Get(modelType);

                localModel = modelValueProvider.GetValue(model, statement.Parameters[0].ValueType, statement.Parameters[0].Value);
            }

            statement.Name = string.IsNullOrEmpty(statement.Name) ? DefaultChildTag : statement.Name;

            TagBuilder builder = new TagBuilder(statement.Name);
            foreach (var attribute in statement.Attributes)
            {
                object attributeValue = model;
                if (attributeValue != null)
                {
                    attributeValue = modelValueProviderFactory.Get(modelType).GetValue(model, attribute.ValueType, attribute.Value);
                }
                else
                {
                    attributeValue = modelValueProviderFactory.Get(typeof(object)).GetValue(model, attribute.ValueType, attribute.Value);
                }

                if (attribute.Key == "class")
                {
                    builder.AddCssClass((string) attributeValue);
                }
                else
                {
                    builder.MergeAttribute(attribute.Key, (string) attributeValue, true);
                }
            }

            if (statement.Children.Count > 0)
            {
                builder.InnerHtml = RenderChildren(statement, localModel);
            }
            else
            {
                if (statement.Parameters.Count > 0)
                {
                    builder.InnerHtml = localModel != null ? localModel.ToString() : "";
                }
            }
            return builder;
        }
Пример #25
0
 public Document(Document document, Statement statement)
 {
     Children = document.Children;
     Children.Add(statement);
 }
Пример #26
0
 public Document(IHost host, Document document, Statement statement)
     : this(host)
 {
     Children = document.Children;
     Children.Add(statement);
 }
Пример #27
0
 private void ParseStatementErrors(Statement s)
 {
     if (s.Errors.Any())
     {
         foreach (var error in s.Errors)
         {
             error.Index += s.Index;
             Errors.Add(error);
         }
     }
 }