* . (svn:ignore), Test (svn:ignore): Ignore generated files.
[mono.git] / mcs / tools / mdoc / Mono.Documentation / validate.cs
1 using System;
2 using System.Collections.Generic;
3 using System.IO;
4 using System.Reflection;
5 using System.Xml;
6 using System.Xml.Schema;
7
8 namespace Mono.Documentation
9 {
10         public class MDocValidator
11         {
12                 static XmlValidatingReader reader;
13                 static XmlSchema schema;
14                 static long errors = 0;
15                 static bool IsValid = true;
16         
17                 public static void Run (string format, IEnumerable<string> files)
18                 {
19                         Stream s = null;
20
21                         switch (format) {
22                                 case "ecma":
23                                         s = Assembly.GetExecutingAssembly ().GetManifestResourceStream ("monodoc-ecma.xsd");
24                                         break;
25
26                                 default:
27                                         throw new NotSupportedException (string.Format ("The format `{0}' is not suppoted.", format));
28                         }
29
30                         if (s == null)
31                                 throw new NotSupportedException (string.Format ("The schema for `{0}' was not found.", format));
32
33                         schema = XmlSchema.Read (s, null);
34                         schema.Compile (null);
35
36                         // skip args[0] because it is the provider name
37                         foreach (string arg in files) {
38                                 if (IsMonodocFile (arg))
39                                         ValidateFile (arg);
40
41                                 if (Directory.Exists (arg))
42                                 {
43                                         RecurseDirectory (arg);
44                                 }
45                         }
46
47                         Console.WriteLine ("Total validation errors: {0}", errors);
48                 }
49
50                 static void ValidateFile (string file)
51                 {
52                         IsValid = true;
53                         try {
54                                 reader = new XmlValidatingReader (new XmlTextReader (file));
55                                 reader.ValidationType = ValidationType.Schema;
56                                 reader.Schemas.Add (schema);
57                                 reader.ValidationEventHandler += new ValidationEventHandler (OnValidationEvent);
58                                 while (reader.Read ()) {
59                                         // do nothing
60                                 }
61                                 reader.Close ();
62                         }
63                         catch (Exception e) {
64                                 Console.WriteLine ("mdvalidator: error: " + e.ToString ());
65                         }
66                 }
67
68                 static void RecurseDirectory (string dir)
69                 {
70                         string[] files = Directory.GetFiles (dir, "*.xml");
71                         foreach (string f in files)
72                         {
73                                 if (IsMonodocFile (f))
74                                         ValidateFile (f);
75                         }
76
77                         string[] dirs = Directory.GetDirectories (dir);
78                         foreach (string d in dirs)
79                                 RecurseDirectory (d);
80                 }
81
82                 static void OnValidationEvent (object sender, ValidationEventArgs a)
83                 {
84                         if (IsValid)
85                                 IsValid = false;
86                         errors ++;
87                         Console.WriteLine (a.Message);
88                 }
89
90                 static bool IsMonodocFile (string file)
91                 {
92                                 if (File.Exists (file) && Path.GetExtension (file).ToLower () == ".xml")
93                                         return true;
94                                 else
95                                         return false;
96                         
97                 }
98         }
99 }
100