New test.
[mono.git] / mcs / class / System.XML / Test / System.Xml.Schema / standalone_tests / xs-psci-compare.cs
1 using System;
2 using System.Collections;
3 using System.IO;
4 using System.Xml;
5 using System.Xml.Schema;
6
7 public class Test
8 {
9         public static void Main (string [] args)
10         {
11                 if (args.Length == 0) {
12                         Console.WriteLine ("USAGE: xsdump masterlistname");
13                         return;
14                 }
15
16                 try {
17                         SchemaDumper.TestDir (args [0], Console.Out);
18                 } catch (Exception ex) {
19                         Console.WriteLine (ex);
20                 }
21         }
22 }
23
24 public class SchemaDumper
25 {
26         public static void TestDir (string masterlist, TextWriter w)
27         {
28                 FileInfo fi = new FileInfo (masterlist);
29                 string dirname = fi.Directory.Parent.FullName;
30
31                 SchemaDumper d = new SchemaDumper (w);
32 #if false
33                 foreach (DirectoryInfo di in new DirectoryInfo (dirname).GetDirectories ())
34                         foreach (FileInfo fi in di.GetFiles ("*.xsd")) {
35                                 try {
36                                         d.IndentLine ("**** File : " + fi.Name);
37                                         d.DumpSchema (XmlSchema.Read (new XmlTextReader (fi.FullName), null));
38                                 } catch (Exception ex) {
39                                         d.IndentLine ("**** Error in " + fi.Name);
40                                 }
41                         }
42 #else
43                 XmlDocument doc = new XmlDocument ();
44                 doc.Load (fi.FullName);
45
46                 foreach (XmlElement test in doc.SelectNodes ("/tests/test")) {
47                         // Test schema
48                         string schemaFile = test.SelectSingleNode ("@schema").InnerText;
49                         if (schemaFile.Length > 2)
50                                 schemaFile = schemaFile.Substring (2);
51                         bool isValidSchema = test.SelectSingleNode ("@out_s").InnerText == "1";
52
53                         if (!isValidSchema)
54                                 continue;
55 #endif
56                         try {
57                                 d.IndentLine ("**** File : " + schemaFile);
58                                 d.depth++;
59                                 XmlTextReader xtr = new XmlTextReader (dirname + "/" + schemaFile);
60                                 d.DumpSchema (XmlSchema.Read (xtr, null));
61                                 xtr.Close ();
62                         } catch (Exception ex) {
63                                 d.IndentLine ("**** Error in " + schemaFile);
64                         } finally {
65                                 d.depth--;
66                         }
67                 }
68         }
69
70         public int depth;
71         TextWriter w;
72         public SchemaDumper (TextWriter w)
73         {
74                 this.w = w;
75         }
76
77         public void IndentLine (object s)
78         {
79                 for (int i = 0; i < depth * 2; i++)
80                         w.Write (' ');
81                 w.WriteLine (s);
82         }
83
84         public void DumpSchema (XmlSchema schema)
85         {
86                 schema.Compile (null);
87
88                 SortedList sl = new SortedList ();
89
90                 IndentLine ("**XmlSchema**");
91                 IndentLine ("TargetNamespace: " + schema.TargetNamespace);
92                 IndentLine ("AttributeGroups:");
93                 foreach (DictionaryEntry entry in schema.AttributeGroups)
94                         sl.Add (entry.Key.ToString (), entry.Value);
95                 foreach (DictionaryEntry entry in sl)
96                         DumpAttributeGroup ((XmlSchemaAttributeGroup) entry.Value);
97                 sl.Clear ();
98
99                 IndentLine ("Attributes:");
100                 foreach (DictionaryEntry entry in schema.Attributes)
101                         sl.Add (entry.Key.ToString (), entry.Value);
102                 foreach (DictionaryEntry entry in sl)
103                         DumpAttribute ((XmlSchemaAttribute) entry.Value);
104                 sl.Clear ();
105
106                 IndentLine ("Elements:");
107                 foreach (DictionaryEntry entry in schema.Elements)
108                         sl.Add (entry.Key.ToString (), entry.Value);
109                 foreach (DictionaryEntry entry in sl)
110                         DumpElement ((XmlSchemaElement) entry.Value);
111                 sl.Clear ();
112
113                 IndentLine ("Groups");
114                 foreach (DictionaryEntry entry in schema.Groups)
115                         sl.Add (entry.Key.ToString (), entry.Value);
116                 foreach (DictionaryEntry entry in sl)
117                         DumpGroup ((XmlSchemaGroup) entry.Value);
118                 sl.Clear ();
119
120                 IndentLine ("IsCompiled: " + schema.IsCompiled);
121
122                 IndentLine ("Notations");
123                 foreach (DictionaryEntry entry in schema.Notations)
124                         sl.Add (entry.Key.ToString (), entry.Value);
125                 foreach (DictionaryEntry entry in sl)
126                         DumpNotation ((XmlSchemaNotation) entry.Value);
127                 sl.Clear ();
128
129                 IndentLine ("SchemaTypes:");
130                 foreach (DictionaryEntry entry in schema.Notations)
131                         sl.Add (entry.Key.ToString (), entry.Value);
132                 foreach (DictionaryEntry entry in sl)
133                         if (entry.Value is XmlSchemaSimpleType)
134                                 DumpSimpleType ((XmlSchemaSimpleType) entry.Value);
135                         else
136                                 DumpComplexType ((XmlSchemaComplexType) entry.Value);
137                 sl.Clear ();
138
139         }
140
141         public void DumpAttributeGroup (XmlSchemaAttributeGroup ag)
142         {
143                 depth++;
144
145                 IndentLine ("**AttributeGroup**");
146                 IndentLine ("Name = " + ag.Name);
147                 if (ag.RedefinedAttributeGroup != null) {
148                         IndentLine ("RedefinedGroup:");
149                         DumpAttributeGroup (ag.RedefinedAttributeGroup);
150                 }
151
152                 depth--;
153         }
154
155         public void DumpAttribute (XmlSchemaAttribute a)
156         {
157                 depth++;
158
159                 IndentLine ("**Attribute**");
160                 IndentLine ("QualifiedName: " + a.QualifiedName);
161                 IndentLine ("RefName: " + a.RefName);
162                 IndentLine ("AttributeType:");
163                 DumpType (a.AttributeType);
164
165                 depth--;
166         }
167
168         public void DumpElement (XmlSchemaElement e)
169         {
170                 depth++;
171
172                 IndentLine ("**Element**");
173                 IndentLine ("QualifiedName: " + e.QualifiedName);
174                 IndentLine ("ElementType:");
175                 DumpType (e.ElementType);
176
177                 depth--;
178         }
179
180         public void DumpGroup (XmlSchemaGroup g)
181         {
182                 depth++;
183
184                 IndentLine ("**Group**");
185                 IndentLine ("Name: " + g.Name);
186
187                 depth--;
188         }
189
190         public void DumpNotation (XmlSchemaNotation n)
191         {
192                 depth++;
193
194                 IndentLine ("**Notation**");
195                 IndentLine ("Name: " + n.Name);
196
197
198                 depth--;
199         }
200
201         public void DumpType (object type)
202         {
203                 depth++;
204
205                 if (type is XmlSchemaComplexType)
206                         DumpComplexType ((XmlSchemaComplexType) type);
207                 else if (type is XmlSchemaSimpleType)
208                         DumpSimpleType ((XmlSchemaSimpleType) type);
209                 else if (type is XmlSchemaDatatype)
210                         DumpDatatype ((XmlSchemaDatatype) type);
211                 else
212                         IndentLine ("Unexpected Type: " + type);
213
214                 depth--;
215         }
216
217         public void DumpSimpleType (XmlSchemaSimpleType s)
218         {
219                 depth++;
220
221                 IndentLine ("**SimpleType**");
222                 IndentLine ("QualifiedName: " + s.QualifiedName);
223                 IndentLine ("BaseSchemaType:");
224                 DumpType (s.BaseSchemaType);
225
226                 depth--;
227         }
228
229         public void DumpComplexType (XmlSchemaComplexType c)
230         {
231                 depth++;
232
233                 IndentLine ("**ComplexType**");
234                 IndentLine ("QualifiedName: " + c.QualifiedName);
235                 IndentLine ("ContentType: " + c.ContentType);
236                 IndentLine ("ContentTypeParticle: ");
237                 DumpParticle (c.ContentTypeParticle);
238                 IndentLine ("BaseSchemaType:");
239                 DumpType (c.BaseSchemaType);
240
241                 depth--;
242         }
243
244         public void DumpParticle (XmlSchemaParticle p)
245         {
246                 if (p is XmlSchemaGroupBase)
247                         DumpGroupBase ((XmlSchemaGroupBase) p);
248                 else if (p is XmlSchemaElement)
249                         DumpElementNoRecurse ((XmlSchemaElement) p);
250                 else if (p is XmlSchemaAny)
251                         DumpAny ((XmlSchemaAny) p);
252                 else
253                         IndentLine (p);
254         }
255
256         public void DumpDatatype (XmlSchemaDatatype d)
257         {
258                 depth++;
259
260                 IndentLine ("**Datatype**");
261                 IndentLine ("TokenizedType: " + d.TokenizedType);
262                 IndentLine ("ValueType: " + d.ValueType);
263
264                 depth--;
265         }
266
267         public void DumpGroupBase (XmlSchemaGroupBase gb)
268         {
269                 depth++;
270
271                 IndentLine ("**GroupBase**");
272                 IndentLine ("Type: " + gb);
273                 IndentLine ("MinOccurs: " + gb.MinOccurs);
274                 IndentLine ("MaxOccurs: " + gb.MaxOccurs);
275                 IndentLine ("Items: ");
276                 foreach (XmlSchemaParticle p in gb.Items)
277                         DumpParticle (p);
278
279                 depth--;
280         }
281
282         public void DumpElementNoRecurse (XmlSchemaElement e)
283         {
284                 depth++;
285
286                 IndentLine ("**Element**");
287                 IndentLine ("QualifiedName: " + e.QualifiedName);
288
289                 depth--;
290         }
291
292         public void DumpAny (XmlSchemaAny any)
293         {
294                 depth++;
295
296                 IndentLine ("**Any**");
297 //              IndentLine ("Namespace: " + any.Namespace);
298
299                 depth--;
300         }
301 }
302