private void WriteGrammarIncludeContents ( RelaxngGrammarContentList starts, RelaxngGrammarContentList defines, RelaxngGrammarContentList divs, RelaxngGrammarContentList includes) { if (includes != null) { foreach (RelaxngInclude inc in includes) inc.WriteRnc (this); w.WriteLine (); } if (divs != null) { foreach (RelaxngDiv div in divs) div.WriteRnc (this); w.WriteLine (); } if (starts != null) { foreach (RelaxngStart s in starts) s.WriteRnc (this); w.WriteLine (); } if (defines != null) foreach (RelaxngDefine def in defines) def.WriteRnc (this); }
// compile into div internal RelaxngDiv Compile(RelaxngGrammar grammar) { grammar.CheckIncludeRecursion(Href); grammar.IncludedUris.Add(Href, Href); if (grammar.Resolver == null) { throw new RelaxngException(this, "To compile 'include' element, XmlResolver is required."); } Uri baseUri = null; try { if (BaseUri != null && BaseUri != String.Empty) { baseUri = new Uri(BaseUri); } } catch (UriFormatException) { if (File.Exists(BaseUri)) { baseUri = new Uri(Path.GetFullPath(BaseUri)); } } Uri uri = grammar.Resolver.ResolveUri(baseUri, Href); RelaxngGrammar g = ReadExternalResource(grammar, uri, (ns != null && ns.Length != 0) || !grammar.IsSourceCompactSyntax ? ns : grammar.DefaultNamespace) as RelaxngGrammar; if (g == null) { throw new RelaxngException(this, "Included syntax must start with \"grammar\" element."); } g.DataProvider = grammar.Provider; g.IsSourceCompactSyntax = grammar.IsSourceCompactSyntax; // process recursive inclusions. foreach (RelaxngInclude inc in g.Includes) { g.Divs.Add(inc.Compile(grammar)); } // process this own div children. // each div subelements are also compiled. foreach (RelaxngDiv cdiv in divs) { cdiv.Compile(g); } foreach (RelaxngDiv cdiv in g.Divs) { cdiv.Compile(g); } // replace redifinitions into div. // starts. if (this.Starts.Count > 0 && g.Starts.Count == 0) { throw new RelaxngException(this, "When the included grammar does not contain start components, this include component must not contain start components."); } RelaxngGrammarContentList appliedStarts = (this.starts.Count > 0) ? this.starts : g.Starts; RelaxngDiv div = new RelaxngDiv(); div.BaseUri = this.BaseUri; div.LinePosition = this.LinePosition; div.LineNumber = this.LineNumber; foreach (RelaxngStart start in appliedStarts) { div.Starts.Add(start); } // defines. Hashtable overrides = new Hashtable(); Hashtable originalDefs = new Hashtable(); foreach (RelaxngDefine def in defines) { overrides.Add(def.Name, def.Name); div.Defines.Add(def); } foreach (RelaxngDefine def in g.Defines) { originalDefs [def.Name] = def.Name; if (!overrides.ContainsKey(def.Name)) { div.Defines.Add(def); } // else discard. } foreach (string name in overrides.Values) { if (!originalDefs.ContainsKey(name)) { throw new RelaxngException(this, "The include component must not contain define components whose name does not appear in the included grammar component."); } } grammar.IncludedUris.Remove(Href); return(div); }