New test.
[mono.git] / mcs / class / Commons.Xml.Relaxng / Commons.Xml.Nvdl / NvdlStructures.cs
1 using System;
2 using System.Collections;
3 using System.Collections.Specialized;
4 using System.Xml;
5 using System.Xml.Schema;
6 using Commons.Xml.Relaxng;
7
8 //using Map = Commons.Xml.Relaxng.ObjectMapping.RelaxngMapping;
9 //using Choice = Commons.Xml.Relaxng.ObjectMapping.RelaxngMappingChoice;
10
11 namespace Commons.Xml.Nvdl
12 {
13         public class Nvdl
14         {
15                 private Nvdl () {}
16
17                 public const string Namespace = "http://purl.oclc.org/dsdl/nvdl/ns/structure/1.0";
18                 public const string BuiltInValidationNamespace = "http://purl.oclc.org/dsdl/nvdl/ns/predefinedSchema/1.0";
19
20                 public const string InstanceNamespace = "http://purl.oclc.org/dsdl/nvdl/ns/instance/1.0";
21
22                 internal const string XmlNamespaceUri = "http://www.w3.org/xml/1998/namespace";
23
24                 private static void OnDefaultEvent (object o, 
25                         NvdlMessageEventArgs e)
26                 {
27                         Console.WriteLine (e.Message);
28                 }
29
30                 internal static NvdlMessageEventHandler HandlePrintMessage =
31                         new NvdlMessageEventHandler (OnDefaultEvent);
32
33                 readonly static NvdlConfig defaultConfig;
34
35                 static Nvdl ()
36                 {
37                         defaultConfig = new NvdlConfig ();
38                         defaultConfig.AddProvider (new NvdlXsdValidatorProvider ());
39                         defaultConfig.AddProvider (new NvdlRelaxngValidatorProvider ());
40                 }
41
42                 internal static NvdlConfig DefaultConfig {
43                         get { return defaultConfig; }
44                 }
45
46                 internal static readonly char [] Whitespaces =
47                         new char [] {' ', '\r', '\n', '\t'};
48
49                 // See 6.4.12.
50                 internal static bool NSMatches (string n1, int i1, string w1,
51                         string n2, int i2, string w2)
52                 {
53                         // quick check
54                         if (n1 == n2)
55                                 return true;
56
57                         // Case 1:
58                         if (n1.Length <= i1 && n2.Length <= i2)
59                                 return true;
60                         // Case 2:
61                         if (n1.Length <= i1 && n2 == w2 ||
62                                 n2.Length <= i2 && n1 == w1)
63                                 return true;
64                         // Case 3:
65                         if (n1.Length > i1 && n2.Length > i2 &&
66                                 n1 [i1] == n2 [i2] &&
67                                 (w1.Length == 0 || n1 [i1] != w1 [0]) &&
68                                 (w2.Length == 0 || n2 [i2] != w2 [0]) &&
69                                 NSMatches (n1, i1 + 1, w1, n2, i2 + 1, w2))
70                                 return true;
71                         // Case 4:
72                         if (w1 != "" &&
73                                 n1.Length > i1 && n1 [i1] == w1 [0] &&
74                                 NSMatches (n1, i1, w1, n2, i2 + 1, w2))
75                                 return true;
76                         // Case 5:
77                         if (w2 != "" &&
78                                 n2.Length > i2 && n2 [i2] == w2 [0] &&
79                                 NSMatches (n1, i1 + 1, w1, n2, i2, w2))
80                                 return true;
81                         return false;
82                 }
83         }
84
85         public class NvdlMessageEventArgs : EventArgs
86         {
87                 string message;
88
89                 public NvdlMessageEventArgs (string message)
90                 {
91                         this.message = message;
92                 }
93
94                 public string Message {
95                         get { return message; }
96                 }
97         }
98
99         public delegate void NvdlMessageEventHandler (object o, NvdlMessageEventArgs e);
100
101         public class NvdlElementBase : IXmlLineInfo
102         {
103                 int line, column;
104                 string sourceUri;
105
106                 public int LineNumber {
107                         get { return line; }
108                         set { line = value; }
109                 }
110                 
111                 public int LinePosition {
112                         get { return column; }
113                         set { column = value; }
114                 }
115                 
116                 public bool HasLineInfo ()
117                 {
118                         return line > 0 && column > 0;
119                 }
120
121                 public string SourceUri {
122                         get { return sourceUri; }
123                         set { sourceUri = value; }
124                 }
125         }
126
127         public class NvdlAttributable : NvdlElementBase
128         {
129                 ArrayList foreign = new ArrayList ();
130
131                 public ArrayList Foreign {
132                         get { return foreign; }
133                 }
134         }
135
136         /*
137         element rules {
138                 (schemaType?,
139                 trigger*,
140                 (rule* | (attribute startMode { xsd:NCName }, mode+)))
141                 & foreign
142         }
143         */
144         public class NvdlRules : NvdlAttributable
145         {
146                 string schemaType;
147                 NvdlTriggerList triggers = new NvdlTriggerList ();
148                 NvdlRuleList rules = new NvdlRuleList ();
149                 NvdlModeList modes = new NvdlModeList ();
150                 string startMode;
151
152 //              [Map.Optional]
153 //              [Map.Attribute]
154                 public string SchemaType {
155                         get { return schemaType; }
156                         set { schemaType = value != null ? value.Trim (Nvdl.Whitespaces) : null; }
157                 }
158
159 //              [Map.ZeroOrMore]
160                 public NvdlTriggerList Triggers {
161                         get { return triggers; }
162                 }
163
164 //              [Map.ZeroOrMore]
165                 public NvdlRuleList Rules {
166                         get { return rules; }
167                 }
168
169 //              [Map.Attribute]
170 //              [MapType ("NCName", XmlSchema.Namespace)]
171                 public string StartMode {
172                         get { return startMode; }
173                         set { startMode = value != null ? value.Trim (Nvdl.Whitespaces) : null; }
174                 }
175
176 //              [Map.OneOrMore]
177                 public NvdlModeList Modes {
178                         get { return modes; }
179                 }
180         }
181
182         /*
183         element trigger {
184                 (attribute ns { xsd:string },
185                 attribute nameList { list { xsd:NCName } })
186                 & foreign
187         }
188         */
189         public class NvdlTrigger : NvdlAttributable
190         {
191                 string ns;
192                 string nameList;
193
194 //              [Map.Attribute]
195                 public string NS {
196                         get { return ns; }
197                         set { ns = value; }
198                 }
199
200 //              [Map.Attribute]
201 //              [Map.List]
202                 public string NameList {
203                         get { return nameList; }
204                         set { nameList = value != null ? value.Trim (Nvdl.Whitespaces) : null; }
205                 }
206         }
207
208         /*
209         element mode {
210                 (attribute name { xsd:NCName },
211                 includedMode*,
212                 rule*)
213                 & foreign
214         }
215         */
216         public abstract class NvdlModeBase : NvdlAttributable
217         {
218                 NvdlModeList includedModes = new NvdlModeList ();
219                 NvdlRuleList rules = new NvdlRuleList ();
220
221 //              [Map.ZeroOrMore]
222                 public NvdlModeList IncludedModes {
223                         get { return includedModes; }
224                 }
225
226 //              [Map.ZeroOrMore]
227                 public NvdlRuleList Rules {
228                         get { return rules; }
229                 }
230         }
231
232         public class NvdlNestedMode : NvdlModeBase
233         {
234         }
235
236         public class NvdlMode : NvdlModeBase
237         {
238                 string name;
239
240 //              [Map.Attribute]
241 //              [MapType ("NCName", XmlSchema.Namespace)]
242                 public string Name {
243                         get { return name; }
244                         set { name = value != null ? value.Trim (Nvdl.Whitespaces) : null; }
245                 }
246         }
247
248         public class NvdlIncludedMode : NvdlModeBase
249         {
250                 string name;
251
252 //              [Map.Attribute]
253 //              [Map.Optional]
254 //              [MapType ("NCName", XmlSchema.Namespace)]
255                 public string Name {
256                         get { return name; }
257                         set { name = value != null ? value.Trim (Nvdl.Whitespaces) : null; }
258                 }
259         }
260
261         public enum NvdlRuleTarget {
262                 None,
263                 Elements,
264                 Attributes,
265                 Both
266         }
267
268         public abstract class NvdlRule : NvdlAttributable
269         {
270                 NvdlRuleTarget match;
271                 NvdlActionList actions = new NvdlActionList ();
272
273                 public NvdlRuleTarget Match {
274                         get { return match; }
275                         set { match = value; }
276                 }
277
278                 public NvdlActionList Actions {
279                         get { return actions; }
280                 }
281         }
282
283         /*
284         element namespace {
285                 (attribute ns { xsd:string },
286                 attribute wildCard {xsd:string{maxLength = "1"}}?,
287                 ruleModel)
288                 & foreign
289         }
290         */
291         public class NvdlNamespace : NvdlRule
292         {
293                 string ns;
294                 string wildcard;
295
296 //              [Map.Attribute]
297                 public string NS {
298                         get { return ns; }
299                         set { ns = value; }
300                 }
301
302 //              [Map.Attribute]
303                 public string Wildcard {
304                         get { return wildcard; }
305                         set {
306                                 if (value != null && value.Length > 1)
307                                         throw new ArgumentException ("wildCard attribute can contain at most one character.");
308                                 wildcard = value;
309                         }
310                 }
311         }
312
313         /*
314         element anyNamespace { ruleModel & foreign}
315         */
316         public class NvdlAnyNamespace : NvdlRule
317         {
318         }
319
320         public abstract class NvdlAction : NvdlAttributable
321         {
322         }
323
324         /*
325         element cancelNestedActions {foreign}
326         */
327         public class NvdlCancelAction : NvdlAction
328         {
329         }
330
331         public abstract class NvdlNoCancelAction : NvdlAction
332         {
333                 NvdlModeUsage modeUsage;
334                 string messageAttr;
335                 NvdlMessageList messages = new NvdlMessageList ();
336
337                 public NvdlModeUsage ModeUsage {
338                         get { return modeUsage; }
339                         set { modeUsage = value; }
340                 }
341
342                 public string SimpleMessage {
343                         get { return messageAttr; }
344                         set { messageAttr = value; }
345                 }
346
347                 public NvdlMessageList Messages {
348                         get { return messages; }
349                 }
350         }
351
352         public abstract class NvdlNoResultAction : NvdlNoCancelAction
353         {
354         }
355
356         public enum NvdlResultType {
357                 Attach,
358                 AttachPlaceholder,
359                 Unwrap
360         }
361
362         public abstract class NvdlResultAction : NvdlNoCancelAction
363         {
364                 public abstract NvdlResultType ResultType { get; }
365         }
366
367         public class NvdlAttach : NvdlResultAction
368         {
369                 public override NvdlResultType ResultType {
370                         get { return NvdlResultType.Attach; }
371                 }
372         }
373
374         public class NvdlAttachPlaceholder : NvdlResultAction
375         {
376                 public override NvdlResultType ResultType {
377                         get { return NvdlResultType.AttachPlaceholder; }
378                 }
379         }
380
381         public class NvdlUnwrap : NvdlResultAction
382         {
383                 public override NvdlResultType ResultType {
384                         get { return NvdlResultType.Unwrap; }
385                 }
386         }
387
388         /*
389         element validate {
390                 (schemaType?,
391                 (message | option)*,
392                 schema,
393                 modeUsage) & foreign
394         }
395
396         schema =
397                 attribute schema { xsd:anyURI } |
398                 element schema {(text | foreignElement), foreignAttribute*}
399         */
400         public class NvdlValidate : NvdlNoResultAction
401         {
402                 string schemaType;
403                 NvdlOptionList options = new NvdlOptionList ();
404                 string schemaUri;
405                 XmlElement schemaBody;
406
407 //              [Map.Attribute]
408 //              [MapType ("NCName", XmlSchema.Namespace)]
409                 public string SchemaType {
410                         get { return schemaType; }
411                         set { schemaType = value != null ? value.Trim (Nvdl.Whitespaces) : null; }
412                 }
413
414                 public NvdlOptionList Options {
415                         get { return options; }
416                 }
417
418 //              [MapType ("anyURI", XmlSchema.Namespace)]
419                 public string SchemaUri {
420                         get { return schemaUri; }
421                         set { schemaUri = value; }
422                 }
423
424                 public XmlElement SchemaBody {
425                         get { return schemaBody; }
426                         set { schemaBody = value; }
427                 }
428         }
429
430         public class NvdlAllow : NvdlNoResultAction
431         {
432         }
433
434         public class NvdlReject : NvdlNoResultAction
435         {
436         }
437
438         public class NvdlMessage : NvdlElementBase
439         {
440                 string text;
441                 string xmlLang;
442                 ArrayList foreignAttributes = new ArrayList ();
443
444                 public string Text {
445                         get { return text; }
446                         set { text = value; }
447                 }
448
449                 public string XmlLang {
450                         get { return xmlLang; }
451                         set { xmlLang = value; }
452                 }
453
454                 public ArrayList ForeignAttributes {
455                         get { return foreignAttributes; }
456                 }
457         }
458
459         public class NvdlOption : NvdlAttributable
460         {
461                 string name;
462                 string arg;
463                 string mustSupport;
464
465                 public string Name {
466                         get { return name; }
467                         set { name = value; }
468                 }
469
470                 public string Arg {
471                         get { return arg; }
472                         set { arg = value; }
473                 }
474
475                 public string MustSupport {
476                         get { return mustSupport; }
477                         set { mustSupport = value != null ? value.Trim (Nvdl.Whitespaces) : null; }
478                 }
479         }
480
481         /*
482         (attribute useMode { xsd:NCName } | nestedMode)?,
483         element context {
484                 (attribute path { path },
485                 (attribute useMode { xsd:NCName } | nestedMode)?)
486                 & foreign
487         }*
488         */
489         public class NvdlModeUsage
490         {
491                 string useMode;
492                 NvdlNestedMode nestedMode;
493                 NvdlContextList contexts = new NvdlContextList ();
494
495                 public string UseMode {
496                         get { return useMode; }
497                         set { useMode = value != null ? value.Trim (Nvdl.Whitespaces) : null; }
498                 }
499
500                 public NvdlNestedMode NestedMode {
501                         get { return nestedMode; }
502                         set { nestedMode = value; }
503                 }
504
505                 public NvdlContextList Contexts {
506                         get { return contexts; }
507                 }
508         }
509
510         public class NvdlContext : NvdlAttributable
511         {
512                 string path;
513                 string useMode;
514                 NvdlNestedMode nestedMode;
515
516                 public string Path {
517                         get { return path; }
518                         set { path = value; }
519                 }
520
521                 public string UseMode {
522                         get { return useMode; }
523                         set { useMode = value != null ? value.Trim (Nvdl.Whitespaces) : null; }
524                 }
525
526                 public NvdlNestedMode NestedMode {
527                         get { return nestedMode; }
528                         set { nestedMode = value; }
529                 }
530         }
531
532         public class NvdlTriggerList : CollectionBase
533         {
534                 public NvdlTrigger this [int i] {
535                         get { return (NvdlTrigger) List [i]; }
536                         set { List [i] = (NvdlTrigger) value; }
537                 }
538
539                 public void Add (NvdlTrigger item)
540                 {
541                         List.Add (item);
542                 }
543
544                 public void Remove (NvdlTrigger item)
545                 {
546                         List.Add (item);
547                 }
548         }
549
550         public class NvdlRuleList : CollectionBase
551         {
552                 public NvdlRule this [int i] {
553                         get { return (NvdlRule) List [i]; }
554                         set { List [i] = (NvdlRule) value; }
555                 }
556
557                 public void Add (NvdlRule item)
558                 {
559                         List.Add (item);
560                 }
561
562                 public void Remove (NvdlRule item)
563                 {
564                         List.Add (item);
565                 }
566         }
567
568         public class NvdlModeList : CollectionBase
569         {
570                 public NvdlModeBase this [int i] {
571                         get { return (NvdlModeBase) List [i]; }
572                         set { List [i] = (NvdlModeBase) value; }
573                 }
574
575                 public void Add (NvdlModeBase item)
576                 {
577                         List.Add (item);
578                 }
579
580                 public void Remove (NvdlModeBase item)
581                 {
582                         List.Add (item);
583                 }
584         }
585
586         public class NvdlContextList : CollectionBase
587         {
588                 public NvdlContext this [int i] {
589                         get { return (NvdlContext) List [i]; }
590                         set { List [i] = (NvdlContext) value; }
591                 }
592
593                 public void Add (NvdlContext item)
594                 {
595                         List.Add (item);
596                 }
597
598                 public void Remove (NvdlContext item)
599                 {
600                         List.Add (item);
601                 }
602         }
603
604         public class NvdlActionList : CollectionBase
605         {
606                 public NvdlAction this [int i] {
607                         get { return (NvdlAction) List [i]; }
608                         set { List [i] = (NvdlAction) value; }
609                 }
610
611                 public void Add (NvdlAction item)
612                 {
613                         List.Add (item);
614                 }
615
616                 public void Remove (NvdlAction item)
617                 {
618                         List.Add (item);
619                 }
620         }
621
622         public class NvdlOptionList : CollectionBase
623         {
624                 public NvdlOption this [int i] {
625                         get { return (NvdlOption) List [i]; }
626                         set { List [i] = (NvdlOption) value; }
627                 }
628
629                 public void Add (NvdlOption item)
630                 {
631                         List.Add (item);
632                 }
633
634                 public void Remove (NvdlOption item)
635                 {
636                         List.Add (item);
637                 }
638         }
639
640         public class NvdlMessageList : CollectionBase
641         {
642                 public NvdlMessage this [int i] {
643                         get { return (NvdlMessage) List [i]; }
644                         set { List [i] = (NvdlMessage) value; }
645                 }
646
647                 public void Add (NvdlMessage item)
648                 {
649                         List.Add (item);
650                 }
651
652                 public void Remove (NvdlMessage item)
653                 {
654                         List.Add (item);
655                 }
656         }
657 }