Merge pull request #5288 from lambdageek/bug-58454
[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                         <baseSerializer>name</baseSerializer> ? (2.0-only)
40                         <implementation>name</implementation> ? (2.0-only)
41                         <namespace>name</namespace> ?
42                         <namespaceImports> ?
43                                 <namespaceImport>namespace</namespaceImport> *
44                         </namespaceImports>
45                         <outFileName>name</outFileName> ?
46                         <readerHooks> ?
47                                 <hook ...> *
48                         </readerHooks>
49                         <writerHooks> ?
50                                 <hook ...> *
51                         </writerHooks>
52                 </serializer>
53         </configuration>
54
55 A configuration file can have multiple "serializer" elements, each of which
56 specifies the class for which to generate a serializer together with several
57 generation options. The source class is specified in the following attributes:
58
59  * class: name of the class (including namespace).
60  * assembly: assembly name. It can include the complete path.
61  
62 Generation options are specified in child elements:
63
64  * reader: name of the reader class.
65  * writer: name of the writer class.
66  * baseSerializer: 2.0 only. name of the base XML serializer class.
67  * implementation: 2.0 only. name of the serializer implementation class.
68  * namespace: namespace of the reader and writer classes.
69  * namespaceImport: a list of namespaces to be added as namespace imports
70    ("using" declarations in C#) on the top of the output source.
71    It contains "namespaceImport" child elements which contains one namespace.
72  * outFileName: name of the generated file.
73  * readerHooks: a list of hooks to apply to the reader.
74  * writerHooks: a list of hooks to apply to the writer.
75
76
77 * Specifying Hooks
78 -----------------------------
79
80 Using hooks you can customize the behavior of readers and writers.
81 A hook specification follows this grammar:
82
83         <hook type="name">
84                 <select> ?
85                         <typeName>name</typeName> ?
86                         <typeAttribute>name</typeAttribute> *
87                         <typeMember>name</typeMember> ?
88                 </select>
89                 <replace>source code</replace> ?
90                 <insertBefore>source code</insertBefore> ?
91                 <insertAfter>source code</insertAfter> ?
92         </hook>
93
94 The "type" attribute specifies the context in which the hook is applied. It can
95 be one of the following:
96
97  * attributes: hook is applied where attributes are serialized/deserialized.
98  * elements: hook is applied where elements are serialized/deserialized.
99  * unknownAttribute: hook is applied where unknown attributes are processed.
100  * unknownElement: hook is applied where unknown elements are processed.
101  * member: hook is applied where a member is serialized/deserialized.
102  * type: hook is applied for the whole type.
103
104 The "select" element specifies the classes and members to which the hook has
105 to be added. It can contain the following elements:
106
107  * typeName: the class with that name will be selected (must include namespace)
108  * typeAttribute: all classes which have that attribute applied will be
109    selected (specify the full attribute class name, including namespace).
110    Several attribute names can be specified.
111  * typeMember: name of the class member for which the hook must be added.
112
113 The hook source code can be specified using any of the following elements:
114
115  * replace: the provided source code will replace all serialization/deserialization
116    operations in the hook context.
117  * insertBefore: the source code will be added before the hook context.
118  * insertAfter: the source code will be added after the hook context.
119
120 When writing the code for a hook you can use some special variables that are
121 defined during the code generation process. The variables are the following:
122
123  * $TYPE: name of the class being generated, without namespace.
124  * $FULLTYPE: full name of the class being generated, including namespace.
125  * $OBJECT: the object being serialized or deserialized. When using a replace
126    reader hook of type "type", the hook code must assign the deserialized object
127    to this variable.
128  * $ELEMENT: name of the element of the object being serialized/deserialized.
129  * $NAMESPACE: namespace of the element of the object being 
130    serialized/deserialized.
131  * $MEMBER: name of the member being serialized/deserialized. Only valid in
132    the "member" context.
133
134 * Hook examples
135 ---------------------------------
136
137 The following example adds a call to a Validate method after the deserialization
138 of any object:
139
140         <hook type="type">
141                 <insertAfter>System.Xml.Schema.XmlSchema.Validate$TYPE ($OBJECT);</insertAfter>
142         </hook>
143         
144 This example specifies the code to be used to deserialize the XmlSchema class:
145
146         <hook type="type">
147                 <select>
148                         <typeName>System.Xml.Schema.XmlSchema</typeName>
149                 </select>
150                 <replace>$OBJECT = System.Xml.Schema.XmlSchema.Read (Reader, null);</replace>
151         </hook>
152
153 That one specifies the code to be used to read XmlSchema instances:
154
155         <hook type="type">
156                 <select>
157                         <typeName>System.Xml.Schema.XmlSchema</typeName>
158                 </select>
159                 <replace>$OBJECT.Write (Writer);</replace>
160         </hook>
161
162 With this two hooks the serializer will print some information when serializing
163 the class "MyClass":
164
165         <hook type="type">
166                 <select>
167                         <typeName>MyNamespace.MyClass</typeName>
168                 </select>
169                 <insertBefore>Console.WriteLine ("Serializing MyClass");</replace>
170                 <insertAfter>Console.WriteLine ("MyClass serialized");</insertAfter>
171         </hook>
172         <hook type="member">
173                 <select>
174                         <typeName>MyNamespace.MyClass</typeName>
175                 </select>
176                 <insertAfter>Console.WriteLine ("Serialized member $MEMBER");</insertAfter>
177         </hook>
178         
179 This hook writes an additional element for all types that have the custom
180 attribute "MyAttribute":
181
182         <hook type="elements">
183                 <select>
184                         <typeAttribute>MyNamespace.MyAttribute</typeAttribute>
185                 </select>
186                 <insertAfter>
187                         Writer.WriteStartElement ("privateData");
188                         Writer.WriteString ($OBJECT.PrivateData);
189                         Writer.WriteEndElement ();
190                 </insertAfter>
191         </hook>
192
193 * Configuration file example
194 ---------------------------------
195
196 This is the configuration file used to generate the serializer for ServiceDescription:
197
198 <configuration>
199         <serializer class="System.Web.Services.Description.ServiceDescription" assembly="System.Web.Services">
200                 <reader>ServiceDescriptionReaderBase</reader>
201                 <writer>ServiceDescriptionWriterBase</writer>
202                 <namespace>System.Web.Services.Description</namespace>
203                 <namespaceImports>
204                         <namespaceImport>System.Xml.Schema</namespaceImport>
205                 </namespaceImports>
206                 <outFileName>ServiceDescriptionSerializerBase.cs</outFileName>
207                 <readerHooks>
208                         <hook type="unknownElement">
209                                 <select>
210                                         <typeAttribute>System.Web.Services.Configuration.XmlFormatExtensionPointAttribute</typeAttribute>
211                                 </select>
212                                 <replace>ServiceDescription.ReadExtension (Reader, $OBJECT);</replace>
213                         </hook>
214                         <hook type="type">
215                                 <select>
216                                         <typeName>System.Xml.Schema.XmlSchema</typeName>
217                                 </select>
218                                 <replace>$OBJECT = XmlSchema.Read (Reader, null);</replace>
219                         </hook>
220                 </readerHooks>
221                 <writerHooks>
222                         <hook type="elements">
223                                 <select>
224                                         <typeAttribute>System.Web.Services.Configuration.XmlFormatExtensionPointAttribute</typeAttribute>
225                                 </select>
226                                 <insertBefore>ServiceDescription.WriteExtensions (Writer, $OBJECT);</insertBefore>
227                         </hook>
228                         <hook type="type">
229                                 <select>
230                                         <typeName>System.Xml.Schema.XmlSchema</typeName>
231                                 </select>
232                                 <replace>$OBJECT.Write (Writer);</replace>
233                         </hook>
234                 </writerHooks>
235         </serializer>
236 </configuration>
237
238
239 -----------------
240 Lluis Sanchez Gual
241 lluis@ximian.com