Example #1
0
        static void WriteComplexType(this SimpleClassFormatter formatter, XmlSchemaComplexType type, String name, CommandLineOptions options, Queue <XmlSchema> schemas, HashSet <XmlSchema> processedSchemas)
        {
            String baseType = null;
            XmlSchemaComplexContentExtension extension = type.ContentModel?.Content as XmlSchemaComplexContentExtension;

            // Check if we've a extension
            if (extension != null)
            {
                // Set base type
                baseType = extension.BaseTypeName.Name.CleanName(options.CleanNames);
                // Add schema for processing
                XmlSchema schema = LoadedSchemas.Schemas().Cast <XmlSchema>().FirstOrDefault(p => p.SchemaTypes.Contains(extension.BaseTypeName));
                if (schema != null && processedSchemas.Add(schema))
                {
                    schemas.Enqueue(schema);
                }
            }

            // Open class definition
            if (options.DataContracts)
            {
                if (name == null)
                {
                    formatter.WriteAttribute(typeof(DataContractAttribute), new KeyValuePair <String, String>("Namespace", $"\"{GetSchema(type).TargetNamespace}\""));
                }
                else
                {
                    formatter.WriteAttribute(typeof(DataContractAttribute), new KeyValuePair <String, String>("Name", $"\"{name}\""), new KeyValuePair <String, String>("Namespace", $"\"{GetSchema(type).TargetNamespace}\""));
                }
            }
            formatter.BeginClass((name ?? type.Name).CleanName(options.CleanNames), baseType, isAbstract: type.IsAbstract);

            if ((type.Particle ?? extension.Particle) is XmlSchemaGroupBase sequence)
            {
                formatter.WriteSequence(sequence, options, schemas, processedSchemas);
            }
            else if (type.Particle != null)
            {
                throw new NotImplementedException();
            }

            // Close class definition
            formatter.CloseBracket();
        }
Example #2
0
        /// <summary>
        /// Writes a member into the file.
        /// </summary>
        /// <param name="formatter">The formatter to use.</param>
        static void WriteMember(this SimpleClassFormatter formatter, XmlSchemaElement element, CommandLineOptions options, Queue <XmlSchema> schemas, HashSet <XmlSchema> processedSchemas)
        {
            String typeName   = null;
            String memberName = element.Name;

            if (element.ElementSchemaType is XmlSchemaSimpleType simple)
            {
                typeName = simple.Name ?? simple.Datatype.ValueType.Name;
            }
            else if (element.ElementSchemaType is XmlSchemaComplexType complex)
            {
                typeName = (complex.Name ?? (complex.Parent as XmlSchemaElement).Name);
                // For complex types we could have a different name than the default name of the element
                if (memberName == null)
                {
                    memberName = typeName;
                }
            }
            else
            {
                throw new NotImplementedException();
            }

            String cleanMemberName = memberName.CleanMemberName(options.CleanNames, formatter, options.RenamingPattern);
            String cleanTypeName   = typeName.CleanMemberName(options.CleanNames, formatter, options.RenamingPattern);

            // Write data contract if enabled
            if (options.DataContracts && memberName != cleanMemberName)
            {
                formatter.WriteAttribute(typeof(DataMemberAttribute), new KeyValuePair <String, String>("Name", $"\"{memberName}\""));
            }

            if (element.MaxOccurs > 1)
            {
                cleanTypeName = $"{cleanTypeName}[]";
            }
            formatter.WriteAutoProperty(cleanTypeName, cleanMemberName);

            XmlSchema memberSchema = GetSchema(element.ElementSchemaType);

            if (memberSchema != null && processedSchemas.Add(memberSchema))
            {
                schemas.Enqueue(memberSchema);
            }
        }