File: NvdlValidator.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (619 lines) | stat: -rw-r--r-- 16,423 bytes parent folder | download | duplicates (15)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
using System;
using System.Collections;
using System.IO;
using System.Xml;

namespace Commons.Xml.Nvdl
{
	internal class NvdlDebug
	{
		public static TextWriter Writer = TextWriter.Null;
//		public static TextWriter Writer = Console.Out;
	}

	internal class NvdlDispatcher
	{
		NvdlValidatingReader validator;
		SimpleRules rules;
		NvdlSection section;
		// Actually this dispatcher puts possibly identical sections
		// onto this stack at every StartElement() (unless
		// IsEmptyElement is true).
		NvdlSectionStack sectionStack = new NvdlSectionStack ();

		// This qname stack is required for section 7.3 check that
		// if parent "element" of an element (note that it is not
		// "element section") must not be located by the same
		// trigger element
		Stack qnameStack = new Stack ();

		public NvdlDispatcher (SimpleRules rules, NvdlValidatingReader source)
		{
			this.validator = source;
			this.rules = rules;
		}

		internal NvdlValidatingReader Validator {
			get { return validator; }
		}

		internal XmlReader Reader {
			get { return validator.Reader; }
		}

		public SimpleRules Rules {
			get { return rules; }
		}

		public void StartElement ()
		{
NvdlDebug.Writer.WriteLine ("  <dispatcher.StartElement {0}. stack depth: {1}. current section ns {2}",
Reader.Name, sectionStack.Count, section == null ? "(none)" : section.Namespace);
			NvdlSection prev = section;
			section = GetSection (section);

			section.StartElement ();
			if (Reader.IsEmptyElement) {
				section.EndSection ();
				section = prev;
			} else {
				sectionStack.Push (section);
				qnameStack.Push (new XmlQualifiedName (Reader.LocalName, Reader.NamespaceURI));
			}
		}

		NvdlSection GetSection (NvdlSection section)
		{
			if (section == null)
				return new NvdlSection (this, null);
			else if (section.Namespace != Reader.NamespaceURI)
				return new NvdlSection (this, section);
			else if (qnameStack.Count > 0 && rules.Triggers.Length > 0) {
				for (int t = 0; t < rules.Triggers.Length; t++) {
					SimpleTrigger st = rules.Triggers [t];
					XmlQualifiedName parent =
						(XmlQualifiedName) qnameStack.Peek ();
					if (st.Cover (Reader.LocalName,
						      Reader.NamespaceURI) &&
					    !st.Cover (parent.Name, parent.Namespace)) {
NvdlDebug.Writer.WriteLine ("======== triggered by {0}", st.Location);
						return new NvdlSection (this, section);
					}
				}
			}
			return section;
		}

		public void EndElement ()
		{
NvdlDebug.Writer.WriteLine ("  <dispatcher.EndElement {0}. depth: {1}",
Reader.Name, sectionStack.Count);
			if (section != null) {
				section = sectionStack.Pop ();
				section.EndElement ();
				section.EndSection ();
				if (sectionStack.Count > 0)
					section = sectionStack.Peek ();
				else
					section = null;
			}

			qnameStack.Pop ();
		}

		public void Text ()
		{
			if (section != null)
				section.Text ();
		}

		public void Whitespace ()
		{
			if (section != null)
				section.Whitespace ();
		}
	}

	//
	// This class is instantiated for every NVDL section.
	//
	// For interpretations, the list might share the same
	// NvdlInterpretation I with other sections (as specified in
	// step 4 (8.6) of the spec.
	//
	internal class NvdlSection
	{
		readonly NvdlDispatcher dispatcher;
		readonly string ns;
		readonly NvdlInterpretationList ilist = new NvdlInterpretationList ();
		readonly ArrayList elementNameStack = new ArrayList ();

		public NvdlSection (NvdlDispatcher dispatcher,
			NvdlSection parentState)
		{
			this.dispatcher = dispatcher;
			this.ns = dispatcher.Reader.NamespaceURI;

			if (parentState == null) {
				foreach (SimpleAction a in FindElementRule (
					dispatcher.Rules.StartMode,
					dispatcher.Reader).Actions)
					ilist.Add (GetInterp (a, dispatcher));
			} else {
				foreach (NvdlInterpretation pi in
					parentState.ilist) {
					PopulateInterp (dispatcher, pi, parentState);
				}
			}

NvdlDebug.Writer.WriteLine ("New section: ns {0} / interp.count {1} / loc: {2}", ns, ilist.Count, ((IXmlLineInfo) dispatcher.Reader).LineNumber);
		}

		private NvdlInterpretation GetInterp (
			SimpleAction a, NvdlDispatcher d)
		{
			return CreateInterp (d, d.Rules.StartMode, a, null);
		}

		private NvdlInterpretation CreateInterp (NvdlDispatcher d,
			SimpleMode m, SimpleAction a, NvdlInterpretation p)
		{
NvdlDebug.Writer.WriteLine ("***** new interp from action {0} from mode {1}", a.Location, m.Location);
			SimpleValidate v = a as SimpleValidate;
			if (v != null)
				return new NvdlValidateInterp (d, m, v, p);
			return new NvdlResultInterp (d, m, (SimpleResultAction) a, p);
		}

		private void PopulateInterp (
			NvdlDispatcher d, NvdlInterpretation i,
			NvdlSection parentState)
		{
			SimpleMode m = FindContextMode (i.Action, parentState);
			SimpleRule rule = FindElementRule (m, dispatcher.Reader);
NvdlDebug.Writer.WriteLine ("***** populate interp from action {0} whose mode is {1}. Rule is {2} whose actions are {3}", i.Action.Location, m.Location, rule.Location, rule.Actions.Length);
			foreach (SimpleAction a in rule.Actions) {
				NvdlInterpretation cur = i;
				for (;cur != null; cur = cur.Parent)
					if (cur.CreatedMode == m && cur.Action == a) {
NvdlDebug.Writer.WriteLine ("------- corresponding PlanElem already exists.");
						break;
					}
				if (cur == null)
					cur = CreateInterp (d, m, a, i);
				ilist.Add (cur);
			}
		}

		private SimpleMode FindContextMode (SimpleAction a, NvdlSection parentState)
		{
			if (a.Contexts != null)
				foreach (SimpleContext ctx in a.Contexts)
					foreach (SimplePath path in ctx.Path)
						if (MatchPath (path, parentState)) {
NvdlDebug.Writer.WriteLine ("------ matched context at {0}.", ctx.Location);
							return ctx.UseMode;
}
			return a.DefaultMode;
		}

		private bool MatchPath (SimplePath path, NvdlSection parentState)
		{
			ArrayList stack = parentState.elementNameStack;
			if (stack.Count == 0)
				return false;
			int elemStep = stack.Count - 1;
			for (int i = path.Steps.Length - 1; i >= 0 && elemStep >= 0;) {
				SimplePathStep ps = path.Steps [i];
				if (ps.Name != stack [elemStep] as string) {
					// reject a/b while allow a//b
					if (!ps.Descendants)
						return false;
					--elemStep;
				}
				else {
					i--;
					elemStep--;
				}
			}
NvdlDebug.Writer.Write ("------ matched path: ");
for (int i = 0; i < stack.Count; i++) NvdlDebug.Writer.Write ('[' + (string) stack [i] + ']');
NvdlDebug.Writer.Write (" -> ");
for (int i = 0; i < path.Steps.Length; i++) NvdlDebug.Writer.Write ('[' + path.Steps [i].Name + ']');
NvdlDebug.Writer.WriteLine ();
			return true;
		}

		public NvdlDispatcher Dispatcher {
			get { return dispatcher; }
		}

		public NvdlInterpretationList Interpretations {
			get { return ilist; }
		}

		public string Namespace {
			get { return ns; }
		}

		public XmlReader Reader {
			get { return dispatcher.Reader; }
		}

		private SimpleRule FindElementRule (SimpleMode mode, XmlReader reader)
		{
			SimpleRule any = null;
			foreach (SimpleRule rule in mode.ElementRules) {
				if (rule.MatchNS (reader.NamespaceURI)) {
					if (!rule.IsAny)
						return rule;
					else
						any = rule;
				}
			}
NvdlDebug.Writer.WriteLine (" : : : : anyNamespace rule being applied.");
			if (any != null)
				return any;
			throw new NvdlValidationException ("NVDL internal error: should not happen. No matching rule was found.", Reader as IXmlLineInfo);
		}

		// It is invoked regardless of IsEmptyElement.
		public void EndSection ()
		{
NvdlDebug.Writer.WriteLine ("    <section.EndSection> ({0})", ns);
			foreach (NvdlInterpretation i in ilist)
				i.EndSection ();
		}

		public void StartElement ()
		{
NvdlDebug.Writer.WriteLine ("    <section.StartElement ({0}) {1}", ns, Reader.Name);
			ValidateStartElement ();
			if (!Reader.IsEmptyElement)
				elementNameStack.Add (Reader.LocalName);
		}

		private void ValidateStartElement ()
		{
			foreach (NvdlInterpretation i in ilist)
				i.StartElement ();
		}

		public void EndElement ()
		{
NvdlDebug.Writer.WriteLine ("    <section.EndElement {0} ({2}). {1} interp.", Reader.Name, ilist.Count, ns);
			ValidateEndElement ();
			elementNameStack.RemoveAt (elementNameStack.Count - 1);
		}

