private static string GetTypeName(ICdt cdt)
 {
     return(cdt.Name + NDR.getConBasicTypeName(cdt) + "Type");
 }
        ///<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(NDR.getConBasicTypeName(cdt))
                    };
                    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(NDR.getConBasicTypeName(cdt))
                    };
                    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(NDR.GetBasicTypeName(sup as UpccAttribute)),
                                                                  "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", UpccSchematype.CDT);
        }
Ejemplo n.º 3
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);
        }