2 using System.Collections;
6 namespace Commons.Xml.Nvdl
8 internal class NvdlDebug
10 public static TextWriter Writer = TextWriter.Null;
11 // public static TextWriter Writer = Console.Out;
14 internal class NvdlDispatcher
16 NvdlValidatingReader validator;
19 // Actually this dispatcher puts possibly identical sections
20 // onto this stack at every StartElement() (unless
21 // IsEmptyElement is true).
22 NvdlSectionStack sectionStack = new NvdlSectionStack ();
24 public NvdlDispatcher (SimpleRules rules, NvdlValidatingReader source)
26 this.validator = source;
30 internal NvdlValidatingReader Validator {
31 get { return validator; }
34 internal XmlReader Reader {
35 get { return validator.Reader; }
38 public SimpleRules Rules {
42 public void StartElement ()
44 NvdlDebug.Writer.WriteLine (" <dispatcher.StartElement {0}. stack depth: {1}. current section ns {2}",
45 Reader.Name, sectionStack.Count, section == null ? "(none)" : section.Namespace);
47 section = new NvdlSection (this, null);
48 else if (section.Namespace != Reader.NamespaceURI)
49 section = new NvdlSection (this, section);
51 sectionStack.Push (section);
52 section.StartElement ();
53 if (Reader.IsEmptyElement)
54 sectionStack.Pop ().EndSection ();
57 public void EndElement ()
59 NvdlDebug.Writer.WriteLine (" <dispatcher.EndElement {0}. depth: {1}",
60 Reader.Name, sectionStack.Count);
61 if (section != null) {
62 section = sectionStack.Pop ();
63 section.EndElement ();
64 section.EndSection ();
65 if (sectionStack.Count > 0)
66 section = sectionStack.Peek ();
78 public void Whitespace ()
81 section.Whitespace ();
86 // This class is instantiated for every NVDL section.
88 // For interpretations, the list might share the same
89 // NvdlInterpretation I with other sections (as specified in
90 // step 4 (8.6) of the spec.
92 internal class NvdlSection
94 readonly NvdlDispatcher dispatcher;
96 readonly NvdlInterpretationList ilist = new NvdlInterpretationList ();
97 readonly ArrayList elementNameStack = new ArrayList ();
99 public NvdlSection (NvdlDispatcher dispatcher,
100 NvdlSection parentState)
102 this.dispatcher = dispatcher;
103 this.ns = dispatcher.Reader.NamespaceURI;
105 if (parentState == null) {
106 foreach (SimpleAction a in FindElementRule (
107 dispatcher.Rules.StartMode,
108 dispatcher.Reader).Actions)
109 ilist.Add (GetInterp (a, dispatcher));
111 foreach (NvdlInterpretation pi in
113 PopulateInterp (dispatcher, pi);
117 NvdlDebug.Writer.WriteLine ("New section: ns {0} / interp.count {1} / loc: {2}", ns, ilist.Count, ((IXmlLineInfo) dispatcher.Reader).LineNumber);
120 private NvdlInterpretation GetInterp (
121 SimpleAction a, NvdlDispatcher d)
123 return CreateInterp (d, d.Rules.StartMode, a, null);
126 private NvdlInterpretation CreateInterp (NvdlDispatcher d,
127 SimpleMode m, SimpleAction a, NvdlInterpretation p)
129 NvdlDebug.Writer.WriteLine ("***** new interp from action {0} from mode {1}", a.Location, m.Location);
130 SimpleValidate v = a as SimpleValidate;
132 return new NvdlValidateInterp (d, m, v, p);
133 return new NvdlResultInterp (d, m, (SimpleResultAction) a, p);
136 private void PopulateInterp (
137 NvdlDispatcher d, NvdlInterpretation i)
139 SimpleMode m = FindContextMode (i.Action);
140 SimpleRule rule = FindElementRule (m, dispatcher.Reader);
141 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);
142 foreach (SimpleAction a in rule.Actions) {
143 NvdlInterpretation cur = i;
144 for (;cur != null; cur = cur.Parent)
145 if (cur.CreatedMode == m && cur.Action == a) {
146 NvdlDebug.Writer.WriteLine ("------- corresponding PlanElem already exists.");
150 cur = CreateInterp (d, m, a, i);
155 private SimpleMode FindContextMode (SimpleAction a)
157 if (a.Contexts != null)
158 foreach (SimpleContext ctx in a.Contexts)
159 foreach (SimplePath path in ctx.Path)
160 if (MatchPath (path))
162 return a.DefaultMode;
165 private bool MatchPath (SimplePath path)
167 int elemStep = elementNameStack.Count - 1;
168 for (int i = path.Steps.Length; i >= 0 && elemStep >= 0;) {
169 SimplePathStep ps = path.Steps [i];
170 if (ps.Name != elementNameStack [elemStep] as string) {
184 public NvdlDispatcher Dispatcher {
185 get { return dispatcher; }
188 public NvdlInterpretationList Interpretations {
189 get { return ilist; }
192 public string Namespace {
196 public XmlReader Reader {
197 get { return dispatcher.Reader; }
200 private SimpleRule FindElementRule (SimpleMode mode, XmlReader reader)
202 SimpleRule any = null;
203 foreach (SimpleRule rule in mode.ElementRules) {
204 if (rule.MatchNS (reader.NamespaceURI)) {
211 NvdlDebug.Writer.WriteLine (" : : : : anyNamespace rule being applied.");
214 throw new NvdlValidationException ("NVDL internal error: should not happen. No matching rule was found.", Reader as IXmlLineInfo);
217 // It is invoked regardless of IsEmptyElement.
218 public void EndSection ()
220 foreach (NvdlInterpretation i in ilist)
224 public void StartElement ()
226 NvdlDebug.Writer.WriteLine (" <state.StartElement {0}", Reader.Name);
227 elementNameStack.Add (Reader.LocalName);
228 ValidateStartElement ();
229 if (Reader.IsEmptyElement)
230 elementNameStack.RemoveAt (elementNameStack.Count - 1);
233 private void ValidateStartElement ()
235 foreach (NvdlInterpretation i in ilist)
239 public void EndElement ()
241 NvdlDebug.Writer.WriteLine (" <state.EndElement {0} (for {2}). {1} interp.", Reader.Name, ilist.Count, Namespace);
242 ValidateEndElement ();
243 elementNameStack.RemoveAt (elementNameStack.Count - 1);
246 private void ValidateEndElement ()
248 foreach (NvdlInterpretation i in ilist)
257 private void ValidateText ()
259 foreach (NvdlInterpretation i in ilist)
263 public void Whitespace ()
265 ValidateWhitespace ();
268 private void ValidateWhitespace ()
270 foreach (NvdlInterpretation i in ilist)
275 internal class NvdlSectionStack : CollectionBase
277 public void Push (NvdlSection state)
282 public NvdlSection this [int i] {
283 get { return (NvdlSection) List [i]; }
286 public NvdlSection Peek ()
288 return (NvdlSection) List [List.Count - 1];
291 public NvdlSection Pop ()
293 NvdlSection ret = this [List.Count - 1];
294 List.RemoveAt (List.Count - 1);
299 internal class NvdlInterpretationList : CollectionBase
301 public void Add (NvdlInterpretation i)
306 public NvdlInterpretation this [int i] {
307 get { return (NvdlInterpretation) List [i]; }
310 public void Remove (NvdlInterpretation i)
315 internal abstract class NvdlInterpretation
317 NvdlDispatcher dispatcher;
318 SimpleMode createdMode; // IM(s)
319 SimpleAction action; // IA(s)
320 NvdlInterpretation parent;
322 public NvdlInterpretation (NvdlDispatcher dispatcher,
323 SimpleMode createdMode, SimpleAction action,
324 NvdlInterpretation parent)
326 this.dispatcher = dispatcher;
327 this.createdMode = createdMode;
328 this.action = action;
329 this.parent = parent;
332 internal NvdlDispatcher Dispatcher {
333 get { return dispatcher; }
336 public SimpleMode CreatedMode {
337 get { return createdMode; }
340 public SimpleAction Action {
341 get { return action; }
344 public NvdlInterpretation Parent {
345 get { return parent; }
348 public abstract void AttachPlaceHolder ();
349 public abstract void DetachPlaceHolder ();
350 public abstract void StartElement ();
351 public abstract void EndElement ();
352 public abstract void Text ();
353 public abstract void Whitespace ();
354 public abstract void ValidateStartElement ();
355 public abstract void ValidateEndElement ();
356 public abstract void ValidateText ();
357 public abstract void ValidateWhitespace ();
358 public abstract void EndSection ();
361 internal class NvdlResultInterp : NvdlInterpretation
365 public NvdlResultInterp (NvdlDispatcher dispatcher,
366 SimpleMode createdMode,
367 SimpleResultAction resultAction,
368 NvdlInterpretation parent)
369 : base (dispatcher, createdMode, resultAction, parent)
371 NvdlDebug.Writer.WriteLine ("++++++ new resultAction " + resultAction.Location);
372 type = resultAction.ResultType;
374 if (type == NvdlResultType.AttachPlaceHolder && parent != null)
375 parent.AttachPlaceHolder ();
378 public override void EndSection ()
380 if (type == NvdlResultType.AttachPlaceHolder && Parent != null)
381 Parent.DetachPlaceHolder ();
384 public override void AttachPlaceHolder ()
386 if (type == NvdlResultType.Unwrap)
387 Parent.AttachPlaceHolder ();
390 public override void DetachPlaceHolder ()
392 if (type == NvdlResultType.Unwrap)
393 Parent.DetachPlaceHolder ();
396 public override void StartElement ()
398 NvdlDebug.Writer.WriteLine (" <result.StartElement : " + type + "/"+ Action.Location);
399 if (type != NvdlResultType.Unwrap)
400 ValidateStartElement (); // unwrap itself does not dispatch to parent interpretation
401 NvdlDebug.Writer.WriteLine (" </result>");
404 public override void EndElement ()
406 NvdlDebug.Writer.WriteLine (" <result.EndElement : " + type + "/" + ((IXmlLineInfo) Dispatcher.Reader).LineNumber);
407 if (type != NvdlResultType.Unwrap)
408 ValidateEndElement (); // unwrap itself does not dispatch to parent interpretation
409 NvdlDebug.Writer.WriteLine (" </result>");
412 public override void Text ()
414 NvdlDebug.Writer.WriteLine (" <result.Text : " + type + "/" + ((IXmlLineInfo) Dispatcher.Reader).LineNumber);
415 if (type != NvdlResultType.Unwrap)
416 ValidateText (); // unwrap itself does not dispatch to parent interpretation
417 NvdlDebug.Writer.WriteLine (" </result>");
420 public override void Whitespace ()
422 NvdlDebug.Writer.WriteLine (" <result.Whitespace : " + type + "/" + ((IXmlLineInfo) Dispatcher.Reader).LineNumber);
423 if (type != NvdlResultType.Unwrap)
424 ValidateWhitespace (); // unwrap itself does not dispatch to parent interpretation
425 NvdlDebug.Writer.WriteLine (" </result>");
428 public override void ValidateStartElement ()
431 case NvdlResultType.Unwrap:
432 NvdlDebug.Writer.WriteLine (": : : : Unwrapping StartElement ");
433 goto case NvdlResultType.Attach;
434 case NvdlResultType.Attach:
435 Parent.ValidateStartElement ();
437 case NvdlResultType.AttachPlaceHolder:
438 throw new NotImplementedException ();
441 public override void ValidateEndElement ()
444 case NvdlResultType.Unwrap:
445 NvdlDebug.Writer.WriteLine (": : : : Unwrapping EndElement ");
446 goto case NvdlResultType.Attach;
447 case NvdlResultType.Attach:
448 Parent.ValidateEndElement ();
450 case NvdlResultType.AttachPlaceHolder:
451 throw new NotImplementedException ();
454 public override void ValidateText ()
457 case NvdlResultType.Unwrap:
458 NvdlDebug.Writer.WriteLine (": : : : Unwrapping Text ");
459 goto case NvdlResultType.Attach;
460 case NvdlResultType.Attach:
461 Parent.ValidateText ();
463 case NvdlResultType.AttachPlaceHolder:
464 throw new NotImplementedException ();
467 public override void ValidateWhitespace ()
470 case NvdlResultType.Unwrap:
471 NvdlDebug.Writer.WriteLine (": : : : Unwrapping Whitespace ");
472 goto case NvdlResultType.Attach;
473 case NvdlResultType.Attach:
474 Parent.ValidateWhitespace ();
476 case NvdlResultType.AttachPlaceHolder:
477 throw new NotImplementedException ();
482 internal class NvdlValidateInterp : NvdlInterpretation
484 NvdlFilteredXmlReader reader; // s
487 public NvdlValidateInterp (NvdlDispatcher dispatcher,
488 SimpleMode createdMode, SimpleValidate validate,
489 NvdlInterpretation parent)
490 : base (dispatcher, createdMode, validate, parent)
492 NvdlDebug.Writer.WriteLine ("++++++ new validate " + validate.Location);
493 this.reader = new NvdlFilteredXmlReader (dispatcher.Reader, this);
494 validator = validate.CreateValidator (this.reader);
496 dispatcher.Validator.OnMessage (validate.Messages);
499 public override void AttachPlaceHolder ()
501 reader.AttachPlaceHolder ();
502 validator.Read (); // check start Element
505 public override void DetachPlaceHolder ()
507 reader.DetachPlaceHolder ();
508 validator.Read (); // check EndElement
511 public override void EndSection ()
515 public override void StartElement ()
517 ValidateStartElement ();
520 public override void EndElement ()
522 ValidateEndElement ();
525 public override void Text ()
530 public override void Whitespace ()
532 ValidateWhitespace ();
535 public override void ValidateStartElement ()
537 NvdlDebug.Writer.WriteLine ("###### interp.ValidateStartElement : " + Action.Location);
541 public override void ValidateEndElement ()
543 NvdlDebug.Writer.WriteLine ("###### interp.ValidateEndElement : " + Action.Location);
547 public override void ValidateText ()
549 NvdlDebug.Writer.WriteLine ("###### interp.ValidateText : " + Action.Location);
553 public override void ValidateWhitespace ()
555 NvdlDebug.Writer.WriteLine ("###### interp.Whitespace : " + Action.Location);