Subversion Repository Public Repository

WOX2

@ 14
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
using System;
using System.IO;

/// <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
{
    /// <summary>
    /// The Easy class is used to serialize/de-serialize objects to/from XML.
    /// </summary>

    public class Easy
    {
        private readonly 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 );
        }

        /// <summary>
        /// Format pObject to XML
        /// </summary>
        /// <param name="pObject">object to serialize</param>
        /// <returns>xml form</returns>
        public string Format( object pObject )
        {
            return Format( pObject, false );
        }

        /// <summary>
        /// Format pObject to XML
        /// </summary>
        /// <param name="pObject">object to serialize</param>
        /// <param name="pPrettyPrint">if true pretty print the xml - NOTE this may cause problems in the "wire" protocol</param>
        /// <returns>xml form</returns>
        public string Format( object pObject, bool pPrettyPrint )
        {
            StringWriter zWriter = new StringWriter();
            BuildXML( zWriter, pObject, pPrettyPrint, false );
            return zWriter.ToString().Replace( "\r\n", "\n" ).Trim();
        }

        /// <summary>
        /// Save an object's type data to a file
        /// </summary>
        /// <param name="pObject">object to serialize the type of</param>
        public string FormatType( object pObject )
        {
            StringWriter zWriter = new StringWriter();
            BuildXML( zWriter, pObject, true, true );
            return zWriter.ToString().Replace( "\r\n", "\n" ).Trim();
        }

        /// <summary>
        /// Parse an object from a String
        /// </summary>
        /// <param name="pXMLData">!null</param>
        public object Parse( string pXMLData )
        {
            TextReader zReader = new StringReader( pXMLData );
            return ParseFromReader( zReader );
        }

        /// <summary>
        /// Internal code to do the work of formatting
        /// </summary>
        /// <param name="pWriter">!null writer</param>
        /// <param name="pObject">null object to serialize</param>
        /// <param name="pPrettyPrint">if true pretty print the xml - NOTE this may cause in the "wire" protocol</param>
        /// <param name="pJustType">if true, then No Data, just type info on object and its fields</param>
        protected void BuildXML( TextWriter pWriter, object pObject, bool pPrettyPrint, bool pJustType )
        {
            using ( XMLwriter zWriter = new XMLwriterImpl( pWriter, pPrettyPrint ) )
            {
                try
                {
                    WoxWriter woxWriter = new SimpleWriter( mWoxHandlers );
                    woxWriter.Write( pObject, null, zWriter, pJustType );
                }
                catch ( Exception e )
                {
                    Util.Dump( e, Console.Error );
                    throw;
                }
            }
        }

        /// <summary>
        /// Internal code to do the parsing
        /// </summary>
        /// <param name="pReader">!null reader</param>
        protected object ParseFromReader( TextReader pReader )
        {
            using ( 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 ( Exception e )
                {
                    Util.Dump( e, Console.Error );
                    throw;
                }
                return null;
            }
        }
    }
}

Commits for WOX2/trunk/CSharp/src/wox/serial/Easy.cs

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