2005-12-27 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / Commons.Xml.Relaxng / Commons.Xml.Relaxng / RelaxngNameClass.cs
1 //
2 // Commons.Xml.Relaxng.RelaxngNameClass.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
6 //
7 // 2003 Atsushi Enomoto "No rights reserved."
8 //
9 // Copyright (c) 2004 Novell Inc.
10 // All rights reserved
11 //
12
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 using System;
35 using System.Collections;
36 using System.IO;
37 using System.Net;
38 using System.Xml;
39 using Commons.Xml.Relaxng.Derivative;
40 using Commons.Xml.Relaxng.Rnc;
41
42 namespace Commons.Xml.Relaxng
43 {
44         public class RelaxngNameClassList : CollectionBase
45         {
46                 public RelaxngNameClassList ()
47                 {
48                 }
49
50                 public void Add (RelaxngNameClass p)
51                 {
52                         List.Add (p);
53                 }
54
55                 public RelaxngNameClass this [int i] {
56                         get { return this.List [i] as RelaxngNameClass; }
57                         set { this.List [i] = value; }
58                 }
59
60                 public void Insert (int pos, RelaxngNameClass p)
61                 {
62                         List.Insert (pos, p);
63                 }
64
65                 public void Remove (RelaxngNameClass p)
66                 {
67                         List.Remove (p);
68                 }
69         }
70
71         public abstract class RelaxngNameClass : RelaxngElementBase
72         {
73                 protected RelaxngNameClass ()
74                 {
75                 }
76
77                 internal abstract RdpNameClass Compile (RelaxngGrammar g);
78
79                 internal abstract void CheckConstraints (bool rejectAnyName, bool rejectNsName);
80
81                 internal bool FindInvalidType (RdpNameClass nc, bool allowNsName)
82                 {
83                         RdpNameClassChoice choice = nc as RdpNameClassChoice;
84                         if (choice != null)
85                                 return FindInvalidType (choice.LValue, allowNsName)
86                                         || FindInvalidType (choice.RValue, allowNsName);
87                         else if (nc is RdpAnyName)
88                                 return true;
89                         else if (nc is RdpNsName && !allowNsName)
90                                 return true;
91                         else
92                                 return false;
93                 }
94         }
95
96         public class RelaxngAnyName : RelaxngNameClass
97         {
98                 RelaxngExceptNameClass except;
99                 public RelaxngAnyName ()
100                 {
101                 }
102
103                 public RelaxngExceptNameClass Except {
104                         get { return except; }
105                         set { except = value; }
106                 }
107
108                 public override void Write (XmlWriter writer)
109                 {
110                         writer.WriteStartElement ("", "anyName", RelaxngGrammar.NamespaceURI);
111                         if (except != null)
112                                 except.Write (writer);
113                         writer.WriteEndElement ();
114                 }
115
116                 internal override void WriteRnc (RncWriter writer)
117                 {
118                         writer.WriteAnyName (this);
119                 }
120
121                 internal override RdpNameClass Compile (RelaxngGrammar g)
122                 {
123                         if (except != null) {
124                                 RdpNameClass exc = except.Compile (g);
125                                 if (FindInvalidType (exc, true))
126                                         throw new RelaxngException (except, "anyName except cannot have anyName children.");
127                                 return new RdpAnyNameExcept (exc);
128                         } else
129                                 return RdpAnyName.Instance;
130                 }
131
132                 internal override void CheckConstraints (bool rejectAnyName, bool rejectNsName) 
133                 {
134                         if (rejectAnyName)
135                                 throw new RelaxngException (this, "Not allowed anyName was found.");
136                         if (except != null)
137                                 foreach (RelaxngNameClass nc in except.Names)
138                                         nc.CheckConstraints (true, rejectNsName);
139                 }
140
141         }
142
143         public class RelaxngNsName : RelaxngNameClass
144         {
145                 string ns;
146                 RelaxngExceptNameClass except;
147                 public RelaxngNsName ()
148                 {
149                 }
150
151                 public string Namespace {
152                         get { return ns; }
153                         set { ns = value; }
154                 }
155
156                 public RelaxngExceptNameClass Except {
157                         get { return except; }
158                         set { except = value; }
159                 }
160
161                 public override void Write (XmlWriter writer)
162                 {
163                         writer.WriteStartElement ("", "nsName", RelaxngGrammar.NamespaceURI);
164                         if (except != null)
165                                 except.Write (writer);
166                         writer.WriteEndElement ();
167                 }
168
169                 internal override void WriteRnc (RncWriter writer)
170                 {
171                         writer.WriteNsName (this);
172                 }
173
174                 internal override RdpNameClass Compile (RelaxngGrammar g)
175                 {
176                         if (except != null) {
177                                 RdpNameClass exc = except.Compile (g);
178                                 if (FindInvalidType (exc, false))
179                                         throw new RelaxngException (except, "nsName except cannot have anyName nor nsName children.");
180                                 return new RdpNsNameExcept (ns, exc);
181                         } else {
182                                 return new RdpNsName (ns);
183                         }
184                 }
185
186                 internal override void CheckConstraints (bool rejectAnyName, bool rejectNsName) 
187                 {
188                         if (rejectNsName)
189                                 throw new RelaxngException (this, "Not allowed nsName was found.");
190                         if (except != null)
191                                 foreach (RelaxngNameClass nc in except.Names)
192                                         nc.CheckConstraints (true, true);
193                 }
194
195         }
196
197         public class RelaxngName : RelaxngNameClass
198         {
199                 string ns;
200                 string ncname;
201
202                 public RelaxngName ()
203                 {
204                 }
205
206                 public RelaxngName (string ncname, string ns)
207                 {
208                         XmlConvert.VerifyNCName (ncname);
209                         this.ncname = ncname;
210                         this.ns = ns;
211                 }
212
213                 public string LocalName {
214                         get { return ncname; }
215                         set {
216                                 XmlConvert.VerifyNCName (value);
217                                 ncname = value;
218                         }
219                 }
220
221                 public string Namespace {
222                         get { return ns; }
223                         set { ns = value; }
224                 }
225
226                 public override void Write (XmlWriter writer)
227                 {
228                         writer.WriteStartElement ("", "name", RelaxngGrammar.NamespaceURI);
229                         writer.WriteAttributeString ("ns", ns);
230                         // Here we just skip qname
231                         writer.WriteString (ncname);
232                         writer.WriteEndElement ();
233                 }
234
235                 internal override void WriteRnc (RncWriter writer)
236                 {
237                         writer.WriteName (this);
238                 }
239
240                 internal override RdpNameClass Compile (RelaxngGrammar g)
241                 {
242                         return new RdpName (ncname, ns);
243                 }
244
245                 internal override void CheckConstraints (bool rejectAnyName, bool rejectNsName) 
246                 {
247                         // no error
248                 }
249         }
250
251         public class RelaxngNameChoice : RelaxngNameClass
252         {
253                 RelaxngNameClassList names = new RelaxngNameClassList ();
254
255                 public RelaxngNameChoice ()
256                 {
257                 }
258
259                 public RelaxngNameClassList Children {
260                         get { return names; }
261                         set { names = value; }
262                 }
263
264                 public override void Write (XmlWriter writer)
265                 {
266                         writer.WriteStartElement ("", "choice", RelaxngGrammar.NamespaceURI);
267                         foreach (RelaxngNameClass nc in Children)
268                                 nc.Write (writer);
269                         writer.WriteEndElement ();
270                 }
271
272                 internal override void WriteRnc (RncWriter writer)
273                 {
274                         writer.WriteNameChoice (this);
275                 }
276
277                 internal override RdpNameClass Compile (RelaxngGrammar g)
278                 {
279                         // Flatten names into RdpChoice. See 4.12.
280                         if (names.Count == 0)
281                                 return null;
282                         RdpNameClass p = ((RelaxngNameClass) names [0]).Compile (g);
283                         if (names.Count == 1)
284                                 return p;
285
286                         for (int i=1; i<names.Count; i++)
287                                 p = new RdpNameClassChoice (p, ((RelaxngNameClass) names [i]).Compile (g));
288                         return p;
289                 }
290
291                 internal override void CheckConstraints (bool rejectAnyName, bool rejectNsName) 
292                 {
293                         foreach (RelaxngNameClass nc in names)
294                                 nc.CheckConstraints (rejectAnyName, rejectNsName);
295                 }
296         }
297
298         public class RelaxngExceptNameClass : RelaxngElementBase
299         {
300                 RelaxngNameClassList names = new RelaxngNameClassList ();
301
302                 public RelaxngExceptNameClass ()
303                 {
304                 }
305
306                 public RelaxngNameClassList Names {
307                         get { return names; }
308                 }
309
310                 public override void Write (XmlWriter writer)
311                 {
312                         writer.WriteStartElement ("", "except", RelaxngGrammar.NamespaceURI);
313                         foreach (RelaxngNameClass nc in Names)
314                                 nc.Write (writer);
315                         writer.WriteEndElement ();
316                 }
317
318                 internal override void WriteRnc (RncWriter writer)
319                 {
320                         writer.WriteNameExcept (this);
321                 }
322
323                 internal RdpNameClass Compile (RelaxngGrammar g)
324                 {
325                         // Flatten names into RdpGroup. See 4.12.
326                         if (names.Count == 0)
327                                 return null;
328                         RdpNameClass p = ((RelaxngNameClass) names [0]).Compile (g);
329                         for (int i=1; i<names.Count; i++) {
330                                 p = new RdpNameClassChoice (
331                                         ((RelaxngNameClass) names [i]).Compile (g),
332                                         p);
333                         }
334                         return p;
335                 }
336         }
337 }