Example #1
0
        public CodeGenerationResult Generate(PhysicalSchema schema, string targetNamespace)
        {
            if (schema == null)
            {
                throw new ArgumentNullException(nameof(schema));
            }

            if (string.IsNullOrEmpty(targetNamespace))
            {
                throw new ArgumentException("Target namespace not provided");
            }

            switch (schema.DefinitionType)
            {
            case DefinitionType.Unknown:
                throw new ArgumentException(
                          "Unable to generate schema since the schema type is not known. This may be due to no schema files being present in the schema definition.");
                break;

            case DefinitionType.Json:
                return(jsonGenerator.Generate(schema, targetNamespace));

                break;

            case DefinitionType.Xsd:
                return(xsdGenerator.Generate(schema, targetNamespace));

                break;

            default:
                throw new ArgumentOutOfRangeException("Schema type was unexpected");
            }
        }
        public CodeGenerationResult Generate(PhysicalSchema schema, string targetNamespace)
        {
            if (schema == null)
            {
                throw new ArgumentNullException(nameof(schema));
            }

            if (string.IsNullOrEmpty(targetNamespace))
            {
                throw new ArgumentException(nameof(targetNamespace));
            }

            if (schema.Files == null || schema.Files.Count < 1)
            {
                throw new ArgumentException("json schema file was not provided", nameof(schema));
            }

            return(Generate(schema.Files[0].Content, schema.Files[0].FileName, targetNamespace));
        }
        internal static CodeNamespaceResult CreateCodeNamespace(PhysicalSchema schema, string targetNamespace)
        {
            XmlSchemaSet xset = new XmlSchemaSet();

            foreach (var file in schema.Files)
            {
                var sr = new StringReader(file.Content);
                xset.Add(XmlSchema.Read(sr, null));
            }

            xset.Compile();
            XmlSchemas schemas = new XmlSchemas();

            foreach (XmlSchema xmlSchema in xset.Schemas())
            {
                schemas.Add(xmlSchema);
            }

            XmlSchemaImporter importer = new XmlSchemaImporter(schemas);

            var ns       = new CodeNamespace(targetNamespace);
            var exporter = new XmlCodeExporter(ns);

            var result = new CodeNamespaceResult();

            foreach (XmlSchemaElement element in xset.GlobalElements.Values)
            {
                XmlTypeMapping mapping = importer.ImportTypeMapping(element.QualifiedName);

                if (string.IsNullOrEmpty(result.RootElementName))
                {
                    result.RootElementName = mapping.TypeName;
                }

                exporter.ExportTypeMapping(mapping);
            }

            result.Code = ns;
            return(result);
        }
        public CodeGenerationResult Generate(PhysicalSchema schema /* Maybe this should take a logical schema instead to help work out the root elements? */, string targetNamespace)
        {
            var code = CreateCodeNamespace(schema, targetNamespace);

            var provider = new Microsoft.CSharp.CSharpCodeProvider();

            var sb = new StringBuilder();

            using (StringWriter sw = new StringWriter(sb))
            {
                provider.CreateGenerator().GenerateCodeFromNamespace(code.Code, sw, new CodeGeneratorOptions());
            }

            var result = new CodeGenerationResult()
            {
                Code = sb.ToString()
            };

            TypeGenerator.BuildGeneratedCode(result, schema.Files[0].FileName /* TODO */, CreateMetadataReferences());
            result.RootTypeName = code.RootElementName;
            return(result);
        }