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.Name = reader.GetAttribute ("name");
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 ());
290 private NvdlCancelAction ReadCancelAction ()
292 NvdlCancelAction el = new NvdlCancelAction ();
294 FillForeignAttribute (el);
295 if (reader.IsEmptyElement) {
301 reader.MoveToContent ();
302 if (reader.NodeType == XmlNodeType.EndElement)
304 if (reader.NamespaceURI != Nvdl.Namespace) {
305 el.Foreign.Add (doc.ReadNode (reader));
308 } while (!reader.EOF);
314 private NvdlValidate ReadValidate ()
316 NvdlValidate el = new NvdlValidate ();
317 NvdlModeUsage mu = new NvdlModeUsage ();
320 el.SchemaType = reader.GetAttribute ("schemaType");
321 el.SimpleMessage = reader.GetAttribute ("message");
322 el.SchemaUri = reader.GetAttribute ("schema");
323 mu.UseMode = reader.GetAttribute ("useMode");
324 FillForeignAttribute (el);
325 if (reader.IsEmptyElement) {
331 reader.MoveToContent ();
332 if (reader.NodeType == XmlNodeType.EndElement)
334 if (reader.NamespaceURI != Nvdl.Namespace) {
335 el.Foreign.Add (doc.ReadNode (reader));
338 switch (reader.LocalName) {
340 el.Messages.Add (ReadMessage ());
343 el.Options.Add (ReadOption ());
346 el.SchemaBody = (XmlElement) doc.ReadNode (reader);
349 mu.NestedMode = ReadNestedMode ();
352 mu.Contexts.Add (ReadContext ());
355 } while (!reader.EOF);
361 private NvdlAllow ReadAllow ()
363 NvdlAllow el = new NvdlAllow ();
364 ReadCommonActionContent (el);
368 private NvdlReject ReadReject ()
370 NvdlReject el = new NvdlReject ();
371 ReadCommonActionContent (el);
375 private NvdlAttach ReadAttach ()
377 NvdlAttach el = new NvdlAttach ();
378 ReadCommonActionContent (el);
382 private NvdlAttachPlaceHolder ReadAttachPlaceHolder ()
384 NvdlAttachPlaceHolder el = new NvdlAttachPlaceHolder ();
385 ReadCommonActionContent (el);
389 private NvdlUnwrap ReadUnwrap ()
391 NvdlUnwrap el = new NvdlUnwrap ();
392 ReadCommonActionContent (el);
396 private void ReadCommonActionContent (NvdlNoCancelAction el)
399 NvdlModeUsage mu = new NvdlModeUsage ();
402 el.SimpleMessage = reader.GetAttribute ("message");
403 mu.UseMode = reader.GetAttribute ("useMode");
404 FillForeignAttribute (el);
405 if (reader.IsEmptyElement) {
411 reader.MoveToContent ();
412 if (reader.NodeType == XmlNodeType.EndElement)
414 if (reader.NamespaceURI != Nvdl.Namespace) {
415 el.Foreign.Add (doc.ReadNode (reader));
418 switch (reader.LocalName) {
420 mu.NestedMode = ReadNestedMode ();
423 el.Messages.Add (ReadMessage ());
426 mu.Contexts.Add (ReadContext ());
429 } while (!reader.EOF);
434 private NvdlMessage ReadMessage ()
436 NvdlMessage el = new NvdlMessage ();
438 el.XmlLang = reader.GetAttribute ("lang", Nvdl.XmlNamespaceUri);
439 FillNonXmlAttributes (el);
440 if (reader.IsEmptyElement) {
445 el.Text = reader.ReadElementString ();
449 private NvdlOption ReadOption ()
451 NvdlOption el = new NvdlOption ();
453 el.Name = reader.GetAttribute ("name");
454 el.Arg = reader.GetAttribute ("arg");
455 el.MustSupport = reader.GetAttribute ("mustSupport");
456 FillForeignAttribute (el);
457 if (reader.IsEmptyElement) {
463 reader.MoveToContent ();
464 if (reader.NodeType == XmlNodeType.EndElement)
466 if (reader.NamespaceURI != Nvdl.Namespace) {
467 el.Foreign.Add (doc.ReadNode (reader));
470 } while (!reader.EOF);
476 private NvdlContext ReadContext ()
478 NvdlContext el = new NvdlContext ();
480 el.Path = reader.GetAttribute ("path");
481 el.UseMode = reader.GetAttribute ("useMode");
482 FillForeignAttribute (el);
483 if (reader.IsEmptyElement) {
489 reader.MoveToContent ();
490 if (reader.NodeType == XmlNodeType.EndElement)
492 if (reader.NamespaceURI != Nvdl.Namespace) {
493 el.Foreign.Add (doc.ReadNode (reader));
496 switch (reader.LocalName) {
498 el.NestedMode = ReadNestedMode ();
501 } while (!reader.EOF);