2 // System.Xml.Schema.XmlSchemaAny.cs
5 // Dwivedi, Ajay kumar Adwiv@Yahoo.com
6 // Atsushi Enomoto ginga@kit.hi-ho.ne.jp
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;
31 using System.Collections.Specialized;
33 using System.Xml.Serialization;
34 using System.ComponentModel;
35 using Mono.Xml.Schema;
37 namespace System.Xml.Schema
40 /// Summary description for XmlSchemaAny.
42 public class XmlSchemaAny : XmlSchemaParticle
44 static XmlSchemaAny anyTypeContent;
45 internal static XmlSchemaAny AnyTypeContent {
47 if (anyTypeContent == null) {
48 anyTypeContent = new XmlSchemaAny ();
49 anyTypeContent.MaxOccursString = "unbounded";
50 anyTypeContent.MinOccurs = 0;
51 anyTypeContent.CompileOccurence (null, null);
52 anyTypeContent.Namespace = "##any";
53 anyTypeContent.wildcard.HasValueAny = true;
54 anyTypeContent.wildcard.ResolvedNamespaces = new StringCollection ();
55 // It is not documented by W3C but it should be.
56 anyTypeContent.wildcard.ResolvedProcessing =
57 anyTypeContent.ProcessContents = XmlSchemaContentProcessing.Lax;
58 anyTypeContent.wildcard.SkipCompile = true;
60 return anyTypeContent;
64 private string nameSpace;
65 private XmlSchemaContentProcessing processing;
66 const string xmlname = "any";
68 private XsdWildcard wildcard;
72 wildcard = new XsdWildcard (this);
75 [System.Xml.Serialization.XmlAttribute("namespace")]
76 public string Namespace
78 get{ return nameSpace; }
79 set{ nameSpace = value; }
82 [DefaultValue(XmlSchemaContentProcessing.None)]
83 [System.Xml.Serialization.XmlAttribute("processContents")]
84 public XmlSchemaContentProcessing ProcessContents
86 get{ return processing; }
87 set{ processing = value; }
90 // Post Compilation Schema Infoset
91 internal bool HasValueAny {
92 get { return wildcard.HasValueAny; }
95 internal bool HasValueLocal {
96 get { return wildcard.HasValueLocal; }
99 internal bool HasValueOther {
100 get { return wildcard.HasValueOther; }
103 internal bool HasValueTargetNamespace {
104 get { return wildcard.HasValueTargetNamespace; }
107 internal StringCollection ResolvedNamespaces {
108 get { return wildcard.ResolvedNamespaces; }
111 internal XmlSchemaContentProcessing ResolvedProcessContents
113 get{ return wildcard.ResolvedProcessing; }
116 internal string TargetNamespace
118 get { return wildcard.TargetNamespace; }
122 /// 1. id must be of type ID
123 /// 2. namespace can have one of the following values:
124 /// a) ##any or ##other
125 /// b) list of anyURI and ##targetNamespace and ##local
127 internal override int Compile(ValidationEventHandler h, XmlSchema schema)
129 // If this is already compiled this time, simply skip.
130 if (CompilationId == schema.CompilationId)
135 XmlSchemaUtil.CompileID(Id,this, schema.IDCollection,h);
136 wildcard.TargetNamespace = AncestorSchema.TargetNamespace;
137 if (wildcard.TargetNamespace == null)
138 wildcard.TargetNamespace = "";
139 CompileOccurence (h, schema);
141 wildcard.Compile (Namespace, h, schema);
143 if (processing == XmlSchemaContentProcessing.None)
144 wildcard.ResolvedProcessing = XmlSchemaContentProcessing.Strict;
146 wildcard.ResolvedProcessing = processing;
148 this.CompilationId = schema.CompilationId;
152 internal override XmlSchemaParticle GetOptimizedParticle (bool isTop)
154 if (OptimizedParticle != null)
155 return OptimizedParticle;
156 // Uncommenting this causes incorrect validation.
157 // It will prevent UPA e.g. msxsdtest/Particles/particlesJf006.xsd
158 // if (ValidatedMaxOccurs == 0) {
159 // OptimizedParticle = XmlSchemaParticle.Empty;
160 // return OptimizedParticle;
163 XmlSchemaAny any = new XmlSchemaAny ();
165 any.CompileOccurence (null, null);
166 any.wildcard = this.wildcard;
167 OptimizedParticle = any;
169 // properties which never contribute to validation
170 any.Namespace = Namespace;
171 any.ProcessContents = ProcessContents;
172 any.Annotation = Annotation;
173 any.UnhandledAttributes = UnhandledAttributes;
175 return OptimizedParticle;
178 internal override int Validate(ValidationEventHandler h, XmlSchema schema)
183 internal override bool ParticleEquals (XmlSchemaParticle other)
185 XmlSchemaAny any = other as XmlSchemaAny;
188 if (this.HasValueAny != any.HasValueAny ||
189 this.HasValueLocal != any.HasValueLocal ||
190 this.HasValueOther != any.HasValueOther ||
191 this.HasValueTargetNamespace != any.HasValueTargetNamespace ||
192 this.ResolvedProcessContents != any.ResolvedProcessContents ||
193 this.ValidatedMaxOccurs != any.ValidatedMaxOccurs ||
194 this.ValidatedMinOccurs != any.ValidatedMinOccurs ||
195 this.ResolvedNamespaces.Count != any.ResolvedNamespaces.Count)
197 for (int i = 0; i < ResolvedNamespaces.Count; i++)
198 if (ResolvedNamespaces [i] != any.ResolvedNamespaces [i])
204 // 3.8.6. Attribute Wildcard Intersection
205 // Only try to examine if their intersection is expressible, and
206 // returns if the result is empty.
207 internal bool ExamineAttributeWildcardIntersection (XmlSchemaAny other,
208 ValidationEventHandler h, XmlSchema schema)
210 return wildcard.ExamineAttributeWildcardIntersection (other, h, schema);
213 internal override bool ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
214 ValidationEventHandler h, XmlSchema schema, bool raiseError)
216 XmlSchemaAny baseAny = baseParticle as XmlSchemaAny;
217 if (baseAny == null) {
219 error (h, "Invalid particle derivation by restriction was found.");
222 // 3.9.6 Particle Derivation OK (Any:Any - NSSubset)
223 if (!ValidateOccurenceRangeOK (baseParticle, h, schema, raiseError))
225 return wildcard.ValidateWildcardSubset (baseAny.wildcard, h, schema, raiseError);
229 internal override void CheckRecursion (int depth, ValidationEventHandler h, XmlSchema schema)
234 internal override void ValidateUniqueParticleAttribution (
235 XmlSchemaObjectTable qnames, ArrayList nsNames,
236 ValidationEventHandler h, XmlSchema schema)
238 // Wildcard Intersection check.
239 foreach (XmlSchemaAny other in nsNames)
240 if (!ExamineAttributeWildcardIntersection (other, h, schema))
241 error (h, "Ambiguous -any- particle was found.");
245 internal override void ValidateUniqueTypeAttribution (XmlSchemaObjectTable labels,
246 ValidationEventHandler h, XmlSchema schema)
251 // 3.10.4 Wildcard Allows Namespace Name. (In fact it is almost copy...)
252 internal bool ValidateWildcardAllowsNamespaceName (string ns,
253 ValidationEventHandler h, XmlSchema schema, bool raiseError)
255 return wildcard.ValidateWildcardAllowsNamespaceName (ns, h, schema, raiseError);
260 // maxOccurs = (nonNegativeInteger | unbounded) : 1
261 // minOccurs = nonNegativeInteger : 1
262 // namespace = ((##any | ##other) | List of (anyURI | (##targetNamespace | ##local)) ) : ##any
263 // processContents = (lax | skip | strict) : strict
264 // {any attributes with non-schema namespace . . .}>
265 // Content: (annotation?)
267 internal static XmlSchemaAny Read(XmlSchemaReader reader, ValidationEventHandler h)
269 XmlSchemaAny any = new XmlSchemaAny();
270 reader.MoveToElement();
272 if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
274 error(h,"Should not happen :1: XmlSchemaAny.Read, name="+reader.Name,null);
279 any.LineNumber = reader.LineNumber;
280 any.LinePosition = reader.LinePosition;
281 any.SourceUri = reader.BaseURI;
283 while(reader.MoveToNextAttribute())
285 if(reader.Name == "id")
287 any.Id = reader.Value;
289 else if(reader.Name == "maxOccurs")
293 any.MaxOccursString = reader.Value;
297 error(h,reader.Value + " is an invalid value for maxOccurs",e);
300 else if(reader.Name == "minOccurs")
304 any.MinOccursString = reader.Value;
308 error(h,reader.Value + " is an invalid value for minOccurs", e);
311 else if(reader.Name == "namespace")
313 any.nameSpace = reader.Value;
315 else if(reader.Name == "processContents")
318 any.processing = XmlSchemaUtil.ReadProcessingAttribute(reader,out innerex);
320 error(h, reader.Value + " is not a valid value for processContents",innerex);
322 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
324 error(h,reader.Name + " is not a valid attribute for any",null);
328 XmlSchemaUtil.ReadUnhandledAttribute(reader,any);
332 reader.MoveToElement();
333 if(reader.IsEmptyElement)
336 // Content: (annotation?)
338 while(reader.ReadNextElement())
340 if(reader.NodeType == XmlNodeType.EndElement)
342 if(reader.LocalName != xmlname)
343 error(h,"Should not happen :2: XmlSchemaAny.Read, name="+reader.Name,null);
346 if(level <= 1 && reader.LocalName == "annotation")
348 level = 2; //Only one annotation
349 XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
350 if(annotation != null)
351 any.Annotation = annotation;
354 reader.RaiseInvalidElementError();