File: NewMonoXSD.cs

package info (click to toggle)
mono 4.6.2.7+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (507 lines) | stat: -rw-r--r-- 15,580 bytes parent folder | download | duplicates (2)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
///
/// MonoXSD.cs -- A reflection-based tool for dealing with XML Schema.
///
/// Authors: Duncan Mak (duncan@ximian.com)
///          Lluis Sanchez Gual (lluis@ximian.com)
///          Atsushi Enomoto (atsushi@ximian.com)
///
/// Copyright (C) 2003, Duncan Mak,
///                     Ximian, Inc.
///

using System;
using System.Collections;
using System.Data;
using System.IO;
using System.Reflection;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using System.CodeDom;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using Microsoft.VisualBasic;

namespace Mono.Util {

	public class Driver
	{
		public static readonly string helpString =
			"xsd.exe - a utility for generating schema or class files\n\n" +
			"xsd.exe <schema>.xsd /classes [/element:NAME] [/language:NAME]\n" +
			"            [/namespace:NAME] [/outputdir:PATH] [/uri:NAME]\n\n" +
			"xsd.exe <schema>.xsd /dataset [/element:NAME] [/language:NAME]\n" +
			"            [/namespace:NAME] [/outputdir:PATH] [/uri:NAME]\n\n" +

			"xsd.exe <assembly>.dll|<assembly>.exe [/outputdir:PATH] [/type:NAME]\n\n" +
			"xsd.exe <instance>.xml [<instance>.xml ...] [/outputdir:PATH]\n\n" +
			"   /c /classes        Generate classes for the specified schema.\n" +
			"   /d /dataset        Generate typed dataset classes for the specified schema.\n" +
			"   /e /element:NAME   Element from schema to generate code for.\n" +
			"                      Multiple elements can be specified.\n" +
			"   /u /uri:NAME       Namespace uri of the elements to generate code for.\n" +
			"   /l /language:NAME  The language, or type name of custom CodeDomProvider\n" +
			"                      to use for the generated code.\n" +
			"                      Shorthand specifiers are: \"CS\" (C#) and \"VB\" (VB.NET).\n" +
			"                      For type name, assembly qualified name is required.\n" +
			"   /g /generator:TYPE Code Generator type name, followed by ','\n" + 
			"                      and assembly file name.\n" +
			"   /o /outputdir:PATH The directory where to generate the code or schemas.\n" +
			"   /n /namespace:NAME Namespace for the generated code.\n" +
			"   /t /type:NAME      Type for which to generate an xml schema.\n" +
			"                      Multiple types can be specified.\n" +
			"   /h /help           Output this help.\n";

		static readonly string incorrectOrder = "Options must be specified after Assembly or schema file names";
		static readonly string duplicatedParam = "The option {0} cannot be specified more that once";
		static readonly string unknownOption = "Unknown option {0}";
		static readonly string incompatibleArgs = "Cannot mix options for generating schemas and for generatic classes";
		static readonly string invalidParams = "Invalid parameters";
		static readonly string tooManyAssem = "Only one assembly name can be specified";
		static readonly string errLoadAssembly = "Could not load assembly: {0}";
		static readonly string typeNotFound = "Type {0} not found in the specified assembly";
		static readonly string languageNotSupported = "The language {0} is not supported";
		static readonly string missingOutputForXsdInput = "Can only generate one of classes or datasets.";
		static readonly string generatorAssemblyNotFound = "Could not load code provider assembly file: {0}";
		static readonly string generatorTypeNotFound = "Could not find specified code provider type: {0}";
		static readonly string generatorTypeIsNotCodeGenerator = "Specified code provider type was not CodeDomProvider: {0}";
		static readonly string generatorThrewException = "Specified CodeDomProvider raised an error while creating its instance: {0}";

		static void Main (string [] args)
		{
			if (args.Length < 1) {
				Console.WriteLine (helpString);
				Environment.Exit (0);
			}

			try
			{
				new Driver().Run (args);
			}
			catch (ApplicationException ex)
			{
				Console.WriteLine (ex.Message);
			}
			catch (Exception ex)
			{
				Console.WriteLine (ex);
			}
		}

		string outputDir = null;


