Ejemplo n.º 1
0
        static string[] unwantedPrefixes = new string[] { "", "xsd", "abs", "cct" }; //,"ccts-cct" ,"ds", "xades" };


        /// <summary>
        /// </summary>
        public UblNamespaceManager(XmlSchemaSet schemaSet, string csDefaultNamespace, bool optimizeCommonBasicComponents)
        {
            this.schemaSet = schemaSet;
            this.csDefaultNamespace = csDefaultNamespace;
            this.OptionOptimizeCommonBasicComponents = optimizeCommonBasicComponents;

            // Build a xml namespace(key) to csharp codenamespace/scope(value) dictionary by looking at all schema root xmlns attributes
            // Will bomb out on Distinct() if schemas use different namespace prefixes for the same namespace (empty ones are removed)
            xml2CSharpNamespaceDictionary = 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 => $"{csDefaultNamespace}.{ CodeIdentifier.MakePascal(val.Name)}");

            // missing references in 2.1. Is it unused?
            xml2CSharpNamespaceDictionary.Add(Constants.CommonSignatureComponentsTargetNamespace, $"{csDefaultNamespace}.Csc");
            xml2CSharpNamespaceDictionary[Constants.Xadesv132TargetNamespace] = $"{csDefaultNamespace}.Xades";
            xml2CSharpNamespaceDictionary[Constants.Xadesv141TargetNamespace] = $"{csDefaultNamespace}.Xades";// 141"; // Probably incorrect

            // add key:xmlns value:cSharpScope for all maindocs. They all point to the same scope value string
            foreach (XmlSchema schema in schemaSet.MaindocSchemas().Cast<XmlSchema>())
            {
                string targetNamespace = schema.TargetNamespace;
                if (!xml2CSharpNamespaceDictionary.ContainsKey(targetNamespace))
                {
                    xml2CSharpNamespaceDictionary.Add(targetNamespace, csDefaultNamespace);
                }
            }

            // using directives in csharp files may vary depending on optimising of types
            if (this.OptionOptimizeCommonBasicComponents)
            {
                codeNamespaceUsings = codeNamespaceUsingsOptimized;
            }
            else
            {
                codeNamespaceUsings = codeNamespaceUsingsNonOptimized;
            }
            // prepend dictionary keys with default C# code namespace separated by a dot.
            codeNamespaceUsings = codeNamespaceUsings.ToDictionary(k => csDefaultNamespace + (k.Key == "" ? "" : ".") + k.Key, v => v.Value);
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            UblXsdSettings ublSettings = new UblXsdSettings
            {
                XsdValidationEvent = SchemaValidationEventHandler,
                UblXsdInputPath = @"..\..\..\UBL-2.1\xsd",  // Content originally from ubl zip download.
                CodeGenOutputPath = @"..\..\..\UblLarsen.Ubl2",
                OptionOptimizeCommonBasicComponents = true, // remove yagni types from schema before code generation. tests wont work when this is set to false
                CSharpDefaultNamespace = "UblLarsen.Ubl2",
                OptionMemberTypeToGenerate = UblXsdSettings.FieldTypesEnum.AutoProperty // or Field, or Property
            };

            XmlSchemaSet schemaSet = new XmlSchemaSet(new NameTable());
            schemaSet.ValidationEventHandler += SchemaValidationEventHandler;
            // Read and add the xsd documents in xsd/maindoc folder
            schemaSet.AddMaindocSchemas(ublSettings);
            schemaSet.Compile();

            if (ublSettings.OptionOptimizeCommonBasicComponents)
            {
                var cbcSchema = schemaSet.Schemas(Constants.CommonBasicComponentsTargetNamespace).Cast<XmlSchema>().Single();
                UblSchemaTypeSimplificationTool.SimplifyCommonBasicComponentsTypes(cbcSchema);
                schemaSet.Reprocess(cbcSchema);
                schemaSet.Compile();
            }

            UblSchemaTypeSimplificationTool.ResolveTypeNameClashesByRenaming(schemaSet);
            schemaSet.Compile();

            var abstractMaindocBaseSchema = UblSchemaInheritanceTools.ModifyMaindocSchemasForInheritance(schemaSet.MaindocSchemas());
            abstractMaindocBaseSchema = schemaSet.Add(abstractMaindocBaseSchema);
            schemaSet.MaindocSchemas().Where(s => s != abstractMaindocBaseSchema).ToList().ForEach(s => schemaSet.Reprocess(s));
            schemaSet.Compile();

            UblCodeGenerator gen = new UblCodeGenerator(ublSettings);
            // namespacelist parameter will drag in all other dependent types. New extensions shold probably be appended here... or...
            var allCodeDecls = gen.CreateCodeTypeDeclarations(schemaSet, 
                new[] {
                    Constants.BaseDocumentTargetNamespace,
                    Constants.SignatureAggregateComponents,
                    Constants.CommonSignatureComponentsTargetNamespace,
                    Constants.Xadesv141TargetNamespace}
                );

            UblDocumentationTool.AddDocumentation(allCodeDecls);

            UblImplicitAssignmentTool.AddImplicitAssignmentOperatorsForXmlTextAttributedDecendants(allCodeDecls);

            XsdTimeTool.IgnoreDateTimeSerializeAsString(allCodeDecls.Where(c => c.Name == "TimeType").Single());

            var mainDocCodeDecls = allCodeDecls.Where(c => c.BaseTypes.Count == 1 && c.BaseTypes[0].BaseType == Constants.abstractBaseSchemaComplexTypeName);
            MainDocsAttributeTool.RenameTopLevelXmlType(mainDocCodeDecls);
            MainDocsAttributeTool.AddXmlRootAttributes(mainDocCodeDecls);

            gen.GenerateAndSaveCodeFilesBySchema(allCodeDecls, new UblNamespaceManager(schemaSet, ublSettings.CSharpDefaultNamespace, ublSettings.OptionOptimizeCommonBasicComponents));

            UblSchemaStatsTool.ShowStats(allCodeDecls);

            Console.WriteLine("DONE");
            Console.ReadLine();
        }