示例#1
0
        public CodeNamespaceProvider(XmlSchemaSet schemaSet, UblGeneratorOptions options)
        {
            _schemaSet = schemaSet;
            _options   = options;

            var mainSchemas = schemaSet.Schemas().Cast <XmlSchema>().Where(x => x.SourceUri.Contains("maindoc"));

            foreach (var schema in mainSchemas)
            {
                var targetNamespace = schema.TargetNamespace;
                _xmlToCsNamespaces[targetNamespace] = string.Empty;
            }

            foreach (var mapping in options.XmlToCsNamespaceMapping)
            {
                _xmlToCsNamespaces[mapping.Key] = mapping.Value;
            }
        }
示例#2
0
        public CodeNamespaceProvider(XmlSchemaSet schemaSet, UblGeneratorOptions options)
        {
            _schemaSet = schemaSet;

            _xml2CSharpNamespaceMapping = schemaSet.Schemas().Cast <XmlSchema>()
                                          .SelectMany(schema => schema.Namespaces.ToArray().Where(qname => !_unwantedPrefixes.Contains(qname.Name)))
                                          .Select(qname => new { qname.Namespace, qname.Name })
                                          .Distinct()
                                          .ToDictionary(key => key.Namespace, val => $"{options.Namespace}.{_realNamespaces[val.Name]}");

            _namespace2Foldermapping = schemaSet.Schemas().Cast <XmlSchema>()
                                       .SelectMany(schema => schema.Namespaces.ToArray().Where(qname => !_unwantedFolderPrefixes.Contains(qname.Name)))
                                       .Select(qname => new { qname.Namespace, qname.Name })
                                       .GroupBy(x => x.Namespace)
                                       .ToDictionary(key => key.Key, val => $"{_realNamespaces[val.First().Name]}");

            var mainSchemas = schemaSet.Schemas().Cast <XmlSchema>().Where(x => x.SourceUri.Contains("maindoc"));

            foreach (var schema in mainSchemas)
            {
                var targetNamespace = schema.TargetNamespace;
                if (!_xml2CSharpNamespaceMapping.ContainsKey(targetNamespace))
                {
                    _xml2CSharpNamespaceMapping[targetNamespace] = options.Namespace;
                }
            }

            _namespaceMappings = _namespaceMappings.ToDictionary(k => options.Namespace + (k.Key == "" ? "" : ".") + _realNamespaces[k.Key], v => v.Value.Select(n => _realNamespaces[n]).ToArray());

            _xml2CSharpNamespaceMapping[Namespaces.Csc]      = $"{options.Namespace}.{_realNamespaces["csc"]}";
            _xml2CSharpNamespaceMapping[Namespaces.Xades132] = $"{options.Namespace}.{_realNamespaces["xades"]}";
            _xml2CSharpNamespaceMapping[Namespaces.Xades141] = $"{options.Namespace}.{_realNamespaces["xades"]}";
            _namespace2Foldermapping[Namespaces.Csc]         = $"{_realNamespaces["csc"]}";
            _namespace2Foldermapping[Namespaces.Xades132]    = $"{_realNamespaces["xades"]}";
            _namespace2Foldermapping[Namespaces.Xades141]    = $"{_realNamespaces["xades"]}";
        }
