using System; using System.CodeDom; using System.Collections.Generic; using System.IO; using System.Xml.Schema; using System.Xml.Serialization; using Xsd2Code.Library.Extensions; using Xsd2Code.Library.Helpers; using System.Xml; using System.Linq; namespace Xsd2Code.Library { /// /// Generator class /// /// /// Revision history: /// /// Modified 2009-02-20 by Ruslan Urban /// Changed signature of the GeneratorFacade class constructor /// Modified 2009-05-18 by Pascal Cabanel /// Use CodeExtentionFactory /// public sealed class Generator { /// /// Extension Namespace const /// public const string ExtensionNamespace = "http://www.myxaml.fr"; /// /// Processes the specified XSD file. /// /// The XSD file. /// The target namespace. /// The language. /// Type of the collection. /// if set to true [enable data binding]. /// if set to true [hide private]. /// if set to true [enable summary comment]. /// The custom usings. /// The collection base. /// if set to true [include serialize method]. /// Name of the serialize method. /// Name of the deserialize method. /// Name of the save to file method. /// Name of the load from file method. /// /// /// result CodeNamespace [Obsolete("Do not use", true)] internal static Result Process(string xsdFile, string targetNamespace, GenerationLanguage language, CollectionType collectionType, bool enableDataBinding, bool hidePrivate, bool enableSummaryComment, List customUsings, string collectionBase, bool includeSerializeMethod, string serializeMethodName, string deserializeMethodName, string saveToFileMethodName, string loadFromFileMethodName, bool generateCloneMethod, TargetFramework targetFramework) { var generatorParams = new GeneratorParams { CollectionObjectType = collectionType, EnableDataBinding = enableDataBinding, HidePrivateFieldInIde = hidePrivate, Language = language, EnableSummaryComment = enableSummaryComment, CustomUsings = customUsings, CollectionBase = collectionBase, IncludeSerializeMethod = includeSerializeMethod, GenerateCloneMethod = generateCloneMethod, TargetFramework = targetFramework, SerializeMethodName = serializeMethodName, DeserializeMethodName = deserializeMethodName, SaveToFileMethodName = saveToFileMethodName, LoadFromFileMethodName = loadFromFileMethodName, }; return Process(generatorParams); } /// /// Initiate code generation process /// /// Generator parameters /// internal static Result Process(GeneratorParams generatorParams) { var ns = new CodeNamespace(); try { #region Set generation context GeneratorContext.GeneratorParams = generatorParams; #endregion #region Get XmlTypeMapping XmlSchema xsd; var schemas = new XmlSchemas(); xsd = XmlSchema.Read(XmlReader.Create(generatorParams.InputFilePath), new ValidationEventHandler(Validate)); XmlSchemaSet schemaSet = new XmlSchemaSet(); schemaSet.Add(xsd); schemaSet.Compile(); foreach (XmlSchema schema in schemaSet.Schemas()) { schemas.Add(schema); } var exporter = new XmlCodeExporter(ns); var importer = new XmlSchemaImporter(schemas); foreach (XmlSchemaElement element in xsd.Elements.Values) { var mapping = importer.ImportTypeMapping(element.QualifiedName); exporter.ExportTypeMapping(mapping); } //Fixes/handles http://xsd2code.codeplex.com/WorkItem/View.aspx?WorkItemId=6941 foreach (XmlSchemaComplexType complex in xsd.Items.OfType()) { var mapping = importer.ImportSchemaType(complex.QualifiedName); exporter.ExportTypeMapping(mapping); } #endregion #region Execute extensions var getExtensionResult = GeneratorFactory.GetCodeExtension(generatorParams); if (!getExtensionResult.Success) return new Result(ns, false, getExtensionResult.Messages); var ext = getExtensionResult.Entity; ext.Process(ns, xsd); #endregion Execute extensions return new Result(ns, true); } catch (Exception e) { return new Result(ns, false, e.Message, MessageType.Error); } } private static void Validate(Object sender, ValidationEventArgs e) { if (e.Severity == XmlSeverityType.Error) throw new Exception("Schema validation failed:\n" + e.Message); } } }