generator: Add option to validate GAPI XML against an XSD schema
With a new --schema option, you can specify the path to an XSD file, and all GAPI XML files will be validated against this schema, including the files given through the --include option.
This commit is contained in:
parent
57c82a89c7
commit
99cb57e7aa
2 changed files with 43 additions and 5 deletions
|
@ -24,6 +24,7 @@ namespace GtkSharp.Generation {
|
|||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Xml;
|
||||
|
||||
public class CodeGenerator {
|
||||
|
@ -36,6 +37,7 @@ namespace GtkSharp.Generation {
|
|||
string glue_filename = "";
|
||||
string glue_includes = "";
|
||||
string gluelib_name = "";
|
||||
string schema_name = "";
|
||||
|
||||
SymbolTable table = SymbolTable.Table;
|
||||
var gens = new List<IGeneratable> ();
|
||||
|
@ -59,6 +61,8 @@ namespace GtkSharp.Generation {
|
|||
{ "gluelib-name=", "Name of the C library into which the C glue code will be compiled. " +
|
||||
"Used to generated correct DllImport attributes.",
|
||||
(string v) => { gluelib_name = v; } },
|
||||
{ "schema=", "Validate all GAPI XML files against this XSD schema.",
|
||||
(string v) => { schema_name = v; } },
|
||||
{ "h|help", "Show this message and exit",
|
||||
v => show_help = v != null },
|
||||
};
|
||||
|
@ -90,14 +94,19 @@ namespace GtkSharp.Generation {
|
|||
return 0;
|
||||
}
|
||||
|
||||
if (!String.IsNullOrEmpty (schema_name) && !File.Exists (schema_name)) {
|
||||
Console.WriteLine ("WARNING: Could not find schema file at '{0}', no validation will be done.", schema_name);
|
||||
schema_name = null;
|
||||
}
|
||||
|
||||
Parser p = new Parser ();
|
||||
foreach (string include in includes) {
|
||||
IGeneratable[] curr_gens = p.Parse (include);
|
||||
IGeneratable[] curr_gens = p.Parse (include, schema_name);
|
||||
table.AddTypes (curr_gens);
|
||||
}
|
||||
|
||||
foreach (string filename in filenames) {
|
||||
IGeneratable[] curr_gens = p.Parse (filename);
|
||||
IGeneratable[] curr_gens = p.Parse (filename, schema_name);
|
||||
table.AddTypes (curr_gens);
|
||||
gens.AddRange (curr_gens);
|
||||
}
|
||||
|
|
|
@ -26,17 +26,28 @@ namespace GtkSharp.Generation {
|
|||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Xml;
|
||||
using System.Xml.Schema;
|
||||
|
||||
public class Parser {
|
||||
const int curr_parser_version = 2;
|
||||
|
||||
private XmlDocument Load (string filename)
|
||||
private XmlDocument Load (string filename, string schema_file)
|
||||
{
|
||||
XmlDocument doc = new XmlDocument ();
|
||||
|
||||
try {
|
||||
XmlReaderSettings settings = new XmlReaderSettings ();
|
||||
if (!String.IsNullOrEmpty (schema_file)) {
|
||||
settings.Schemas.Add (null, schema_file);
|
||||
settings.ValidationType = ValidationType.Schema;
|
||||
settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
|
||||
settings.ValidationEventHandler += ValidationEventHandler;
|
||||
}
|
||||
|
||||
Stream stream = File.OpenRead (filename);
|
||||
doc.Load (stream);
|
||||
XmlReader reader = XmlReader.Create (stream, settings);
|
||||
doc.Load (reader);
|
||||
|
||||
stream.Close ();
|
||||
} catch (XmlException e) {
|
||||
Console.WriteLine ("Invalid XML file.");
|
||||
|
@ -47,9 +58,27 @@ namespace GtkSharp.Generation {
|
|||
return doc;
|
||||
}
|
||||
|
||||
private void ValidationEventHandler(object sender, ValidationEventArgs e)
|
||||
{
|
||||
switch (e.Severity)
|
||||
{
|
||||
case XmlSeverityType.Error:
|
||||
Console.WriteLine("Error: {0}", e.Message);
|
||||
break;
|
||||
case XmlSeverityType.Warning:
|
||||
Console.WriteLine("Warning: {0}", e.Message);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public IGeneratable[] Parse (string filename)
|
||||
{
|
||||
XmlDocument doc = Load (filename);
|
||||
return Parse (filename, null);
|
||||
}
|
||||
|
||||
public IGeneratable[] Parse (string filename, string schema_file)
|
||||
{
|
||||
XmlDocument doc = Load (filename, schema_file);
|
||||
if (doc == null)
|
||||
return null;
|
||||
|
||||
|
|
Loading…
Reference in a new issue