		ArrayList lookupTypes = new ArrayList();
		ArrayList assemblies = new ArrayList();

		ArrayList schemaNames = new ArrayList();
		ArrayList inferenceNames = new ArrayList();
		ArrayList elements = new ArrayList();
		string language = null;
		string namesp = null;
		string uri = null;
		string providerOption = null;
		CodeDomProvider provider;

		public void Run (string[] args)
		{
			ArrayList unknownFiles = new ArrayList();
			bool generateClasses = false;
			bool readingFiles = true;
			bool schemasOptions = false;
			bool assemblyOptions = false;
			bool generateDataset = false;
			bool inference = false;

			foreach (string arg in args)
			{
				if (!arg.StartsWith ("--") && !arg.StartsWith ("/") ||
					(arg.StartsWith ("/") && arg.IndexOfAny (Path.InvalidPathChars) == -1)
					) 
				{
					if ((arg.EndsWith (".dll") || arg.EndsWith (".exe")) && !arg.Substring (1).StartsWith ("generator:") && !arg.Substring (1).StartsWith ("g:"))
					{
						if (!readingFiles) throw new ApplicationException (incorrectOrder);
						assemblies.Add (arg);
						assemblyOptions = true;
						continue;
					}
					else if (arg.EndsWith (".xsd"))
					{
						if (!readingFiles) Error (incorrectOrder);
						schemaNames.Add (arg);
						schemasOptions = true;
						continue;
					}
					else if (arg.EndsWith (".xml"))
					{
						if (generateClasses || generateDataset) Error (duplicatedParam);
						inferenceNames.Add (arg);
						inference = true;
						continue;
					}
					else if (!arg.StartsWith ("/"))
					{
						if (!readingFiles) Error (incorrectOrder);
						unknownFiles.Add (arg);
						continue;
					}
				}

				readingFiles = false;

				int i = arg.IndexOf (":");
				if (i == -1) i = arg.Length;
				string option = arg.Substring (1,i-1);
				string param = (i<arg.Length-1) ? arg.Substring (i+1) : "";

				if (option == "classes" || option == "c")
				{
					if (generateClasses || generateDataset || inference) Error (duplicatedParam, option);
					generateClasses = true;
					schemasOptions = true;
				}
				else if (option == "dataset" || option == "d")
				{
					if (generateClasses || generateDataset || inference) Error (duplicatedParam, option);
					generateDataset = true;
					schemasOptions = true;
				}
				else if (option == "element" || option == "e")
				{
					elements.Add (param);
					schemasOptions = true;
				}
				else if (option == "language" || option == "l")
				{
					if (provider != null) Error (duplicatedParam, option);
					if (language != null) Error (duplicatedParam, option);
					language = param;
					schemasOptions = true;
				}
				else if (option == "namespace" || option == "n")
				{
					if (namesp != null) Error (duplicatedParam, option);
					namesp = param;
					schemasOptions = true;
				}
				else if (option == "outputdir" || option == "o")
				{
					if (outputDir != null) Error (duplicatedParam, option);
					outputDir = param;
				}
				else if (option == "uri" || option == "u")
				{
					if (uri != null) Error (duplicatedParam, option);
					uri = param;
					schemasOptions = true;
				}
				else if (option == "type" || option == "t")
				{
					lookupTypes.Add (param);
					assemblyOptions = true;
				}
				else if (option == "generator" || option == "g")
				{
					providerOption = param;
				}
				else if (option == "help" || option == "h")
				{
					Console.WriteLine (helpString);
					return;
				}
				else if (option == "nologo")
				{
					// ignore, since we do not output a logo anyway
				}
				else
					Error (unknownOption, option);
			}

			if (!schemasOptions && !assemblyOptions && !inference)
				Error (invalidParams);

			if (schemasOptions && assemblyOptions)
				Error (incompatibleArgs);

			if (assemblies.Count > 1)
				Error (tooManyAssem);

			if (outputDir == null) outputDir = ".";

			string typename = null;
			Type generatorType = null;

			if (language != null) {
				switch (language) {
				case "CS":
					provider = new CSharpCodeProvider ();
					break;
				case "VB":
					provider = new VBCodeProvider ();
					break;
				default:
					typename = StripQuot (language);

					generatorType = Type.GetType (typename);
					if (generatorType == null)
						Error (generatorTypeNotFound, typename);
					break;
				}
			}

			if (providerOption != null) {
				string param = providerOption;
				int comma = param.IndexOf (',');
				if (comma < 0) {
					typename = StripQuot (param);
					generatorType = Type.GetType (param);
				} else {
					typename = param.Substring (0, comma);
					string asmName = param.Substring (comma + 1);
					Assembly asm = Assembly.LoadFile (asmName);
					if (asm == null)
						Error (generatorAssemblyNotFound, asmName);
					generatorType = asm.GetType (typename);
				}
				if (generatorType == null)
					Error (generatorTypeNotFound, typename);
			}
			if (generatorType != null) {
				if (!generatorType.IsSubclassOf (typeof (CodeDomProvider)))
					Error (generatorTypeIsNotCodeGenerator, typename);
				try {
					provider = (CodeDomProvider) Activator.CreateInstance (generatorType, null);
				} catch (Exception ex) {
					Error (generatorThrewException, generatorType.AssemblyQualifiedName.ToString () + " --> " + ex.Message);
				}
				Console.WriteLine ("Loaded custom generator type " + generatorType + " .");
			}
			if (provider == null)
				provider = new CSharpCodeProvider ();

			if (schemasOptions)
			{
				if (!generateClasses && !generateDataset)
					Error (missingOutputForXsdInput);
				schemaNames.AddRange (unknownFiles);
				if (generateClasses)
					GenerateClasses ();
				else if (generateDataset)
					GenerateDataset ();
			}
			else if (inference)
			{
				foreach (string xmlfile in inferenceNames) {
					string genFile = Path.Combine (outputDir, Path.GetFileNameWithoutExtension (xmlfile) + ".xsd");
					DataSet ds = new DataSet ();
					ds.InferXmlSchema (xmlfile, null);
					ds.WriteXmlSchema (genFile);
					Console.WriteLine ("Written file " + genFile);
				}
			}
			else
			{
				assemblies.AddRange (unknownFiles);
				GenerateSchemas ();
			}
		}

