~ifolder-dev/simias/trunk-packaging

« back to all changes in this revision

Viewing changes to src/core/Simias.log4net/src/ObjectRenderer/.svn/text-base/DefaultRenderer.cs.svn-base

  • Committer: Jorge O. Castro
  • Date: 2007-12-03 06:56:46 UTC
  • Revision ID: jorge@ubuntu.com-20071203065646-mupcnjcwgm5mnhyt
* Remove a bunch of .svn directories we no longer need.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#region Copyright & License
2
 
//
3
 
// Copyright 2001-2006 The Apache Software Foundation
4
 
//
5
 
// Licensed under the Apache License, Version 2.0 (the "License");
6
 
// you may not use this file except in compliance with the License.
7
 
// You may obtain a copy of the License at
8
 
//
9
 
// http://www.apache.org/licenses/LICENSE-2.0
10
 
//
11
 
// Unless required by applicable law or agreed to in writing, software
12
 
// distributed under the License is distributed on an "AS IS" BASIS,
13
 
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 
// See the License for the specific language governing permissions and
15
 
// limitations under the License.
16
 
//
17
 
#endregion
18
 
 
19
 
using System;
20
 
using System.Text;
21
 
using System.IO;
22
 
using System.Collections;
23
 
 
24
 
using log4net.Util;
25
 
 
26
 
namespace log4net.ObjectRenderer
27
 
