Пример #1
0
        public static void GenerateXSD(GeneratorContext context, XmlSchema schema)
        {
            var enums         = new List <IEnum>();
            var generatedBDTs = new List <string>();

            //loop the bdt's
            foreach (IBdt bdt in context.Elements
                     .OfType <IBdt>().Where(x => !x.isDirectXSDType))
            {
                var xsdBdtName = NDR.GetXsdTypeNameFromBdt(bdt);
                //get the enum from the CON attribute and add it to the enums list.
                var enumToAdd = bdt.Con?.BasicType?.Enum;
                if (enumToAdd != null && !enums.Any(x => x.Name == enumToAdd.Name))
                {
                    enums.Add(enumToAdd);
                }
                //make sure we don't generate two BDT's witht he same xsdBdtName
                if (!generatedBDTs.Contains(xsdBdtName))
                {
                    //add the xsdBdtName to the list
                    generatedBDTs.Add(xsdBdtName);

                    var sups = new List <IBdtSup>(bdt.Sups);
                    if (!sups.Any())
                    {
                        var simpleType = new XmlSchemaSimpleType {
                            Name = xsdBdtName
                        };
                        var simpleTypeRestriction = new XmlSchemaSimpleTypeRestriction();
                        simpleTypeRestriction.BaseTypeName = GetXmlQualifiedName(NDR.getConBasicTypeName(bdt), context, bdt.Con?.BasicType);
                        simpleType.Content = simpleTypeRestriction;
                        if (bdt.Con != null &&
                            bdt.Con.BasicType != null &&
                            bdt.Con.BasicType.Prim != null)
                        {
                            var XSDtype = bdt.Con.BasicType.Prim.xsdType;
                            if (!string.IsNullOrEmpty(XSDtype))
                            {
                                simpleTypeRestriction.BaseTypeName = new XmlQualifiedName(NSPREFIX_XSD + ":" + XSDtype);
                            }
                        }

                        if (context.Annotate)
                        {
                            simpleType.Annotation = GetTypeAnnotation(bdt);
                        }
                        schema.Items.Add(simpleType);
                    }
                    else
                    {
                        //create the complex type
                        var complexType = new XmlSchemaComplexType();
                        complexType.Name = xsdBdtName;
                        //add the simple content extension
                        var simpleContent          = new XmlSchemaSimpleContent();
                        var simpleContentExtension = new XmlSchemaSimpleContentExtension();
                        //get the xsd type if the con is a primitive
                        if (bdt.Con.BasicType != null &&
                            bdt.Con.BasicType.Prim != null)
                        {
                            simpleContentExtension.BaseTypeName = GetXmlQualifiedName(bdt.Con.BasicType.Prim.xsdType, context, bdt.Con.BasicType);
                        }
                        else
                        {
                            var basicEnum = bdt.Con?.BasicType?.Enum;
                            if (basicEnum != null)
                            {
                                //enum was already added tot he list above
                                simpleContentExtension.BaseTypeName = new XmlQualifiedName(context.NamespacePrefix + ":" + NDR.GetBasicTypeName(basicEnum));
                            }
                            else
                            {
                                simpleContentExtension.BaseTypeName = GetXmlQualifiedName(NDR.getConBasicTypeName(bdt), context, bdt.Con.BasicType);
                            }
                        }

                        foreach (IBdtSup sup in sups)
                        {
                            var attribute = new XmlSchemaAttribute();
                            // Deviation from rule [R ABC1]: Using only attribute name and type as xml attribute name (instead of complete DEN), following the examples given in the specification.
                            attribute.Name = sup.Name;
                            //set optional or required
                            attribute.Use = sup.IsOptional() ? XmlSchemaUse.Optional : XmlSchemaUse.Required;
                            //set the type of the attribute
                            if (sup.BasicType != null &&
                                sup.BasicType.IsEnum)
                            {
                                //figure out if the set of values is restricted
                                var basicEnum = sup.BasicType.Enum as UpccEnum;
                                //add the enum to the list
                                if (!enums.Any(x => x.Name == basicEnum.Name))
                                {
                                    enums.Add(basicEnum);
                                }
                                if (basicEnum.CodelistEntries.Any())
                                {
                                    //add the restrictions
                                    var restrictedtype = new XmlSchemaSimpleType();
                                    var restriction    = new XmlSchemaSimpleTypeRestriction();
                                    restriction.BaseTypeName = new XmlQualifiedName(context.NamespacePrefix + ":" + NDR.GetBasicTypeName(basicEnum));
                                    addEnumerationValues(restriction, basicEnum);
                                    //add the restriction to the simple type
                                    restrictedtype.Content = restriction;
                                    //set the type of the attribute
                                    attribute.SchemaType = restrictedtype;
                                }
                                else
                                {
                                    attribute.SchemaTypeName = new XmlQualifiedName(context.NamespacePrefix + ":" + NDR.GetBasicTypeName(basicEnum));
                                }
                            }
                            //set regular type name if not restricted
                            if (attribute.SchemaTypeName.IsEmpty &&
                                attribute.SchemaType == null)
                            {
                                attribute.SchemaTypeName = GetXmlQualifiedName(NDR.GetBasicTypeName(sup as UpccAttribute), context, sup.BasicType);
                            }
                            //annotate if needed
                            if (context.Annotate)
                            {
                                attribute.Annotation = GetAttributeAnnotation(sup);
                            }
                            //add the attribute
                            simpleContentExtension.Attributes.Add(attribute);
                        }

                        simpleContent.Content    = simpleContentExtension;
                        complexType.ContentModel = simpleContent;
                        if (context.Annotate)
                        {
                            complexType.Annotation = GetTypeAnnotation(bdt);
                        }
                        schema.Items.Add(complexType);
                    }
                }
            }
            context.AddElements(enums);
        }
        private static XmlSchemaComplexType GenerateComplexTypeForMa(GeneratorContext context, XmlSchema schema, IMa ma, string abiePrefix)
        {
            var maType = new XmlSchemaComplexType();

            maType.Name = ma.Name + "Type";

            var sequence = new XmlSchemaSequence();

            foreach (IAsma asma in ma.Asmas.OrderBy(a => a.Name))
            {
                XmlSchemaElement elementAsma = null;

                // Take care of ASMAS aggregating ABIEs
                if (asma.AssociatedBieAggregator.IsAbie)
                {
                    elementAsma = new XmlSchemaElement
                    {
                        Name           = NDR.GetXsdElementNameFromAsma(asma),
                        SchemaTypeName =
                            new XmlQualifiedName(NSPREFIX_BIE + ":" +
                                                 asma.AssociatedBieAggregator.Name +
                                                 "Type")
                    };
                }
                else if (asma.AssociatedBieAggregator.IsMa)
                {
                    elementAsma = new XmlSchemaElement
                    {
                        Name           = NDR.GetXsdElementNameFromAsma(asma),
                        SchemaTypeName =
                            new XmlQualifiedName(context.NamespacePrefix + ":" +
                                                 asma.AssociatedBieAggregator.Name +
                                                 "Type")
                    };
                }

                var refAsma = new XmlSchemaElement
                {
                    RefName =
                        new XmlQualifiedName(context.NamespacePrefix + ":" +
                                             NDR.GetXsdElementNameFromAsma(asma))
                };

                // every shared ASCC may only appear once in the XSD file
                if (!globalAsmas.Contains(elementAsma.Name))
                {
                    // R 9241: for ASBIEs with AggregationKind = shared a global element must be declared.
                    schema.Items.Add(elementAsma);
                    globalAsmas.Add(elementAsma.Name);
                }

                sequence.Items.Add(refAsma);
            }
            maType.Particle = sequence;
            if (context.Annotate)
            {
                maType.Annotation = GetMaAnnotation(ma);
            }

            return(maType);
        }
