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
package wox.serial;

import java.lang.reflect.*;
import org.litesoft.core.typeutils.Objects;
import java.util.*;

/**
 * Version: 1.0
 * Author: Carlos R. Jaimez Gonzalez
 * Author: Simon M. Lucas
 * Site: http://woxserializer.sourceforge.net/
 *
 * Version: 1.5
 * Author: Steven M Lewis
 *
 * Version: 2.0
 * Author: George Smith
 * SVN: http://svn.xp-dev.com/svn/WOX2/
 * Note: XML form for vs 2 is more compact and therefor incompatible with vs 1
 */

public abstract class AbstractHandler implements WoxHandler
{
    public static final String[] EMPTY_STRING_ARRAY = new String[0];

    @Override
    public String[] getForParseFieldValueWoxTypeNames()
    {
        return EMPTY_STRING_ARRAY;
    }

    @Override
    public boolean willParseFieldValueForWoxType( String pWoxType )
    {
        return false;
    }

    @Override
    public Object parseFieldValue( String pFieldValue )
    {
        throw new ApplicationException( "Parse Field Value not supported by " + getClass().getSimpleName() );
    }

    @Override
    public String createFieldValue( Object pFieldValue )
    {
        return null;
    }

    @Override
    public boolean willFieldValueNeverHaveAnySpaces()
    {
        return false;
    }

    @Override
    public Object createArrayFor( Class pFieldElementType, String[] pValues )
    {
        throw new ApplicationException( "Create Array For not supported by " + getClass().getSimpleName() );
    }

    @Override
    public Object getNonNullDefaultValue()
    {
        return null;
    }

    @Override
    public boolean supportsJustTypeWriting()
    {
        return false;
    }

    protected void consume( XMLreader pReader )
    {
        while ( pReader.readStartElement() )
        {
            // consume Subtree
        }
    }

    protected Field getField( WoxHandlers pWoxHandlers, Field[] pFields, String pName )
    {
        for ( Field zField : pFields )
        {
            if ( pWoxHandlers.customWoxFieldNameNormalizer( zField.getName() ).equals( pName ) )
            {
                return zField;
            }
        }
        return null;
    }

    protected static Field[] getFields( Class pType )
    {
        List<Field> holder = new ArrayList<Field>();
        accumulateFields( pType, holder );
        return holder.toArray( new Field[holder.size()] );
    }

    protected static void accumulateFields( Class pType, List<Field> pFields )
    {
        while ( pType != Object.class )
        {
            Field[] fields = pType.getDeclaredFields(); // BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic );
            for ( Field zField : fields )
            {
                // added SLewis skip fields marked as not serialized
                int zModifiers = zField.getModifiers();
                if ( !Modifier.isTransient( zModifiers ) && !Modifier.isStatic( zModifiers ) )
                {
                    pFields.add( zField );
                }
            }
            pType = pType.getSuperclass();
        }
    }

