Debian Codegraph @

mcs/class/Commons.Xml.Relaxng/Test/standalone_tests/relaxngtest.cs

raw
1 using System;
2 using System.IO;
3 using System.Xml;
4 using Commons.Xml.Relaxng;
5 using Commons.Xml.Relaxng.Derivative;
6
7 public class Test
8 {
9 static char SEP = Path.DirectorySeparatorChar;
10 static bool skip_error = true;
11
12 public static void Main (string [] args)
13 {
14 if (args.Length > 0 && args [0] == "--skip-error")
15 skip_error = true;
16
17 Console.WriteLine ("Started: " + DateTime.Now.ToString ("yyyy-MM-dd HH:mm:ss.fff"));
18 RunTest ();
19 Console.WriteLine ("Finished: " + DateTime.Now.ToString ("yyyy-MM-dd HH:mm:ss.fff"));
20 }
21
22 static void RunTest ()
23 {
24 foreach (DirectoryInfo di in new DirectoryInfo (@"relax-ng").GetDirectories ()) {
25 XmlTextReader xtr = null;
26 FileInfo fi = new FileInfo (di.FullName + "/i.rng");
27 // Invalid grammar case:
28 if (fi.Exists) {
29 xtr = new XmlTextReader (fi.FullName);
30 try {
31 RelaxngPattern.Read (xtr).Compile ();
32 Console.WriteLine ("Expected error: " + di.Name);
33 } catch (RelaxngException ex) {
34 } catch (XmlException ex) {
35 } catch (ArgumentNullException ex) {
36 } catch (UriFormatException ex) {
37 } catch (Exception ex) {
38 Console.WriteLine ("Unexpected error type : " + di.Name + " : " + ex.Message);
39 } finally {
40 xtr.Close ();
41 }
42 continue;
43 }
44
45 // Valid grammar case:
46 xtr = new XmlTextReader (di.FullName + "/c.rng");
47 RelaxngPattern p = null;
48 try {
49 p = RelaxngPattern.Read (xtr);
50 p.Compile ();
51 } catch (Exception ex) {
52 Console.WriteLine ("Invalidated grammar: " + di.Name + " : " + ex.Message);
53 continue;
54 } finally {
55 xtr.Close ();
56 }
57
58
59 // Instance validation
60 foreach (FileInfo inst in di.GetFiles ("*.xml")) {
61 try {
62 RelaxngValidatingReader vr = new RelaxngValidatingReader (new XmlTextReader (inst.FullName), p);
63 if (skip_error)
64 vr.InvalidNodeFound += RelaxngValidatingReader.IgnoreError;
65 while (!vr.EOF)
66 vr.Read ();
67 if (inst.Name.IndexOf ("i.") >= 0 && !skip_error)
68 Console.WriteLine ("Incorrectly validated instance: " + di.Name + "/" + inst.Name);
69 } catch (RelaxngException ex) {
70 string path = di.Name + "/" + inst.Name;
71 if (skip_error)
72 Console.WriteLine ("Failed to skip error : " + path + ex.Message);
73 if (inst.Name.IndexOf ("i.") >= 0)
74 continue;
75 Console.WriteLine ("Invalidated instance: " + path + " : " + ex.Message);
76 }
77 }
78 }
79 }
80 }