Subversion Repository Public Repository

WOX2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
using System;
using System.Collections.Generic;

/// <header>
/// <wox>
///     <version>1.0</version>
///     <author>Carlos R. Jaimez Gonzalez</author>
///     <author>Simon M. Lucas</author>
///     <site>http://woxserializer.sourceforge.net/</site>
/// </wox>
/// <wox>
///     <version>1.5</version>
///     <author>Steven M Lewis</author>
/// </wox>
/// <wox>
///     <version>2.0</version>
///     <author>George A Smith</author>
///     <svn>http://woxserializer.sourceforge.net/</svn>
///     <note>XML form for vs 2 is more compact and therefor incompatible with vs 1</note>
/// </wox>
/// </header>

namespace wox.serial
{
    public class WoxHandlers
    {
        private readonly Dictionary<string, WoxHandler> mWoxTypeReadObjectHandlers = new Dictionary<string, WoxHandler>();
        private readonly List<WoxHandler> mAskEachInTurnWoxTypeReadObjectHandlers = new List<WoxHandler>();

        private readonly Dictionary<string, WoxHandler> mWoxTypeParseFieldValueHandlers = new Dictionary<string, WoxHandler>();
        private readonly List<WoxHandler> mAskEachInTurnWoxTypeParseFieldValueHandlers = new List<WoxHandler>();

        // NativeType Handlers
        private readonly List<WoxIsAHandler> mIsAHandlers = new List<WoxIsAHandler>();
        private readonly List<WoxGenericsHandler> mGenericsHandlers = new List<WoxGenericsHandler>();
        private readonly Dictionary<string, WoxTypeHandler> mTypeHandlers = new Dictionary<string, WoxTypeHandler>();

        // Mappers
        private readonly List<WoxTypeMapper> mTypeMappers = new List<WoxTypeMapper>();
        private readonly List<WoxFieldNameMapper> mFieldNameMappers = new List<WoxFieldNameMapper>();

        public WoxHandlers()
        {
            // Note: As they are Inserted, they are actually checked from the Bottom Up!

            AddIsAHandler( DefaultObjectHandler.INSTANCE ); // Needs to be FIRST (so will be checked LAST)!

            AddIsAHandler( GuidHandler.INSTANCE );

            AddIsAHandler( ArrayHandler.INSTANCE );

            AddIsAHandler( WOXdateHandler.INSTANCE );

            AddIsAHandler( WOXcharHandler.INSTANCE );

            AddIsAHandler( WOXdecimalHandler.INSTANCE );

            AddIsAHandler( WOXfloatHandler.INSTANCE );
            AddIsAHandler( WOXdoubleHandler.INSTANCE );

            AddIsAHandler( WOXbyteHandler.INSTANCE );
            AddIsAHandler( WOXshortHandler.INSTANCE );
            AddIsAHandler( WOXintHandler.INSTANCE );
            AddIsAHandler( WOXlongHandler.INSTANCE );

            AddIsAHandler( WOXbooleanHandler.INSTANCE );

            AddIsAHandler( WOXstringHandler.INSTANCE );

            AddGenericsHandler( ListHandler.INSTANCE );

            AddGenericsHandler( MapHandler.INSTANCE );
        }

        public string CustomNativeTypeToWoxType( string pNativeType )
        {
            foreach ( WoxTypeMapper zMapper in mTypeMappers )
            {
                String zWoxType = zMapper.GetWoxTypeFor( pNativeType );
                if ( zWoxType != null )
                {
                    return zWoxType;
                }
            }
            // Assume no translation: WoxType == NativeType (Namespace/Package and all!)
            return pNativeType;
        }

        public string CustomWoxTypeToNativeType( string pWoxType )
        {
            foreach ( WoxTypeMapper zMapper in mTypeMappers )
            {
                String zNativeType = zMapper.GetNativeTypeFor( pWoxType );
                if ( zNativeType != null )
                {
                    return zNativeType;
                }
            }
            // Assume no translation: NativeType == WoxType (Namespace/Package and all!)
            return pWoxType;
        }

        public void AddTypeMapper( WoxTypeMapper pWoxMapper )
        {
            if ( pWoxMapper != null )
            {
                mTypeMappers.Add( pWoxMapper );
            }
        }

        public string CustomWoxFieldNameNormalizer( string pNativeFieldName )
        {
            foreach (WoxFieldNameMapper zMapper in mFieldNameMappers)
            {
                String zWoxFieldName = zMapper.GetWoxFieldNameFor( pNativeFieldName );
                if (zWoxFieldName != null)
                {
                    return zWoxFieldName;
                }
            }
            // Assume no translation: Native == Wox
            return pNativeFieldName;
        }

        public void AddWoxFieldNameMapper(WoxFieldNameMapper pWoxMapper)
        {
            if ( pWoxMapper != null )
            {
                mFieldNameMappers.Add( pWoxMapper );
            }
        }

