Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: exclude imported types #364

Open
asherber opened this issue Nov 27, 2022 · 4 comments
Open

Feature request: exclude imported types #364

asherber opened this issue Nov 27, 2022 · 4 comments

Comments

@asherber
Copy link

Xsd2Code used to have an eit flag to exclude imported types, so that code would only be generated for the types in the schema files being looked at. This is useful when a schema makes use of a number of imported types that have already had their code files generated; we only care about the new types being added to the ecosystem.

In my case, I can fake this by using a custom OutputWriter that ignores all namespaces except the one that I'm interested in (since each of my XSDs defines a unique target namespaces) or by cleaning up after code generation and deleting the code files I don't need. But it would be nice if the generator could have a flag to handle this natively.

@mganss
Copy link
Owner

mganss commented Nov 29, 2022

That's a very good idea. Would you be interested in providing a PR?

@asherber
Copy link
Author

I'm happy to help, but this is an area of code I'm not very familiar with. (Which is why I've always used existing utilities!) Can you suggest some pseudo code or a place where this might go, to get me started?

@mganss
Copy link
Owner

mganss commented Nov 29, 2022

I think this could fit somewhere in here:

public void Generate(IEnumerable<string> files)
{
var set = new XmlSchemaSet();
var settings = new XmlReaderSettings { DtdProcessing = DtdProcessing.Ignore };
var readers = files.Select(f => XmlReader.Create(f, settings));
set.XmlResolver = new XmlUrlResolver();
set.ValidationEventHandler += (s, e) =>
{
var ex = e.Exception as Exception;
while (ex != null)
{
Log?.Invoke(ex.Message);
ex = ex.InnerException;
}
};
foreach (var reader in readers)
set.Add(null, reader);
Generate(set);
}
public void Generate(XmlSchemaSet set)
{
set.CompilationSettings.EnableUpaCheck = EnableUpaCheck;
set.Compile();
var m = new ModelBuilder(_configuration, set);
var namespaces = m.GenerateCode();
var writer = _configuration.OutputWriter ?? new FileOutputWriter(OutputFolder ?? ".") { Configuration = _configuration };
foreach (var ns in namespaces)
{
if (Version != null)
{
var comment = new StringBuilder($"This code was generated by {Version.Title}");
if (CreateGeneratedCodeAttributeVersion)
{
comment.Append($" version {Version.Version}");
}
if (GenerateCommandLineArgumentsComment)
{
comment.Append(" using the following command:");
}
ns.Comments.Add(new CodeCommentStatement(comment.ToString()));
if (GenerateCommandLineArgumentsComment)
{
ns.Comments.Add(new CodeCommentStatement(CommandLineArgumentsProvider?.CommandLineArguments ?? "N/A"));
}
}
writer.Write(ns);
}
}

@asherber
Copy link
Author

Thanks, I'll take a look!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants