New test.
[mono.git] / mcs / class / System.XML / System.Xml.Schema / XmlSchemaGroupBase.cs
1 //\r
2 // XmlSchemaGroupBase.cs\r
3 //\r
4 // Authors:\r
5 //      Dwivedi, Ajay kumar Adwiv@Yahoo.com\r
6 //      Atsushi Enomoto atsushi@ximian.com\r
7 //\r
8
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29 using System;\r
30 using System.Xml.Serialization;\r
31 \r
32 namespace System.Xml.Schema\r
33 {\r
34         public abstract class XmlSchemaGroupBase : XmlSchemaParticle\r
35         {\r
36                 private XmlSchemaObjectCollection compiledItems;\r
37 \r
38                 protected XmlSchemaGroupBase ()\r
39                 {\r
40                         compiledItems = new XmlSchemaObjectCollection ();\r
41                 }\r
42 \r
43                 [XmlIgnore]\r
44                 public abstract XmlSchemaObjectCollection Items { get; }\r
45 \r
46                 internal XmlSchemaObjectCollection CompiledItems \r
47                 {\r
48                         get{ return compiledItems; }\r
49                 }\r
50 \r
51                 internal void CopyOptimizedItems (XmlSchemaGroupBase gb)\r
52                 {\r
53                         for (int i = 0; i < Items.Count; i++) {\r
54                                 XmlSchemaParticle p = Items [i] as XmlSchemaParticle;\r
55                                 p = p.GetOptimizedParticle (false);\r
56                                 if (p == XmlSchemaParticle.Empty)\r
57                                         continue;\r
58                                 gb.Items.Add (p);\r
59                                 gb.CompiledItems.Add (p);\r
60                         }\r
61                 }\r
62 \r
63                 internal override bool ParticleEquals (XmlSchemaParticle other)\r
64                 {\r
65                         XmlSchemaGroupBase gb = other as XmlSchemaGroupBase;\r
66                         if (gb == null)\r
67                                 return false;\r
68                         if (this.GetType () != gb.GetType ())\r
69                                 return false;\r
70 \r
71                         if (this.ValidatedMaxOccurs != gb.ValidatedMaxOccurs ||\r
72                                 this.ValidatedMinOccurs != gb.ValidatedMinOccurs)\r
73                                 return false;\r
74                         if (this.CompiledItems.Count != gb.CompiledItems.Count)\r
75                                 return false;\r
76                         for (int i = 0; i < CompiledItems.Count; i++) {\r
77                                 XmlSchemaParticle p1 = this.CompiledItems [i] as XmlSchemaParticle;\r
78                                 XmlSchemaParticle p2 = gb.CompiledItems [i] as XmlSchemaParticle;\r
79                                 if (!p1.ParticleEquals (p2))\r
80                                         return false;\r
81                         }\r
82                         return true;\r
83                 }\r
84 \r
85                 internal override void CheckRecursion (int depth, ValidationEventHandler h, XmlSchema schema)\r
86                 {\r
87                         foreach (XmlSchemaParticle p in this.Items)\r
88                                 p.CheckRecursion (depth, h, schema);\r
89                 }\r
90 \r
91                 internal bool ValidateNSRecurseCheckCardinality (XmlSchemaAny any,\r
92                         ValidationEventHandler h, XmlSchema schema, bool raiseError)\r
93                 {\r
94                         foreach (XmlSchemaParticle p in Items)\r
95                                 if (!p.ValidateDerivationByRestriction (any, h, schema, raiseError))\r
96                                         return false;\r
97                         return ValidateOccurenceRangeOK (any, h, schema, raiseError);\r
98                 }\r
99 \r
100                 internal bool ValidateRecurse (XmlSchemaGroupBase baseGroup,\r
101                         ValidationEventHandler h, XmlSchema schema, bool raiseError)\r
102                 {\r
103                         return ValidateSeqRecurseMapSumCommon (baseGroup, h, schema, false, false, raiseError);\r
104                 }\r
105 \r
106                 internal bool ValidateSeqRecurseMapSumCommon (XmlSchemaGroupBase baseGroup,\r
107                         ValidationEventHandler h, XmlSchema schema, bool isLax, bool isMapAndSum, bool raiseError)\r
108                 {\r
109                         int index = 0;\r
110                         int baseIndex = 0;\r
111                         decimal baseOccured = 0;\r
112                         if (baseGroup.CompiledItems.Count == 0 && this.CompiledItems.Count > 0) {\r
113                                 if (raiseError)\r
114                                         error (h, "Invalid particle derivation by restriction was found. base particle does not contain particles.");\r
115                                 return false;\r
116                         }\r
117 \r
118                         for (int i = 0; i < CompiledItems.Count; i++) {\r
119                                 // get non-empty derived particle\r
120                                 XmlSchemaParticle pd = null;\r
121                                 while (this.CompiledItems.Count > index) {\r
122                                         pd = ((XmlSchemaParticle) this.CompiledItems [index]);//.GetOptimizedParticle (false);\r
123                                         if (pd != XmlSchemaParticle.Empty)// && pd.ValidatedMaxOccurs > 0)\r
124                                                 break;\r
125                                         else\r
126                                                 index++;\r
127                                 }\r
128                                 if (index >= CompiledItems.Count) {\r
129                                         if (raiseError)\r
130                                                 error (h, "Invalid particle derivation by restriction was found. Cannot be mapped to base particle.");\r
131                                         return false;\r
132                                 }\r
133 \r
134                                 // get non-empty base particle\r
135                                 XmlSchemaParticle pb = null;\r
136                                 while (baseGroup.CompiledItems.Count > baseIndex) {\r
137                                         pb = ((XmlSchemaParticle) baseGroup.CompiledItems [baseIndex]);//.GetOptimizedParticle (false);\r
138                                         if (pb == XmlSchemaParticle.Empty && pb.ValidatedMaxOccurs > 0)\r
139                                                 continue;\r
140                                         if (!pd.ValidateDerivationByRestriction (pb, h, schema, false)) {\r
141                                                 if (!isLax && !isMapAndSum && pb.MinOccurs > baseOccured && !pb.ValidateIsEmptiable ()) {\r
142                                                         if (raiseError)\r
143                                                                 error (h, "Invalid particle derivation by restriction was found. Invalid sub-particle derivation was found.");\r
144                                                         return false;\r
145                                                 }\r
146                                                 else {\r
147                                                         baseOccured = 0;\r
148                                                         baseIndex++;\r
149                                                 }\r
150                                         } else {\r
151                                                 baseOccured += pb.ValidatedMinOccurs;\r
152                                                 if (baseOccured >= baseGroup.ValidatedMaxOccurs) {\r
153                                                         baseOccured = 0;\r
154                                                         baseIndex++;\r
155                                                 }\r
156                                                 index++;\r
157                                                 break;\r
158                                         }\r
159                                 }\r
160                         }\r
161                         if (this.CompiledItems.Count > 0 && index != this.CompiledItems.Count) {\r
162                                 if (raiseError)\r
163                                         error (h, "Invalid particle derivation by restriction was found. Extraneous derived particle was found.");\r
164                                 return false;\r
165                         }\r
166                         if (!isLax && !isMapAndSum) {\r
167                                 if (baseOccured > 0)\r
168                                         baseIndex++;\r
169                                 for (int i = baseIndex; i < baseGroup.CompiledItems.Count; i++) {\r
170                                         XmlSchemaParticle p = baseGroup.CompiledItems [i] as XmlSchemaParticle;\r
171                                         if (!p.ValidateIsEmptiable ()) {\r
172                                                 if (raiseError)\r
173                                                         error (h, "Invalid particle derivation by restriction was found. There is a base particle which does not have mapped derived particle and is not emptiable.");\r
174                                                 return false;\r
175                                         }\r
176                                 }\r
177                         }\r
178                         return true;\r
179                 }\r
180         }\r
181 }\r