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
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 class MapHandler extends AbstractHandler implements WoxIsAHandler
{
    public static final MapHandler INSTANCE = new MapHandler();

    public static final String WOX_TYPE = "map";

    private static final String GENERIC_WOX_TYPE_NAME_STARTS_WITH = WOX_TYPE + "<";

    protected MapHandler()
    {
    }

    @Override
    public boolean isFor( Class pType )
    {
        return Map.class.isAssignableFrom( pType );
    }

    @Override
    public String[] getForReadObjectWoxTypeNames()
    {
        return null; // Ask Us
    }

    @Override
    public Class getNativeTypeFor( WoxHandlers pWoxHandlers, String pWoxType )
    {
        if ( WOX_TYPE.equals( pWoxType ) )
        {
            return HashMap.class;
        }
        if ( pWoxType.startsWith( GENERIC_WOX_TYPE_NAME_STARTS_WITH ) )
        {
            Class zType = commonNativeType( pWoxHandlers, pWoxType );
            if ( zType != null )
            {
                return zType;
            }
        }
        throw new ApplicationException( "No Native Type for Wox Type: " + pWoxType );
    }

    @Override
    public String getWoxTypeFor( WoxHandlers pWoxHandlers, Class pType, Field pField, boolean pJustType )
    {
        if ( null != getGenericTypes( pField ) )
        {
            return commonWoxType( pWoxHandlers, pType, pField, pJustType );
        }
        if ( Map.class.isAssignableFrom( pType ) )
        {
            return WOX_TYPE;
        }
        throw new ApplicationException( "No Wox Type for Native Type: " + pType );
    }

    @Override
    public boolean willReadObjectForWoxType( String pWoxType )
    {
        return WOX_TYPE.equals( pWoxType ) || pWoxType.startsWith( GENERIC_WOX_TYPE_NAME_STARTS_WITH );
    }

    @Override
    public Object readObject( WoxHandlers pWoxHandlers, WoxReader pWoxReader, XMLreader pReader )
    {
        XMLreader zSubtree = pReader.createSubtreeReader();
        zSubtree.readStartElement(); //position the cursor to our element

        try
        {
            Map<Object, Object> zMap = new HashMap<Object, Object>();

            while ( zSubtree.readStartElement() )
            {
                // currently at the "entry"
                //position the cursor in the KEY object
                zSubtree.readStartElement();
                Object zKey = pWoxReader.read( zSubtree );

                //position the cursor in the VALUE object
                zSubtree.readStartElement();
                Object zValue = pWoxReader.read( zSubtree );

                //Add the key and the value
                zMap.put( zKey, zValue );
            }

            return zMap;
        }
        catch ( Exception e )
        {
            throw new ApplicationException( "Map Handler Read Object Exception is: " + e.getMessage(), e );
        }
        finally
        {
            consume( zSubtree );
        }
    }

    @Override
    public void writeObject( WoxHandlers pWoxHandlers, WoxWriter pWoxWriter, Object pObject, Field pField, XMLwriter pWriter, boolean pJustType )
    {
        Class zType = pObject.getClass();
        String zWoxTypeName = commonWoxType( pWoxHandlers, zType, pField, pJustType );

        pWriter.writeStartElement( Serial.OBJECT );
        pWriter.writeAttributeString( Serial.TYPE, zWoxTypeName );

        //we already know it is a Map, so, we can simply Enumerate over the entries.
        Map<Object, Object> zMap = Util.cast( pObject );

        if ( !zMap.isEmpty() )
        {
            pWriter.writeAttributeString( Serial.ID, pWoxWriter.createIDfor( pObject ) );

            for ( Map.Entry<Object, Object> zEntry : zMap.entrySet() )
            {
                pWriter.writeStartElement( Serial.OBJECT );
                pWriter.writeAttributeString( Serial.TYPE, Serial.MAP_ENTRY );
                pWoxWriter.write( zEntry.getKey(), pField, pWriter, pJustType );
                pWoxWriter.write( zEntry.getValue(), pField, pWriter, pJustType );
                pWriter.writeEndElement();
            }
        }
        pWriter.writeEndElement();
    }
}

Commits for WOX2/trunk/Java/src/wox/serial/MapHandler.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
8 Diff Diff GeorgeS picture GeorgeS Fri 05 Feb, 2010 19:15:51 +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