Пример #3
0
 public static void GenerateXSD(GeneratorContext context, IEnumerable <IBdt> bdts, XmlSchema schema)
 {
     context.AddElements(bdts);
     GenerateXSD(context, schema);
 }
 private static void AddGlobalTypeDefinitions(XmlSchema schema, IEnumerable <IMa> mas, GeneratorContext context)
 {
     foreach (IMa ma in mas)
     {
         schema.Items.Add(GenerateComplexTypeForMa(context, schema, ma,
                                                   context.NamespacePrefix));
     }
 }
        internal static XmlSchemaComplexType GenerateComplexTypeACC(GeneratorContext context, XmlSchema schema, IAcc acc, string accPrefix)
        {
            // R A4CE, R AF95: a complex type must be defined for each ABIE
            XmlSchemaComplexType complexTypeACC = new XmlSchemaComplexType();

            // R 9D83: the name of the ABIE must be the DictionaryEntryName with all whitespace and separators
            //         removed. The 'Details' suffix is replaced with 'Type'.
            complexTypeACC.Name = acc.Name + "Type";

            if (context.Annotate)
            {
                complexTypeACC.Annotation = GetACCAnnotation(acc);
            }

            // create the sequence for the BBIEs within the ABIE
            XmlSchemaSequence sequenceBCCs = new XmlSchemaSequence();

            foreach (IBcc bcc in acc.Bccs.OrderBy(a => a.Name))
            {
                // R 89A6: for every BBIE a named element must be locally declared
                XmlSchemaElement elementBCC = new XmlSchemaElement();

                // R AEFE, R 96D9, R9A40, R A34A are implemented in GetXsdElementNameFromBbie(...)
                elementBCC.Name = NDR.GenerateBCCName(bcc);

                // R 8B85: every BBIE type must be named the property term and qualifiers and the
                //         representation term of the basic business information entity (BBIE) it represents
                //         with the word 'Type' appended.
                elementBCC.SchemaTypeName =
                    new XmlQualifiedName(NSPREFIX_CDT + ":" + bcc.Cdt.Name + bcc.Cdt.Con.BasicType.Name + "Type");


                // R 90F9: cardinality of elements within the ABIE
                elementBCC.MinOccursString = AdjustLowerBound(bcc.LowerBound);
                elementBCC.MaxOccursString = AdjustUpperBound(bcc.UpperBound);

                if (context.Annotate)
                {
                    elementBCC.Annotation = GetBCCAnnotation(bcc);
                }

                // add the element created to the sequence
                sequenceBCCs.Items.Add(elementBCC);
            }


            foreach (IAscc ascc in acc.Asccs.OrderBy(a => a.Name))
            {
                XmlSchemaElement elementASCC = new XmlSchemaElement();

                // R A08A: name of the ASBIE
                elementASCC.Name           = NDR.GenerateASCCName(ascc);
                elementASCC.SchemaTypeName =
                    new XmlQualifiedName(accPrefix + ":" + ascc.AssociatedAcc.Name + "Type");

                if (context.Annotate)
                {
                    elementASCC.Annotation = GetASCCAnnotiation(ascc);
                }

                // R 9241: for ASBIEs with AggregationKind = shared a global element must be declared.
                // ASCCs are always shared (as defined in UPCC)
                XmlSchemaElement refASCC = new XmlSchemaElement();
                refASCC.RefName = new XmlQualifiedName(accPrefix + ":" + elementASCC.Name);

                // every shared ASCC may only appear once in the XSD file
                if (!globalASCCs.Contains(elementASCC.Name))
                {
                    schema.Items.Add(elementASCC);
                    globalASCCs.Add(elementASCC.Name);
                }
                sequenceBCCs.Items.Add(refASCC);
            }

            // add the sequence created to the complex type
            complexTypeACC.Particle = sequenceBCCs;
            return(complexTypeACC);
        }
 internal static XmlSchemaComplexType GenerateComplexTypeACC(GeneratorContext context, XmlSchema schema, IAcc acc)
 {
     return(GenerateComplexTypeACC(context, schema, acc, context.NamespacePrefix));
 }
        ///<summary>
        ///</summary>
        ///<param name="context"></param>
        ///<param name="accs"></param>
        public static void GenerateXSD(GeneratorContext context, IEnumerable <IAcc> accs)
        {
            // Create XML schema file and prepare the XML schema header
            // R 88E2: all XML schema files must use UTF-8 encoding
            // R B387: every XML schema must have a declared target namespace
            globalASCCs = new List <string>();

            var schema = new XmlSchema {
                TargetNamespace = context.TargetNamespace
            };

            schema.Namespaces.Add(context.NamespacePrefix, context.TargetNamespace);
            schema.Version = context.DocLibrary.VersionIdentifier.DefaultTo("1");

            // TODO: discuss R A0E5 and R A9C5 with Christian E. and Michi S. since this is something that should be added to the context
            // R A0E5: all XML schemas must contain elementFormDefault and set it to qualified
            schema.ElementFormDefault = XmlSchemaForm.Qualified;
            // R A9C5: All XML schemas must contain attributeFormDefault and set it to unqualified
            schema.AttributeFormDefault = XmlSchemaForm.Unqualified;

            // R 9B18: all XML schemas must utilize the xsd prefix when referring to the W3C XML schema namespace

            schema.Namespaces.Add(NSPREFIX_XSD, NS_XSD);
            if (context.Annotate)
            {
                XmlSchemaImport import = new XmlSchemaImport
                {
                    Namespace =
                        "urn:un:unece:uncefact:documentation:standard:XMLNDRDocumentation:3",
                    SchemaLocation = "documentation/standard/XMLNDR_Documentation_3p0.xsd"
                };

                schema.Includes.Add(import);
                schema.Namespaces.Add(NSPREFIX_DOC, NS_DOC);
            }

            // add namespace to be able to utilize BDTs
            schema.Namespaces.Add(NSPREFIX_CDT, context.TargetNamespace);

            // R 8FE2: include BDT XML schema file
            // TODO: check with christian e. if we can retrieve the bdt schema file from the context
            XmlSchemaInclude cdtInclude = new XmlSchemaInclude();

            cdtInclude.SchemaLocation = "CoreDataType_" + schema.Version + ".xsd";
            schema.Includes.Add(cdtInclude);

            foreach (IAcc acc in accs.OrderBy(a => a.Name))
            {
                // finally add the complex type to the schema
                schema.Items.Add(GenerateComplexTypeACC(context, schema, acc));

                // R 9DA0: for each ABIE a named element must be globally declared
                // R 9A25: the name of the ABIE element must be the DictionaryEntryName with whitespace
                //         and the 'Details' suffix removed
                // R B27B: every ABIE global element declaration must be of the complexType that represents
                //         the ABIE
                XmlSchemaElement elementACC = new XmlSchemaElement();
                elementACC.Name           = acc.Name;
                elementACC.SchemaTypeName = new XmlQualifiedName(context.NamespacePrefix + ":" + acc.Name + "Type");
                schema.Items.Add(elementACC);
            }

            context.AddSchema(schema, "CoreComponent_" + schema.Version + ".xsd", UpccSchematype.ACC);
        }
