2 // System.Xml.Schema.XmlSchemaAll.cs
\r
5 // Dwivedi, Ajay kumar Adwiv@Yahoo.com
\r
6 // Atsushi Enomoto ginga@kit.hi-ho.ne.jp
\r
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
30 using System.Collections;
\r
32 using System.Xml.Serialization;
\r
34 namespace System.Xml.Schema
\r
37 /// Summary description for XmlSchemaAll.
\r
39 public class XmlSchemaAll : XmlSchemaGroupBase
\r
41 private XmlSchema schema;
\r
42 private XmlSchemaObjectCollection items;
\r
43 const string xmlname = "all";
\r
44 private bool emptiable;
\r
46 public XmlSchemaAll()
\r
48 items = new XmlSchemaObjectCollection();
\r
51 [XmlElement("element",typeof(XmlSchemaElement))]
\r
52 public override XmlSchemaObjectCollection Items
\r
54 get{ return items; }
\r
57 internal bool Emptiable
\r
59 get { return emptiable; }
\r
64 /// 1. MaxOccurs must be one. (default is also one)
\r
65 /// 2. MinOccurs must be zero or one.
\r
67 internal override int Compile(ValidationEventHandler h, XmlSchema schema)
\r
69 // If this is already compiled this time, simply skip.
\r
70 if (CompilationId == schema.CompilationId)
\r
73 this.schema = schema;
\r
75 if(MaxOccurs != Decimal.One)
\r
76 error(h,"maxOccurs must be 1");
\r
77 if(MinOccurs != Decimal.One && MinOccurs != Decimal.Zero)
\r
78 error(h,"minOccurs must be 0 or 1");
\r
80 XmlSchemaUtil.CompileID(Id, this, schema.IDCollection, h);
\r
81 CompileOccurence (h, schema);
\r
83 foreach(XmlSchemaObject obj in Items)
\r
89 XmlSchemaElement elem = obj as XmlSchemaElement;
\r
92 if(elem.ValidatedMaxOccurs != Decimal.One && elem.ValidatedMaxOccurs != Decimal.Zero)
\r
94 elem.error (h,"The {max occurs} of all the elements of 'all' must be 0 or 1. ");
\r
96 errorCount += elem.Compile(h, schema);
\r
100 error(h,"XmlSchemaAll can only contain Items of type Element");
\r
104 this.CompilationId = schema.CompilationId;
\r
108 internal override XmlSchemaParticle GetOptimizedParticle (bool isTop)
\r
110 if (OptimizedParticle != null)
\r
111 return OptimizedParticle;
\r
112 if (Items.Count == 0 || ValidatedMaxOccurs == 0) {
\r
113 OptimizedParticle = XmlSchemaParticle.Empty;
\r
114 return OptimizedParticle;
\r
116 else if (Items.Count == 1) {
\r
117 if (ValidatedMinOccurs == 1 && ValidatedMaxOccurs == 1) {
\r
118 XmlSchemaSequence seq = new XmlSchemaSequence ();
\r
119 this.CopyInfo (seq);
\r
120 XmlSchemaParticle p = (XmlSchemaParticle) Items [0];
\r
121 p = p.GetOptimizedParticle (false);
\r
122 if (p == XmlSchemaParticle.Empty)
\r
123 OptimizedParticle = p;
\r
126 seq.CompiledItems.Add (p);
\r
127 seq.Compile (null, schema);
\r
128 OptimizedParticle = seq;
\r
130 return OptimizedParticle;
\r
134 XmlSchemaAll all = new XmlSchemaAll ();
\r
136 CopyOptimizedItems (all);
\r
137 OptimizedParticle = all;
\r
138 all.ComputeEmptiable ();
\r
140 return OptimizedParticle;
\r
143 internal override int Validate(ValidationEventHandler h, XmlSchema schema)
\r
145 if (IsValidated (schema.CompilationId))
\r
148 // 3.8.6 All Group Limited :: 1.
\r
149 // Beware that this section was corrected: E1-26 of http://www.w3.org/2001/05/xmlschema-errata#Errata1
\r
150 if (!this.parentIsGroupDefinition && ValidatedMaxOccurs != 1)
\r
151 error (h, "-all- group is limited to be content of a model group, or that of a complex type with maxOccurs to be 1.");
\r
153 CompiledItems.Clear ();
\r
154 foreach (XmlSchemaParticle obj in Items) {
\r
155 errorCount += obj.Validate (h, schema);
\r
156 if (obj.ValidatedMaxOccurs != 0 &&
\r
157 obj.ValidatedMaxOccurs != 1)
\r
158 error (h, "MaxOccurs of a particle inside -all- compositor must be either 0 or 1.");
\r
159 CompiledItems.Add (obj);
\r
161 ComputeEmptiable ();
\r
163 ValidationId = schema.ValidationId;
\r
167 private void ComputeEmptiable ()
\r
170 for (int i = 0; i < Items.Count; i++) {
\r
171 if (((XmlSchemaParticle) Items [i]).ValidatedMinOccurs > 0) {
\r
178 internal override bool ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
\r
179 ValidationEventHandler h, XmlSchema schema, bool raiseError)
\r
181 XmlSchemaAny any = baseParticle as XmlSchemaAny;
\r
182 XmlSchemaAll derivedAll = baseParticle as XmlSchemaAll;
\r
184 // NSRecurseCheckCardinality
\r
185 return ValidateNSRecurseCheckCardinality (any, h, schema, raiseError);
\r
186 } else if (derivedAll != null) {
\r
188 if (!ValidateOccurenceRangeOK (derivedAll, h, schema, raiseError))
\r
190 return ValidateRecurse (derivedAll, h, schema, raiseError);
\r
194 error (h, "Invalid -all- particle derivation was found.");
\r
199 internal override decimal GetMinEffectiveTotalRange ()
\r
201 return GetMinEffectiveTotalRangeAllAndSequence ();
\r
204 internal override void ValidateUniqueParticleAttribution (XmlSchemaObjectTable qnames, ArrayList nsNames,
\r
205 ValidationEventHandler h, XmlSchema schema)
\r
207 foreach (XmlSchemaElement el in this.Items)
\r
208 el.ValidateUniqueParticleAttribution (qnames, nsNames, h, schema);
\r
211 internal override void ValidateUniqueTypeAttribution (XmlSchemaObjectTable labels,
\r
212 ValidationEventHandler h, XmlSchema schema)
\r
214 foreach (XmlSchemaElement el in this.Items)
\r
215 el.ValidateUniqueTypeAttribution (labels, h, schema);
\r
221 // maxOccurs = 1 : 1
\r
222 // minOccurs = (0 | 1) : 1
\r
223 // {any attributes with non-schema namespace . . .}>
\r
224 // Content: (annotation?, element*)
\r
226 internal static XmlSchemaAll Read(XmlSchemaReader reader, ValidationEventHandler h)
\r
228 XmlSchemaAll all = new XmlSchemaAll();
\r
229 reader.MoveToElement();
\r
231 if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
\r
233 error(h,"Should not happen :1: XmlSchemaAll.Read, name="+reader.Name,null);
\r
234 reader.SkipToEnd();
\r
238 all.LineNumber = reader.LineNumber;
\r
239 all.LinePosition = reader.LinePosition;
\r
240 all.SourceUri = reader.BaseURI;
\r
243 while(reader.MoveToNextAttribute())
\r
245 if(reader.Name == "id")
\r
247 all.Id = reader.Value;
\r
249 else if(reader.Name == "maxOccurs")
\r
253 all.MaxOccursString = reader.Value;
\r
257 error(h,reader.Value + " is an invalid value for maxOccurs",e);
\r
260 else if(reader.Name == "minOccurs")
\r
264 all.MinOccursString = reader.Value;
\r
268 error(h,reader.Value + " is an invalid value for minOccurs",e);
\r
271 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
\r
273 error(h,reader.Name + " is not a valid attribute for all",null);
\r
277 XmlSchemaUtil.ReadUnhandledAttribute(reader,all);
\r
281 reader.MoveToElement();
\r
282 if(reader.IsEmptyElement)
\r
285 //Content: (annotation?, element*)
\r
287 while(reader.ReadNextElement())
\r
289 if(reader.NodeType == XmlNodeType.EndElement)
\r
291 if(reader.LocalName != xmlname)
\r
292 error(h,"Should not happen :2: XmlSchemaAll.Read, name="+reader.Name,null);
\r
295 if(level <= 1 && reader.LocalName == "annotation")
\r
297 level = 2; //Only one annotation
\r
298 XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
\r
299 if(annotation != null)
\r
300 all.Annotation = annotation;
\r
303 if(level <=2 && reader.LocalName == "element")
\r
306 XmlSchemaElement element = XmlSchemaElement.Read(reader,h);
\r
307 if(element != null)
\r
308 all.items.Add(element);
\r
311 reader.RaiseInvalidElementError();
\r