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