示例#1
0
文件: Program.cs 项目: stegru/pov.net
        public static string RenderElement(SceneElement element)
        {
            SceneWriter     output       = new SceneWriter();
            OutputAttribute outputAttr   = OutputAttribute.Combine(element.GetAllAttributes <OutputAttribute>());
            ElementContext  sceneContext = new ElementContext(new ElementContext(null, new Scene()), element, outputAttr);

            sceneContext.LoadChildren(true);
            sceneContext.Write(output);
            return(output.ToString());
        }
示例#2
0
        public void CombineAttributes()
        {
            OutputAttribute attr1 = new OutputAttribute()
            {
                ContentStart = "one"
            };
            OutputAttribute attr2 = new OutputAttribute()
            {
                ContentStart = "two",
                ContentEnd   = "two"
            };

            OutputAttribute output = OutputAttribute.Combine(new[] { attr1, attr2 });

            Assert.True(true);
        }
示例#3
0
        /// <summary>Gets objects and their output attributes from this element</summary>
        /// <param name="parent">The element context of the parent element.</param>
        /// <returns></returns>
        protected virtual IEnumerable <(object value, OutputAttribute output)> GetValues(ElementContext parent)
        {
            string[] excludeMembers = parent.OutputAttribute.ExcludeMembers;
            if (excludeMembers == null && parent.Parent?.Element is SceneItemWrapper <SceneItem> )
            {
                excludeMembers = parent.Parent.OutputAttribute.ExcludeMembers;
            }

            foreach ((object value, MemberInfo type, OutputAttribute output) in this.GetProperties())
            {
                OutputAttribute outputAttr;

                if (type is PropertyInfo propertyInfo)
                {
                    if (excludeMembers != null && excludeMembers.Length > 0)
                    {
                        if (excludeMembers.Contains(type.Name))
                        {
                            continue;
                        }
                    }

                    OutputAttribute[] propertyAttrs = type.GetCustomAttributesFast <OutputAttribute>().ToArray();
                    if (propertyAttrs.Length == 0)
                    {
                        // Property has no Output attribute
                        continue;
                    }

                    // Combine the attributes of the property
                    outputAttr = OutputAttribute.Combine(propertyAttrs);
                    if (output != null)
                    {
                        outputAttr.Combine(output);
                    }

                    outputAttr.Depth        = propertyInfo.GetPropertyInheritanceDepth();
                    outputAttr.PropertyName = propertyInfo.Name;
                }
                else
                {
                    outputAttr = output;
                }

                if (value is ICollection enumerable)
                {
                    int index = 0;

                    if (enumerable.Cast <object>().All(e => e == null))
                    {
                        continue;
                    }

                    if (!outputAttr.IsCollection)
                    {
                        outputAttr.IsCollection = true;
                        // Unwrap the enumeration
                        foreach (SceneElement element in enumerable.OfType <SceneElement>())
                        {
                            outputAttr.Index = index++;
                            yield return(element, outputAttr);
                        }

                        continue;
                    }
                }

                yield return(value, outputAttr);
            }
        }
示例#4
0
        /// <summary>Gets the child elements of this element.</summary>
        /// <param name="parent">The parent element's context.</param>
        /// <returns>Enumeration of elements.</returns>
        public virtual IEnumerable <ElementContext> GetElements(ElementContext parent)
        {
            IEnumerable <(object, OutputAttribute)> values =
                this.GetValues(parent)
                .Concat(this.GetAdditionalValues());

            if (parent.OutputAttribute.AdditionalItems != null)
            {
                values = values.Concat(parent.OutputAttribute.AdditionalItems);
            }

            Keyword[] excludeKeywords = parent.OutputAttribute.ExcludeKeywords;
            if (excludeKeywords == null && parent.Parent?.Element is SceneItemWrapper <SceneItem> )
            {
                excludeKeywords = parent.Parent.OutputAttribute.ExcludeKeywords;
            }

            foreach ((object valueOrig, OutputAttribute output) in values)
            {
                object value = valueOrig;
                if (value == null || value is IValue v && !v.HasValue)
                {
                    continue;
                }

                if (output != null)
                {
                    if (output.Wrapper && value is SceneItem item)
                    {
                        value = new SceneItemWrapper <SceneItem>(item);
                    }
                }

                OutputAttribute outputAttribute =
                    OutputAttribute.Combine(value.GetType().GetAllAttributes <OutputAttribute>());

                if (output != null)
                {
                    outputAttribute.Combine(output);
                }

                if (outputAttribute.Ignore)
                {
                    continue;
                }

                if (outputAttribute.PovVersion > parent.Scene.PovVersion)
                {
                    continue;
                }

                if (excludeKeywords != null)
                {
                    if (excludeKeywords.Contains(outputAttribute.Keyword))
                    {
                        continue;
                    }
                }


                SceneElement   element = value as SceneElement ?? SceneElement.CreateElement(value, outputAttribute);
                ElementContext context = new ElementContext(parent, element, outputAttribute);
                yield return(context);
            }
        }