        public void AddTypeHandler( WoxTypeHandler pWoxHandler )
        {
            if ( pWoxHandler != null )
            {
                AddWoxHandlerForWoxType( pWoxHandler );
                string[] zForTypeNames = pWoxHandler.GetForNativeTypeNames();
                if ( zForTypeNames != null )
                {
                    foreach ( string zTypeName in zForTypeNames )
                    {
                        if ( !String.IsNullOrEmpty( zTypeName ) )
                        {
                            mTypeHandlers[zTypeName] = pWoxHandler;
                        }
                    }
                }
            }
        }

        public void AddGenericsHandler( WoxGenericsHandler pWoxHandler )
        {
            if ( pWoxHandler != null )
            {
                AddWoxHandlerForWoxType( pWoxHandler );
                mGenericsHandlers.Insert( 0, pWoxHandler );
            }
        }

        public void AddIsAHandler( WoxIsAHandler pWoxHandler )
        {
            if ( pWoxHandler != null )
            {
                AddWoxHandlerForWoxType( pWoxHandler );
                mIsAHandlers.Insert( 0, pWoxHandler );
            }
        }

        private void AddWoxHandlerForWoxType( WoxHandler pWoxHandler )
        {
            string[] zForReadObjectWoxTypeNames = pWoxHandler.GetForReadObjectWoxTypeNames();
            if ( zForReadObjectWoxTypeNames == null )
            {
                mAskEachInTurnWoxTypeReadObjectHandlers.Insert( 0, pWoxHandler );
            }
            else
            {
                foreach ( string zTypeName in zForReadObjectWoxTypeNames )
                {
                    if ( !String.IsNullOrEmpty( zTypeName ) )
                    {
                        mWoxTypeReadObjectHandlers[zTypeName] = pWoxHandler;
                    }
                }
                if ( pWoxHandler is WoxTypeAskUsHandler )
                {
                    mAskEachInTurnWoxTypeReadObjectHandlers.Insert( 0, pWoxHandler );
                }
            }

            string[] zForParseFieldValueWoxTypeNames = pWoxHandler.GetForParseFieldValueWoxTypeNames();
            if ( zForParseFieldValueWoxTypeNames == null )
            {
                mAskEachInTurnWoxTypeParseFieldValueHandlers.Insert( 0, pWoxHandler );
            }
            else
            {
                foreach ( string zTypeName in zForParseFieldValueWoxTypeNames )
                {
                    if ( !String.IsNullOrEmpty( zTypeName ) )
                    {
                        mWoxTypeParseFieldValueHandlers[zTypeName] = pWoxHandler;
                    }
                }
                if ( pWoxHandler is WoxTypeAskUsHandler )
                {
                    mAskEachInTurnWoxTypeParseFieldValueHandlers.Insert( 0, pWoxHandler );
                }
            }
        }

        public WoxHandler GetNativeHandler( Type pNativeType )
        {
            if ( pNativeType != null )
            {
                string zTypename = pNativeType.ToString();
                WoxTypeHandler rv;
                if ( mTypeHandlers.TryGetValue( zTypename, out rv ) )
                {
                    return rv;
                }
                if ( mGenericsHandlers.Count != 0 )
                {
                    foreach ( WoxGenericsHandler zStartsWithHandler in mGenericsHandlers )
                    {
                        if ( zStartsWithHandler.IsFor( zTypename ) )
                        {
                            return zStartsWithHandler;
                        }
                    }
                }
                foreach ( WoxIsAHandler zIsAHandler in mIsAHandlers )
                {
                    if ( zIsAHandler.IsFor( pNativeType ) )
                    {
                        return zIsAHandler;
                    }
                }
            }
            return null;
        }

        public WoxHandler GetWoxHandler( String pWoxType )
        {
            if ( pWoxType != null )
            {
                WoxHandler rv;
                if ( mWoxTypeReadObjectHandlers.TryGetValue( pWoxType, out rv ) )
                {
                    return rv;
                }
                foreach ( WoxHandler zHandler in mAskEachInTurnWoxTypeReadObjectHandlers )
                {
                    if ( zHandler.WillReadObjectForWoxType( pWoxType ) )
                    {
                        return zHandler;
                    }
                }
            }
            return null;
        }

        public WoxHandler GetWoxFieldHandler( String pWoxType )
        {
            if ( pWoxType != null )
            {
                WoxHandler rv;
                if ( mWoxTypeParseFieldValueHandlers.TryGetValue( pWoxType, out rv ) )
                {
                    return rv;
                }
                foreach ( WoxHandler zHandler in mAskEachInTurnWoxTypeParseFieldValueHandlers )
                {
                    if ( zHandler.WillParseFieldValueForWoxType( pWoxType ) )
                    {
                        return zHandler;
                    }
                }
            }
            return null;
        }
    }
}

Commits for WOX2/trunk/CSharp/src/wox/serial/WoxHandlers.cs

Diff revisions: vs.
Revision Author Commited Message
14 Diff Diff GeorgeS picture GeorgeS Fri 19 Feb, 2010 19:15:03 +0000
3 GeorgeS picture GeorgeS Thu 04 Feb, 2010 23:53:47 +0000