{
28
 
        /// <summary>
29
 
        /// The default object Renderer.
30
 
        /// </summary>
31
 
        /// <remarks>
32
 
        /// <para>
33
 
        /// The default renderer supports rendering objects and collections to strings.
34
 
        /// </para>
35
 
        /// <para>
36
 
        /// See the <see cref="RenderObject"/> method for details of the output.
37
 
        /// </para>
38
 
        /// </remarks>
39
 
        /// <author>Nicko Cadell</author>
40
 
        /// <author>Gert Driesen</author>
41
 
        public sealed class DefaultRenderer : IObjectRenderer
42
 
        {
43
 
                #region Constructors
44
 
 
45
 
                /// <summary>
46
 
                /// Default constructor
47
 
                /// </summary>
48
 
                /// <remarks>
49
 
                /// <para>
50
 
                /// Default constructor
51
 
                /// </para>
52
 
                /// </remarks>
53
 
                public DefaultRenderer()
54
 
                {
55
 
                }
56
 
 
57
 
                #endregion
58
 
 
59
 
                #region Implementation of IObjectRenderer
60
 
 
61
 
                /// <summary>
62
 
                /// Render the object <paramref name="obj"/> to a string
63
 
                /// </summary>
64
 
                /// <param name="rendererMap">The map used to lookup renderers</param>
65
 
                /// <param name="obj">The object to render</param>
66
 
                /// <param name="writer">The writer to render to</param>
67
 
                /// <remarks>
68
 
                /// <para>
69
 
                /// Render the object <paramref name="obj"/> to a string.
70
 
                /// </para>
71
 
                /// <para>
72
 
                /// The <paramref name="rendererMap"/> parameter is
73
 
                /// provided to lookup and render other objects. This is
74
 
                /// very useful where <paramref name="obj"/> contains
75
 
                /// nested objects of unknown type. The <see cref="RendererMap.FindAndRender(object)"/>
76
 
                /// method can be used to render these objects.
77
 
                /// </para>
78
 
                /// <para>
79
 
                /// The default renderer supports rendering objects to strings as follows:
80
 
                /// </para>
81
 
                /// <list type="table">
82
 
                ///             <listheader>
83
 
                ///                     <term>Value</term>
84
 
                ///                     <description>Rendered String</description>
85
 
                ///             </listheader>
86
 
                ///             <item>
87
 
                ///                     <term><c>null</c></term>
88
 
                ///                     <description>
89
 
                ///                     <para>"(null)"</para>
90
 
                ///                     </description>
91
 
                ///             </item>
92
 
                ///             <item>
93
 
                ///                     <term><see cref="Array"/></term>
94
 
                ///                     <description>
95
 
                ///                     <para>
96
 
                ///                     For a one dimensional array this is the
97
 
                ///                     array type name, an open brace, followed by a comma
98
 
                ///                     separated list of the elements (using the appropriate
99
 
                ///                     renderer), followed by a close brace. 
100
 
                ///                     </para>
101
 
                ///                     <para>
102
 
                ///                     For example: <c>int[] {1, 2, 3}</c>.
103
 
                ///                     </para>
104
 
                ///                     <para>
105
 
                ///                     If the array is not one dimensional the 
106
 
                ///                     <c>Array.ToString()</c> is returned.
107
 
                ///                     </para>
108
 
                ///                     </description>
109
 
                ///             </item>
110
 
                ///             <item>
111
 
                ///                     <term><see cref="IEnumerable"/>, <see cref="ICollection"/> &amp; <see cref="IEnumerator"/></term>
112
 
                ///                     <description>
113
 
                ///                     <para>
114
 
                ///                     Rendered as an open brace, followed by a comma
115
 
                ///                     separated list of the elements (using the appropriate
116
 
                ///                     renderer), followed by a close brace.
117
 
                ///                     </para>
118
 
                ///                     <para>
119
 
                ///                     For example: <c>{a, b, c}</c>.
120
 
                ///                     </para>
121
 
                ///                     <para>
122
 
                ///                     All collection classes that implement <see cref="ICollection"/> its subclasses, 
123
 
                ///                     or generic equivalents all implement the <see cref="IEnumerable"/> interface.
124
 
                ///                     </para>
125
 
                ///                     </description>
126
 
                ///             </item>         
127
 
                ///             <item>
128
 
                ///                     <term><see cref="DictionaryEntry"/></term>
129
 
                ///                     <description>
130
 
                ///                     <para>
131
 
                ///                     Rendered as the key, an equals sign ('='), and the value (using the appropriate
132
 
                ///                     renderer). 
133
 
                ///                     </para>
134
 
                ///                     <para>
135
 
                ///                     For example: <c>key=value</c>.
136
 
                ///                     </para>
137
 
                ///                     </description>
138
 
                ///             </item>         
139
 
                ///             <item>
140
 
                ///                     <term>other</term>
141
 
                ///                     <description>
142
 
                ///                     <para><c>Object.ToString()</c></para>
143
 
                ///                     </description>
144
 
                ///             </item>
145
 
                /// </list>
146
 
                /// </remarks>
147
 
                public void RenderObject(RendererMap rendererMap, object obj, TextWriter writer)
148
 
                {
149
 
                        if (rendererMap == null)
150
 
                        {
151
 
                                throw new ArgumentNullException("rendererMap");
152
 
                        }
153
 
 
154
 
                        if (obj == null)
155
 
                        {
156
 
                                writer.Write(SystemInfo.NullText);
157
 
                                return;
158
 
                        }
159
 
                        
160
 
                        Array objArray = obj as Array;
161
 
                        if (objArray != null)
162
 
                        {
163
 
                                RenderArray(rendererMap, objArray, writer);
164
 
                                return;
165
 
                        }
166
 
 
167
 
                        // Test if we are dealing with some form of collection object
168
 
                        IEnumerable objEnumerable = obj as IEnumerable;
169
 
                        if (objEnumerable != null)
170
 
                        {
171
 
                                // Get a collection interface if we can as its .Count property may be more
172
 
                                // performant than getting the IEnumerator object and trying to advance it.
173
 
                                ICollection objCollection = obj as ICollection;
174
 
                                if (objCollection != null && objCollection.Count == 0)
175
 
                                {
176
 
                                        writer.Write("{}");
177
 
                                        return;
178
 
                                }
179
 
                                
180
 
                                // This is a special check to allow us to get the enumerator from the IDictionary
181
 
                                // interface as this guarantees us DictionaryEntry objects. Note that in .NET 2.0
182
 
                                // the generic IDictionary<> interface enumerates KeyValuePair objects rather than
183
 
                                // DictionaryEntry ones. However the implementation of the plain IDictionary 
184
 
                                // interface on the generic Dictionary<> still returns DictionaryEntry objects.
185
 
                                IDictionary objDictionary = obj as IDictionary;
186
 
                                if (objDictionary != null)
187
 
                                {
188
 
                                        RenderEnumerator(rendererMap, objDictionary.GetEnumerator(), writer);
189
 
                                        return;
190
 
                                }
191
 
 
192
 
                                RenderEnumerator(rendererMap, objEnumerable.GetEnumerator(), writer);
193
 
                                return;
194
 
                        }
195
 
 
196
 
                        IEnumerator objEnumerator = obj as IEnumerator;
197
 
                        if (objEnumerator != null)
198
 
                        {
199
 
                                RenderEnumerator(rendererMap, objEnumerator, writer);
200
 
                                return;
201
 
                        }
202
 
                        
203
 
                        if (obj is DictionaryEntry)
204
 
                        {
205
 
                                RenderDictionaryEntry(rendererMap, (DictionaryEntry)obj, writer);
206
 
                                return;
207
 
                        }
208
 
 
209
 
                        string str = obj.ToString();
210
 
                        writer.Write( (str==null) ? SystemInfo.NullText : str );
211
 
                }
212
 
 
213
 
                #endregion
214
 
 
215
 
                /// <summary>
216
 
                /// Render the array argument into a string
217
 
                /// </summary>
218
 
                /// <param name="rendererMap">The map used to lookup renderers</param>
219
 
                /// <param name="array">the array to render</param>
220
 
                /// <param name="writer">The writer to render to</param>
221
 
                /// <remarks>
222
 
                /// <para>
223
 
                /// For a one dimensional array this is the
224
 
                ///     array type name, an open brace, followed by a comma
225
 
                ///     separated list of the elements (using the appropriate
226
 
                ///     renderer), followed by a close brace. For example:
227
 
                ///     <c>int[] {1, 2, 3}</c>.
228
 
                ///     </para>
229
 
                ///     <para>
230
 
                ///     If the array is not one dimensional the 
231
 
                ///     <c>Array.ToString()</c> is returned.
232
 
                ///     </para>
233
 
                /// </remarks>
234
 
                private void RenderArray(RendererMap rendererMap, Array array, TextWriter writer)
235
 
                {
236
 
                        if (array.Rank != 1)
237
 
                        {
238
 
                                writer.Write(array.ToString());
239
 
                        }
240
 
                        else
241
 
                        {
242
 
                                writer.Write(array.GetType().Name + " {");
243
 
                                int len = array.Length;
244
 
 
245
 
                                if (len > 0)
246
 
                                {
247
 
                                        rendererMap.FindAndRender(array.GetValue(0), writer);
248
 
                                        for(int i=1; i<len; i++)
249
 
                                        {
250
 
                                                writer.Write(", ");
251
 
                                                rendererMap.FindAndRender(array.GetValue(i), writer);
252
 
                                        }
253
 
                                }
254
 
                                writer.Write("}");
255
 
                        }
256
 
                }
257
 
 
258
 
                /// <summary>
259
 
                /// Render the enumerator argument into a string
260
 
                /// </summary>
261
 
                /// <param name="rendererMap">The map used to lookup renderers</param>
262
 
                /// <param name="enumerator">the enumerator to render</param>
263
 
                /// <param name="writer">The writer to render to</param>
264
 
                /// <remarks>
265
 
                /// <para>
266
 
                /// Rendered as an open brace, followed by a comma
267
 
                ///     separated list of the elements (using the appropriate
268
 
                ///     renderer), followed by a close brace. For example:
269
 
                ///     <c>{a, b, c}</c>.
270
 
                ///     </para>
271
 
                /// </remarks>
272
 
                private void RenderEnumerator(RendererMap rendererMap, IEnumerator enumerator, TextWriter writer)
273
 
                {
274
 
                        writer.Write("{");
275
 
 
276
 
                        if (enumerator != null && enumerator.MoveNext())
277
 
                        {
278
 
                                rendererMap.FindAndRender(enumerator.Current, writer);
279
 
 
280
 
                                while (enumerator.MoveNext())
281
 
                                {
282
 
                                        writer.Write(", ");
283
 
                                        rendererMap.FindAndRender(enumerator.Current, writer);
284
 
                                }
285
 
                        }
286
 
 
287
 
                        writer.Write("}");
288
 
                }
289
 
 
290
 
                /// <summary>
291
 
                /// Render the DictionaryEntry argument into a string
292
 
                /// </summary>
293
 
                /// <param name="rendererMap">The map used to lookup renderers</param>
294
 
                /// <param name="entry">the DictionaryEntry to render</param>
295
 
                /// <param name="writer">The writer to render to</param>
296
 
                /// <remarks>
297
 
                /// <para>
298
 
                /// Render the key, an equals sign ('='), and the value (using the appropriate
299
 
                ///     renderer). For example: <c>key=value</c>.
300
 
                ///     </para>
301
 
                /// </remarks>
302
 
                private void RenderDictionaryEntry(RendererMap rendererMap, DictionaryEntry entry, TextWriter writer)
303
 
                {
304
 
                        rendererMap.FindAndRender(entry.Key, writer);
305
 
                        writer.Write("=");
306
 
                        rendererMap.FindAndRender(entry.Value, writer);
307
 
                }       
308
 
        }
309
 
}