2010-04-06 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System.XML / System.Xml.Schema / XmlSchemaGroupRef.cs
blob1c9c3fd817e7c282b5b258971a40cab359efd0c6
1 //
2 // System.Xml.Schema.XmlSchemaGroupBase.cs
3 //
4 // Author:
5 // Dwivedi, Ajay kumar Adwiv@Yahoo.com
6 // Atsushi Enomoto ginga@kit.hi-ho.ne.jp
7 //
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.
29 using System;
30 using System.Collections;
31 using System.Xml;
32 using System.Xml.Serialization;
34 namespace System.Xml.Schema
36 /// <summary>
37 /// Summary description for XmlSchemaGroupRef.
38 /// </summary>
39 public class XmlSchemaGroupRef : XmlSchemaParticle
41 private XmlSchema schema;
42 private XmlQualifiedName refName;
43 const string xmlname = "group";
44 private XmlSchemaGroup referencedGroup;
46 public XmlSchemaGroupRef()
48 refName = XmlQualifiedName.Empty;
51 // Attribute
52 [System.Xml.Serialization.XmlAttribute("ref")]
53 public XmlQualifiedName RefName
55 get{ return refName; }
56 set{ refName = value; }
59 // Post Compilation Schema Information
60 [XmlIgnore]
61 public XmlSchemaGroupBase Particle
63 get{
64 if (TargetGroup != null)
65 return TargetGroup.Particle;
66 else
67 return null;
71 internal XmlSchemaGroup TargetGroup
73 get {
74 if (referencedGroup != null && referencedGroup.IsCircularDefinition)
75 return null;
76 else
77 return referencedGroup;
81 /// <remarks>
82 /// 1. RefName must be present
83 /// </remarks>
84 internal override int Compile(ValidationEventHandler h, XmlSchema schema)
86 // If this is already compiled this time, simply skip.
87 if (CompilationId == schema.CompilationId)
88 return 0;
89 this.schema = schema;
91 XmlSchemaUtil.CompileID(Id,this,schema.IDCollection,h);
92 CompileOccurence (h, schema);
94 if(refName == null || refName.IsEmpty)
96 error(h,"ref must be present");
98 else if(!XmlSchemaUtil.CheckQName(RefName))
99 error(h, "RefName must be a valid XmlQualifiedName");
101 this.CompilationId = schema.CompilationId;
102 return errorCount;
105 internal override int Validate(ValidationEventHandler h, XmlSchema schema)
107 if (IsValidated (schema.ValidationId))
108 return errorCount;
110 referencedGroup = schema.Groups [RefName] as XmlSchemaGroup;
111 // it might be missing sub components.
112 if (referencedGroup == null) {
113 if (!schema.IsNamespaceAbsent (RefName.Namespace))
114 error (h, "Referenced group " + RefName + " was not found in the corresponding schema.");
116 // See Errata E1-26: minOccurs=0 is now allowed.
117 else if (referencedGroup.Particle is XmlSchemaAll && ValidatedMaxOccurs != 1)
118 error (h, "Group reference to -all- particle must have schema component {maxOccurs}=1.");
119 if (TargetGroup != null)
120 TargetGroup.Validate (h, schema);
122 ValidationId = schema.ValidationId;
123 return errorCount;
126 bool busy; // only for avoiding infinite loop on illegal recursion cases.
127 internal override XmlSchemaParticle GetOptimizedParticle (bool isTop)
129 if (busy)
130 return XmlSchemaParticle.Empty;
131 if (OptimizedParticle != null)
132 return OptimizedParticle;
133 busy = true;
134 XmlSchemaGroup g = referencedGroup != null ? referencedGroup : schema.Groups [RefName] as XmlSchemaGroup;
135 if (g != null && g.Particle != null) {
136 OptimizedParticle = g.Particle;
137 OptimizedParticle = OptimizedParticle.GetOptimizedParticle (isTop);
138 if (OptimizedParticle != XmlSchemaParticle.Empty && (ValidatedMinOccurs != 1 || ValidatedMaxOccurs != 1)) {
139 OptimizedParticle = OptimizedParticle.GetShallowClone ();
140 OptimizedParticle.OptimizedParticle = null;
141 OptimizedParticle.MinOccurs = this.MinOccurs;
142 OptimizedParticle.MaxOccurs = this.MaxOccurs;
143 OptimizedParticle.CompileOccurence (null, null);
146 else
147 OptimizedParticle = XmlSchemaParticle.Empty;
148 busy = false;
149 return OptimizedParticle;
153 internal override bool ParticleEquals (XmlSchemaParticle other)
155 return this.GetOptimizedParticle (true).ParticleEquals (other);
158 internal override bool ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
159 ValidationEventHandler h, XmlSchema schema, bool raiseError)
161 if (TargetGroup != null)
162 return TargetGroup.Particle.ValidateDerivationByRestriction (baseParticle, h, schema, raiseError);
163 else
164 return false; // should not occur
168 internal override void CheckRecursion (int depth, ValidationEventHandler h, XmlSchema schema)
170 if (TargetGroup == null)
171 return;
173 if (this.recursionDepth == -1) {
174 recursionDepth = depth;
175 TargetGroup.Particle.CheckRecursion (depth, h, schema);
176 recursionDepth = -2;
177 } else if (depth == recursionDepth)
178 throw new XmlSchemaException ("Circular group reference was found.", this, null);
181 internal override void ValidateUniqueParticleAttribution (XmlSchemaObjectTable qnames, ArrayList nsNames,
182 ValidationEventHandler h, XmlSchema schema)
184 if (TargetGroup != null)
185 TargetGroup.Particle.ValidateUniqueParticleAttribution (qnames, nsNames, h, schema);
188 internal override void ValidateUniqueTypeAttribution (XmlSchemaObjectTable labels,
189 ValidationEventHandler h, XmlSchema schema)
191 if (TargetGroup != null)
192 TargetGroup.Particle.ValidateUniqueTypeAttribution (labels, h, schema);
196 #region Read
197 // <group
198 // id = ID
199 // ref = QName
200 // minOccurs = ? : 1
201 // maxOccurs = ? : 1>
202 // Content: (annotation?)
203 // </group>
204 internal static XmlSchemaGroupRef Read(XmlSchemaReader reader, ValidationEventHandler h)
206 XmlSchemaGroupRef groupref = new XmlSchemaGroupRef();
207 reader.MoveToElement();
209 if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
211 error(h,"Should not happen :1: XmlSchemaGroup.Read, name="+reader.Name,null);
212 reader.Skip();
213 return null;
216 groupref.LineNumber = reader.LineNumber;
217 groupref.LinePosition = reader.LinePosition;
218 groupref.SourceUri = reader.BaseURI;
220 while(reader.MoveToNextAttribute())
222 if(reader.Name == "id")
224 groupref.Id = reader.Value;
226 else if(reader.Name == "ref")
228 Exception innerex;
229 groupref.refName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
230 if(innerex != null)
231 error(h, reader.Value + " is not a valid value for ref attribute",innerex);
233 else if(reader.Name == "maxOccurs")
237 groupref.MaxOccursString = reader.Value;
239 catch(Exception e)
241 error(h,reader.Value + " is an invalid value for maxOccurs",e);
244 else if(reader.Name == "minOccurs")
248 groupref.MinOccursString = reader.Value;
250 catch(Exception e)
252 error(h,reader.Value + " is an invalid value for minOccurs", e);
255 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
257 error(h,reader.Name + " is not a valid attribute for group",null);
259 else
261 XmlSchemaUtil.ReadUnhandledAttribute(reader,groupref);
265 reader.MoveToElement();
266 if(reader.IsEmptyElement)
267 return groupref;
269 // Content: (annotation?)
270 int level = 1;
271 while(reader.ReadNextElement())
273 if(reader.NodeType == XmlNodeType.EndElement)
275 if(reader.LocalName != xmlname)
276 error(h,"Should not happen :2: XmlSchemaGroupRef.Read, name="+reader.Name,null);
277 break;
279 if(level <= 1 && reader.LocalName == "annotation")
281 level = 2; //Only one annotation
282 XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
283 if(annotation != null)
284 groupref.Annotation = annotation;
285 continue;
287 reader.RaiseInvalidElementError();
289 return groupref;
291 #endregion