Merge pull request #1155 from steffen-kiess/json-string
[mono.git] / mcs / class / Commons.Xml.Relaxng / Commons.Xml.Relaxng.Derivative / RdpNameClasses.cs
1 //
2 // Commons.Xml.Relaxng.Derivative.RdpNameClasses.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.Xml;
36
37 namespace Commons.Xml.Relaxng.Derivative
38 {
39         public enum RdpNameClassType
40         {
41                 None = 0,
42                 AnyName = 1,
43                 AnyNameExcept = 2,
44                 NsName = 3,
45                 NsNameExcept = 4,
46                 Name = 5,
47                 NameClassChoice = 6
48         }
49
50         public abstract class RdpNameClass
51         {
52                 public abstract bool HasInfiniteName { get; }
53                 public abstract RdpNameClassType NameClassType { get; }
54                 public abstract bool Contains (string name, string ns);
55         }
56
57         public class RdpAnyName : RdpNameClass
58         {
59                 static RdpAnyName instance;
60                 static RdpAnyName ()
61                 {
62                         instance = new RdpAnyName ();
63                 }
64
65                 public static RdpAnyName Instance {
66                         get { return instance; }
67                 }
68
69                 private RdpAnyName () {}
70
71                 public override bool HasInfiniteName {
72                         get { return true; }
73                 }
74
75                 public override RdpNameClassType NameClassType {
76                         get { return RdpNameClassType.AnyName; }
77                 }
78
79                 public override bool Contains (string name, string ns)
80                 {
81                         return true;
82                 }
83         }
84
85         public class RdpAnyNameExcept : RdpNameClass
86         {
87                 RdpNameClass except;
88
89                 public RdpAnyNameExcept (RdpNameClass except)
90                 {
91                         this.except = except;
92                 }
93
94                 public override bool HasInfiniteName {
95                         get { return true; }
96                 }
97
98                 public override RdpNameClassType NameClassType {
99                         get { return RdpNameClassType.AnyNameExcept; }
100                 }
101
102                 public RdpNameClass ExceptNameClass {
103                         get { return except; }
104                 }
105
106                 public override bool Contains (string name, string ns)
107                 {
108                         return (except == null) || !except.Contains (name, ns);
109                 }
110         }
111
112         public class RdpNsName : RdpNameClass
113         {
114                 string ns;
115
116                 public RdpNsName (string ns)
117                 {
118                         this.ns = ns;
119                 }
120
121                 public override bool HasInfiniteName {
122                         get { return true; }
123                 }
124
125                 public override RdpNameClassType NameClassType {
126                         get { return RdpNameClassType.NsName; }
127                 }
128
129                 public string NamespaceURI {
130                         get { return ns; }
131                 }
132
133                 public override bool Contains (string name, string ns)
134                 {
135                         return NamespaceURI == ns;
136                 }
137         }
138
139         public class RdpNsNameExcept : RdpNsName
140         {
141                 string ns;
142                 RdpNameClass except;
143
144                 public RdpNsNameExcept (string ns, RdpNameClass except)
145                         : base (ns)
146                 {
147                         this.ns = ns;
148                         this.except = except;
149                 }
150
151                 public override bool HasInfiniteName {
152                         get { return true; }
153                 }
154
155                 public override RdpNameClassType NameClassType {
156                         get { return RdpNameClassType.NsNameExcept; }
157                 }
158
159                 public RdpNameClass ExceptNameClass {
160                         get { return except; }
161                 }
162
163                 public override bool Contains (string name, string ns)
164                 {
165                         return this.ns == ns &&
166                                 (except == null || !except.Contains (name, ns));
167                 }
168         }
169
170         public class RdpName : RdpNameClass
171         {
172                 string local;
173                 string ns;
174
175                 public RdpName (string local, string ns)
176                 {
177                         this.ns = ns;
178                         this.local = local;
179                 }
180
181                 public override bool HasInfiniteName {
182                         get { return false; }
183                 }
184
185                 public override RdpNameClassType NameClassType {
186                         get { return RdpNameClassType.Name; }
187                 }
188
189                 public string NamespaceURI {
190                         get { return ns; }
191                 }
192
193                 public string LocalName {
194                         get { return local; }
195                 }
196
197                 public override bool Contains (string name, string ns)
198                 {
199                         return this.ns == ns && this.local == name;
200                 }
201         }
202
203         public class RdpNameClassChoice : RdpNameClass
204         {
205                 RdpNameClass l;
206                 RdpNameClass r;
207
208                 public RdpNameClassChoice (RdpNameClass l, RdpNameClass r)
209                 {
210                         this.l = l;
211                         this.r = r;
212                 }
213
214                 public override bool HasInfiniteName {
215                         get { return l.HasInfiniteName || r.HasInfiniteName; }
216                 }
217
218                 public override RdpNameClassType NameClassType {
219                         get { return RdpNameClassType.NameClassChoice; }
220                 }
221
222                 public RdpNameClass LValue {
223                         get { return l; }
224                 }
225
226                 public RdpNameClass RValue {
227                         get { return r; }
228                 }
229
230                 public override bool Contains (string name, string ns)
231                 {
232                         return l.Contains (name, ns) || r.Contains (name, ns);
233                 }
234         }
235
236 }
237