* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / tools / genxs / README
1 Mono Xml Serializer Generator Tool - README
2
3 genxs is a tool for generating custom XML serialization writers and readers for
4 classes.
5
6
7 * Usage
8 -----------------------------
9
10 The tool takes as input a configuration file which specifies several
11 information, such as the class for which to generate the reader and writer, the
12 name and namespace of the classes to generate, and a collection of hooks to
13 apply. Hooks are very useful, because with them you can eaily customize the
14 behavior of the serializer without needing to modify the generated file, so you
15 can safely regenerate it if the source class is modified.
16
17 To use the tool, run the following command:
18
19         mono genxs.exe configurationFile [destinationFolder]
20
21 where:
22  * configurationFile: the configuration file path
23  * destinationFolder: the folder where to generate the files
24         
25 NOTE: This tool only runs in the Mono runtime, since it uses some internal
26 classes not available in other runtimes.
27
28
29 * Configuration file format
30 -----------------------------
31
32 The configuration file is an xml document based on the following grammar
33 ("?" means optional, "*" 0 or more):
34         
35         <configuration>
36                 <serializer class="name" assembly="name"> *
37                         <reader>name</reader> ?
38                         <writer>name</writer> ?
39                         <namespace>name</namespace> ?
40                         <namespaceImports> ?
41                                 <namespaceImport>namespace</namespaceImport> *
42                         </namespaceImports>
43                         <outFileName>name</outFileName> ?
44                         <readerHooks> ?
45                                 <hook ...> *
46                         </readerHooks>
47                         <writerHooks> ?
48                                 <hook ...> *
49                         </writerHooks>
50                 </serializer>
51         </configuration>
52
53 A configuration file can have multiple "serializer" elements, each of which
54 specifies the class for which to generate a serializer together with several
55 generation options. The source class is specified in the following attributes:
56
57  * class: name of the class (including namespace).
58  * assembly: assembly name. It can include the complete path.
59  
60 Generation options are specified in child elements:
61
62  * reader: name of the reader class.
63  * writer: name of the writer class.
64  * namespace: namespace of the reader and writer classes.
65  * namespaceImport: a list of namespaces to be added as namespace imports
66    ("using" declarations in C#) on the top of the output source.
67    It contains "namespaceImport" child elements which contains one namespace.
68  * outFileName: name of the generated file.
69  * readerHooks: a list of hooks to apply to the reader.
70  * writerHooks: a list of hooks to apply to the writer.
71
72
73 * Specifying Hooks
74 -----------------------------
75
76 Using hooks you can customize the behavior of readers and writers.
77 A hook specification follows this grammar:
78
79         <hook type="name">
80                 <select> ?
81                         <typeName>name</typeName> ?
82                         <typeAttribute>name</typeAttribute> *
83                         <typeMember>name</typeMember> ?
84                 </select>
85                 <replace>source code</replace> ?
86                 <insertBefore>source code</insertBefore> ?
87                 <insertAfter>source code</insertAfter> ?
88         </hook>
89
90 The "type" attribute specifies the context in which the hook is applied. It can
91 be one of the following:
92
93  * attributes: hook is applied where attributes are serialized/deserialized.
94  * elements: hook is applied where elements are serialized/deserialized.
95  * unknownAttribute: hook is applied where unknown attributes are processed.
96  * unknownElement: hook is applied where unknown elements are processed.
97  * member: hook is applied where a member is serialized/deserialized.
98  * type: hook is applied for the whole type.
99
100 The "select" element specifies the classes and members to which the hook has
101 to be added. It can contain the following elements:
102
103  * typeName: the class with that name will be selected (must include namespace)
104  * typeAttribute: all classes which have that attribute applied will be
105    selected (specify the full attribute class name, including namespace).
106    Several attribute names can be specified.
107  * typeMember: name of the class member for which the hook must be added.
108
109 The hook source code can be specified using any of the following elements:
110
111  * replace: the provided source code will replace all serialization/deserialization
112    operations in the hook context.
113  * insertBefore: the source code will be added before the hook context.
114  * insertAfter: the source code will be added after the hook context.
115
116 When writing the code for a hook you can use some special variables that are
117 defined during the code generation process. The variables are the following:
118
119  * $TYPE: name of the class being generated, without namespace.
120  * $FULLTYPE: full name of the class being generated, including namespace.
121  * $OBJECT: the object being serialized or deserialized. When using a replace
122    reader hook of type "type", the hook code must assign the deserialized object
123    to this variable.
124  * $ELEMENT: name of the element of the object being serialized/deserialized.
125  * $NAMESPACE: namespace of the element of the object being 
126    serialized/deserialized.
127  * $MEMBER: name of the member being serialized/deserialized. Only valid in
128    the "member" context.
129
130 * Hook examples
131 ---------------------------------
132
133 The following example adds a call to a Validate method after the deserialization
134 of any object:
135
136         <hook type="type">
137                 <insertAfter>System.Xml.Schema.XmlSchema.Validate$TYPE ($OBJECT);</insertAfter>
138         </hook>
139         
140 This example specifies the code to be used to deserialize the XmlSchema class:
141
142         <hook type="type">
143                 <select>
144                         <typeName>System.Xml.Schema.XmlSchema</typeName>
145                 </select>
146                 <replace>$OBJECT = System.Xml.Schema.XmlSchema.Read (Reader, null);</replace>
147         </hook>
148
149 That one specifies the code to be used to read XmlSchema instances:
150
151         <hook type="type">
152                 <select>
153                         <typeName>System.Xml.Schema.XmlSchema</typeName>
154                 </select>
155                 <replace>$OBJECT.Write (Writer);</replace>
156         </hook>
157
158 With this two hooks the serializer will print some information when serializing
159 the class "MyClass":
160
161         <hook type="type">
162                 <select>
163                         <typeName>MyNamespace.MyClass</typeName>
164                 </select>
165                 <insertBefore>Console.WriteLine ("Serializing MyClass");</replace>
166                 <insertAfter>Console.WriteLine ("MyClass serialized");</insertAfter>
167         </hook>
168         <hook type="member">
169                 <select>
170                         <typeName>MyNamespace.MyClass</typeName>
171                 </select>
172                 <insertAfter>Console.WriteLine ("Serialized member $MEMBER");</insertAfter>
173         </hook>
174         
175 This hook writes an additional element for all types that have the custom
176 attribute "MyAttribute":
177
178         <hook type="elements">
179                 <select>
180                         <typeAttribute>MyNamespace.MyAttribute</typeAttribute>
181                 </select>
182                 <insertAfter>
183                         Writer.WriteStartElement ("privateData");
184                         Writer.WriteString ($OBJECT.PrivateData);
185                         Writer.WriteEndElement ();
186                 </insertAfter>
187         </hook>
188
189 * Configuration file example
190 ---------------------------------
191
192 This is the configuration file used to generate the serializer for ServiceDescription:
193
194 <configuration>
195         <serializer class="System.Web.Services.Description.ServiceDescription" assembly="System.Web.Services">
196                 <reader>ServiceDescriptionReaderBase</reader>
197                 <writer>ServiceDescriptionWriterBase</writer>
198                 <namespace>System.Web.Services.Description</namespace>
199                 <namespaceImports>
200                         <namespaceImport>System.Xml.Schema</namespaceImport>
201                 </namespaceImports>
202                 <outFileName>ServiceDescriptionSerializerBase.cs</outFileName>
203                 <readerHooks>
204                         <hook type="unknownElement">
205                                 <select>
206                                         <typeAttribute>System.Web.Services.Configuration.XmlFormatExtensionPointAttribute</typeAttribute>
207                                 </select>
208                                 <replace>ServiceDescription.ReadExtension (Reader, $OBJECT);</replace>
209                         </hook>
210                         <hook type="type">
211                                 <select>
212                                         <typeName>System.Xml.Schema.XmlSchema</typeName>
213                                 </select>
214                                 <replace>$OBJECT = XmlSchema.Read (Reader, null);</replace>
215                         </hook>
216                 </readerHooks>
217                 <writerHooks>
218                         <hook type="elements">
219                                 <select>
220                                         <typeAttribute>System.Web.Services.Configuration.XmlFormatExtensionPointAttribute</typeAttribute>
221                                 </select>
222                                 <insertBefore>ServiceDescription.WriteExtensions (Writer, $OBJECT);</insertBefore>
223                         </hook>
224                         <hook type="type">
225                                 <select>
226                                         <typeName>System.Xml.Schema.XmlSchema</typeName>
227                                 </select>
228                                 <replace>$OBJECT.Write (Writer);</replace>
229                         </hook>
230                 </writerHooks>
231         </serializer>
232 </configuration>
233
234
235 -----------------
236 Lluis Sanchez Gual
237 lluis@ximian.com