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
|
using System;
using System.Collections;
using System.Reflection;
/// <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>
/// This is a simple object to XML serializer. The SimpleWriter class
/// extends WoxWriter. It takes a C# object and a XMLwriter object.
/// It writes (converts) an object to XML, and writes it using the XMLwriter
/// to the specified storage media.
/// </summary>
public class SimpleWriter : Serial, WoxWriter
{
private readonly Hashtable mInstanceToIDs = new Hashtable();
private readonly WoxHandlers mWoxHandlers;
private int mNextID = 1;
// not much point writing out final values - at least not yet -
// the reader is not able to set them (though there's probably
// a hidden way of doing this)
public SimpleWriter( WoxHandlers pWoxHandlers )
{
mWoxHandlers = pWoxHandlers;
}
#region WoxWriter Members
public string CreateIDfor( object pObject )
{
string zID = (mNextID++).ToString();
mInstanceToIDs.Add( pObject, zID );
return zID;
}
/// <summary>
/// This method is the entry point to write an object in XML.
/// </summary>
/// <param name="pObject">The object to be serialized</param>
/// <param name="pField">Optional Field information to augment the Type infomation</param>
/// <param name="pWriter"></param>
/// <param name="pJustType"></param>
public void Write( object pObject, FieldInfo pField, XMLwriter pWriter, bool pJustType )
{
if ( pObject == null )
{
pWriter.WriteStartElement( OBJECT );
pWriter.WriteEndElement();
return;
}
if ( mInstanceToIDs.ContainsKey( pObject ) )
{
pWriter.WriteStartElement( OBJECT );
pWriter.WriteAttributeString( IDREF, mInstanceToIDs[pObject].ToString() );
pWriter.WriteEndElement();
return;
}
Type zNativeType = pObject.GetType();
WoxHandler zWoxHandler = mWoxHandlers.GetNativeHandler( zNativeType );
if ( pJustType && !zWoxHandler.SupportsJustTypeWriting() )
{
throw new ApplicationException( "No significant Type infomation for: " + zNativeType );
}
zWoxHandler.WriteObject( mWoxHandlers, this, pObject, null, pWriter, pJustType );
}
#endregion
}
}
|