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
using System;
using System.Collections;
using System.Reflection;

/// <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 ListHandler : AbstractHandler, WoxGenericsHandler
    {
        public static readonly ListHandler INSTANCE = new ListHandler();

        public const String WOX_TYPE = "list";

        private const string GENERIC_WOX_TYPE_NAME_STARTS_WITH = WOX_TYPE + "<";

        private const string GENERIC_NATIVE_TYPE_NAME_STARTS_WITH = "System.Collections.Generic.List";

        private static readonly string NON_GENERIC_NATIVE_TYPE_NAME = typeof (ArrayList).ToString();

        protected ListHandler()
        {
        }

        #region WoxStartsWithHandler Members

        public bool IsFor( string pTypeName )
        {
            return NON_GENERIC_NATIVE_TYPE_NAME.Equals( pTypeName ) || //
                   pTypeName.StartsWith( GENERIC_NATIVE_TYPE_NAME_STARTS_WITH );
        }

        public string GetGenericsNativeBaseTypeFor( WoxHandlers pWoxHandlers, string pWoxBaseType )
        {
            return GenericsBaseTypeFor( pWoxBaseType, "Wox", WOX_TYPE, "Native", GENERIC_NATIVE_TYPE_NAME_STARTS_WITH );
        }

        public string GetGenericsWoxBaseTypeFor( WoxHandlers pWoxHandlers, string pNativeBaseType )
        {
            return GenericsBaseTypeFor( pNativeBaseType, "Native", GENERIC_NATIVE_TYPE_NAME_STARTS_WITH, "Wox", WOX_TYPE );
        }

        public override string[] GetForReadObjectWoxTypeNames()
        {
            return null; // Ask Us
        }

        public override Type GetNativeTypeFor( WoxHandlers pWoxHandlers, string pWoxType )
        {
            if ( WOX_TYPE.Equals( pWoxType ) )
            {
                return typeof (ArrayList);
            }
            if ( pWoxType.StartsWith( GENERIC_WOX_TYPE_NAME_STARTS_WITH ) )
            {
                Type zType = CommonNativeType( pWoxHandlers, pWoxType );
                if ( zType != null )
                {
                    return zType;
                }
            }
            throw new ApplicationException( "No Native Type for Wox Type: " + pWoxType );
        }

        public override string GetWoxTypeFor( WoxHandlers pWoxHandlers, Type pType, FieldInfo pField, bool pJustType )
        {
            if ( typeof (ArrayList).Equals( pType ) )
            {
                return WOX_TYPE;
            }
            if ( pType.ToString().StartsWith( GENERIC_NATIVE_TYPE_NAME_STARTS_WITH ) )
            {
                return CommonWoxType( pWoxHandlers, pType, pField, pJustType );
            }
            throw new ApplicationException( "No Wox Type for Native Type: " + pType );
        }

        public override bool WillReadObjectForWoxType( string pWoxType )
        {
            return WOX_TYPE.Equals( pWoxType ) || pWoxType.StartsWith( GENERIC_WOX_TYPE_NAME_STARTS_WITH );
        }

        public override object ReadObject( WoxHandlers pWoxHandlers, WoxReader pWoxReader, XMLreader pReader )
        {
            XMLreader zSubtree = pReader.CreateSubtreeReader();
            zSubtree.ReadStartElement(); //position the cursor to our element

            try
            {
                string zWoxTypeName = zSubtree.GetAttribute( Serial.TYPE );

                ArrayList zList = ReadObjects( pWoxReader, zSubtree );

                Type zNativeType = GetNativeTypeFor( pWoxHandlers, zWoxTypeName );

                if ( typeof (ArrayList).Equals( zNativeType ) )
                {
                    return zList;
                }

                object zGenericList = Util.CreateInstance( zNativeType, zWoxTypeName );

                MethodInfo zMethodInfo = zGenericList.GetType().GetMethod( "Add" );
                //populate the List with the elements
                for ( int i = 0; i < zList.Count; i++ )
                {
                    Object[] args = {zList[i]};
                    zMethodInfo.Invoke( zGenericList, args );
                }
                return zGenericList;
            }
            catch ( Exception e )
            {
                throw new ApplicationException( "List Handler Read Object Exception is: " + e.Message, e );
            }
            finally
            {
                Consume( zSubtree );
            }
        }

        public override void WriteObject( WoxHandlers pWoxHandlers, WoxWriter pWoxWriter, object pObject, FieldInfo pField, XMLwriter pWriter,
                                          bool pJustType )
        {
            Type zType = pObject.GetType();
            string zWoxTypeName = CommonWoxType( pWoxHandlers, zType, pField, pJustType );

            pWriter.WriteStartElement( Serial.OBJECT );
            pWriter.WriteAttributeString( Serial.TYPE, zWoxTypeName );

            //we already know it is either a Generic List or an ArrayList, so, we simply get the underlying array
            MethodInfo zMethodInfo = zType.GetMethod( "ToArray", new Type[0] );
            var zArray = (Array) zMethodInfo.Invoke( pObject, null ); // null == NO Params

            int zLength = zArray.GetLength( 0 ); // 0 == 1st Dimension!

            if ( zLength != 0 )
            {
                pWriter.WriteAttributeString( Serial.ID, pWoxWriter.CreateIDfor( pObject ) );

                foreach ( object zEntry in zArray )
                {
                    pWoxWriter.Write( zEntry, pField, pWriter, pJustType );
                }
            }
            pWriter.WriteEndElement();
        }

        #endregion
    }
}

Commits for WOX2/trunk/CSharp/src/wox/serial/ListHandler.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