2 using System.Collections.Generic;
3 using System.Diagnostics;
5 using System.Reflection;
7 using System.Xml.Schema;
11 namespace Mono.Documentation
13 public class MDocValidator : MDocCommand
15 XmlReaderSettings settings;
18 public override void Run (IEnumerable<string> args)
20 string[] validFormats = {
23 string format = "ecma";
24 var p = new OptionSet () {
26 "The documentation {0:FORMAT} used within PATHS. " +
27 "Valid formats include:\n " +
28 string.Join ("\n ", validFormats) + "\n" +
29 "If no format provided, `ecma' is used.",
32 List<string> files = Parse (p, args, "validate",
34 "Validate PATHS against the specified format schema.");
37 if (Array.IndexOf (validFormats, format) < 0)
38 Error ("Invalid documentation format: {0}.", format);
42 public void Run (string format, IEnumerable<string> files)
48 s = Assembly.GetExecutingAssembly ().GetManifestResourceStream ("monodoc-ecma.xsd");
52 throw new NotSupportedException (string.Format ("The format `{0}' is not suppoted.", format));
56 throw new NotSupportedException (string.Format ("The schema for `{0}' was not found.", format));
58 settings = new XmlReaderSettings ();
59 settings.Schemas.Add (XmlSchema.Read (s, null));
60 settings.Schemas.Compile ();
61 settings.ValidationType = ValidationType.Schema;
62 settings.ValidationEventHandler += OnValidationEvent;
64 // skip args[0] because it is the provider name
65 foreach (string arg in files) {
66 if (IsMonodocFile (arg))
69 if (Directory.Exists (arg))
71 RecurseDirectory (arg);
75 Message (errors == 0 ? TraceLevel.Info : TraceLevel.Error,
76 "Total validation errors: {0}", errors);
79 void ValidateFile (string file)
82 using (var reader = XmlReader.Create (new XmlTextReader (file), settings)) {
83 while (reader.Read ()) {
89 Message (TraceLevel.Error, "mdoc: {0}", e.ToString ());
93 void RecurseDirectory (string dir)
95 string[] files = Directory.GetFiles (dir, "*.xml");
96 foreach (string f in files)
98 if (IsMonodocFile (f))
102 string[] dirs = Directory.GetDirectories (dir);
103 foreach (string d in dirs)
104 RecurseDirectory (d);
107 void OnValidationEvent (object sender, ValidationEventArgs a)
110 Message (TraceLevel.Error, "mdoc: {0}", a.Message);
113 static bool IsMonodocFile (string file)
115 if (File.Exists (file) && Path.GetExtension (file).ToLower () == ".xml")