		private void ValidateEndElement ()
		{
			foreach (NvdlInterpretation i in ilist)
				i.EndElement ();
		}

		public void Text ()
		{
			ValidateText ();
		}

		private void ValidateText ()
		{
			foreach (NvdlInterpretation i in ilist)
				i.Text ();
		}

		public void Whitespace ()
		{
			ValidateWhitespace ();
		}

		private void ValidateWhitespace ()
		{
			foreach (NvdlInterpretation i in ilist)
				i.Whitespace ();
		}
	}

	internal class NvdlSectionStack : CollectionBase
	{
		public void Push (NvdlSection state)
		{
			List.Add (state);
		}

		public NvdlSection this [int i] {
			get { return (NvdlSection) List [i]; }
		}

		public NvdlSection Peek ()
		{
			return (NvdlSection) List [List.Count - 1];
		}

		public NvdlSection Pop ()
		{
			NvdlSection ret = this [List.Count - 1];
			List.RemoveAt (List.Count - 1);
			return ret;
		}
	}

	internal class NvdlInterpretationList : CollectionBase
	{
		public void Add (NvdlInterpretation i)
		{
			List.Add (i);
		}

		public NvdlInterpretation this [int i] {
			get { return (NvdlInterpretation) List [i]; }
		}

		public void Remove (NvdlInterpretation i)
		{
			List.Remove (i);
		}
	}
	internal abstract class NvdlInterpretation
	{
		NvdlDispatcher dispatcher;
		SimpleMode createdMode; // IM(s)
		SimpleAction action; // IA(s)
		NvdlInterpretation parent;

		public NvdlInterpretation (NvdlDispatcher dispatcher,
			SimpleMode createdMode, SimpleAction action,
			NvdlInterpretation parent)
		{
			this.dispatcher = dispatcher;
			this.createdMode = createdMode;
			this.action = action;
			this.parent = parent;
		}

		internal NvdlDispatcher Dispatcher {
			get { return dispatcher; }
		}

		public SimpleMode CreatedMode {
			get { return createdMode; }
		}

		public SimpleAction Action {
			get { return action; }
		}

		public NvdlInterpretation Parent {
			get { return parent; }
		}

		public abstract void AttachPlaceholder ();
		public abstract void DetachPlaceholder ();
		public abstract void StartElement ();
		public abstract void EndElement ();
		public abstract void Text ();
		public abstract void Whitespace ();
		public abstract void ValidateStartElement ();
		public abstract void ValidateEndElement ();
		public abstract void ValidateText ();
		public abstract void ValidateWhitespace ();
		public abstract void EndSection ();
	}

	internal class NvdlResultInterp : NvdlInterpretation
	{
		NvdlResultType type;

		public NvdlResultInterp (NvdlDispatcher dispatcher,
			SimpleMode createdMode,
			SimpleResultAction resultAction,
			NvdlInterpretation parent)
			: base (dispatcher, createdMode, resultAction, parent)
		{
NvdlDebug.Writer.WriteLine ("++++++ new resultAction " + resultAction.Location);
			type = resultAction.ResultType;

			if (type == NvdlResultType.AttachPlaceholder && parent != null)
				parent.AttachPlaceholder ();
		}

		public override void EndSection ()
		{
			if (type == NvdlResultType.AttachPlaceholder && Parent != null)
				Parent.DetachPlaceholder ();
		}

		public override void AttachPlaceholder ()
		{
			if (type == NvdlResultType.Unwrap)
				Parent.AttachPlaceholder ();
		}

		public override void DetachPlaceholder ()
		{
			if (type == NvdlResultType.Unwrap)
				Parent.DetachPlaceholder ();
		}

		public override void StartElement ()
		{
NvdlDebug.Writer.WriteLine ("            <result.StartElement : " + type + "/"+ Action.Location);
			if (type != NvdlResultType.Unwrap)
				ValidateStartElement (); // unwrap itself does not dispatch to parent interpretation
NvdlDebug.Writer.WriteLine ("            </result>");
		}

		public override void EndElement ()
		{
NvdlDebug.Writer.WriteLine ("            <result.EndElement : " + type + "/" + ((IXmlLineInfo) Dispatcher.Reader).LineNumber);
			if (type != NvdlResultType.Unwrap)
				ValidateEndElement (); // unwrap itself does not dispatch to parent interpretation
NvdlDebug.Writer.WriteLine ("            </result>");
		}

		public override void Text ()
		{
NvdlDebug.Writer.WriteLine ("            <result.Text : " + type + "/" + ((IXmlLineInfo) Dispatcher.Reader).LineNumber);
			if (type != NvdlResultType.Unwrap)
				ValidateText (); // unwrap itself does not dispatch to parent interpretation
NvdlDebug.Writer.WriteLine ("            </result>");
		}

