diff options
Diffstat (limited to 'src/internal/WixBuildTools.XsdGen/XsdGen.cs')
| -rw-r--r-- | src/internal/WixBuildTools.XsdGen/XsdGen.cs | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/src/internal/WixBuildTools.XsdGen/XsdGen.cs b/src/internal/WixBuildTools.XsdGen/XsdGen.cs new file mode 100644 index 00000000..a1374df3 --- /dev/null +++ b/src/internal/WixBuildTools.XsdGen/XsdGen.cs | |||
| @@ -0,0 +1,124 @@ | |||
| 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. | ||
| 2 | |||
| 3 | namespace WixToolset.Tools | ||
| 4 | { | ||
| 5 | using System; | ||
| 6 | using System.CodeDom; | ||
| 7 | using System.CodeDom.Compiler; | ||
| 8 | using System.Collections; | ||
| 9 | using System.IO; | ||
| 10 | using System.Xml; | ||
| 11 | using System.Xml.Schema; | ||
| 12 | using Microsoft.CSharp; | ||
| 13 | using WixToolset; | ||
| 14 | |||
| 15 | /// <summary> | ||
| 16 | /// Generates a strongly-typed C# class from an XML schema (XSD). | ||
| 17 | /// </summary> | ||
| 18 | public class XsdGen | ||
| 19 | { | ||
| 20 | private string xsdFile; | ||
| 21 | private string outFile; | ||
| 22 | private string outputNamespace; | ||
| 23 | private string commonNamespace; | ||
| 24 | private bool showHelp; | ||
| 25 | |||
| 26 | /// <summary> | ||
| 27 | /// Constructor for the XsdGen class. | ||
| 28 | /// </summary> | ||
| 29 | /// <param name="args">Command-line arguments passed to the program.</param> | ||
| 30 | private XsdGen(string[] args) | ||
| 31 | { | ||
| 32 | this.ParseCommandlineArgs(args); | ||
| 33 | |||
| 34 | // show usage information | ||
| 35 | if (this.showHelp) | ||
| 36 | { | ||
| 37 | Console.WriteLine("usage: XsdGen.exe <schema>.xsd <outputFile> <namespace> [<commonNamespace>]"); | ||
| 38 | return; | ||
| 39 | } | ||
| 40 | |||
| 41 | // ensure that the schema file exists | ||
| 42 | if (!File.Exists(this.xsdFile)) | ||
| 43 | { | ||
| 44 | throw new ApplicationException(String.Format("Schema file does not exist: '{0}'.", this.xsdFile)); | ||
| 45 | } | ||
| 46 | |||
| 47 | XmlSchema document = null; | ||
| 48 | using (StreamReader xsdFileReader = new StreamReader(this.xsdFile)) | ||
| 49 | { | ||
| 50 | document = XmlSchema.Read(xsdFileReader, new ValidationEventHandler(this.ValidationHandler)); | ||
| 51 | } | ||
| 52 | |||
| 53 | CodeCompileUnit codeCompileUnit = StronglyTypedClasses.Generate(document, this.outputNamespace, this.commonNamespace); | ||
| 54 | |||
| 55 | using (CSharpCodeProvider codeProvider = new CSharpCodeProvider()) | ||
| 56 | { | ||
| 57 | ICodeGenerator generator = codeProvider.CreateGenerator(); | ||
| 58 | |||
| 59 | CodeGeneratorOptions options = new CodeGeneratorOptions(); | ||
| 60 | options.BlankLinesBetweenMembers = true; | ||
| 61 | options.BracingStyle = "C"; | ||
| 62 | options.IndentString = " "; | ||
| 63 | |||
| 64 | using (StreamWriter csharpFileWriter = new StreamWriter(this.outFile)) | ||
| 65 | { | ||
| 66 | generator.GenerateCodeFromCompileUnit(codeCompileUnit, csharpFileWriter, options); | ||
| 67 | } | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | /// <summary> | ||
| 72 | /// The main entry point for the application. | ||
| 73 | /// </summary> | ||
| 74 | /// <param name="args">The command line arguments.</param> | ||
| 75 | /// <returns>The error code.</returns> | ||
| 76 | [STAThread] | ||
| 77 | public static int Main(string[] args) | ||
| 78 | { | ||
| 79 | try | ||
| 80 | { | ||
| 81 | XsdGen xsdGen = new XsdGen(args); | ||
| 82 | } | ||
| 83 | catch (Exception e) | ||
| 84 | { | ||
| 85 | Console.WriteLine("XsdGen.exe : fatal error MSF0000: {0}\r\n\r\nStack Trace:\r\n{1}", e.Message, e.StackTrace); | ||
| 86 | return 1; | ||
| 87 | } | ||
| 88 | |||
| 89 | return 0; | ||
| 90 | } | ||
| 91 | |||
| 92 | /// <summary> | ||
| 93 | /// Validation event handler. | ||
| 94 | /// </summary> | ||
| 95 | /// <param name="sender">Sender for the event.</param> | ||
| 96 | /// <param name="e">Event args.</param> | ||
| 97 | public void ValidationHandler(object sender, ValidationEventArgs e) | ||
| 98 | { | ||
| 99 | } | ||
| 100 | |||
| 101 | /// <summary> | ||
| 102 | /// Parse the command line arguments. | ||
| 103 | /// </summary> | ||
| 104 | /// <param name="args">Command-line arguments.</param> | ||
| 105 | private void ParseCommandlineArgs(string[] args) | ||
| 106 | { | ||
| 107 | if (3 > args.Length) | ||
| 108 | { | ||
| 109 | this.showHelp = true; | ||
| 110 | } | ||
| 111 | else | ||
| 112 | { | ||
| 113 | this.xsdFile = args[0]; | ||
| 114 | this.outFile = args[1]; | ||
| 115 | this.outputNamespace = args[2]; | ||
| 116 | |||
| 117 | if (args.Length >= 4) | ||
| 118 | { | ||
| 119 | this.commonNamespace = args[3]; | ||
| 120 | } | ||
| 121 | } | ||
| 122 | } | ||
| 123 | } | ||
| 124 | } | ||
