/// <summary> /// Performs building codedom tree objects from Xsd schema /// </summary> /// <param name="xsdContext">Holds data about XSD schema</param> /// <param name="codeDomContext">Holds codeDomTree</param> public void Execute(XsdContext xsdContext, CodeDomContext codeDomContext) { XmlSchemaImporter schemaImporter = new XmlSchemaImporter(xsdContext.XmlSchemas); // Step 1: Create code namespace xsdContext.CodeExporter = new XmlCodeExporter(codeDomContext.CodeNamespace); List <object> maps = new List <object>(); // Find out schema types of objects and add to collection foreach (XmlSchema xsd in xsdContext.XmlSchemaList) { foreach (XmlSchemaType schemaType in xsd.SchemaTypes.Values) { maps.Add(schemaImporter.ImportSchemaType(schemaType.QualifiedName)); } foreach (XmlSchemaElement schemaElement in xsd.Elements.Values) { maps.Add(schemaImporter.ImportTypeMapping(schemaElement.QualifiedName)); } } // export object collection to namespace. Now defined namespace will have all schema objects such as complexTypes, simpleTypes etc. foreach (XmlTypeMapping map in maps) { xsdContext.CodeExporter.ExportTypeMapping(map); } }
/// <summary> /// Adds required interface implementatio /// </summary> /// <param name="xsdContext">Holds data about XSD schema</param> /// <param name="codeDomContext">Holds codeDomTree</param> public void Execute(XsdContext xsdContext, CodeDomContext codeDomContext) { foreach (CodeTypeDeclaration codedomElement in codeDomContext.CodeNamespace.Types) { if (codedomElement.IsClass) { // Implement IEntity this.ImplementIEntity(codedomElement, xsdContext); // Replace DataType="date" with DataType="dateTime" in System.Xml.Serialization.XmlElementAttribute this.ReplaceDateTime(codedomElement); // Replace Item and Items property names to AnyIntuitObject and AnyIntuitObjects this.ReplaceItem(codedomElement); // It adds [JsonPropertyAttribute] to Section1 property of Section class this.ModifySectionClass(codedomElement); // adds [JsonIgnore] attribute if [XmlIgnore] presents this.AddJsonIgnore(codedomElement); } else if (codedomElement.IsEnum) { // Adds Query and Report enums this.AddOperationEnums(codedomElement); //This adds [JsonPropertyAttribute] attribute to the members of AccountTypeEnum this.ModifyAccountTypeEnumeration(codedomElement); } } }
/// <summary> /// Executes operation which would generate class from memory stream which contains parsed xsd schema /// </summary> /// <param name="xsdContext">Holds data about XSD schema</param> /// <param name="codeDomContext"> Holds codeDomTree </param> public void Execute(XsdContext xsdContext, CodeDomContext codeDomContext) { // Check for invalid characters in identifiers CodeGenerator.ValidateIdentifiers(codeDomContext.CodeNamespace); // output the C# code CSharpCodeProvider codeProvider = new CSharpCodeProvider(); CodeCompileUnit cu = new CodeCompileUnit(); codeDomContext.CodeNamespace.Imports.Add(new CodeNamespaceImport("Newtonsoft.Json")); cu.Namespaces.Add(codeDomContext.CodeNamespace); using (StringWriter writer = new StringWriter(CultureInfo.InvariantCulture)) { codeProvider.GenerateCodeFromNamespace(codeDomContext.CodeNamespace, writer, new CodeGeneratorOptions()); // Console.WriteLine(writer.GetStringBuilder().ToString()); StreamWriter outfile = new StreamWriter(this.GetOutputFilePath(), false); outfile.Write(writer.GetStringBuilder().ToString()); outfile.Flush(); outfile.Close(); } }
/// <summary> /// Facade method to generate classes from xsd for QBO /// </summary> private static void GenerateFMSEntities() { // Create xsd context objects XsdContext xsdContextQbo = new XsdContext(); // Create codedom context objects for QBO CodeDomContext codedomContextQbo = new CodeDomContext(DataObjectConstants.FMSENTITYNAMESPACE); TraceLogger target = new TraceLogger(); try { // create all tasks objects List <IXsdExtensionTask> xsdTasks = new List <IXsdExtensionTask>(); IXsdExtensionTask loadTask = new SchemaLoader(); IXsdExtensionTask domTreeTask = new CodeDomTreeBuilder(); IXsdExtensionTask annotationTask = new SchemaTagHandler(); IXsdExtensionTask hierarchyManager = new HierarchyManager(); IXsdExtensionTask classGenerator = new ClassGenerator(); // Add to xsdTasks to execute. It will be executed the same order. xsdTasks.Add(loadTask); xsdTasks.Add(domTreeTask); xsdTasks.Add(annotationTask); xsdTasks.Add(hierarchyManager); xsdTasks.Add(classGenerator); // Execute each tasks for QBO foreach (IXsdExtensionTask xsdTask in xsdTasks) { // QBO tasks execution Console.WriteLine(xsdTask.GetType().FullName + " started.."); target.Log(TraceLevel.Verbose, xsdTask.GetType().FullName + " started.."); xsdTask.Execute(xsdContextQbo, codedomContextQbo); Console.WriteLine(xsdTask.GetType().FullName + " completed"); target.Log(TraceLevel.Verbose, xsdTask.GetType().FullName + " completed.."); } } catch (System.Xml.Schema.XmlSchemaException schemaException) { Console.WriteLine("---------------------------"); Console.WriteLine("Error:"); Console.WriteLine(schemaException.Message); target.Log(TraceLevel.Error, schemaException.Message); Console.WriteLine("---------------------------"); } catch (Exception ex) { Console.WriteLine("---------------------------"); Console.WriteLine("Error:"); Console.WriteLine(ex.Message); target.Log(TraceLevel.Error, ex.Message); Console.WriteLine("---------------------------"); } }
/// <summary> /// Loads xsd schema files into memory and compiles. /// </summary> /// <param name="xsdContext">Holds data about XSD schema</param> /// <param name="codeDomContext">Holds code dom tree object</param> public void Execute(XsdContext xsdContext, CodeDomContext codeDomContext) { TraceLogger target = new TraceLogger(); XmlSchema xmlSchema = null; // Get XSD files string[] xsdFiles = null; string configKey = DataObjectConstants.INPUTSCHEMAFOLDER; // Get QBO xsd files from specified directory // if path is NOT specified in the config, use default location if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings[configKey])) { if (!Directory.Exists(ConfigurationManager.AppSettings[configKey])) { throw new FileNotFoundException(DataObjectConstants.XSDDIRINVALID); } target.Log(TraceLevel.Verbose, DataObjectConstants.FILESFROM + ConfigurationManager.AppSettings[configKey]); xsdFiles = Directory.GetFiles(ConfigurationManager.AppSettings[configKey], DataObjectConstants.FILESEXTENSIONS, SearchOption.AllDirectories); } else { string defaultXsdFilePath = this.GetXSDPath(); target.Log(TraceLevel.Verbose, DataObjectConstants.FILESFROM + defaultXsdFilePath); xsdFiles = Directory.GetFiles(defaultXsdFilePath, DataObjectConstants.FILESEXTENSIONS, SearchOption.AllDirectories); } if (xsdFiles.Length == 0) { target.Log(TraceLevel.Verbose, DataObjectConstants.XSDFILESNOTFOUND); } // Read .xsd files and create XmlSchema List foreach (string xsdFilePath in xsdFiles) { target.Log(TraceLevel.Verbose, DataObjectConstants.XSDFILESLOADEDFROM + xsdFilePath); using (FileStream stream = new FileStream(xsdFilePath, FileMode.Open, FileAccess.Read)) { xmlSchema = XmlSchema.Read(stream, null); xsdContext.XmlSchemaList.Add(xmlSchema); } // Console.WriteLine("xsd.IsCompiled {0} {1}", xsdPath, xmlSchema.IsCompiled); xsdContext.XmlSchemas.Add(xmlSchema); } xsdContext.XmlSchemas.Compile(null, true); }
/// <summary> /// Executes operation which would parse annotation section of XSD and adds to the CodeDOM /// </summary> /// <param name="xsdContext">Holds data about XSD schema</param> /// <param name="codeDomContext">Code dom tree</param> public void Execute(XsdContext xsdContext, CodeDomContext codeDomContext) { codeDom = codeDomContext.CodeNamespace; XmlSchema customerSchema = null; // Fetch each schema and update annotation foreach (XmlSchema xmlSchema in xsdContext.XmlSchemaList) { customerSchema = xmlSchema; XmlSchemaObjectCollection schemObjects = customerSchema.Items; foreach (XmlSchemaObject schemaObject in schemObjects) { HandleSchemaObject(schemaObject); } } }