private void CreateElementReference(XmlSchemaSequence itemElements, Property property) { // TODO refactor the model so base classes are used here var dataTypes = property.DataTypeName.Split(' '); if (!createdElements.ContainsKey(property.Name)) { XmlSchemaElement element = new XmlSchemaElement(); element.Name = property.Name; // if a reference to an item if (CogsModel.ItemTypes.Exists(x => dataTypes.Contains(x.Name))) { element.SchemaTypeName = new XmlQualifiedName("ReferenceType", TargetNamespace); } else if (CogsTypes.SimpleTypeNames.Contains(property.DataTypeName)) { if (string.Equals(property.DataTypeName, CogsTypes.CogsDate, StringComparison.OrdinalIgnoreCase)) { // make sure we use the right casing for the xs: type string xsTypeName = CogsTypes.SimpleTypeNames.First(x => x.Equals(property.DataTypeName, StringComparison.OrdinalIgnoreCase)); element.SchemaTypeName = new XmlQualifiedName(xsTypeName, TargetNamespace); } else { element.SchemaTypeName = new XmlQualifiedName(property.DataTypeName, XmlSchema.Namespace); } } else { element.SchemaTypeName = new XmlQualifiedName(property.DataTypeName, TargetNamespace); } CogsSchema.Items.Add(element); createdElements[property.Name] = property.DataTypeName; } // include reference to datatype property XmlSchemaElement datatypeRef = new XmlSchemaElement(); datatypeRef.RefName = new XmlQualifiedName(property.Name, TargetNamespace); datatypeRef.MinOccursString = property.MinCardinality == "" ? "0" : property.MinCardinality ?? "0"; datatypeRef.MaxOccursString = property.MaxCardinality == "n" ? "unbounded" : property.MaxCardinality ?? "unbounded"; datatypeRef.AddSchemaDocumentation(property.Description); itemElements.Items.Add(datatypeRef); }
XmlSchemaComplexType CreateItemContainerType(out XmlSchemaChoice itemChoices) { // Item Container type XmlSchemaComplexType containerType = new XmlSchemaComplexType(); containerType.Name = "ItemContainerType"; containerType.AddSchemaDocumentation("Used for serializing a set of items."); var sequence = new XmlSchemaSequence(); containerType.Particle = sequence; // Top level reference element XmlSchemaElement element = new XmlSchemaElement(); element.Name = "TopLevelReference"; element.SchemaTypeName = new XmlQualifiedName("ReferenceType", TargetNamespace); element.AddSchemaDocumentation("Denote which items in the Fragment Instance are the main items of interest."); CogsSchema.Items.Add(element); // include top level reference XmlSchemaElement elementRef = new XmlSchemaElement(); elementRef.RefName = new XmlQualifiedName("TopLevelReference", TargetNamespace); elementRef.MinOccurs = 0; elementRef.MaxOccursString = "unbounded"; sequence.Items.Add(elementRef); itemChoices = new XmlSchemaChoice(); itemChoices.MinOccurs = 0; itemChoices.MaxOccursString = "unbounded"; sequence.Items.Add(itemChoices); return(containerType); }
public void Publish(CogsModel model2) { if (CogsLocation == null) { throw new InvalidOperationException("Cogs location must be specified"); } if (TargetDirectory == null) { throw new InvalidOperationException("Target directory must be specified"); } if (Overwrite && Directory.Exists(TargetDirectory)) { Directory.Delete(TargetDirectory, true); Thread.Sleep(50); } Directory.CreateDirectory(TargetDirectory); CogsModel = model2; CogsSchema = new XmlSchema(); CogsSchema.TargetNamespace = TargetNamespace; CogsSchema.Namespaces.Add(TargetNamespacePrefix, TargetNamespace); CogsSchema.ElementFormDefault = XmlSchemaForm.Qualified; CogsSchema.AttributeFormDefault = XmlSchemaForm.Unqualified; // create built in types XmlSchemaComplexType referenceType = CreateReferenceType(); // Create the container /* * XmlSchemaComplexType containerType = new XmlSchemaComplexType(); * containerType.Name = "FragmentInstance"; * containerType.AddSchemaDocumentation("A Fragment Instance is used to transfer items plus any associated notes and other material. TopLevelReference provides a record of the main item of the FragmentInstance."); * cogsSchema.Items.Add(containerType); * * XmlSchemaSequence containerSequence = new XmlSchemaSequence(); * containerType.Particle = containerSequence; */ XmlSchemaChoice itemChoices = null; XmlSchemaComplexType containerType = CreateItemContainerType(out itemChoices); CogsSchema.Items.Add(containerType); XmlSchemaElement container = new XmlSchemaElement(); container.Name = "ItemContainer"; container.AddSchemaDocumentation("A Item Container is used to transfer items plus any associated notes and other material. TopLevelReference provides a record of the main item of the Item Container."); container.SchemaTypeName = new XmlQualifiedName("ItemContainerType", TargetNamespace); CogsSchema.Items.Add(container); foreach (var item in CogsModel.ItemTypes) { CreateDataType(item); // create a usage for the container XmlSchemaElement element = new XmlSchemaElement(); element.Name = item.Name; element.SchemaTypeName = new XmlQualifiedName(item.Name, TargetNamespace); CogsSchema.Items.Add(element); // include item in container via element reference XmlSchemaElement elementRef = new XmlSchemaElement(); elementRef.RefName = new XmlQualifiedName(item.Name, TargetNamespace); itemChoices.Items.Add(elementRef); } CreateCogsDataType(); foreach (var dataType in CogsModel.ReusableDataTypes) { CreateDataType(dataType); } XmlSchemaSet schemaSet = new XmlSchemaSet(); schemaSet.ValidationEventHandler += new ValidationEventHandler(ValidationCallback); schemaSet.Add(CogsSchema); schemaSet.Compile(); foreach (XmlSchema schema in schemaSet.Schemas()) { CogsSchema = schema; } // Write the complete schema XmlWriterSettings settings = new XmlWriterSettings(); settings.Encoding = Encoding.UTF8; settings.Indent = true; settings.IndentChars = " "; using (XmlWriter writer = XmlWriter.Create(Path.Combine(TargetDirectory, "schema.xsd"), settings)) { CogsSchema.Write(writer); } }