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