Exemplo n.º 1
0
 /// <summary>
 ///     Ctor.
 /// </summary>
 /// <param name="getter">for retrieving the value</param>
 /// <param name="name">property name</param>
 /// <param name="output">for rendering the getter result</param>
 public GetterPair(
     EventPropertyGetter getter,
     string name,
     OutputValueRenderer output)
 {
     Getter = getter;
     Name = name;
     Output = output;
 }
Exemplo n.º 2
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);
                    }
                }
            }
        }
Exemplo 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());
        }
Exemplo n.º 4
0
        private static void RecursiveRender(EventBean theEvent, StringBuilder buf, int level, RendererMeta meta, RendererMetaOptions rendererOptions)
        {
            String delimiter = "";

            // simple properties
            GetterPair[] simpleProps = meta.SimpleProperties;
            if (rendererOptions.Renderer == null)
            {
                foreach (GetterPair simpleProp in simpleProps.OrderBy(prop => prop.Name))
                {
                    Object value = simpleProp.Getter.Get(theEvent);
                    WriteDelimitedIndentedProp(buf, delimiter, level, simpleProp.Name);
                    simpleProp.Output.Render(value, buf);
                    delimiter = COMMA_DELIMITER_NEWLINE;
                }
            }
            else
            {
                EventPropertyRendererContext context = rendererOptions.RendererContext;
                context.SetStringBuilderAndReset(buf);
                foreach (GetterPair simpleProp in simpleProps.OrderBy(prop => prop.Name))
                {
                    Object value = simpleProp.Getter.Get(theEvent);
                    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;
                }
            }

            GetterPair[] indexProps = meta.IndexProperties;
            foreach (GetterPair indexProp in indexProps.OrderBy(prop => prop.Name))
            {
                WriteDelimitedIndentedProp(buf, delimiter, level, indexProp.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
                    {
                        EventPropertyRendererContext context = rendererOptions.RendererContext;
                        context.SetStringBuilderAndReset(buf);
                        context.DefaultRenderer = indexProp.Output;
                        context.PropertyName    = indexProp.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 (int i = 0; i < asArray.Length; i++)
                            {
                                var arrayItem = asArray.GetValue(i);
                                buf.Append(arrayDelimiter);
                                indexProp.Output.Render(arrayItem, buf);
                                arrayDelimiter = ", ";
                            }
                        }
                        else
                        {
                            EventPropertyRendererContext context = rendererOptions.RendererContext;
                            context.SetStringBuilderAndReset(buf);
                            for (int i = 0; i < asArray.Length; i++)
                            {
                                Object 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;
            }

            GetterPair[] mappedProps = meta.MappedProperties;
            foreach (GetterPair mappedProp in mappedProps.OrderBy(prop => prop.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;
                }

                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
                            {
                                OutputValueRenderer outRenderer = OutputValueRendererFactory.GetOutputValueRenderer(entry.Value.GetType(), rendererOptions);
                                if (rendererOptions.Renderer == null)
                                {
                                    outRenderer.Render(entry.Value, buf);
                                }
                                else
                                {
                                    EventPropertyRendererContext 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;
            }

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

                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");
                        continue;
                    }
                    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");
                        continue;
                    }


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

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

                    for (int i = 0; i < nestedEventArray.Length; i++)
                    {
                        EventBean 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;
            }

            buf.Append(NEWLINE);
        }