Ejemplo n.º 1
0
        private static void RecursiveRender(EventBean theEvent, StringBuilder buf, int level, RendererMeta meta,
                                            RendererMetaOptions rendererMetaOptions)
        {
            GetterPair[] simpleProps = meta.SimpleProperties;
            foreach (GetterPair simpleProp in simpleProps.OrderBy(prop => prop.Name))
            {
                var value = simpleProp.Getter.Get(theEvent);
                if (value == null)
                {
                    continue;
                }

                Ident(buf, level);
                buf.Append('<');
                buf.Append(simpleProp.Name);
                buf.Append('>');

                if (rendererMetaOptions.Renderer == null)
                {
                    simpleProp.Output.Render(value, buf);
                }
                else
                {
                    EventPropertyRendererContext context = rendererMetaOptions.RendererContext;
                    context.SetStringBuilderAndReset(buf);
                    context.PropertyName    = simpleProp.Name;
                    context.PropertyValue   = value;
                    context.DefaultRenderer = simpleProp.Output;
                    rendererMetaOptions.Renderer.Render(context);
                }

                buf.Append("</");
                buf.Append(simpleProp.Name);
                buf.Append('>');
                buf.Append(NEWLINE);
            }

            GetterPair[] indexProps = meta.IndexProperties;
            foreach (GetterPair indexProp in indexProps.OrderBy(prop => prop.Name))
            {
                object value = indexProp.Getter.Get(theEvent);

                if (value == null)
                {
                    continue;
                }

                if (value is string)
                {
                    Ident(buf, level);

                    buf.Append('<');
                    buf.Append(indexProp.Name);
                    buf.Append('>');

                    if (rendererMetaOptions.Renderer == null)
                    {
                        indexProp.Output.Render(value, buf);
                    }
                    else
                    {
                        EventPropertyRendererContext context = rendererMetaOptions.RendererContext;
                        context.SetStringBuilderAndReset(buf);
                        context.PropertyName    = indexProp.Name;
                        context.PropertyValue   = value;
                        context.DefaultRenderer = indexProp.Output;
                        rendererMetaOptions.Renderer.Render(context);
                    }

                    buf.Append("</");
                    buf.Append(indexProp.Name);
                    buf.Append('>');
                    buf.Append(NEWLINE);

                    continue;
                }

                var asArray = value as Array;
                if (asArray == null)
                {
                    Log.Warn("Property '" + indexProp.Name + "' returned a non-array object");
                    continue;
                }

                for (int i = 0; i < asArray.Length; i++)
                {
                    object arrayItem = asArray.GetValue(i);

                    if (arrayItem == null)
                    {
                        continue;
                    }

                    Ident(buf, level);
                    buf.Append('<');
                    buf.Append(indexProp.Name);
                    buf.Append('>');

                    if (rendererMetaOptions.Renderer == null)
                    {
                        indexProp.Output.Render(arrayItem, buf);
                    }
                    else
                    {
                        EventPropertyRendererContext context = rendererMetaOptions.RendererContext;
                        context.SetStringBuilderAndReset(buf);
                        context.PropertyName         = indexProp.Name;
                        context.PropertyValue        = arrayItem;
                        context.IndexedPropertyIndex = i;
                        context.DefaultRenderer      = indexProp.Output;
                        rendererMetaOptions.Renderer.Render(context);
                    }

                    buf.Append("</");
                    buf.Append(indexProp.Name);
                    buf.Append('>');
                    buf.Append(NEWLINE);
                }
            }

            GetterPair[] mappedProps = meta.MappedProperties;
            foreach (GetterPair mappedProp in mappedProps.OrderBy(prop => prop.Name))
            {
                object value = mappedProp.Getter.Get(theEvent);

                if ((value != null) && (!(value is DataMap)))
                {
                    Log.Warn("Property '" + mappedProp.Name + "' expected to return Map and returned " + value.GetType() +
                             " instead");
                    continue;
                }

                Ident(buf, level);
                buf.Append('<');
                buf.Append(mappedProp.Name);
                buf.Append('>');
                buf.Append(NEWLINE);

                if (value != null)
                {
                    var map = (DataMap)value;
                    if (map.IsNotEmpty())
                    {
                        String localDelimiter = "";
                        foreach (var entry in map)
                        {
                            if (entry.Key == null)
                            {
                                continue;
                            }

                            buf.Append(localDelimiter);
                            Ident(buf, level + 1);
                            buf.Append('<');
                            buf.Append(entry.Key);
                            buf.Append('>');

                            if (entry.Value != null)
                            {
                                OutputValueRenderer outputValueRenderer = OutputValueRendererFactory.GetOutputValueRenderer(
                                    entry.Value.GetType(), rendererMetaOptions);
                                if (rendererMetaOptions.Renderer == null)
                                {
                                    outputValueRenderer.Render(entry.Value, buf);
                                }
                                else
                                {
                                    EventPropertyRendererContext context = rendererMetaOptions.RendererContext;
                                    context.SetStringBuilderAndReset(buf);
                                    context.PropertyName      = mappedProp.Name;
                                    context.PropertyValue     = entry.Value;
                                    context.MappedPropertyKey = entry.Key;
                                    context.DefaultRenderer   = outputValueRenderer;
                                    rendererMetaOptions.Renderer.Render(context);
                                }
                            }

                            buf.Append('<');
                            buf.Append(entry.Key);
                            buf.Append('>');
                            localDelimiter = NEWLINE;
                        }
                    }
                }

                buf.Append(NEWLINE);
                Ident(buf, level);
                buf.Append("</");
                buf.Append(mappedProp.Name);
                buf.Append('>');
                buf.Append(NEWLINE);
            }

            var nestedProps = meta.NestedProperties;

            foreach (NestedGetterPair nestedProp in nestedProps.OrderBy(prop => prop.Name))
            {
                var value = nestedProp.Getter.GetFragment(theEvent);
                if (value == null)
                {
                    continue;
                }

                if (!nestedProp.IsArray)
                {
                    if (!(value is EventBean))
                    {
                        Log.Warn("Property '" + nestedProp.Name + "' expected to return EventBean and returned " +
                                 value.GetType() + " instead");
                        buf.Append("null");
                        continue;
                    }
                    RenderElementFragment((EventBean)value, buf, level, nestedProp, rendererMetaOptions);
                }
                else
                {
                    if (!(value is EventBean[]))
                    {
                        Log.Warn("Property '" + nestedProp.Name + "' expected to return EventBean[] and returned " +
                                 value.GetType() + " instead");
                        buf.Append("null");
                        continue;
                    }

                    var nestedEventArray = (EventBean[])value;
                    for (int i = 0; i < nestedEventArray.Length; i++)
                    {
                        EventBean arrayItem = nestedEventArray[i];
                        if (arrayItem == null)
                        {
                            continue;
                        }
                        RenderElementFragment(arrayItem, buf, level, nestedProp, rendererMetaOptions);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Ctor.
        /// </summary>
        /// <param name="eventType">to render</param>
        /// <param name="stack">the stack of properties to avoid looping</param>
        /// <param name="options">rendering options</param>
        public RendererMeta(EventType eventType, Stack <EventTypePropertyPair> stack, RendererMetaOptions options)
        {
            var gettersSimple  = new List <GetterPair>();
            var gettersIndexed = new List <GetterPair>();
            var gettersMapped  = new List <GetterPair>();
            var gettersNested  = new List <NestedGetterPair>();

            var descriptors = eventType.PropertyDescriptors;

            foreach (EventPropertyDescriptor desc in descriptors)
            {
                String propertyName = desc.PropertyName;

                if ((!desc.IsIndexed) && (!desc.IsMapped) && (!desc.IsFragment))
                {
                    var getter = eventType.GetGetter(propertyName);
                    if (getter == null)
                    {
                        log.Warn("No getter returned for event type '" + eventType.Name + "' and property '" +
                                 propertyName + "'");
                        continue;
                    }
                    gettersSimple.Add(new GetterPair(getter, propertyName,
                                                     OutputValueRendererFactory.GetOutputValueRenderer(
                                                         desc.PropertyType, options)));
                }

                if (desc.IsIndexed && !desc.RequiresIndex && (!desc.IsFragment))
                {
                    var getter = eventType.GetGetter(propertyName);
                    if (getter == null)
                    {
                        log.Warn("No getter returned for event type '" + eventType.Name + "' and property '" +
                                 propertyName + "'");
                        continue;
                    }
                    gettersIndexed.Add(new GetterPair(getter, propertyName,
                                                      OutputValueRendererFactory.GetOutputValueRenderer(
                                                          desc.PropertyType, options)));
                }

                if (desc.IsMapped && !desc.RequiresMapKey && (!desc.IsFragment))
                {
                    var getter = eventType.GetGetter(propertyName);
                    if (getter == null)
                    {
                        log.Warn("No getter returned for event type '" + eventType.Name + "' and property '" +
                                 propertyName + "'");
                        continue;
                    }
                    gettersMapped.Add(new GetterPair(getter, propertyName,
                                                     OutputValueRendererFactory.GetOutputValueRenderer(
                                                         desc.PropertyType, options)));
                }

                if (desc.IsFragment)
                {
                    var getter       = eventType.GetGetter(propertyName);
                    var fragmentType = eventType.GetFragmentType(propertyName);
                    if (getter == null)
                    {
                        log.Warn("No getter returned for event type '" + eventType.Name + "' and property '" +
                                 propertyName + "'");
                        continue;
                    }
                    if (fragmentType == null)
                    {
                        log.Warn("No fragment type returned for event type '" + eventType.Name + "' and property '" +
                                 propertyName + "'");
                        continue;
                    }

                    var pair = new EventTypePropertyPair(fragmentType.FragmentType, propertyName);
                    if ((options.PreventLooping && stack.Contains(pair)))
                    {
                        continue; // prevent looping behavior on self-references
                    }

                    stack.Push(pair);
                    var fragmentMetaData = new RendererMeta(fragmentType.FragmentType, stack, options);
                    stack.Pop();

                    gettersNested.Add(new NestedGetterPair(getter, propertyName, fragmentMetaData,
                                                           fragmentType.IsIndexed));
                }
            }

            gettersSimple.Sort((gp1, gp2) => gp1.Name.CompareTo(gp2.Name));
            gettersIndexed.Sort((gp1, gp2) => gp1.Name.CompareTo(gp2.Name));
            gettersMapped.Sort((gp1, gp2) => gp1.Name.CompareTo(gp2.Name));
            gettersNested.Sort((gp1, gp2) => gp1.Name.CompareTo(gp2.Name));

            simpleProperties = gettersSimple.ToArray();
            indexProperties  = gettersIndexed.ToArray();
            mappedProperties = gettersMapped.ToArray();
            nestedProperties = gettersNested.ToArray();
        }
Ejemplo n.º 3
0
        private String RenderAttElements(EventBean theEvent, int level, RendererMeta meta)
        {
            var buf = new StringBuilder();

            GetterPair[] indexProps = meta.IndexProperties;
            foreach (GetterPair indexProp in indexProps.OrderBy(prop => prop.Name))
            {
                var value = indexProp.Getter.Get(theEvent);
                if (value == null)
                {
                    continue;
                }

                if (value is string)
                {
                    continue;
                }

                var asArray = value as Array;
                if (asArray == null)
                {
                    Log.Warn("Property '" + indexProp.Name + "' returned a non-array object");
                    continue;
                }

                for (int i = 0; i < asArray.Length; i++)
                {
                    object arrayItem = asArray.GetValue(i);
                    if (arrayItem == null)
                    {
                        continue;
                    }

                    Ident(buf, level);
                    buf.Append('<');
                    buf.Append(indexProp.Name);
                    buf.Append('>');

                    if (_rendererMetaOptions.Renderer == null)
                    {
                        indexProp.Output.Render(arrayItem, buf);
                    }
                    else
                    {
                        EventPropertyRendererContext context = _rendererMetaOptions.RendererContext;
                        context.SetStringBuilderAndReset(buf);
                        context.PropertyName         = indexProp.Name;
                        context.PropertyValue        = arrayItem;
                        context.IndexedPropertyIndex = i;
                        context.DefaultRenderer      = indexProp.Output;
                        _rendererMetaOptions.Renderer.Render(context);
                    }

                    buf.Append("</");
                    buf.Append(indexProp.Name);
                    buf.Append('>');
                    buf.Append(NEWLINE);
                }
            }

            GetterPair[] mappedProps = meta.MappedProperties;
            foreach (GetterPair mappedProp in mappedProps.OrderBy(prop => prop.Name))
            {
                object value = mappedProp.Getter.Get(theEvent);

                if ((value != null) && (!(value is DataMap)))
                {
                    Log.Warn("Property '" + mappedProp.Name + "' expected to return Map and returned " + value.GetType() +
                             " instead");
                    continue;
                }

                Ident(buf, level);
                buf.Append('<');
                buf.Append(mappedProp.Name);

                if (value != null)
                {
                    var map = (DataMap)value;
                    if (map.IsNotEmpty())
                    {
                        foreach (var entry in map)
                        {
                            if ((entry.Key == null) || (entry.Value == null))
                            {
                                continue;
                            }

                            buf.Append(" ");
                            buf.Append(entry.Key);
                            buf.Append("=\"");

                            OutputValueRenderer outputValueRenderer =
                                OutputValueRendererFactory.GetOutputValueRenderer(entry.Value.GetType(), _rendererMetaOptions);

                            if (_rendererMetaOptions.Renderer == null)
                            {
                                outputValueRenderer.Render(entry.Value, buf);
                            }
                            else
                            {
                                EventPropertyRendererContext context = _rendererMetaOptions.RendererContext;
                                context.SetStringBuilderAndReset(buf);
                                context.PropertyName      = mappedProp.Name;
                                context.PropertyValue     = entry.Value;
                                context.MappedPropertyKey = entry.Key;
                                context.DefaultRenderer   = outputValueRenderer;
                                _rendererMetaOptions.Renderer.Render(context);
                            }

                            buf.Append("\"");
                        }
                    }
                }

                buf.Append("/>");
                buf.Append(NEWLINE);
            }

            NestedGetterPair[] nestedProps = meta.NestedProperties;
            foreach (NestedGetterPair nestedProp in nestedProps.OrderBy(prop => prop.Name))
            {
                object value = nestedProp.Getter.GetFragment(theEvent);

                if (value == null)
                {
                    continue;
                }

                if (!nestedProp.IsArray)
                {
                    if (!(value is EventBean))
                    {
                        Log.Warn("Property '" + nestedProp.Name + "' expected to return EventBean and returned " +
                                 value.GetType() + " instead");
                        buf.Append("null");
                        continue;
                    }
                    var nestedEventBean = (EventBean)value;
                    RenderAttInner(buf, level, nestedEventBean, nestedProp);
                }
                else
                {
                    if (!(value is EventBean[]))
                    {
                        Log.Warn("Property '" + nestedProp.Name + "' expected to return EventBean[] and returned " +
                                 value.GetType() + " instead");
                        buf.Append("null");
                        continue;
                    }

                    var nestedEventArray = (EventBean[])value;
                    for (int i = 0; i < nestedEventArray.Length; i++)
                    {
                        EventBean arrayItem = nestedEventArray[i];
                        RenderAttInner(buf, level, arrayItem, nestedProp);
                    }
                }
            }

            return(buf.ToString());
        }
Ejemplo n.º 4
0
        private static void RecursiveRender(EventBean theEvent, StringBuilder buf, int level, RendererMeta meta, RendererMetaOptions rendererOptions)
        {
            var delimiter = "";

            var renderActions = new SortedList <string, Action>();

            // simple properties
            var simpleProps = meta.SimpleProperties;

            if (rendererOptions.Renderer == null)
            {
                foreach (var simpleProp in simpleProps.OrderBy(prop => prop.Name))
                {
                    var name  = simpleProp.Name;
                    var value = simpleProp.Getter.Get(theEvent);
                    renderActions.Add(name, () =>
                    {
                        WriteDelimitedIndentedProp(buf, delimiter, level, name);
                        simpleProp.Output.Render(value, buf);
                        delimiter = COMMA_DELIMITER_NEWLINE;
                    });
                }
            }
            else
            {
                var context = rendererOptions.RendererContext;
                context.SetStringBuilderAndReset(buf);
                foreach (var simpleProp in simpleProps.OrderBy(prop => prop.Name))
                {
                    var name  = simpleProp.Name;
                    var value = simpleProp.Getter.Get(theEvent);
                    renderActions.Add(name, () =>
                    {
                        WriteDelimitedIndentedProp(buf, delimiter, level, simpleProp.Name);
                        context.DefaultRenderer = simpleProp.Output;
                        context.PropertyName    = simpleProp.Name;
                        context.PropertyValue   = value;
                        rendererOptions.Renderer.Render(context);
                        delimiter = COMMA_DELIMITER_NEWLINE;
                    });
                }
            }

            var indexProps = meta.IndexProperties;

            foreach (var indexProp in indexProps.OrderBy(prop => prop.Name))
            {
                var name = indexProp.Name;

                renderActions.Add(
                    name, () =>
                {
                    WriteDelimitedIndentedProp(buf, delimiter, level, name);

                    var value = indexProp.Getter.Get(theEvent);
                    if (value == null)
                    {
                        buf.Append("null");
                    }
                    else if (value is string)
                    {
                        if (rendererOptions.Renderer == null)
                        {
                            indexProp.Output.Render(value, buf);
                        }
                        else
                        {
                            var context = rendererOptions.RendererContext;
                            context.SetStringBuilderAndReset(buf);
                            context.DefaultRenderer = indexProp.Output;
                            context.PropertyName    = name;
                            context.PropertyValue   = value;
                            rendererOptions.Renderer.Render(context);
                        }
                    }
                    else
                    {
                        var asArray = value as Array;
                        if (asArray == null)
                        {
                            buf.Append("[]");
                        }
                        else
                        {
                            buf.Append('[');

                            var arrayDelimiter = "";

                            if (rendererOptions.Renderer == null)
                            {
                                for (var i = 0; i < asArray.Length; i++)
                                {
                                    var arrayItem = asArray.GetValue(i);
                                    buf.Append(arrayDelimiter);
                                    indexProp.Output.Render(arrayItem, buf);
                                    arrayDelimiter = ", ";
                                }
                            }
                            else
                            {
                                var context = rendererOptions.RendererContext;
                                context.SetStringBuilderAndReset(buf);
                                for (var i = 0; i < asArray.Length; i++)
                                {
                                    var arrayItem = asArray.GetValue(i);
                                    buf.Append(arrayDelimiter);
                                    context.PropertyName         = indexProp.Name;
                                    context.PropertyValue        = arrayItem;
                                    context.IndexedPropertyIndex = i;
                                    context.DefaultRenderer      = indexProp.Output;
                                    rendererOptions.Renderer.Render(context);
                                    arrayDelimiter = ", ";
                                }
                            }

                            buf.Append(']');
                        }
                    }

                    delimiter = COMMA_DELIMITER_NEWLINE;
                });
            }

            var mappedProps = meta.MappedProperties;

            foreach (var mappedProp in mappedProps.OrderBy(prop => prop.Name))
            {
                var name  = mappedProp.Name;
                var value = mappedProp.Getter.Get(theEvent);

                if ((value != null) && (!(value.GetType().IsGenericStringDictionary())))
                {
                    Log.Warn("Property '" + mappedProp.Name + "' expected to return Map and returned " + value.GetType() + " instead");
                    continue;
                }

                renderActions.Add(
                    name, () =>
                {
                    WriteDelimitedIndentedProp(buf, delimiter, level, mappedProp.Name);

                    if (value == null)
                    {
                        buf.Append("null");
                        buf.Append(NEWLINE);
                    }
                    else
                    {
                        var map = MagicMarker.GetStringDictionary(value);
                        if (map.IsEmpty())
                        {
                            buf.Append("{}");
                            buf.Append(NEWLINE);
                        }
                        else
                        {
                            buf.Append('{');
                            buf.Append(NEWLINE);

                            var localDelimiter = "";
                            foreach (var entry in map)
                            {
                                if (entry.Key == null)
                                {
                                    continue;
                                }

                                buf.Append(localDelimiter);
                                Ident(buf, level + 1);
                                buf.Append('\"');
                                buf.Append(entry.Key);
                                buf.Append("\": ");

                                if (entry.Value == null)
                                {
                                    buf.Append("null");
                                }
                                else
                                {
                                    var outRenderer =
                                        OutputValueRendererFactory.GetOutputValueRenderer(
                                            entry.Value.GetType(), rendererOptions);
                                    if (rendererOptions.Renderer == null)
                                    {
                                        outRenderer.Render(entry.Value, buf);
                                    }
                                    else
                                    {
                                        var context = rendererOptions.RendererContext;
                                        context.SetStringBuilderAndReset(buf);
                                        context.PropertyName      = mappedProp.Name;
                                        context.PropertyValue     = entry.Value;
                                        context.MappedPropertyKey = entry.Key;
                                        context.DefaultRenderer   = outRenderer;
                                        rendererOptions.Renderer.Render(context);
                                    }
                                }

                                localDelimiter = COMMA_DELIMITER_NEWLINE;
                            }

                            buf.Append(NEWLINE);
                            Ident(buf, level);
                            buf.Append('}');
                        }
                    }

                    delimiter = COMMA_DELIMITER_NEWLINE;
                });
            }

            var nestedProps = meta.NestedProperties;

            foreach (var nestedProp in nestedProps.OrderBy(prop => prop.Name))
            {
                var name  = nestedProp.Name;
                var value = nestedProp.Getter.GetFragment(theEvent);

                renderActions.Add(
                    name, () =>
                {
                    WriteDelimitedIndentedProp(buf, delimiter, level, nestedProp.Name);

                    if (value == null)
                    {
                        buf.Append("null");
                    }
                    else if (!nestedProp.IsArray)
                    {
                        if (!(value is EventBean))
                        {
                            Log.Warn(
                                "Property '" + nestedProp.Name + "' expected to return EventBean and returned " +
                                value.GetType() + " instead");
                            buf.Append("null");
                            return;
                        }

                        var nestedEventBean = (EventBean)value;
                        buf.Append('{');
                        buf.Append(NEWLINE);

                        RecursiveRender(nestedEventBean, buf, level + 1, nestedProp.Metadata, rendererOptions);

                        Ident(buf, level);
                        buf.Append('}');
                    }
                    else
                    {
                        if (!(value is EventBean[]))
                        {
                            Log.Warn(
                                "Property '" + nestedProp.Name + "' expected to return EventBean[] and returned " +
                                value.GetType() + " instead");
                            buf.Append("null");
                            return;
                        }

                        var arrayDelimiterBuf = new StringBuilder();
                        arrayDelimiterBuf.Append(',');
                        arrayDelimiterBuf.Append(NEWLINE);
                        Ident(arrayDelimiterBuf, level + 1);

                        var nestedEventArray = (EventBean[])value;
                        var arrayDelimiter   = "";
                        buf.Append('[');

                        for (var i = 0; i < nestedEventArray.Length; i++)
                        {
                            var arrayItem = nestedEventArray[i];
                            buf.Append(arrayDelimiter);
                            arrayDelimiter = arrayDelimiterBuf.ToString();

                            buf.Append('{');
                            buf.Append(NEWLINE);

                            RecursiveRender(arrayItem, buf, level + 2, nestedProp.Metadata, rendererOptions);

                            Ident(buf, level + 1);
                            buf.Append('}');
                        }

                        buf.Append(']');
                    }

                    delimiter = COMMA_DELIMITER_NEWLINE;
                });
            }

            foreach (var entry in renderActions)
            {
                entry.Value.Invoke();
            }

            buf.Append(NEWLINE);
        }