1 // Author: Dwivedi, Ajay kumar
5 // Permission is hereby granted, free of charge, to any person obtaining
6 // a copy of this software and associated documentation files (the
7 // "Software"), to deal in the Software without restriction, including
8 // without limitation the rights to use, copy, modify, merge, publish,
9 // distribute, sublicense, and/or sell copies of the Software, and to
10 // permit persons to whom the Software is furnished to do so, subject to
11 // the following conditions:
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 using System.Xml.Serialization;
28 namespace System.Xml.Schema
31 /// Summary description for XmlSchemaSimpleContentRestriction.
33 public class XmlSchemaSimpleContentRestriction : XmlSchemaContent
36 private XmlSchemaAnyAttribute any;
37 private XmlSchemaObjectCollection attributes;
38 private XmlSchemaSimpleType baseType;
39 private XmlQualifiedName baseTypeName;
40 private XmlSchemaObjectCollection facets;
41 const string xmlname = "restriction";
43 public XmlSchemaSimpleContentRestriction()
45 baseTypeName = XmlQualifiedName.Empty;
46 attributes = new XmlSchemaObjectCollection();
47 facets = new XmlSchemaObjectCollection();
50 [System.Xml.Serialization.XmlAttribute("base")]
51 public XmlQualifiedName BaseTypeName
53 get{ return baseTypeName; }
54 set{ baseTypeName = value; }
57 [XmlElement("simpleType", Type=typeof (XmlSchemaSimpleType))]
58 public XmlSchemaSimpleType BaseType
60 get{ return baseType; }
61 set{ baseType = value; }
64 [XmlElement("minExclusive",typeof(XmlSchemaMinExclusiveFacet))]
65 [XmlElement("minInclusive",typeof(XmlSchemaMinInclusiveFacet))]
66 [XmlElement("maxExclusive",typeof(XmlSchemaMaxExclusiveFacet))]
67 [XmlElement("maxInclusive",typeof(XmlSchemaMaxInclusiveFacet))]
68 [XmlElement("totalDigits",typeof(XmlSchemaTotalDigitsFacet))]
69 [XmlElement("fractionDigits",typeof(XmlSchemaFractionDigitsFacet))]
70 [XmlElement("length",typeof(XmlSchemaLengthFacet))]
71 [XmlElement("minLength",typeof(XmlSchemaMinLengthFacet))]
72 [XmlElement("maxLength",typeof(XmlSchemaMaxLengthFacet))]
73 [XmlElement("enumeration",typeof(XmlSchemaEnumerationFacet))]
74 [XmlElement("whiteSpace",typeof(XmlSchemaWhiteSpaceFacet))]
75 [XmlElement("pattern",typeof(XmlSchemaPatternFacet))]
76 public XmlSchemaObjectCollection Facets
81 [XmlElement("attribute",typeof(XmlSchemaAttribute))]
82 [XmlElement("attributeGroup",typeof(XmlSchemaAttributeGroupRef))]
83 public XmlSchemaObjectCollection Attributes
85 get{ return attributes; }
88 [XmlElement("anyAttribute")]
89 public XmlSchemaAnyAttribute AnyAttribute
95 // internal properties
96 internal override bool IsExtension {
100 internal override void SetParent (XmlSchemaObject parent)
102 base.SetParent (parent);
103 if (BaseType != null)
104 BaseType.SetParent (this);
105 if (AnyAttribute != null)
106 AnyAttribute.SetParent (this);
107 foreach (XmlSchemaObject obj in Attributes)
108 obj.SetParent (this);
112 /// 1. Base must be present and a QName
114 internal override int Compile(ValidationEventHandler h, XmlSchema schema)
116 // If this is already compiled this time, simply skip.
117 if (CompilationId == schema.CompilationId)
120 if (this.isRedefinedComponent) {
121 if (Annotation != null)
122 Annotation.isRedefinedComponent = true;
123 if (AnyAttribute != null)
124 AnyAttribute.isRedefinedComponent = true;
125 foreach (XmlSchemaObject obj in Attributes)
126 obj.isRedefinedComponent = true;
129 if(BaseTypeName == null || BaseTypeName.IsEmpty)
131 error(h, "base must be present, as a QName");
133 else if(!XmlSchemaUtil.CheckQName(BaseTypeName))
134 error(h,"BaseTypeName must be a QName");
138 errorCount += BaseType.Compile(h,schema);
141 if(this.AnyAttribute != null)
143 errorCount += AnyAttribute.Compile(h,schema);
146 foreach(XmlSchemaObject obj in Attributes)
148 if(obj is XmlSchemaAttribute)
150 XmlSchemaAttribute attr = (XmlSchemaAttribute) obj;
151 errorCount += attr.Compile(h,schema);
153 else if(obj is XmlSchemaAttributeGroupRef)
155 XmlSchemaAttributeGroupRef atgrp = (XmlSchemaAttributeGroupRef) obj;
156 errorCount += atgrp.Compile(h,schema);
159 error(h,obj.GetType() +" is not valid in this place::SimpleContentRestriction");
162 //TODO: Compile Facets: Looks like they are a part of datatypes. So we'll do them with the datatypes
165 XmlSchemaUtil.CompileID(Id,this,schema.IDCollection,h);
166 this.CompilationId = schema.CompilationId;
170 internal override XmlQualifiedName GetBaseTypeName ()
175 internal override XmlSchemaParticle GetParticle ()
180 internal override int Validate(ValidationEventHandler h, XmlSchema schema)
182 if (IsValidated (schema.ValidationId))
185 if (baseType != null) {
186 baseType.Validate (h, schema);
187 actualBaseSchemaType = baseType;
189 else if (baseTypeName != XmlQualifiedName.Empty) {
190 XmlSchemaType st = schema.FindSchemaType (baseTypeName);
192 st.Validate (h, schema);
193 actualBaseSchemaType = st;
194 } else if (baseTypeName == XmlSchemaComplexType.AnyTypeName) {
195 actualBaseSchemaType = XmlSchemaComplexType.AnyType;
196 } else if (XmlSchemaUtil.IsBuiltInDatatypeName (baseTypeName)) {
197 actualBaseSchemaType = XmlSchemaDatatype.FromName (baseTypeName);
198 if (actualBaseSchemaType == null)
199 error (h, "Invalid schema datatype name is specified.");
201 // otherwise, it might be missing sub components.
202 else if (!schema.IsNamespaceAbsent (baseTypeName.Namespace))
203 error (h, "Referenced base schema type " + baseTypeName + " was not found in the corresponding schema.");
206 ValidationId = schema.ValidationId;
213 //{any attributes with non-schema namespace . . .}>
214 //Content: (annotation?, (simpleType?, (minExclusive | minInclusive | maxExclusive | maxInclusive | totalDigits | fractionDigits | length | minLength | maxLength | enumeration | whiteSpace | pattern)*)?, ((attribute | attributeGroup)*, anyAttribute?))
216 internal static XmlSchemaSimpleContentRestriction Read(XmlSchemaReader reader, ValidationEventHandler h)
218 XmlSchemaSimpleContentRestriction restriction = new XmlSchemaSimpleContentRestriction();
219 reader.MoveToElement();
221 if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
223 error(h,"Should not happen :1: XmlSchemaComplexContentRestriction.Read, name="+reader.Name,null);
228 restriction.LineNumber = reader.LineNumber;
229 restriction.LinePosition = reader.LinePosition;
230 restriction.SourceUri = reader.BaseURI;
232 while(reader.MoveToNextAttribute())
234 if(reader.Name == "base")
237 restriction.baseTypeName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
239 error(h, reader.Value + " is not a valid value for base attribute",innerex);
241 else if(reader.Name == "id")
243 restriction.Id = reader.Value;
245 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
247 error(h,reader.Name + " is not a valid attribute for restriction",null);
251 XmlSchemaUtil.ReadUnhandledAttribute(reader,restriction);
255 reader.MoveToElement();
256 if(reader.IsEmptyElement)
259 //Content: 1.annotation?,
261 // 3.(minExclusive |...| enumeration | whiteSpace | pattern)*,
262 // 4.(attribute | attributeGroup)*,
265 while(reader.ReadNextElement())
267 if(reader.NodeType == XmlNodeType.EndElement)
269 if(reader.LocalName != xmlname)
270 error(h,"Should not happen :2: XmlSchemaSimpleContentRestriction.Read, name="+reader.Name,null);
273 if(level <= 1 && reader.LocalName == "annotation")
275 level = 2; //Only one annotation
276 XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
277 if(annotation != null)
278 restriction.Annotation = annotation;
281 if(level <=2 && reader.LocalName == "simpleType")
284 XmlSchemaSimpleType stype = XmlSchemaSimpleType.Read(reader,h);
286 restriction.baseType = stype;
291 if(reader.LocalName == "minExclusive")
294 XmlSchemaMinExclusiveFacet minex = XmlSchemaMinExclusiveFacet.Read(reader,h);
296 restriction.facets.Add(minex);
299 else if(reader.LocalName == "minInclusive")
302 XmlSchemaMinInclusiveFacet mini = XmlSchemaMinInclusiveFacet.Read(reader,h);
304 restriction.facets.Add(mini);
307 else if(reader.LocalName == "maxExclusive")
310 XmlSchemaMaxExclusiveFacet maxex = XmlSchemaMaxExclusiveFacet.Read(reader,h);
312 restriction.facets.Add(maxex);
315 else if(reader.LocalName == "maxInclusive")
318 XmlSchemaMaxInclusiveFacet maxi = XmlSchemaMaxInclusiveFacet.Read(reader,h);
320 restriction.facets.Add(maxi);
323 else if(reader.LocalName == "totalDigits")
326 XmlSchemaTotalDigitsFacet total = XmlSchemaTotalDigitsFacet.Read(reader,h);
328 restriction.facets.Add(total);
331 else if(reader.LocalName == "fractionDigits")
334 XmlSchemaFractionDigitsFacet fraction = XmlSchemaFractionDigitsFacet.Read(reader,h);
336 restriction.facets.Add(fraction);
339 else if(reader.LocalName == "length")
342 XmlSchemaLengthFacet length = XmlSchemaLengthFacet.Read(reader,h);
344 restriction.facets.Add(length);
347 else if(reader.LocalName == "minLength")
350 XmlSchemaMinLengthFacet minlen = XmlSchemaMinLengthFacet.Read(reader,h);
352 restriction.facets.Add(minlen);
355 else if(reader.LocalName == "maxLength")
358 XmlSchemaMaxLengthFacet maxlen = XmlSchemaMaxLengthFacet.Read(reader,h);
360 restriction.facets.Add(maxlen);
363 else if(reader.LocalName == "enumeration")
366 XmlSchemaEnumerationFacet enumeration = XmlSchemaEnumerationFacet.Read(reader,h);
367 if(enumeration != null)
368 restriction.facets.Add(enumeration);
371 else if(reader.LocalName == "whiteSpace")
374 XmlSchemaWhiteSpaceFacet ws = XmlSchemaWhiteSpaceFacet.Read(reader,h);
376 restriction.facets.Add(ws);
379 else if(reader.LocalName == "pattern")
382 XmlSchemaPatternFacet pattern = XmlSchemaPatternFacet.Read(reader,h);
384 restriction.facets.Add(pattern);
390 if(reader.LocalName == "attribute")
393 XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader,h);
395 restriction.Attributes.Add(attr);
398 if(reader.LocalName == "attributeGroup")
401 XmlSchemaAttributeGroupRef attr = XmlSchemaAttributeGroupRef.Read(reader,h);
403 restriction.attributes.Add(attr);
407 if(level <= 5 && reader.LocalName == "anyAttribute")
410 XmlSchemaAnyAttribute anyattr = XmlSchemaAnyAttribute.Read(reader,h);
412 restriction.AnyAttribute = anyattr;
415 reader.RaiseInvalidElementError();