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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

/// <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 abstract class AbstractHandler : WoxHandler
    {
        public static readonly string[] EMPTY_STRING_ARRAY = new string[0];

        #region WoxHandler Members

        abstract public Type GetNativeTypeFor( WoxHandlers pWoxHandlers, string pWoxType );

        abstract public string GetWoxTypeFor( WoxHandlers pWoxHandlers, Type pType, FieldInfo pField, bool pJustType );

        abstract public string[] GetForReadObjectWoxTypeNames();

        abstract public bool WillReadObjectForWoxType( string pWoxType );

        abstract public object ReadObject( WoxHandlers pWoxHandlers, WoxReader pWoxReader, XMLreader pReader );

        abstract public void WriteObject( WoxHandlers pWoxHandlers, WoxWriter pWoxWriter, object pObject, FieldInfo pField, XMLwriter pWriter,
                                          bool pJustType );

        public virtual string[] GetForParseFieldValueWoxTypeNames()
        {
            return EMPTY_STRING_ARRAY;
        }

        public virtual bool WillParseFieldValueForWoxType( string pWoxType )
        {
            return false;
        }

        public virtual object ParseFieldValue( string pFieldValue )
        {
            throw new ApplicationException( "Parse Field Value not supported by " + GetType().Name );
        }

        public virtual string CreateFieldValue( object pFieldValue )
        {
            return null;
        }

        public virtual bool WillFieldValueNeverHaveAnySpaces()
        {
            return false;
        }

        public virtual object CreateArrayFor( Type pFieldElementType, string[] pValues )
        {
            throw new ApplicationException( "Create Array For not supported by " + GetType().Name );
        }

        public virtual object GetNonNullDefaultValue()
        {
            return null;
        }

        public virtual bool SupportsJustTypeWriting()
        {
            return false;
        }

        #endregion

        protected void Consume( XMLreader pReader )
        {
            while ( pReader.ReadStartElement() )
            {
                // Consume Subtree
            }
        }

        protected FieldInfo GetField( WoxHandlers pWoxHandlers, FieldInfo[] pFields, String pName )
        {
            foreach ( FieldInfo zField in pFields )
            {
                if ( pWoxHandlers.CustomWoxFieldNameNormalizer( zField.Name ).Equals( pName ) )
                {
                    return zField;
                }
            }
            return null;
        }

        protected static FieldInfo[] GetFields( Type pType )
        {
            IList<FieldInfo> holder = new List<FieldInfo>();
            AccumulateFields( pType, holder );
            return holder.ToArray();
        }

        protected static void AccumulateFields( Type pType, IList<FieldInfo> pFieldInfos )
        {
            while ( pType != typeof (Object) )
            {
                FieldInfo[] fields = pType.GetFields( BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic );
                foreach ( FieldInfo zField in fields )
                {
                    // added SLewis skip fields marked as not serialized
                    if ( !zField.IsNotSerialized && !zField.IsStatic )
                    {
                        pFieldInfos.Add( zField );
                    }
                }
                pType = pType.BaseType;
            }
        }

        protected virtual void ProcessFields( WoxHandlers pWoxHandlers, WoxReader pWoxReader, XMLreader pReader, Type pType, object pInstance )
        {
            FieldInfo[] zFields = GetFields( pType );

            while ( pReader.ReadStartElement() )
            {
                XMLreader zSubtree = pReader.CreateSubtreeReader();
                zSubtree.ReadStartElement(); //position the cursor to our element

                String zFieldName = pReader.GetAttribute( Serial.NAME );

                try
                {
                    FieldInfo zField = GetField( pWoxHandlers, zFields, zFieldName );
                    if ( zField == null )
                    {
                        throw new ApplicationException( "No Field" );
                    }
                    FieldRead( pWoxHandlers, pWoxReader, pReader, pType, zFieldName, zField, pInstance );
                }
                catch ( Exception e )
                {
                    throw new ApplicationException(
                        "ProcessingFields for (" + pType + "." + zFieldName + " on " + pInstance + ") Read Exception is: " + e.Message, e );
                }
                finally
                {
                    Consume( zSubtree );
                }
            }
        }

        protected virtual void ProcessFields( WoxHandlers pWoxHandlers, WoxWriter pWoxWriter, XMLwriter pWriter, bool pJustType, Type pType,
                                              object pInstance )
        {
            FieldInfo[] zFields = GetFields( pType );

            foreach ( FieldInfo zField in zFields )
            {
                string zFieldName = zField.Name;
                try
                {
                    // zField.setAccessible(true);
                    object zFieldValue = zField.GetValue( pInstance );
                    if ( pJustType || (zFieldValue != null) ) // do not send null fields
                    {
                        FieldWrite( pWoxHandlers, pWoxWriter, pWriter, pJustType, pType, zFieldName, zField, pInstance, zFieldValue );
                    }
                }
                catch ( Exception e )
                {
                    throw new ApplicationException(
                        "ProcessingFields for (" + pType + "." + zFieldName + " on " + pInstance + ") Write Exception is: " + e.Message, e );
                    // Shouldn't we at least comment on what went wrong: pWriter.addContent(new Comment(e.toString()));
                }
            }
        }

        protected virtual void FieldWrite( WoxHandlers pWoxHandlers, WoxWriter pWoxWriter, XMLwriter pWriter, bool pJustType, Type pObjectType,
                                           string pFieldName, FieldInfo pField, object pObjectInstance, object pFieldValue )
        {
            Type zType = pField.FieldType;

            WoxHandler zHandler = pWoxHandlers.GetNativeHandler( zType );

            if ( !pJustType )
            {
                object zDefaultValue = zHandler.GetNonNullDefaultValue();
                if ( (zDefaultValue != null) && zDefaultValue.Equals( pFieldValue ) )
                {
                    return;
                }
            }

            pWriter.WriteStartElement( Serial.FIELD );
            pWriter.WriteAttributeString( Serial.NAME, pWoxHandlers.CustomWoxFieldNameNormalizer( pFieldName ) );

            if ( pJustType )
            {
                pWriter.WriteAttributeString( Serial.TYPE, zHandler.GetWoxTypeFor( pWoxHandlers, zType, pField, pJustType ) );
                if ( typeof (Array).IsAssignableFrom( zType ) ) // Special Case for Arrays
                {
                    Type zElementType = zType.GetElementType();
                    String zElementWoxType = CommonWoxType( pWoxHandlers, zElementType, pField, pJustType );
                    pWriter.WriteAttributeString( Serial.ELEMENT_TYPE, zElementWoxType );
                }
            }
            else
            {
                string zValue = zType.IsEnum ? pFieldValue.ToString() : zHandler.CreateFieldValue( pFieldValue );

                if ( zValue != null )
                {
                    pWriter.WriteAttributeString( Serial.TYPE, zHandler.GetWoxTypeFor( pWoxHandlers, zType, pField, pJustType ) );
                    // Since we don't write nulls, and a "" will read as null, then we should assume a read of null is a ""
                    pWriter.WriteAttributeString( Serial.VALUE, zValue );
                }
                else
                {
                    pWoxWriter.Write( pFieldValue, pField, pWriter, false );
                }
            }
            pWriter.WriteEndElement();
        }

        protected virtual void FieldRead( WoxHandlers pWoxHandlers, WoxReader pWoxReader, XMLreader pReader, Type pObjectType, string pFieldName,
                                          FieldInfo pField, object pObjectInstance )
        {
            object zValue;
            string zWoxType = pReader.GetAttribute( Serial.TYPE );
            if ( zWoxType == null )
            {
                // No TYPE == Sub Object
                zValue = pWoxReader.Read( AdvanceToFirstChild( pReader ) );
            }
            else
            {
                Type zType = pField.FieldType;
                // Since we don't write nulls, and a "" will read as null, then we should assume a read of null is a ""
                string zFieldValue = pReader.GetAttribute( Serial.VALUE ) ?? "";
                if ( zType.IsEnum )
                {
                    zValue = Enum.Parse( zType, zFieldValue );
                }
                else
                {
                    WoxHandler zHandler = pWoxHandlers.GetWoxFieldHandler( zWoxType );
                    zValue = zHandler.ParseFieldValue( zFieldValue );
                }
            }
            pField.SetValue( pObjectInstance, zValue );
        }

        protected static XMLreader AdvanceToFirstChild( XMLreader pReader )
        {
            if ( pReader.ReadStartElement() )
            {
                return pReader;
            }
            throw new ApplicationException( "Expected Child Element NOT Found" );
        }

        protected ArrayList ReadObjects( WoxReader pWoxReader, XMLreader pSubtree )
        {
            ArrayList zList = new ArrayList();
            while ( pSubtree.ReadStartElement() )
            {
                Object zChild = pWoxReader.Read( pSubtree );
                zList.Add( zChild );
            }
            return zList;
        }

        protected static string GenericsBaseTypeFor( string pBaseType, string pLabelFrom, string pStartsWithFrom, string pLabelTo,
                                                     string pStartsWithTo )
        {
            if ( pStartsWithFrom.Equals( pBaseType ) )
            {
                return pStartsWithTo;
            }
            throw new ApplicationException( "No " + pLabelTo + " Base Type for " + pLabelFrom + " Base Type of: " + pBaseType );
        }

        protected static string CommonWoxType( WoxHandlers pWoxHandlers, Type pNativeType, FieldInfo pField, bool pJustType )
        {
            if ( pNativeType.IsArray )
            {
                return CommonWoxType( pWoxHandlers, pNativeType.GetElementType(), pField, pJustType ) + "[]";
            }

            if ( !pNativeType.IsGenericType )
            {
                WoxHandler zHandler = pWoxHandlers.GetNativeHandler( pNativeType );

                if ( zHandler is WoxDefaultHandler )
                {
                    if ( pNativeType.Equals( typeof (object) ) )
                    {
                        return Serial.WOX_TYPE_OBJECT;
                    }
                    // Unless we re-map the type, ASSUME that the Wox Type is actually the Native (CSharp) Type (Namespace and all)!
                    return pWoxHandlers.CustomNativeTypeToWoxType( pNativeType.ToString() );
                }

                return zHandler.GetWoxTypeFor( pWoxHandlers, pNativeType, pField, pJustType );
            }

            var zGenericsHandler = (WoxGenericsHandler) pWoxHandlers.GetNativeHandler( pNativeType );

            string zBaseName = "";
            string ns = pNativeType.Namespace;
            if ( !String.IsNullOrEmpty( ns ) )
            {
                zBaseName = ns + ".";
            }
            string name = pNativeType.Name;
            zBaseName += name.Substring( 0, name.IndexOf( "`" ) );

            var sb = new StringBuilder( zGenericsHandler.GetGenericsWoxBaseTypeFor( pWoxHandlers, zBaseName ) );

            sb.Append( '<' );
            Type[] arguments = pNativeType.GetGenericArguments();
            sb.Append( CommonWoxType( pWoxHandlers, arguments[0], pField, pJustType ) );
            for ( int i = 1; i < arguments.Length; i++ )
            {
                sb.Append( ',' ).Append( CommonWoxType( pWoxHandlers, arguments[i], pField, pJustType ) );
            }
            sb.Append( '>' );
            return sb.ToString();
        }

        protected static Type CommonNativeType( WoxHandlers pWoxHandlers, string pWoxType )
        {
            if ( pWoxType.EndsWith( "[]" ) )
            {
                string zWoxType = pWoxType.Substring( 0, pWoxType.Length - 2 );
                Type zType = CommonNativeType( pWoxHandlers, zWoxType );
                if ( zType == null )
                {
                    throw new ApplicationException( "No (Array) Native Type for Wox Type: " + zWoxType );
                }
                string zNativeArrayTypeAsString = zType + "[]";
                return Util.ResolveType( zNativeArrayTypeAsString );
            }

            int zAt = pWoxType.IndexOf( '<' );
            bool zEndsWithIt = pWoxType.EndsWith( ">" );
            if ( !zEndsWithIt && (zAt == -1) )
            {
                WoxHandler zHandler = pWoxHandlers.GetWoxHandler( pWoxType );

                if ( zHandler is WoxDefaultHandler )
                {
                    if ( Serial.WOX_TYPE_OBJECT.Equals( pWoxType ) )
                    {
                        return typeof (object);
                    }
                    // Unless we re-map the type, ASSUME that the Wox Type is actually the Native (CSharp) Type (Namespace and all)!
                    return Util.ResolveType( pWoxHandlers.CustomWoxTypeToNativeType( pWoxType ) );
                }

                return zHandler.GetNativeTypeFor( pWoxHandlers, pWoxType );
            }

            if ( !zEndsWithIt || (zAt == -1) )
            {
                throw new ApplicationException( "Bad Generics Form: " + pWoxType );
            }

            var zGenericsHandler = (WoxGenericsHandler) pWoxHandlers.GetWoxHandler( pWoxType );

            String zBaseTypeName = zGenericsHandler.GetGenericsNativeBaseTypeFor( pWoxHandlers, pWoxType.Substring( 0, zAt ) );

            String[] zTypeNames = SplitGenericTypes( pWoxType.Substring( zAt + 1, pWoxType.Length - (zAt + 2) ) );

            if ( zTypeNames == null )
            {
                throw new ApplicationException( "Bad Generics Form: " + pWoxType );
            }

            zBaseTypeName += "`" + zTypeNames.Length;

            var zGenericTypes = new Type[zTypeNames.Length];
            for ( int i = 0; i < zTypeNames.Length; i++ )
            {
                string zTypeName = zTypeNames[i];

                Type zType = CommonNativeType( pWoxHandlers, zTypeName );
                if ( zType == null )
                {
                    throw new ApplicationException( "Unable to locate Native Generic Type for: " + zTypeName );
                }
                zGenericTypes[i] = zType;
            }

            Type baseType = Util.ResolveType( zBaseTypeName );

            return baseType.MakeGenericType( zGenericTypes );
        }

        private static string[] SplitGenericTypes( string pTypeList )
        {
            int zAt = pTypeList.IndexOf( '>' ); // No <...> == Happy Case!
            if ( zAt == -1 )
            {
                return pTypeList.Split( ',' );
            }
            var zSubstitutions = new Dictionary<char, string>();
            for ( int zFrom; zAt != -1; zAt = pTypeList.IndexOf( '>' ) )
            {
                if ( -1 == (zFrom = FindStart( pTypeList, zAt )) )
                {
                    return null;
                }
                var zSubChar = (char) (zSubstitutions.Count + 1);
                if ( pTypeList.IndexOf( zSubChar ) != -1 )
                {
                    return null;
                }
                zSubstitutions[zSubChar] = pTypeList.Substring( zFrom, (zAt - zFrom) + 1 );
                pTypeList = pTypeList.Substring( 0, zFrom ) + zSubChar + pTypeList.Substring( zAt + 1 );
            }
            string[] zStrings = pTypeList.Split( ',' );
            for ( int i = 0; i < zStrings.Length; i++ )
            {
                string zStr = zStrings[i];
                for ( int zSubIndex = zSubstitutions.Count; zSubIndex > 0; zSubIndex-- )
                {
                    var zSubChar = (char) zSubIndex;
                    if ( -1 != (zAt = zStr.IndexOf( zSubChar )) )
                    {
                        zStr = zStr.Substring( 0, zAt ) + zSubstitutions[zSubChar] + zStr.Substring( zAt + 1 );
                    }
                }
                zStrings[i] = zStr;
            }
            return zStrings;
        }

        private static int FindStart( string pText, int pAt )
        {
            while ( --pAt >= 0 )
            {
                if ( pText[pAt] == '<' )
                {
                    return pAt;
                }
            }
            return -1;
        }
    }
}

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

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