~ifolder-dev/simias/trunk-packaging

« back to all changes in this revision

Viewing changes to src/core/Simias.log4net/src/ObjectRenderer/.svn/text-base/RendererMap.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-2005 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.IO;
21
 
using log4net.Util;
22
 
 
23
 
namespace log4net.ObjectRenderer
24
 
{
25
 
        /// <summary>
26
 
        /// Map class objects to an <see cref="IObjectRenderer"/>.
27
 
        /// </summary>
28
 
        /// <remarks>
29
 
        /// <para>
30
 
        /// Maintains a mapping between types that require special
31
 
        /// rendering and the <see cref="IObjectRenderer"/> that
32
 
        /// is used to render them.
33
 
        /// </para>
34
 
        /// <para>
35
 
        /// The <see cref="FindAndRender(object)"/> method is used to render an
36
 
        /// <c>object</c> using the appropriate renderers defined in this map.
37
 
        /// </para>
38
 
        /// </remarks>
39
 
        /// <author>Nicko Cadell</author>
40
 
        /// <author>Gert Driesen</author>
41
 
        public class RendererMap
42
 
        {
43
 
                #region Member Variables
44
 
 
45
 
                private System.Collections.Hashtable m_map;
46
 
                private System.Collections.Hashtable m_cache = new System.Collections.Hashtable();
47
 
 
48
 
                private static IObjectRenderer s_defaultRenderer = new DefaultRenderer();
49
 
 
50
 
                #endregion
51
 
 
52
 
                #region Constructors
53
 
 
54
 
                /// <summary>
55
 
                /// Default Constructor
56
 
                /// </summary>
57
 
                /// <remarks>
58
 
                /// <para>
59
 
                /// Default constructor.
60
 
                /// </para>
61
 
                /// </remarks>
62
 
                public RendererMap() 
63
 
                {
64
 
                        m_map = System.Collections.Hashtable.Synchronized(new System.Collections.Hashtable());
65
 
                }
66
 
 
67
 
                #endregion
68
 
 
69
 
                /// <summary>
70
 
                /// Render <paramref name="obj"/> using the appropriate renderer.
71
 
                /// </summary>
72
 
                /// <param name="obj">the object to render to a string</param>
73
 
                /// <returns>the object rendered as a string</returns>
74
 
                /// <remarks>
75
 
                /// <para>
76
 
                /// This is a convenience method used to render an object to a string.
77
 
                /// The alternative method <see cref="FindAndRender(object,TextWriter)"/>
78
 
                /// should be used when streaming output to a <see cref="TextWriter"/>.
79
 
                /// </para>
80
 
                /// </remarks>
81
 
                public string FindAndRender(object obj)
82
 
                {
83
 
                        // Optimisation for strings
84
 
                        string strData = obj as String;
85
 
                        if (strData != null)
86
 
                        {
87
 
                                return strData;
88
 
                        }
89
 
 
90
 
                        StringWriter stringWriter = new StringWriter(System.Globalization.CultureInfo.InvariantCulture);
91
 
                        FindAndRender(obj, stringWriter);
92
 
                        return stringWriter.ToString();
93
 
                }
94
 
 
95
 
                /// <summary>
96
 
                /// Render <paramref name="obj"/> using the appropriate renderer.
97
 
                /// </summary>
98
 
                /// <param name="obj">the object to render to a string</param>
99
 
                /// <param name="writer">The writer to render to</param>
100
 
                /// <remarks>
101
 
                /// <para>
102
 
                /// Find the appropriate renderer for the type of the
103
 
                /// <paramref name="obj"/> parameter. This is accomplished by calling the
104
 
                /// <see cref="Get(Type)"/> method. Once a renderer is found, it is
105
 
                /// applied on the object <paramref name="obj"/> and the result is returned
106
 
                /// as a <see cref="string"/>.
107
 
                /// </para>
108
 
                /// </remarks>
109
 
                public void FindAndRender(object obj, TextWriter writer) 
110
 
                {
111
 
                        if (obj == null)
112
 
                        {
113
 
                                writer.Write(SystemInfo.NullText);
114
 
                        }
115
 
                        else 
116
 
                        {
117
 
                                // Optimisation for strings
118
 
                                string str = obj as string;
119
 
                                if (str != null)
120
 
                                {
121
 
                                        writer.Write(str);
122
 
                                }
123
 
                                else
124
 
                                {
125
 
                                        // Lookup the renderer for the specific type
126
 
                                        try
127
 
                                        {
128
 
                                                Get(obj.GetType()).RenderObject(this, obj, writer);
129
 
                                        }
130
 
                                        catch(Exception ex)
131
 
                                        {
132
 
                                                // Exception rendering the object
133
 
                                                log4net.Util.LogLog.Error("RendererMap: Exception while rendering object of type ["+obj.GetType().FullName+"]", ex);
134
 
 
135
 
                                                // return default message
136
 
                                                string objectTypeName = "";
137
 
                                                if (obj != null && obj.GetType() != null)
138
 
                                                {
139
 
                                                        objectTypeName = obj.GetType().FullName;
140
 
                                                }
141
 
 
142
 
                                                writer.Write("<log4net.Error>Exception rendering object type ["+objectTypeName+"]");
143
 
                                                if (ex != null)
144
 
                                                {
145
 
                                                        string exceptionText = null;
146
 
 
147
 
                                                        try
148
 
                                                        {
149
 
                                                                exceptionText = ex.ToString();
150
 
                                                        }
151
 
                                                        catch
152
 
                                                        {
153
 
                                                                // Ignore exception
154
 
                                                        }
155
 
 
156
 
                                                        writer.Write("<stackTrace>" + exceptionText + "</stackTrace>");
157
 
                                                }
158
 
                                                writer.Write("</log4net.Error>");
159
 
                                        }
160
 
                                }
161
 
                        }
162
 
                }
163
 
 
164
 
                /// <summary>
165
 
                /// Gets the renderer for the specified object type
166
 
                /// </summary>
167
 
                /// <param name="obj">the object to lookup the renderer for</param>
168
 
                /// <returns>the renderer for <paramref name="obj"/></returns>
169
 
                /// <remarks>
170
 
                /// <param>
171
 
                /// Gets the renderer for the specified object type.
172
 
                /// </param>
173
 
                /// <param>
174
 
                /// Syntactic sugar method that calls <see cref="Get(Type)"/> 
175
 
                /// with the type of the object parameter.
176
 
                /// </param>
177
 
                /// </remarks>
178
 
                public IObjectRenderer Get(Object obj) 
179
 
                {
180
 
                        if (obj == null) 
181
 
                        {
182
 
                                return null;
183
 
                        }
184
 
                        else
185
 
                        {
186
 
                                return Get(obj.GetType());
187
 
                        }
188
 
                }
189
 
  
190
 
                /// <summary>
191
 
                /// Gets the renderer for the specified type
192
 
                /// </summary>
193
 
                /// <param name="type">the type to lookup the renderer for</param>
194
 
                /// <returns>the renderer for the specified type</returns>
195
 
                /// <remarks>
196
 
                /// <para>
197
 
                /// Returns the renderer for the specified type.
198
 
                /// If no specific renderer has been defined the
199
 
                /// <see cref="DefaultRenderer"/> will be returned.
200
 
                /// </para>
201
 
                /// </remarks>
202
 
                public IObjectRenderer Get(Type type) 
203
 
                {
204
 
                        if (type == null)
205
 
                        {
206
 
                                throw new ArgumentNullException("type");
207
 
                        }
208
 
 
209
 
                        IObjectRenderer result = null;
210
 
 
211
 
                        // Check cache
212
 
                        result = (IObjectRenderer)m_cache[type];
213
 
 
214
 
                        if (result == null)
215
 
                        {
216
 
                                for(Type cur = type; cur != null; cur = cur.BaseType)
217
 
                                {
218
 
                                        // Search the type's interfaces
219
 
                                        result = SearchTypeAndInterfaces(cur);
220
 
                                        if (result != null)
221
 
                                        {
222
 
                                                break;
223
 
                                        }
224
 
                                }
225
 
 
226
 
                                // if not set then use the default renderer
227
 
                                if (result == null)
228
 
                                {
229
 
                                        result = s_defaultRenderer;
230
 
                                }
231
 
 
232
 
                                // Add to cache
233
 
                                m_cache[type] = result;
234
 
                        }
235
 
 
236
 
                        return result;
237
 
                }  
238
 
 
239
 
                /// <summary>
240
 
                /// Internal function to recursively search interfaces
241
 
                /// </summary>
242
 
                /// <param name="type">the type to lookup the renderer for</param>
243
 
                /// <returns>the renderer for the specified type</returns>
244
 
                private IObjectRenderer SearchTypeAndInterfaces(Type type) 
245
 
                {
246
 
                        IObjectRenderer r = (IObjectRenderer)m_map[type];
247
 
                        if (r != null) 
248
 
                        {
249
 
                                return r;
250
 
                        } 
251
 
                        else 
252
 
                        {
253
 
                                foreach(Type t in type.GetInterfaces())
254
 
                                {
255
 
                                        r = SearchTypeAndInterfaces(t);
256
 
                                        if (r != null)
257
 
                                        {
258
 
                                                return r; 
259
 
                                        }
260
 
                                }
261
 
                        }
262
 
                        return null;
263
 
                }
264
 
 
265
 
                /// <summary>
266
 
                /// Get the default renderer instance
267
 
                /// </summary>
268
 
                /// <value>the default renderer</value>
269
 
                /// <remarks>
270
 
                /// <para>
271
 
                /// Get the default renderer
272
 
                /// </para>
273
 
                /// </remarks>
274
 
                public IObjectRenderer DefaultRenderer
275
 
                {
276
 
                        get { return s_defaultRenderer; }
277
 
                }
278
 
 
279
 
                /// <summary>
280
 
                /// Clear the map of renderers
281
 
                /// </summary>
282
 
                /// <remarks>
283
 
                /// <para>
284
 
                /// Clear the custom renderers defined by using
285
 
                /// <see cref="Put"/>. The <see cref="DefaultRenderer"/>
286
 
                /// cannot be removed.
287
 
                /// </para>
288
 
                /// </remarks>
289
 
                public void Clear() 
290
 
                {
291
 
                        m_map.Clear();
292
 
                        m_cache.Clear();
293
 
                }
294
 
 
295
 
                /// <summary>
296
 
                /// Register an <see cref="IObjectRenderer"/> for <paramref name="typeToRender"/>. 
297
 
                /// </summary>
298
 
                /// <param name="typeToRender">the type that will be rendered by <paramref name="renderer"/></param>
299
 
                /// <param name="renderer">the renderer for <paramref name="typeToRender"/></param>
300
 
                /// <remarks>
301
 
                /// <para>
302
 
                /// Register an object renderer for a specific source type.
303
 
                /// This renderer will be returned from a call to <see cref="Get(Type)"/>
304
 
                /// specifying the same <paramref name="typeToRender"/> as an argument.
305
 
                /// </para>
306
 
                /// </remarks>
307
 
                public void Put(Type typeToRender, IObjectRenderer renderer) 
308
 
                {
309
 
                        m_cache.Clear();
310
 
 
311
 
                        if (typeToRender == null)
312
 
                        {
313
 
                                throw new ArgumentNullException("typeToRender");
314
 
                        }
315
 
                        if (renderer == null)
316
 
                        {
317
 
                                throw new ArgumentNullException("renderer");
318
 
                        }
319
 
 
320
 
                        m_map[typeToRender] = renderer;
321
 
                }       
322
 
        }
323
 
}