    protected void processFields( WoxHandlers pWoxHandlers, WoxReader pWoxReader, XMLreader pReader, Class pType, Object pInstance )
    {
        Field[] 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
            {
                Field 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.getMessage(), e );
            }
            finally
            {
                consume( zSubtree );
            }
        }
    }

    protected void processFields( WoxHandlers pWoxHandlers, WoxWriter pWoxWriter, XMLwriter pWriter, boolean pJustType, Class pType, Object pInstance )
    {
        Field[] zFields = getFields( pType );

        for ( Field zField : zFields )
        {
            String zFieldName = zField.getName();
            if ( !zFieldName.startsWith( "this$" ) )
            {
                try
                {
                    zField.setAccessible( true );
                    Object zFieldValue = zField.get( 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.getMessage(), e );
                    // Shouldn't we at least comment on what went wrong; pWriter.addContent(new Comment(e.toString()));
                }
            }
        }
    }

    @SuppressWarnings({"UnusedDeclaration"})
    protected void fieldWrite( WoxHandlers pWoxHandlers, WoxWriter pWoxWriter, XMLwriter pWriter, boolean pJustType, Class pObjectType, String pFieldName, Field pField, Object pObjectInstance, Object pFieldValue )
    {
        Class zType = pField.getType();

        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 ( zType.isArray() ) // Special Case for Arrays
            {
                Class zElementType = zType.getComponentType();
                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();
    }

    @SuppressWarnings({"UnusedDeclaration"})
    protected void fieldRead( WoxHandlers pWoxHandlers, WoxReader pWoxReader, XMLreader pReader, Class pObjectType, String pFieldName, Field pField, Object pObjectInstance )
    {
        Object zValue;
        String zWoxType = pReader.getAttribute( Serial.TYPE );
        if ( zWoxType == null )
        {
            // No TYPE == Sub Object
            zValue = pWoxReader.read( advanceToFirstChild( pReader ) );
        }
        else
        {
            Class zType = pField.getType();
            // 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 ( zFieldValue == null )
            {
                zFieldValue = "";
            }
            if ( zType.isEnum() )
            {
                //noinspection unchecked
                zValue = Enum.valueOf( zType, zFieldValue );
            }
            else
            {
                WoxHandler zHandler = pWoxHandlers.getWoxFieldHandler( zWoxType );
                zValue = zHandler.parseFieldValue( zFieldValue );
            }
        }
        try
        {
            pField.setAccessible( true );
            pField.set( pObjectInstance, zValue );
        }
        catch ( IllegalAccessException e )
        {
            throw new RuntimeException( e );
        }
    }

    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<Object> zList = new ArrayList<Object>();
        while ( pSubtree.readStartElement() )
        {
            Object zChild = pWoxReader.read( pSubtree );
            zList.add( zChild );
        }
        return zList;
    }

    protected static Type[] getGenericTypes( Field pField )
    {
        if ( pField != null )
        {
            Type zType = pField.getGenericType();
            if ( zType instanceof ParameterizedType )
            {
                return getGenericTypes( (ParameterizedType) zType );
            }
        }
        return null;
    }

    protected static Type[] getGenericTypes( ParameterizedType pType )
    {
        if ( pType != null )
        {
            Type[] zArguments = pType.getActualTypeArguments();
            if ( (zArguments != null) && (zArguments.length != 0) )
            {
                return zArguments;
            }
        }
        return null;
    }

    protected static String commonWoxType( WoxHandlers pWoxHandlers, Class pNativeType, Field pField, boolean pJustType )
    {
        return innerCommonWoxType( pWoxHandlers, pNativeType, getGenericTypes( pField ), pJustType );
    }

    private static String innerCommonWoxType( WoxHandlers pWoxHandlers, Class pNativeType, Type[] pGenericTypes, boolean pJustType )
    {
        if ( pNativeType.isArray() )
        {
            return innerCommonWoxType( pWoxHandlers, pNativeType.getComponentType(), pGenericTypes, pJustType ) + "[]";
        }

        if ( pGenericTypes == null )
        {
            WoxHandler zHandler = pWoxHandlers.getNativeHandler( pNativeType );

            if ( zHandler instanceof WoxDefaultHandler )
            {
                if ( pNativeType.equals( Object.class ) )
                {
                    return Serial.WOX_TYPE_OBJECT;
                }
                // Unless we re-map the type, ASSUME that the Wox Type is actually the Native (Java) Type (Package and all)!
                return pWoxHandlers.customNativeTypeToWoxType( pNativeType.getName() );
            }

            return zHandler.getWoxTypeFor( pWoxHandlers, pNativeType, null, pJustType );
        }

        StringBuilder zWoxType = new StringBuilder( innerCommonWoxType( pWoxHandlers, pNativeType, null, pJustType ) );

        char zPrepend = '<';

        for ( Type zArgument : pGenericTypes )
        {
            zWoxType.append( zPrepend );
            zPrepend = ',';
            if ( zArgument instanceof Class )
            {
                zWoxType.append( innerCommonWoxType( pWoxHandlers, (Class) zArgument, null, pJustType ) );
                continue;
            }
            if ( zArgument instanceof ParameterizedType )
            {
                ParameterizedType zType = (ParameterizedType) zArgument;
                Type zRawType = zType.getRawType();
                if ( zRawType instanceof Class )
                {
                    zWoxType.append( innerCommonWoxType( pWoxHandlers, (Class) zRawType, getGenericTypes( zType ), pJustType ) );
                    continue;
                }
                System.out.println( "PT: " + zArgument.getClass().getName() + ": " + zArgument );
                System.out.println( "  Raw: " + zRawType );
            }
            else
            {
                System.out.println( "!PT: " + zArgument.getClass().getName() + ": " + zArgument );
            }
            zWoxType.append( Serial.WOX_TYPE_OBJECT );
        }

        return zWoxType.append( '>' ).toString();
    }

    protected static Class commonNativeType( WoxHandlers pWoxHandlers, String pWoxType )
    {
        if ( pWoxType.endsWith( "[]" ) )
        {
            String zWoxType = pWoxType.substring( 0, pWoxType.length() - 2 );
            Class zType = commonNativeType( pWoxHandlers, zWoxType );
            if ( zType == null )
            {
                throw new NullPointerException( "No (Array) Native Type for Wox Type: " + zWoxType );
            }
            return Array.newInstance( zType, 0 ).getClass();
        }

        // Since Java "ignores" Generics at Runtime, we can simply strip them off!
        if ( pWoxType.endsWith( ">" ) )
        {
            int zAt = pWoxType.indexOf( '<' );

            if ( zAt == -1 )
            {
                throw new ApplicationException( "Bad Generics Form: " + pWoxType );
            }
            pWoxType = pWoxType.substring( 0, zAt );
        }

        WoxHandler zHandler = pWoxHandlers.getWoxHandler( pWoxType );

        if ( zHandler instanceof WoxDefaultHandler )
        {
            if ( Serial.WOX_TYPE_OBJECT.equals( pWoxType ) )
            {
                return Object.class;
            }
            // Unless we re-map the type, ASSUME that the Wox Type is actually the Native (Java) Type (Package and all)!
            return Util.resolveType( pWoxHandlers.customWoxTypeToNativeType( pWoxType ) );
        }

        return zHandler.getNativeTypeFor( pWoxHandlers, pWoxType );
    }
}

Commits for WOX2/trunk/Java/src/wox/serial/AbstractHandler.java

Diff revisions: vs.
Revision Author Commited Message
17 Diff Diff GeorgeS picture GeorgeS Mon 02 Jun, 2014 23:44:02 +0000

Extracting commonfoundation

15 Diff Diff GeorgeS picture GeorgeS Fri 19 Feb, 2010 22:29:49 +0000
7 Diff Diff GeorgeS picture GeorgeS Fri 05 Feb, 2010 19:14:48 +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
2 GeorgeS picture GeorgeS Thu 04 Feb, 2010 23:49:40 +0000