kmpm / xsd2code

Open Xsd2Code - Modified fork of http://xsd2code.codeplex.com. Aiming for VS2015
MIT License
19 stars 34 forks source link

Generate code using XSD markup instead of file #16

Open furqansafdar opened 5 years ago

furqansafdar commented 5 years ago

I couldn't find a way to directly generate the code using XSD markup without using the file. Is it supported?

kmpm commented 5 years ago

I think you have to describe your wanted workflow a bit better. Without using which file? If not using a file with XSD then how would you feed the information to this solution?

furqansafdar commented 5 years ago

Simply, I wish to feed XSD markup to GeneratorParams as opposed to file input and wants GeneratorFacade class to return me the generated Assembly.

I have already extended the library for my needs but it would be nice to find this support part of the library itself. Also there should be extensibility options to override the functionality.

public class GeneratorParamsEx : GeneratorParams
{
    /// <summary>
    /// Gets or sets the XSD markup.
    /// </summary>
    /// <value>The output file path.</value>
    [Browsable(false)]
    public string XsdMarkup { get; set; }
}

public class GeneratorFacadeEx
{
    /// <summary>
    /// Instance of CodeDom provider
    /// </summary>
    private CodeDomProvider providerField;

    public GeneratorFacadeEx(GeneratorParams generatorParams)
    {
        var provider = CodeDomProviderFactory.GetProvider(generatorParams.Language);
        this.Init(provider, generatorParams);
    }

    /// <summary>
    /// Generator facade class constructor
    /// </summary>
    /// <param name="provider">Code DOM provider</param>
    /// <param name="generatorParams">Generator parameters</param>
    public GeneratorFacadeEx(CodeDomProvider provider, GeneratorParams generatorParams)
    {
        this.Init(provider, generatorParams);
    }

    /// <summary>
    /// Generator parameters
    /// </summary>
    public GeneratorParams GeneratorParams
    {
        get { return GeneratorContext.GeneratorParams; }
    }

    /// <summary>
    /// Initialize generator
    /// </summary>
    /// <param name="provider"></param>
    /// <param name="generatorParams"></param>
    public void Init(CodeDomProvider provider, GeneratorParams generatorParams)
    {
        this.providerField = provider;
        GeneratorContext.GeneratorParams = generatorParams.Clone();
    }

    #region Methods

    /// <summary>
    /// Processes the code generation.
    /// </summary>
    /// <returns>true if sucess or false.</returns>
    public Assembly Generate(GeneratorParams generatorParams)
    {
        GeneratorContext.GeneratorParams = generatorParams;
        var processResult = this.Process();
        return processResult;
    }

    /// <summary>
    /// Processes the code generation.
    /// </summary>
    /// <returns>true if sucess or false.</returns>
    public Assembly Generate()
    {
        var processResult = this.Process();
        return processResult;
    }

    /// <summary>
    /// Processes the specified file name.
    /// </summary>
    /// <returns>true if sucess else false</returns>
    private Assembly Process()
    {
        try
        {
            var result = Generator.Process(GeneratorContext.GeneratorParams);
            if (!result.Success) return null;

            var ns = result.Entity;
            using (var outputStream = new StringWriter())
            {
                this.providerField.GenerateCodeFromNamespace(ns, outputStream, new CodeGeneratorOptions());

                var parameters = new System.CodeDom.Compiler.CompilerParameters
                {
                    GenerateInMemory = true,
                    GenerateExecutable = false,
                    //IncludeDebugInformation = true,
                    //TreatWarningsAsErrors = false
                };
                // add referenced assemblies
                //parameters.ReferencedAssemblies.Add("mscorlib.dll");
                parameters.ReferencedAssemblies.Add("System.Xml.dll");
                parameters.ReferencedAssemblies.Add("Microsoft.CSharp.dll");

                var res = providerField.CompileAssemblyFromSource(
                    parameters,
                    outputStream.GetStringBuilder().ToString()
                );
                outputStream.Close();
                return res.CompiledAssembly;
            }
        }
        catch (Exception e)
        {
            var errorMessage = "Failed to generate code\n";
            errorMessage += "Exception :\n";
            errorMessage += e.Message;

            Debug.WriteLine(string.Empty);
            Debug.WriteLine("XSD2Code - ----------------------------------------------------------");
            Debug.WriteLine("XSD2Code - " + e.Message);
            Debug.WriteLine("XSD2Code - ----------------------------------------------------------");
            Debug.WriteLine(string.Empty);
        }
        return null;
    }

    #endregion
}
flonou commented 4 years ago

Hi @furqansafdar. I want to make sure I understand your need. Do you wish to give the library the XSD data as a string instead of a file ? (meaning, that XSD string would be equivalent to the content of an xsd file ?)

furqansafdar commented 4 years ago

@flonou, yes your understanding is correct. I want to feed XSD string instead of physical file.

flonou commented 4 years ago

24 Solves the first part of this issue (xsd string input)

furqansafdar commented 3 years ago

Thanks @flonou, but still i see the extensibility option is not provided, by that i means changing the private fields of GeneratorParams class to protected and secondly, the Assembly output, as i showed in my sample code. Can i participate to update the library directly?

flonou commented 3 years ago

Yes I did not meant that the issues wasn't valid anymore :) You can of course participate by submitting a PR