示例#3
0
        public void Generate(UblGeneratorOptions options)
        {
            Console.WriteLine("Validating options...");

            _options = options;
            options.Validate();

            var baseInputDirectory = options.XsdBasePath;
            var maindocfiles       = new List <FileInfo>();

            if (!string.IsNullOrEmpty(baseInputDirectory))
            {
                maindocfiles = new DirectoryInfo(baseInputDirectory).GetFiles("*.xsd").ToList();
            }

            var maindocSchemaSet = new XmlSchemaSet()
            {
                // XmlResolver = xsdResolver
                // XmlResolver = null
            };

            Console.WriteLine("Read common UBL files from embedded resources...");

            var nameTable      = new NameTable();
            var readerSettings = new XmlReaderSettings
            {
                ValidationType = ValidationType.Schema,
                DtdProcessing  = DtdProcessing.Parse,
                NameTable      = nameTable
            };

            AddCommonFiles(maindocSchemaSet, readerSettings);

            Console.WriteLine($"Add schema files from {baseInputDirectory}...");

            var schemasToGenerate = new List <XmlSchema>();

            foreach (var maindocfile in maindocfiles)
            {
                using (var reader = XmlReader.Create(maindocfile.FullName, readerSettings))
                {
                    var schema = XmlSchema.Read(reader, null);

                    // ReplaceKnownSchemaIncludes(schema, knownSchema);
                    maindocSchemaSet.Add(schema);
                    schemasToGenerate.Add(schema);
                }
            }

            Console.WriteLine("Fixing schema...");

            maindocSchemaSet.Compile();

            foreach (var schemaFixer in _schemaFixers)
            {
                schemaFixer(maindocSchemaSet);
                maindocSchemaSet.Compile();
            }

            Console.WriteLine("Creating compile unit...");

            var namespaceProvider = new CodeNamespaceProvider(maindocSchemaSet, options);
            var compileUnit       = CreateCodeNamespace(maindocSchemaSet, namespaceProvider);

            foreach (var ns in compileUnit.Namespaces.OfType <CodeNamespace>())
            {
                _globalCodeFixer.Fix(ns);
            }

            var codeProvider   = new CSharpCodeProvider();
            var codegenOptions = new CodeGeneratorOptions()
            {
                BracingStyle             = "C",
                IndentString             = "    ",
                BlankLinesBetweenMembers = true,
                VerbatimOrder            = true
            };

            Console.WriteLine("Generating source files...");

            if (options.GenerateCommonFiles)
            {
                foreach (var schema in maindocSchemaSet.Schemas().Cast <XmlSchema>())
                {
                    GenerateCodeForSchema(options, compileUnit, namespaceProvider, codeProvider, codegenOptions, schema);
                }
            }
            else
            {
                foreach (var schema in schemasToGenerate)
                {
                    GenerateCodeForSchema(options, compileUnit, namespaceProvider, codeProvider, codegenOptions, schema);
                }
            }
        }
示例#4
0
        private void GenerateCodeForSchema(UblGeneratorOptions options, CodeCompileUnit compileUnit, CodeNamespaceProvider namespaceProvider, CSharpCodeProvider codeProvider, CodeGeneratorOptions codegenOptions, XmlSchema schema)
        {
            var tmpCodeNamespace  = compileUnit.Namespaces.OfType <CodeNamespace>().Single(x => x.UserData[CodeTypeMemberExtensions.XmlSchemaKey] == schema);
            var codeDeclsBySchema = (from t in tmpCodeNamespace.Types.Cast <CodeTypeDeclaration>()
                                     group t by t.GetSchema()
                                     into g
                                     select g)
                                    .ToDictionary(k => k.Key, v => v.ToArray());

            var codeNamespace = namespaceProvider.CreateCodeNamespace(schema);

            if (codeDeclsBySchema.ContainsKey(schema))
            {
                codeNamespace.Types.AddRange(codeDeclsBySchema[schema]);
            }

            if (codeNamespace.Types.Count == 0)
            {
                return;
            }

            _namespacedCodeFixer.Fix(codeNamespace);

            var sb = new StringBuilder();

            using (var sw = new StringWriter(sb))
            {
                codeProvider.GenerateCodeFromNamespace(codeNamespace, sw, codegenOptions);
            }

            sb = sb.Replace("Namespace=\"", "Namespace = \"");

            var fileContent = sb.ToString();
            var lines       = fileContent.Split(new[] { Environment.NewLine }, StringSplitOptions.None).ToList();

            foreach (var fixer in _conditionalFeatureFixers)
            {
                int lineNum = 0;
                while (true)
                {
                    lineNum = fixer(lines, lineNum);
                    if (lineNum < 0)
                    {
                        break;
                    }
                }
            }

            sb = new StringBuilder(string.Join(Environment.NewLine, lines));

            var    xsdFilename = new Uri(schema.SourceUri).LocalPath;
            var    fi          = new FileInfo(xsdFilename);
            var    foldername  = namespaceProvider.GetNamespaceFolderName(schema);
            string targetPath  = Path.Combine(options.OutputPath, foldername);

            if (!Directory.Exists(targetPath))
            {
                Directory.CreateDirectory(targetPath);
            }

            var outputFile = Path.Combine(targetPath, Path.ChangeExtension(fi.Name, ".generated.cs"));

            using (var ofile = File.CreateText(outputFile))
            {
                ofile.Write(sb);
                Console.WriteLine($"Written: {outputFile}");
            }
        }
