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

import java.lang.reflect.*;

/**
 * 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 AbstractWrappableStringableHandler extends AbstractStringableHandler
{
    protected final Class mBaseNativeType;
    protected final String mBaseWoxType;
    protected final Class mWrappedNativeType;
    protected final String mWrappedWoxType;

    protected AbstractWrappableStringableHandler( Class pBaseNativeType, String pBaseWoxType, Class pWrappedNativeType )
    {
        mBaseNativeType = pBaseNativeType;
        mBaseWoxType = pBaseWoxType;
        mWrappedNativeType = pWrappedNativeType;
        mWrappedWoxType = pBaseWoxType + "Wrapper";
    }

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

    @Override
    public Class getNativeTypeFor( WoxHandlers pWoxHandlers, String pWoxType )
    {
        if ( mBaseWoxType.equals( pWoxType ) )
        {
            return mBaseNativeType;
        }
        if ( mWrappedWoxType.equals( pWoxType ) )
        {
            return mWrappedNativeType;
        }
        throw new ApplicationException( "No Native Type for Wox Type: " + pWoxType );
    }

    @Override
    public String getWoxTypeFor( WoxHandlers pWoxHandlers, Class pType, Field pField, boolean pJustType )
    {
        if ( mWrappedNativeType.equals( pType ) )
        {
            return pJustType ? mWrappedWoxType : mBaseWoxType;
        }
        if ( mBaseNativeType.equals( pType ) )
        {
            return mBaseWoxType;
        }
        throw new ApplicationException( "No Wox Type for Native Type: " + pType );
    }

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

    @Override
    public boolean isFor( Class pType )
    {
        return mWrappedNativeType.equals( pType ) || mBaseNativeType.equals( pType );
    }

    @Override
    public String[] getForParseFieldValueWoxTypeNames()
    {
        return new String[]{mBaseWoxType, mWrappedWoxType};
    }

    @Override
    public boolean willParseFieldValueForWoxType( String pWoxType )
    {
        return Util.areEqual( mBaseWoxType, pWoxType ) || Util.areEqual( mWrappedWoxType, pWoxType );
    }

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

    @Override
    public Object createArrayFor( Class pFieldElementType, String[] pValues )
    {
        Class zComponentType;
        if ( pFieldElementType == null )
        {
            zComponentType = anyNullIndicators( pValues ) ? mWrappedNativeType : mBaseNativeType;
        }
        else if ( mBaseNativeType.equals( pFieldElementType ) )
        {
            zComponentType = mBaseNativeType;
        }
        else if ( mWrappedNativeType.equals( pFieldElementType ) )
        {
            zComponentType = mWrappedNativeType;
        }
        else
        {
            throw new ApplicationException( "Not Supported Field Native Type: " + pFieldElementType );
        }
        Object zArray = Array.newInstance( zComponentType, pValues.length );
        for ( int i = 0; i < pValues.length; i++ )
        {
            Array.set( zArray, i, createInstance( pValues[i] ) );
        }
        return zArray;
    }

    private static boolean anyNullIndicators( String[] pValues )
    {
        for ( String zValue : pValues )
        {
            if ( Serial.NULL.equals( zValue ) )
            {
                return true;
            }
        }
        return false;
    }

    @Override
    protected Object createInstance( String pValue )
    {
        if ( Util.isNullOrEmpty( pValue ) || Serial.NULL.equals( pValue ) )
        {
            return null;
        }
        return nonNullValueParse( pValue );
    }

    @Override
    protected String stringify( Object pObject )
    {
        return (pObject == null) ? Serial.NULL : nonNullValueToString( pObject );
    }

    abstract protected Object nonNullValueParse( String pValue );

    protected String nonNullValueToString( Object pObject )
    {
        return pObject.toString();
    }
}

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

Diff revisions: vs.
Revision Author Commited Message
15 Diff Diff GeorgeS picture GeorgeS Fri 19 Feb, 2010 22:29:49 +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