2 // RELAX NG Compact Syntax writer
\r
5 // Atsushi Enomoto <atsushi@ximian.com>
\r
7 // (C)2005 Novell Inc.
\r
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Collections;
\r
35 using Commons.Xml.Relaxng;
\r
38 using NSResolver = System.Xml.IXmlNamespaceResolver;
\r
40 using NSResolver = System.Xml.XmlNamespaceManager;
\r
43 namespace Commons.Xml.Relaxng.Rnc
\r
45 internal class RncWriter
\r
47 static readonly XmlNamespaceManager defaultNamespaceManager;
\r
51 XmlNamespaceManager n = new XmlNamespaceManager (
\r
53 n.AddNamespace ("xs", "http://www.w3.org/2001/XMLSchema-datatypes");
\r
54 n.AddNamespace ("xsi", "http://www.w3.org/2001/XMLSchema-instance");
\r
55 defaultNamespaceManager = n;
\r
61 public RncWriter (TextWriter writer)
\r
62 : this (writer, defaultNamespaceManager)
\r
66 public RncWriter (TextWriter writer, NSResolver nsmgr)
\r
72 #region Utility methods
\r
74 private void WriteNames (RelaxngNameClassList l, bool wrap)
\r
78 throw new RelaxngException ("name choice must contain at least one name class.");
\r
80 l [0].WriteRnc (this);
\r
85 l [0].WriteRnc (this);
\r
86 for (int i = 1; i < l.Count; i++) {
\r
88 l [i].WriteRnc (this);
\r
96 private void WritePatterns (RelaxngPatternList l, bool parens)
\r
98 WritePatterns (l, ',', parens);
\r
101 private void WritePatterns (RelaxngPatternList l,
\r
102 char sep, bool parens)
\r
109 parens = (l [0] is RelaxngBinaryContentPattern
\r
110 || l [0] is RelaxngData && ((RelaxngData) l [0]).Except != null);
\r
113 l [0].WriteRnc (this);
\r
120 l [0].WriteRnc (this);
\r
121 for (int i = 1; i < l.Count; i++) {
\r
126 l [i].WriteRnc (this);
\r
134 private void WriteGrammarIncludeContents (
\r
135 RelaxngGrammarContentList starts,
\r
136 RelaxngGrammarContentList defines,
\r
137 RelaxngGrammarContentList divs,
\r
138 RelaxngGrammarContentList includes)
\r
140 if (includes != null)
\r
141 foreach (RelaxngInclude inc in includes)
\r
142 inc.WriteRnc (this);
\r
144 foreach (RelaxngDiv div in divs)
\r
145 div.WriteRnc (this);
\r
146 if (starts != null)
\r
147 foreach (RelaxngStart s in starts)
\r
149 if (defines != null)
\r
150 foreach (RelaxngDefine def in defines)
\r
151 def.WriteRnc (this);
\r
154 private void WriteQName (string name, string ns)
\r
156 string prefix = String.Empty;
\r
157 if (ns != null && ns != String.Empty) {
\r
160 // XmlNamespaceManager sucks.
\r
161 ns = nsmgr.NameTable.Add (ns);
\r
163 prefix = nsmgr.LookupPrefix (ns);
\r
165 if (prefix == null)
\r
166 throw new RelaxngException (String.Format ("Namespace '{0}' is not mapped to a prefix in argument XmlNamespaceManager.", ns));
\r
167 if (prefix != String.Empty) {
\r
174 private void WriteLiteral (string value)
\r
177 for (int i = 0; i < value.Length; i++) {
\r
178 switch (value [i]) {
\r
180 w.Write ("\\x{22}");
\r
183 w.Write ("\\x{13}");
\r
186 w.Write ("\\x{10}");
\r
188 case '\t': // It is not required, but would be better.
\r
189 w.Write ("\\x{9}");
\r
192 w.Write (value [i]);
\r
202 // Note that it might not be used directly when a grammar
\r
203 // contains more than one "start" (compact syntax does not
\r
204 // support "combine" attribute).
\r
205 public void WriteStart (RelaxngStart start)
\r
208 if (start.Combine == null)
\r
211 w.Write (start.Combine.Trim () == "interleave" ?
\r
213 start.Pattern.WriteRnc (this);
\r
217 // Note that it might not be used directly when a grammar
\r
218 // contains more than one "define" for an identical name
\r
219 // (compact syntax does not support "combine" attribute).
\r
220 public void WriteDefine (RelaxngDefine define)
\r
222 w.Write (define.Name);
\r
223 if (define.Combine == null)
\r
226 w.Write (define.Combine.Trim () == "interleave" ?
\r
228 if (define.Patterns.Count == 0)
\r
231 define.Patterns [0].WriteRnc (this);
\r
232 for (int i = 1; i < define.Patterns.Count; i++) {
\r
234 define.Patterns [i].WriteRnc (this);
\r
240 public void WriteInclude (RelaxngInclude include)
\r
242 w.Write ("include ");
\r
243 w.Write (include.Href);
\r
245 // FIXME: optInherit?
\r
247 if (include.Starts.Count > 0 ||
\r
248 include.Defines.Count > 0 ||
\r
249 include.Divs.Count > 0) {
\r
251 WriteGrammarIncludeContents (include.Starts,
\r
252 include.Defines, include.Divs, null);
\r
258 public void WriteDiv (RelaxngDiv div)
\r
260 w.Write ("div { ");
\r
261 WriteGrammarIncludeContents (div.Starts,
\r
262 div.Defines, div.Divs, div.Includes);
\r
266 public void WriteNotAllowed (RelaxngNotAllowed na)
\r
268 w.Write ("notAllowed ");
\r
271 public void WriteEmpty (RelaxngEmpty empty)
\r
273 w.Write ("empty ");
\r
276 public void WriteText (RelaxngText text)
\r
281 public void WriteData (RelaxngData data)
\r
283 WriteQName (data.Type, data.DatatypeLibrary);
\r
284 if (data.ParamList.Count > 0) {
\r
286 foreach (RelaxngParam p in data.ParamList)
\r
290 if (data.Except != null)
\r
291 data.Except.WriteRnc (this);
\r
294 public void WriteValue (RelaxngValue v)
\r
296 WriteQName (v.Type, v.DatatypeLibrary);
\r
298 WriteLiteral (v.Value);
\r
301 public void WriteList (RelaxngList p)
\r
303 w.Write ("list {");
\r
304 WritePatterns (p.Patterns, false);
\r
308 public void WriteMixed (RelaxngMixed p)
\r
310 w.Write ("mixed {");
\r
311 WritePatterns (p.Patterns, false);
\r
315 public void WriteElement (RelaxngElement element)
\r
317 w.Write ("element ");
\r
318 element.NameClass.WriteRnc (this);
\r
320 WritePatterns (element.Patterns, false);
\r
324 public void WriteAttribute (RelaxngAttribute attribute)
\r
326 w.Write ("attribute ");
\r
327 attribute.NameClass.WriteRnc (this);
\r
329 if (attribute.Pattern == null)
\r
332 attribute.Pattern.WriteRnc (this);
\r
336 public void WriteRef (RelaxngRef r)
\r
341 public void WriteParentRef (RelaxngParentRef r)
\r
343 w.Write ("parent ");
\r
348 public void WriteExternalRef (RelaxngExternalRef r)
\r
350 w.Write ("external ");
\r
352 // FIXME: optInherit?
\r
356 public void WriteOneOrMore (RelaxngOneOrMore p)
\r
358 WritePatterns (p.Patterns, true);
\r
362 public void WriteZeroOrMore (RelaxngZeroOrMore p)
\r
364 WritePatterns (p.Patterns, true);
\r
368 public void WriteOptional (RelaxngOptional p)
\r
370 WritePatterns (p.Patterns, true);
\r
374 public void WriteChoice (RelaxngChoice p)
\r
376 WritePatterns (p.Patterns, '|', false);
\r
379 public void WriteGroup (RelaxngGroup p)
\r
381 WritePatterns (p.Patterns, ',', false);
\r
384 public void WriteInterleave (RelaxngInterleave p)
\r
386 WritePatterns (p.Patterns, '&', false);
\r
389 public void WriteParam (RelaxngParam p)
\r
393 WriteLiteral (p.Value);
\r
396 public void WriteDataExcept (RelaxngExcept e)
\r
399 WritePatterns (e.Patterns, true);
\r
402 public void WriteGrammar (RelaxngGrammar g)
\r
404 w.WriteLine ("grammar {");
\r
405 WriteGrammarIncludeContents (g.Starts,
\r
406 g.Defines, g.Divs, g.Includes);
\r
410 public void WriteAnyName (RelaxngAnyName n)
\r
413 if (n.Except != null)
\r
414 n.Except.WriteRnc (this);
\r
417 public void WriteNsName (RelaxngNsName n)
\r
419 WriteQName ("*", n.Namespace);
\r
420 if (n.Except != null)
\r
421 n.Except.WriteRnc (this);
\r
424 public void WriteName (RelaxngName n)
\r
426 WriteQName (n.LocalName, n.Namespace);
\r
429 public void WriteNameChoice (RelaxngNameChoice c)
\r
431 WriteNames (c.Children, false);
\r
434 public void WriteNameExcept (RelaxngExceptNameClass e)
\r
437 WriteNames (e.Names, true);
\r