		public override void Whitespace ()
		{
NvdlDebug.Writer.WriteLine ("            <result.Whitespace : " + type + "/" + ((IXmlLineInfo) Dispatcher.Reader).LineNumber);
			if (type != NvdlResultType.Unwrap)
				ValidateWhitespace (); // unwrap itself does not dispatch to parent interpretation
NvdlDebug.Writer.WriteLine ("            </result>");
		}

		public override void ValidateStartElement ()
		{
			switch (type) {
			case NvdlResultType.Unwrap:
NvdlDebug.Writer.WriteLine (": : : : Unwrapping StartElement ");
				goto case NvdlResultType.Attach;
			case NvdlResultType.Attach:
				Parent.ValidateStartElement ();
				break;
			case NvdlResultType.AttachPlaceholder:
				break;
			}
		}
		public override void ValidateEndElement ()
		{
			switch (type) {
			case NvdlResultType.Unwrap:
NvdlDebug.Writer.WriteLine (": : : : Unwrapping EndElement ");
				goto case NvdlResultType.Attach;
			case NvdlResultType.Attach:
				Parent.ValidateEndElement ();
				break;
			case NvdlResultType.AttachPlaceholder:
				break;
			}
		}
		public override void ValidateText ()
		{
			switch (type) {
			case NvdlResultType.Unwrap:
NvdlDebug.Writer.WriteLine (": : : : Unwrapping Text ");
				goto case NvdlResultType.Attach;
			case NvdlResultType.Attach:
				Parent.ValidateText ();
				break;
			case NvdlResultType.AttachPlaceholder:
				break;
			}
		}
		public override void ValidateWhitespace ()
		{
			switch (type) {
			case NvdlResultType.Unwrap:
NvdlDebug.Writer.WriteLine (": : : : Unwrapping Whitespace ");
				goto case NvdlResultType.Attach;
			case NvdlResultType.Attach:
				Parent.ValidateWhitespace ();
				break;
			case NvdlResultType.AttachPlaceholder:
				break;
			}
		}
	}

	internal class NvdlValidateInterp : NvdlInterpretation
	{
		NvdlFilteredXmlReader reader; // s
		SimpleValidate validate;
		XmlReader validator;

		public NvdlValidateInterp (NvdlDispatcher dispatcher,
			SimpleMode createdMode, SimpleValidate validate,
			NvdlInterpretation parent)
			: base (dispatcher, createdMode, validate, parent)
		{
NvdlDebug.Writer.WriteLine ("++++++ new validate " + validate.Location);
			this.reader = new NvdlFilteredXmlReader (dispatcher.Reader, this);
			this.validate = validate;
			validator = validate.CreateValidator (this.reader);

			dispatcher.Validator.OnMessage (validate.Messages);
		}

		public override void AttachPlaceholder ()
		{
			reader.AttachPlaceholder ();
			validator.Read (); // check start Element
		}

		public override void DetachPlaceholder ()
		{
			reader.DetachPlaceholder ();
			// since placeholder is *empty*, we don't have to
			// validate this virtual element.
			//validator.Read ();
		}

		public override void EndSection ()
		{
		}

		public override void StartElement ()
		{
NvdlDebug.Writer.WriteLine ("  <validate.StartElement {0} {1}", validator.Name, validator.IsEmptyElement ? "(EmptyElement)" : "");
			ValidateStartElement ();
		}

		public override void EndElement ()
		{
NvdlDebug.Writer.WriteLine ("  <validate.EndElement {0}", validator.Name);
			ValidateEndElement ();
		}

		public override void Text ()
		{
			ValidateText ();
		}

		public override void Whitespace ()
		{
			ValidateWhitespace ();
		}

		public override void ValidateStartElement ()
		{
NvdlDebug.Writer.WriteLine ("###### interp.ValidateStartElement : " + Action.Location);
			ReadValidator ();
		}

		public override void ValidateEndElement ()
		{
NvdlDebug.Writer.WriteLine ("###### interp.ValidateEndElement : " + Action.Location);
			ReadValidator ();
		}

		public override void ValidateText ()
		{
NvdlDebug.Writer.WriteLine ("###### interp.ValidateText : " + Action.Location);
			ReadValidator ();
		}

		public override void ValidateWhitespace ()
		{
NvdlDebug.Writer.WriteLine ("###### interp.Whitespace : " + Action.Location);
			ReadValidator ();
		}

		void ReadValidator ()
		{
			try {
				validator.Read ();
			} catch (Exception ex) {
				if (!validate.Generator.HandleError (ex, validator, validate.Location))
					throw;
			}
		}
	}
}