forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXsdCodeGenerator.cs
73 lines (62 loc) · 2.4 KB
/
XsdCodeGenerator.cs
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
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using Microsoft.CSharp;
namespace NHibernate.Tool.HbmXsd
{
/// <summary>Responsible for code generation from an XML schema.</summary>
public class XsdCodeGenerator
{
/// <summary>Generates C# classes.</summary>
/// <param name="outputFileName">The file to which the generated classes are written.</param>
/// <param name="namespace">The namespace to which the generated classes belong.</param>
/// <param name="schema">The schema from which the classes are generated.</param>
public void Execute(string outputFileName, string @namespace, XmlSchema schema)
{
if (schema == null)
throw new ArgumentNullException("schema");
CodeNamespace code = new CodeNamespace(@namespace);
GenerateClasses(code, schema);
CustomizeGeneratedCode(code, schema);
WriteCSharpCodeFile(code, outputFileName);
}
private static void GenerateClasses(CodeNamespace code, XmlSchema schema)
{
XmlSchemas schemas = new XmlSchemas();
schemas.Add(schema);
XmlSchemaImporter importer = new XmlSchemaImporter(schemas);
CodeCompileUnit codeCompileUnit = new CodeCompileUnit();
CodeGenerationOptions options = CodeGenerationOptions.None;
XmlCodeExporter exporter = new XmlCodeExporter(code, codeCompileUnit, options);
foreach (XmlSchemaObject item in schema.Items)
{
XmlSchemaElement element = item as XmlSchemaElement;
if (element != null)
{
XmlQualifiedName name = new XmlQualifiedName(element.Name, schema.TargetNamespace);
XmlTypeMapping map = importer.ImportTypeMapping(name);
exporter.ExportTypeMapping(map);
}
}
}
/// <summary>
/// If overridden by inheritors, customizes the generated code DOM before writing it to file.
/// </summary>
/// <param name="code">The customizable code DOM.</param>
/// <param name="sourceSchema">The source XML Schema.</param>
protected virtual void CustomizeGeneratedCode(CodeNamespace code, XmlSchema sourceSchema)
{
}
private static void WriteCSharpCodeFile(CodeNamespace code, string outputFileName)
{
CSharpCodeProvider provider = new CSharpCodeProvider();
CodeGeneratorOptions options = new CodeGeneratorOptions();
using (StreamWriter writer = new StreamWriter(outputFileName, false))
provider.GenerateCodeFromNamespace(code, writer, options);
}
}
}