Subversion Repository Public Repository

WOX2

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

import java.io.*;

/**
 * The Easy class is used to serialize/de-serialize objects to/from XML.
 *
 * 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 Easy
{
    private final WoxHandlers mWoxHandlers = new WoxHandlers();

    public void add( WoxFieldNameMapper pWoxMapper )
    {
        mWoxHandlers.addWoxFieldNameMapper( pWoxMapper );
    }

    public void add( WoxTypeMapper pWoxMapper )
    {
        mWoxHandlers.addTypeMapper( pWoxMapper );
    }

    public void add( WoxTypeHandler pWoxHandler )
    {
        mWoxHandlers.addTypeHandler( pWoxHandler );
    }

    public void add( WoxGenericsHandler pWoxHandler )
    {
        mWoxHandlers.addGenericsHandler( pWoxHandler );
    }

    public void add( WoxIsAHandler pWoxHandler )
    {
        mWoxHandlers.addIsAHandler( pWoxHandler );
    }

    /**
     * Format pObject to XML
     *
     * @param pObject - object to serialize
     *
     * @return xml form
     */
    public String format( Object pObject )
    {
        return format( pObject, false );
    }

    /**
     * Format pObject to XML
     *
     * @param pObject      - object to serialize
     * @param pPrettyPrint - if true pretty print the xml - NOTE this may cause problems in the "wire" protocol
     *
     * @return xml form
     */
    public String format( Object pObject, boolean pPrettyPrint )
    {
        try
        {
            StringWriter zWriter = new StringWriter();
            buildXML( zWriter, pObject, pPrettyPrint, false );
            return zWriter.toString().replace( "\r\n", "\n" ).trim();
        }
        catch ( IOException e )
        {
            throw new IllegalStateException( e ); // With StringWriter, should not be possible
        }
    }

    /**
     * Format an object's type data to XML
     *
     * @param pObject - object to serialize the type of
     */
    public String formatType( Object pObject )
            throws IOException
    {
        try
        {
            StringWriter zWriter = new StringWriter();
            buildXML( zWriter, pObject, true, true );
            return zWriter.toString().replace( "\r\n", "\n" ).trim();
        }
        catch ( IOException e )
        {
            throw new IllegalStateException( e ); // With StringWriter, should not be possible
        }
    }

    /**
     * Parse an object from a String
     *
     * @param pXMLData - !null
     */
    public Object parse( String pXMLData )
    {
        Reader zReader = new StringReader( pXMLData );
        try
        {
            return parseFromReader( zReader );
        }
        catch ( IOException e )
        {
            throw new IllegalStateException( e ); // With StringReader, should not be possible
        }
    }

    /**
     * Internal code to do the work of formatting
     *
     * @param pWriter      - !null writer
     * @param pObject      - null object to serialize
     * @param pPrettyPrint - if true pretty print the xml - NOTE this may cause in the "wire" protocol
     * @param pJustType    - if true, then No Data, just type info on object and its fields
     */
    protected void buildXML( Writer pWriter, Object pObject, boolean pPrettyPrint, boolean pJustType )
            throws IOException
    {
        XMLwriter zWriter = new XMLwriterImpl( pWriter, pPrettyPrint );
        try
        {
            WoxWriter woxWriter = new SimpleWriter( mWoxHandlers );
            woxWriter.write( pObject, null, zWriter, pJustType );
        }
        catch ( RuntimeException e )
        {
            e.printStackTrace();
            throw e;
        }
        finally
        {
            zWriter.dispose();
        }
    }

    /**
     * Internal code to do the parsing
     *
     * @param pReader - !null reader
     */
    protected Object parseFromReader( Reader pReader )
            throws IOException
    {
        XMLreader zReader = new XMLreaderImpl( pReader );
        try
        {
            //this creates the WOX reader
            SimpleReader woxReader = new SimpleReader( mWoxHandlers );
            if ( zReader.readStartElement() )
            {
                //reads the object from the XML file. We pass the xmlReader positioned in the first node!
                return woxReader.read( zReader );
            }
        }
        catch ( RuntimeException e )
        {
            e.printStackTrace();
            throw e;
        }
        finally
        {
            zReader.dispose();
        }
        return null;
    }
}

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

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