示例#5
0
        public void Generate(UblGeneratorOptions options)
        {
            _options = options;
            options.Validate();

            var baseInputDirectory = options.XsdBasePath;
            var commonDirectory    = Path.Combine(baseInputDirectory, "common");
            var xmldsigFilename    = new DirectoryInfo(commonDirectory).GetFiles("UBL-xmldsig-core-schema-*.xsd").Single().FullName;
            var maindocDirectory   = Path.Combine(baseInputDirectory, "maindoc");
            var maindocfiles       = new DirectoryInfo(maindocDirectory).GetFiles("*.xsd").ToList();
            // var extrafiles = new DirectoryInfo(commonDirectory).GetFiles("UBL-CommonSignatureComponents*.xsd").ToList();
            // var maindocfiles = new DirectoryInfo(maindocDirectory).GetFiles("UBL-Order-2.1.xsd").ToList();
            // maindocfiles.Add(new DirectoryInfo(maindocDirectory).GetFiles("UBL-BaseDocument-*.xsd").Single());

            var maindocSchemaSet = new XmlSchemaSet();

            var nameTable      = new NameTable();
            var readerSettings = new XmlReaderSettings
            {
                ValidationType = ValidationType.Schema,
                DtdProcessing  = DtdProcessing.Parse,
                NameTable      = nameTable,
            };

            using (var reader = XmlReader.Create(xmldsigFilename, readerSettings))
            {
                var schema = XmlSchema.Read(reader, null);
                maindocSchemaSet.Add(schema);
            }

            foreach (var maindocfile in maindocfiles)
            {
                using (var reader = XmlReader.Create(maindocfile.FullName, readerSettings))
                {
                    var schema = XmlSchema.Read(reader, null);
                    maindocSchemaSet.Add(schema);
                }
            }

            //foreach (var extrafile in extrafiles)
            //{
            //    using (var reader = XmlReader.Create(extrafile.FullName, readerSettings))
            //    {
            //        var schema = XmlSchema.Read(reader, null);
            //        maindocSchemaSet.Add(schema);
            //    }
            //}

            maindocSchemaSet.Compile();

            foreach (var schemaFixer in _schemaFixers)
            {
                schemaFixer(maindocSchemaSet);
                maindocSchemaSet.Compile();
            }

            var rootNamespaces = maindocSchemaSet.Schemas().OfType <XmlSchema>()
                                 .Where(x => x.SourceUri.Contains("maindoc"))
                                 .Select(x => x.TargetNamespace)
                                 .Concat(new[]
            {
                Namespaces.Xmldsig,
                Namespaces.Sac,
                Namespaces.Csc,
                Namespaces.Xades141,
            });

            var tempCodeNamespace = CreateCodeNamespace(maindocSchemaSet, rootNamespaces.ToArray());

            _globalCodeFixer.Fix(tempCodeNamespace);

            var codeDeclsBySchema = (from t in tempCodeNamespace.Types.Cast <CodeTypeDeclaration>()
                                     group t by t.GetSchema() into g
                                     select g)
                                    .ToDictionary(k => k.Key, v => v.ToArray());

            var codeProvider   = new Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider();
            var codegenOptions = new CodeGeneratorOptions()
            {
                BracingStyle             = "C",
                IndentString             = "    ",
                BlankLinesBetweenMembers = true,
                VerbatimOrder            = true
            };

            var namespaceProvider = new CodeNamespaceProvider(maindocSchemaSet, options);

            foreach (var schema in maindocSchemaSet.Schemas().Cast <XmlSchema>())
            {
                var codeNamespace = namespaceProvider.CreateCodeNamespace(schema.TargetNamespace);
                if (codeDeclsBySchema.ContainsKey(schema))
                {
                    codeNamespace.Types.AddRange(codeDeclsBySchema[schema]);
                }

                if (codeNamespace.Types.Count == 0)
                {
                    continue;
                }

                _namespacedCodeFixer.Fix(codeNamespace);

                var sb = new StringBuilder();
                using (var sw = new StringWriter(sb))
                {
                    codeProvider.GenerateCodeFromNamespace(codeNamespace, sw, codegenOptions);
                }

                sb = sb.Replace("Namespace=\"", "Namespace = \"");

                var fileContent = sb.ToString();
                var lines       = fileContent.Split(new[] { Environment.NewLine }, StringSplitOptions.None).ToList();

                foreach (var fixer in _conditionalFeatureFixers)
                {
                    int lineNum = 0;
                    while (true)
                    {
                        lineNum = fixer(lines, lineNum);
                        if (lineNum < 0)
                        {
                            break;
                        }
                    }
                }

                sb = new StringBuilder(string.Join(Environment.NewLine, lines));

                var    xsdFilename = new Uri(schema.SourceUri).LocalPath;
                var    fi          = new FileInfo(xsdFilename);
                var    foldername  = namespaceProvider.GetNamespaceFolderName(schema);
                string targetPath  = Path.Combine(options.OutputPath, foldername);
                if (!Directory.Exists(targetPath))
                {
                    Directory.CreateDirectory(targetPath);
                }
                var outputFile = Path.Combine(targetPath, Path.ChangeExtension(fi.Name, ".generated.cs"));

                using (var ofile = File.CreateText(outputFile))
                {
                    ofile.Write(sb);
                }
            }
        }