예제 #1
0
 /// <summary>
 /// Writes a csharp header into the file.
 /// </summary>
 /// <param name="formatter">The formatter to use.</param>
 /// <param name="targetNamespace">The namespace for the class file.</param>
 /// <param name="options">Provides settings for the generation.</param>
 static void WriteCSharpHeader(this SimpleClassFormatter formatter, String targetNamespace, CommandLineOptions options)
 {
     formatter.UseNamespace(typeof(Int32));
     // Emit data contract namespace if required
     if (options.DataContracts)
     {
         formatter.UseNamespace(typeof(DataMemberAttribute));
     }
     formatter.BeginNamespace(targetNamespace);
 }
예제 #2
0
        static void WriteSequence(this SimpleClassFormatter formatter, XmlSchemaGroupBase sequence, CommandLineOptions options, Queue <XmlSchema> schemas, HashSet <XmlSchema> processedSchemas)
        {
            // Parse schema elements
            foreach (XmlSchemaElement sequenceItem in sequence.Items.OfType <XmlSchemaElement>().Where(p => p.ElementSchemaType != null))
            {
                formatter.WriteMember(sequenceItem, options, schemas, processedSchemas);
            }

            foreach (XmlSchemaGroupBase item in sequence.Items.OfType <XmlSchemaGroupBase>())
            {
                formatter.WriteSequence(item, options, schemas, processedSchemas);
            }
        }
예제 #3
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);
            }
        }
예제 #4
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();
        }
예제 #5
0
        /// <summary>
        /// Cleans a member name string into a .NET comform one.
        /// </summary>
        /// <param name="value">The value to clean.</param>
        /// <param name="upperCase">Specifies if all characters should be upper case.</param>
        /// <param name="formatter">The formatter which is used.</param>
        /// <param name="replacementPattern">Determines</param>
        /// <returns>The value of the string.</returns>
        public static String CleanMemberName(this String value, Boolean upperCase, SimpleClassFormatter formatter, String replacementPattern)
        {
            StringBuilder builder = new StringBuilder();

            // Remove unneccessary and forbidden chars
            foreach (String item in value.Split(SplitChars, StringSplitOptions.RemoveEmptyEntries))
            {
                // Ensure first character is always upper-case
                if (upperCase)
                {
                    // Append only the first character
                    builder.Append(Char.ToUpper(item[0]));
                    // Append rest of the string
                    builder.Append(item.Substring(1));
                }
                else
                {
                    builder.Append(item);
                }
            }
            String result = builder.ToString();

            return(result == formatter.CurrentTypeName ? String.Format(replacementPattern, result) : result);
        }
예제 #6
0
        /// <summary>
        /// Converts the content of the xsd into a class file.
        /// </summary>
        /// <param name="options">The command line options.</param>
        /// <returns>Retrieves the code which indicates a sucessful operation.</returns>
        private static Int32 ConvertXsd(CommandLineOptions options)
        {
            // Create a list with all files accepted by the converter and leave out any included classes which are not necessary
            Queue <XmlSchema> schemas = new Queue <XmlSchema>(options.InputFiles
                                                              .FilterValidUris()
                                                              .Select(LoadSchema)
                                                              .Distinct());
            // All schemas which have been processed
            HashSet <XmlSchema> processedSchemas = new HashSet <XmlSchema>(schemas);

            // Compile schemas
            LoadedSchemas.Compile();

            SimpleClassFormatter formatter = null;

            if (options.SingleElementFiles)
            {
                formatter = new SimpleClassFormatter(Path.Combine(Environment.CurrentDirectory, String.Join("_", options.InputFiles.Select(p => Path.GetFileNameWithoutExtension(p))) + ".cs"));
                formatter.WriteCSharpHeader(options.TargetNamespace, options);
            }

            // Go through all schemas which should be exported
            while (schemas.Count > 0)
            {
                XmlSchema schema = schemas.Dequeue();

                if (!options.SingleElementFiles)
                {
                    formatter = new SimpleClassFormatter(Path.Combine(Environment.CurrentDirectory, Path.GetFileNameWithoutExtension(LoadedLocations.First(p => p.Value == schema).Key) + ".cs"));
                    formatter.WriteCSharpHeader(options.TargetNamespace, options);
                }

                foreach (XmlSchemaElement element in schema.Elements.Values)
                {
                    // Elements do not have
                    if (element.SchemaType is XmlSchemaComplexType complex)
                    {
                        formatter.WriteComplexType(complex, element.Name, options, schemas, processedSchemas);
                    }
                    else
                    {
                        throw new NotImplementedException();
                    }
                }
                foreach (XmlSchemaType type in schema.SchemaTypes.Values)
                {
                    switch (type)
                    {
                    case XmlSchemaComplexType complex:
                        formatter.WriteComplexType(complex, null, options, schemas, processedSchemas);
                        break;

                    case XmlSchemaSimpleType simple:
                        formatter.WriteSimpleType(simple, options, schemas, processedSchemas);
                        break;
                    }
                }

                if (!options.SingleElementFiles)
                {
                    formatter.Dispose();
                }
            }

            // Release file
            if (options.SingleElementFiles)
            {
                formatter.Dispose();
            }

            return(0);
        }
예제 #7
0
 /// <summary>
 /// Writes a simple type into the file.
 /// </summary>
 /// <param name="formatter"></param>
 /// <param name="type"></param>
 /// <param name="options"></param>
 /// <param name="schemas"></param>
 /// <param name="processedSchemas"></param>
 static void WriteSimpleType(this SimpleClassFormatter formatter, XmlSchemaSimpleType type, CommandLineOptions options, Queue <XmlSchema> schemas, HashSet <XmlSchema> processedSchemas)
 {
     throw new NotImplementedException();
 }