2 using System.Collections;
4 using Commons.Xml.Relaxng;
6 namespace Commons.Xml.Nvdl
8 public class NvdlReader
10 public static NvdlRules Read (XmlReader reader)
12 return new NvdlReader (reader).ReadRules ();
16 IXmlLineInfo lineInfo;
17 XmlDocument doc = new XmlDocument ();
19 private NvdlReader (XmlReader reader)
21 // FIXME: use .rnc validation.
23 this.lineInfo = reader as IXmlLineInfo;
24 reader.MoveToContent ();
27 private void FillForeignAttribute (NvdlAttributable el)
29 if (!reader.MoveToFirstAttribute ())
32 if (reader.NamespaceURI == "")
34 XmlAttribute a = doc.CreateAttribute (
38 a.Value = reader.Value;
40 } while (reader.MoveToNextAttribute ());
41 reader.MoveToElement ();
44 private void FillNonXmlAttributes (NvdlMessage el)
46 if (!reader.MoveToFirstAttribute ())
49 if (reader.NamespaceURI == Nvdl.XmlNamespaceUri)
51 XmlAttribute a = doc.CreateAttribute (
55 a.Value = reader.Value;
56 el.ForeignAttributes.Add (a);
57 } while (reader.MoveToNextAttribute ());
58 reader.MoveToElement ();
61 private void FillLocation (NvdlElementBase el)
63 el.SourceUri = reader.BaseURI;
64 if (lineInfo != null) {
65 el.LineNumber = lineInfo.LineNumber;
66 el.LinePosition = lineInfo.LinePosition;
70 private NvdlRuleTarget ParseMatch (string s)
73 return NvdlRuleTarget.None;
74 if (s.IndexOf ("elements") >= 0)
75 return (s.IndexOf ("attributes") >= 0) ?
77 NvdlRuleTarget.Elements;
79 return (s.IndexOf ("attributes") >= 0) ?
80 NvdlRuleTarget.Attributes :
84 private NvdlRules ReadRules ()
86 NvdlRules el = new NvdlRules ();
88 el.SchemaType = reader.GetAttribute ("schemaType");
89 el.StartMode = reader.GetAttribute ("startMode");
90 FillForeignAttribute (el);
91 if (reader.IsEmptyElement) {
97 reader.MoveToContent ();
98 if (reader.NodeType == XmlNodeType.EndElement)
100 if (reader.NamespaceURI != Nvdl.Namespace) {
101 el.Foreign.Add (doc.ReadNode (reader));
104 switch (reader.LocalName) {
106 el.Modes.Add (ReadMode ());
109 el.Rules.Add (ReadNamespace ());
112 el.Rules.Add (ReadAnyNamespace ());
115 el.Triggers.Add (ReadTrigger ());
118 } while (!reader.EOF);
124 private NvdlTrigger ReadTrigger ()
126 NvdlTrigger el = new NvdlTrigger ();
128 el.NS = reader.GetAttribute ("ns");
129 el.NameList = reader.GetAttribute ("nameList");
130 FillForeignAttribute (el);
131 if (reader.IsEmptyElement) {
137 reader.MoveToContent ();
138 if (reader.NodeType == XmlNodeType.EndElement)
140 if (reader.NamespaceURI != Nvdl.Namespace) {
141 el.Foreign.Add (doc.ReadNode (reader));
144 } while (!reader.EOF);
150 private NvdlMode ReadMode ()
152 NvdlMode el = new NvdlMode ();
154 el.Name = reader.GetAttribute ("name");
159 private NvdlIncludedMode ReadIncludedMode ()
161 NvdlIncludedMode el = new NvdlIncludedMode ();
163 el.Name = reader.GetAttribute ("name");
168 private NvdlNestedMode ReadNestedMode ()
170 NvdlNestedMode el = new NvdlNestedMode ();
176 private void ReadModeCommon (NvdlModeBase el)
178 FillForeignAttribute (el);
179 if (reader.IsEmptyElement) {
185 reader.MoveToContent ();
186 if (reader.NodeType == XmlNodeType.EndElement)
188 if (reader.NamespaceURI != Nvdl.Namespace) {
189 el.Foreign.Add (doc.ReadNode (reader));
192 switch (reader.LocalName) {
194 el.IncludedModes.Add (ReadIncludedMode ());
197 el.Rules.Add (ReadNamespace ());
200 el.Rules.Add (ReadAnyNamespace ());
203 } while (!reader.EOF);
209 private NvdlAnyNamespace ReadAnyNamespace ()
211 NvdlAnyNamespace el = new NvdlAnyNamespace ();
213 el.Match = ParseMatch (reader.GetAttribute ("match"));
214 FillForeignAttribute (el);
215 if (reader.IsEmptyElement) {
221 reader.MoveToContent ();
222 if (reader.NodeType == XmlNodeType.EndElement)
224 if (reader.NamespaceURI != Nvdl.Namespace) {
225 el.Foreign.Add (doc.ReadNode (reader));
229 } while (!reader.EOF);
235 private NvdlNamespace ReadNamespace ()
237 NvdlNamespace el = new NvdlNamespace ();
239 el.NS = reader.GetAttribute ("ns");
240 el.Wildcard = reader.GetAttribute ("wildCard");
241 el.Match = ParseMatch (reader.GetAttribute ("match"));
242 FillForeignAttribute (el);
243 if (reader.IsEmptyElement) {
249 reader.MoveToContent ();
250 if (reader.NodeType == XmlNodeType.EndElement)
252 if (reader.NamespaceURI != Nvdl.Namespace) {
253 el.Foreign.Add (doc.ReadNode (reader));
257 } while (!reader.EOF);
263 private void ReadAction (NvdlRule el)
265 switch (reader.LocalName) {
266 case "cancelNestedActions":
267 el.Actions.Add (ReadCancelAction ());
270 el.Actions.Add (ReadValidate ());
273 el.Actions.Add (ReadAllow ());
276 el.Actions.Add (ReadReject ());
279 el.Actions.Add (ReadAttach ());
281 case "attachPlaceHolder":
282 el.Actions.Add (ReadAttachPlaceholder ());
285 el.Actions.Add (ReadUnwrap ());
288 throw new NotSupportedException ();
292 private NvdlCancelAction ReadCancelAction ()
294 NvdlCancelAction el = new NvdlCancelAction ();
296 FillForeignAttribute (el);
297 if (reader.IsEmptyElement) {
303 reader.MoveToContent ();
304 if (reader.NodeType == XmlNodeType.EndElement)
306 if (reader.NamespaceURI != Nvdl.Namespace) {
307 el.Foreign.Add (doc.ReadNode (reader));
310 } while (!reader.EOF);
316 private NvdlValidate ReadValidate ()
318 NvdlValidate el = new NvdlValidate ();
319 NvdlModeUsage mu = new NvdlModeUsage ();
322 el.SchemaType = reader.GetAttribute ("schemaType");
323 el.SimpleMessage = reader.GetAttribute ("message");
324 el.SchemaUri = reader.GetAttribute ("schema");
325 mu.UseMode = reader.GetAttribute ("useMode");
326 FillForeignAttribute (el);
327 if (reader.IsEmptyElement) {
333 reader.MoveToContent ();
334 if (reader.NodeType == XmlNodeType.EndElement)
336 if (reader.NamespaceURI != Nvdl.Namespace) {
337 el.Foreign.Add (doc.ReadNode (reader));
340 switch (reader.LocalName) {
342 el.Messages.Add (ReadMessage ());
345 el.Options.Add (ReadOption ());
348 el.SchemaBody = (XmlElement) doc.ReadNode (reader);
351 mu.NestedMode = ReadNestedMode ();
354 mu.Contexts.Add (ReadContext ());
357 throw new NotSupportedException ();
359 } while (!reader.EOF);
365 private NvdlAllow ReadAllow ()
367 NvdlAllow el = new NvdlAllow ();
368 ReadCommonActionContent (el);
372 private NvdlReject ReadReject ()
374 NvdlReject el = new NvdlReject ();
375 ReadCommonActionContent (el);
379 private NvdlAttach ReadAttach ()
381 NvdlAttach el = new NvdlAttach ();
382 ReadCommonActionContent (el);
386 private NvdlAttachPlaceholder ReadAttachPlaceholder ()
388 NvdlAttachPlaceholder el = new NvdlAttachPlaceholder ();
389 ReadCommonActionContent (el);
393 private NvdlUnwrap ReadUnwrap ()
395 NvdlUnwrap el = new NvdlUnwrap ();
396 ReadCommonActionContent (el);
400 private void ReadCommonActionContent (NvdlNoCancelAction el)
403 NvdlModeUsage mu = new NvdlModeUsage ();
406 el.SimpleMessage = reader.GetAttribute ("message");
407 mu.UseMode = reader.GetAttribute ("useMode");
408 FillForeignAttribute (el);
409 if (reader.IsEmptyElement) {
415 reader.MoveToContent ();
416 if (reader.NodeType == XmlNodeType.EndElement)
418 if (reader.NamespaceURI != Nvdl.Namespace) {
419 el.Foreign.Add (doc.ReadNode (reader));
422 switch (reader.LocalName) {
424 mu.NestedMode = ReadNestedMode ();
427 el.Messages.Add (ReadMessage ());
430 mu.Contexts.Add (ReadContext ());
433 throw new NotSupportedException ();
435 } while (!reader.EOF);
440 private NvdlMessage ReadMessage ()
442 NvdlMessage el = new NvdlMessage ();
444 el.XmlLang = reader.GetAttribute ("lang", Nvdl.XmlNamespaceUri);
445 FillNonXmlAttributes (el);
446 if (reader.IsEmptyElement) {
451 el.Text = reader.ReadElementString ();
455 private NvdlOption ReadOption ()
457 NvdlOption el = new NvdlOption ();
459 el.Name = reader.GetAttribute ("name");
460 el.Arg = reader.GetAttribute ("arg");
461 el.MustSupport = reader.GetAttribute ("mustSupport");
462 FillForeignAttribute (el);
463 if (reader.IsEmptyElement) {
469 reader.MoveToContent ();
470 if (reader.NodeType == XmlNodeType.EndElement)
472 if (reader.NamespaceURI != Nvdl.Namespace) {
473 el.Foreign.Add (doc.ReadNode (reader));
476 } while (!reader.EOF);
482 private NvdlContext ReadContext ()
484 NvdlContext el = new NvdlContext ();
486 el.Path = reader.GetAttribute ("path");
487 el.UseMode = reader.GetAttribute ("useMode");
488 FillForeignAttribute (el);
489 if (reader.IsEmptyElement) {
495 reader.MoveToContent ();
496 if (reader.NodeType == XmlNodeType.EndElement)
498 if (reader.NamespaceURI != Nvdl.Namespace) {
499 el.Foreign.Add (doc.ReadNode (reader));
502 switch (reader.LocalName) {
504 el.NestedMode = ReadNestedMode ();
507 throw new NotSupportedException ();
509 } while (!reader.EOF);