Пример #8
0
        ///<summary>
        ///</summary>
        ///<param name="context"></param>
        ///<param name="cdts"></param>
        public static void GenerateXSD(GeneratorContext context, IEnumerable <ICdt> cdts)
        {
            var schema = new XmlSchema {
                TargetNamespace = context.TargetNamespace
            };

            schema.Namespaces.Add(context.NamespacePrefix, context.TargetNamespace);
            schema.Namespaces.Add("xsd", "http://www.w3.org/2001/XMLSchema");
            schema.Namespaces.Add("ccts", "urn:un:unece:uncefact:documentation:standard:XMLNDRDocumentation:3");
            schema.Version = context.DocLibrary.VersionIdentifier.DefaultTo("1");

            foreach (ICdt cdt in cdts)
            {
                var sups = new List <ICdtSup>(cdt.Sups);
                if (sups.Count == 0)
                {
                    var simpleType = new XmlSchemaSimpleType {
                        Name = GetTypeName(cdt)
                    };
                    var simpleTypeRestriction = new XmlSchemaSimpleTypeRestriction
                    {
                        BaseTypeName = GetXmlQualifiedName(cdt.Con.BasicType.Name)
                    };
                    simpleType.Content = simpleTypeRestriction;
                    if (context.Annotate)
                    {
                        simpleType.Annotation = GetTypeAnnotation(cdt);
                    }
                    schema.Items.Add(simpleType);
                }
                else
                {
                    var complexType = new XmlSchemaComplexType
                    {
                        // Deviation from rule [R 90FB]: Using simpler and shorter names for CDT complex types.
                        Name = GetTypeName(cdt)
                    };
                    var simpleContent          = new XmlSchemaSimpleContent();
                    var simpleContentExtension = new XmlSchemaSimpleContentExtension
                    {
                        BaseTypeName = GetXmlQualifiedName(cdt.Con.BasicType.Name)
                    };
                    foreach (ICdtSup sup in sups)
                    {
                        var attribute = new XmlSchemaAttribute
                        {
                            // Deviation from rule [R ABC1]: Using only attribute name and type as xml attribute name (instead of complete DEN), following the examples given in the specification.
                            Name           = GetAttributeName(sup),
                            SchemaTypeName = new XmlQualifiedName(GetXSDType(sup.BasicType.Name),
                                                                  "http://www.w3.org/2001/XMLSchema"),
                        };
                        if (context.Annotate)
                        {
                            attribute.Annotation = GetAttributeAnnotation(sup);
                        }
                        simpleContentExtension.Attributes.Add(attribute);
                    }

                    simpleContent.Content    = simpleContentExtension;
                    complexType.ContentModel = simpleContent;
                    if (context.Annotate)
                    {
                        complexType.Annotation = GetTypeAnnotation(cdt);
                    }
                    schema.Items.Add(complexType);
                }
            }

            context.AddSchema(schema, "CoreDataType_" + schema.Version + ".xsd");
        }