		public void GenerateSchemas ()
		{
			Assembly assembly = null;
			try
			{
				assembly = Assembly.LoadFrom ((string) assemblies [0]);
			}
			catch (Exception ex)
			{
				Error (errLoadAssembly, ex.Message);
			}
			
			Type[] types;
			
			if (lookupTypes.Count > 0)
			{
				types = new Type [lookupTypes.Count];
				for (int n=0; n<lookupTypes.Count; n++)
				{
					Type t = assembly.GetType ((string)lookupTypes[n]);
					if (t == null) Error (typeNotFound, (string)lookupTypes[n]);
					types[n] = t;
				}
			}
			else
				types = assembly.GetExportedTypes ();

			XmlReflectionImporter ri = new XmlReflectionImporter ();
			XmlSchemas schemas = new XmlSchemas ();
			XmlSchemaExporter sx = new XmlSchemaExporter (schemas);

			foreach (Type type in types)
			{
				XmlTypeMapping tm = ri.ImportTypeMapping (type);
				sx.ExportTypeMapping (tm);
			}

			if (schemas.Count == 1)
			{
				string fileName = Path.Combine (outputDir, "schema.xsd");
				WriteSchema (fileName, schemas [0]);
			}
			else
			{
				for (int n=0; n<schemas.Count; n++)
				{
					string fileName = Path.Combine (outputDir, "schema" + n + ".xsd");
					WriteSchema (fileName, schemas [n]);
				}
			}
		}

		void WriteSchema (string fileName, XmlSchema schema)
		{
			StreamWriter sw = new StreamWriter (fileName);
			schema.Write (sw);
			sw.Close ();
			Console.WriteLine ("Written file " + fileName);
		}
		
		public void GenerateClasses ()
		{
			if (namesp == null) namesp = "Schemas";
			if (uri == null) uri = "";
			string targetFile = "";

			XmlSchemas schemas = new XmlSchemas();
			foreach (string fileName in schemaNames)
			{
				StreamReader sr = new StreamReader (fileName);
				schemas.Add (XmlSchema.Read (sr, new ValidationEventHandler (HandleValidationError)));
				sr.Close ();

				if (targetFile == "") targetFile = Path.GetFileNameWithoutExtension (fileName);
				else targetFile += "_" + Path.GetFileNameWithoutExtension (fileName);
			}

			targetFile += "." + provider.FileExtension;

			CodeCompileUnit cunit = new CodeCompileUnit ();
			CodeNamespace codeNamespace = new CodeNamespace (namesp);
			cunit.Namespaces.Add (codeNamespace);
			codeNamespace.Comments.Add (new CodeCommentStatement ("\nThis source code was auto-generated by MonoXSD\n"));

			// Locate elements to generate

			ArrayList qnames = new ArrayList ();
			if (elements.Count > 0)
			{
				foreach (string name in elements)
					qnames.Add (new XmlQualifiedName (name, uri));
			}
			else
			{
				foreach (XmlSchema schema in schemas) {
					if (!schema.IsCompiled) schema.Compile (new ValidationEventHandler (HandleValidationError));
					foreach (XmlSchemaElement el in schema.Elements.Values)
						if (!qnames.Contains (el.QualifiedName))
							qnames.Add (el.QualifiedName);
				}
			}

			// Import schemas and generate the class model

			XmlSchemaImporter importer = new XmlSchemaImporter (schemas);
			XmlCodeExporter sx = new XmlCodeExporter (codeNamespace, cunit);

			ArrayList maps = new ArrayList();

			foreach (XmlQualifiedName qname in qnames)
			{
				XmlTypeMapping tm = importer.ImportTypeMapping (qname);
				if (tm != null) maps.Add (tm);
			}
			
			foreach (XmlTypeMapping tm in maps)
			{
				sx.ExportTypeMapping (tm);
			}

			// Generate the code
			
			ICodeGenerator gen = provider.CreateGenerator();

			string genFile = Path.Combine (outputDir, targetFile);
			StreamWriter sw = new StreamWriter(genFile, false);
			gen.GenerateCodeFromCompileUnit (cunit, sw, new CodeGeneratorOptions());
			sw.Close();

			Console.WriteLine ("Written file " + genFile);
		}

		public void GenerateDataset ()
		{
			if (namesp == null) namesp = "";
			if (uri == null) uri = "";
			string targetFile = "";

			DataSet dataset = new DataSet ();
			foreach (string fileName in schemaNames)
			{
				dataset.ReadXmlSchema (fileName);

				if (targetFile == "") targetFile = Path.GetFileNameWithoutExtension (fileName);
				else targetFile += "_" + Path.GetFileNameWithoutExtension (fileName);
			}

			targetFile += "." + provider.FileExtension;

			CodeCompileUnit cunit = new CodeCompileUnit ();
			CodeNamespace codeNamespace = new CodeNamespace (namesp);
			cunit.Namespaces.Add (codeNamespace);
			codeNamespace.Comments.Add (new CodeCommentStatement ("\nThis source code was auto-generated by MonoXSD\n"));

			// Generate the code
			
			ICodeGenerator gen = provider.CreateGenerator ();

			TypedDataSetGenerator.Generate (dataset, codeNamespace, gen);

			string genFile = Path.Combine (outputDir, targetFile);
			StreamWriter sw = new StreamWriter(genFile, false);
			gen.GenerateCodeFromCompileUnit (cunit, sw, new CodeGeneratorOptions());
			sw.Close();

			Console.WriteLine ("Written file " + genFile);
		}

		void HandleValidationError (object o, ValidationEventArgs e)
		{
			Console.WriteLine ("{0}: {1} {2}",
				e.Severity == XmlSeverityType.Error ? "Error" : "Warning",
				e.Message,
				e.Exception != null ? e.Exception.Message : null);
		}

		public void Error (string msg)
		{
			throw new ApplicationException (msg);
		}

		public void Error (string msg, string param)
		{
			throw new ApplicationException (string.Format(msg,param));
		}

		private string StripQuot (string input)
		{
			if (input.Length < 2)
				return input;
			if (input [0] == '"' && input [input.Length -1] == '"' ||
				input [0] == '\'' && input [input.Length - 1] == '\'')
				return input.Substring (1, input.Length - 2);
			else
				return language;
		}
	}
}