1 // Author: Dwivedi, Ajay kumar
\r
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;
\r
28 namespace System.Xml.Schema
\r
31 /// Summary description for XmlSchemaSimpleContentRestriction.
\r
33 public class XmlSchemaSimpleContentRestriction : XmlSchemaContent
\r
36 private XmlSchemaAnyAttribute any;
\r
37 private XmlSchemaObjectCollection attributes;
\r
38 private XmlSchemaSimpleType baseType;
\r
39 private XmlQualifiedName baseTypeName;
\r
40 private XmlSchemaObjectCollection facets;
\r
41 const string xmlname = "restriction";
\r
43 public XmlSchemaSimpleContentRestriction()
\r
45 baseTypeName = XmlQualifiedName.Empty;
\r
46 attributes = new XmlSchemaObjectCollection();
\r
47 facets = new XmlSchemaObjectCollection();
\r
50 [System.Xml.Serialization.XmlAttribute("base")]
\r
51 public XmlQualifiedName BaseTypeName
\r
53 get{ return baseTypeName; }
\r
54 set{ baseTypeName = value; }
\r
57 [XmlElement("simpleType", Type=typeof (XmlSchemaSimpleType))]
\r
58 public XmlSchemaSimpleType BaseType
\r
60 get{ return baseType; }
\r
61 set{ baseType = value; }
\r
64 [XmlElement("minExclusive",typeof(XmlSchemaMinExclusiveFacet))]
\r
65 [XmlElement("minInclusive",typeof(XmlSchemaMinInclusiveFacet))]
\r
66 [XmlElement("maxExclusive",typeof(XmlSchemaMaxExclusiveFacet))]
\r
67 [XmlElement("maxInclusive",typeof(XmlSchemaMaxInclusiveFacet))]
\r
68 [XmlElement("totalDigits",typeof(XmlSchemaTotalDigitsFacet))]
\r
69 [XmlElement("fractionDigits",typeof(XmlSchemaFractionDigitsFacet))]
\r
70 [XmlElement("length",typeof(XmlSchemaLengthFacet))]
\r
71 [XmlElement("minLength",typeof(XmlSchemaMinLengthFacet))]
\r
72 [XmlElement("maxLength",typeof(XmlSchemaMaxLengthFacet))]
\r
73 [XmlElement("enumeration",typeof(XmlSchemaEnumerationFacet))]
\r
74 [XmlElement("whiteSpace",typeof(XmlSchemaWhiteSpaceFacet))]
\r
75 [XmlElement("pattern",typeof(XmlSchemaPatternFacet))]
\r
76 public XmlSchemaObjectCollection Facets
\r
78 get{ return facets; }
\r
81 [XmlElement("attribute",typeof(XmlSchemaAttribute))]
\r
82 [XmlElement("attributeGroup",typeof(XmlSchemaAttributeGroupRef))]
\r
83 public XmlSchemaObjectCollection Attributes
\r
85 get{ return attributes; }
\r
88 [XmlElement("anyAttribute")]
\r
89 public XmlSchemaAnyAttribute AnyAttribute
\r
95 // internal properties
\r
96 internal override bool IsExtension {
\r
97 get { return false; }
\r
101 /// 1. Base must be present and a QName
\r
103 internal override int Compile(ValidationEventHandler h, XmlSchema schema)
\r
105 // If this is already compiled this time, simply skip.
\r
106 if (CompilationId == schema.CompilationId)
\r
109 if (BaseType != null)
\r
110 BaseType.Parent = this;
\r
111 if (AnyAttribute != null)
\r
112 AnyAttribute.Parent = this;
\r
113 foreach (XmlSchemaObject obj in Attributes)
\r
117 if (this.isRedefinedComponent) {
\r
118 if (Annotation != null)
\r
119 Annotation.isRedefinedComponent = true;
\r
120 if (AnyAttribute != null)
\r
121 AnyAttribute.isRedefinedComponent = true;
\r
122 foreach (XmlSchemaObject obj in Attributes)
\r
123 obj.isRedefinedComponent = true;
\r
126 if(BaseTypeName == null || BaseTypeName.IsEmpty)
\r
128 error(h, "base must be present, as a QName");
\r
130 else if(!XmlSchemaUtil.CheckQName(BaseTypeName))
\r
131 error(h,"BaseTypeName must be a QName");
\r
133 if(BaseType != null)
\r
135 errorCount += BaseType.Compile(h,schema);
\r
138 if(this.AnyAttribute != null)
\r
140 errorCount += AnyAttribute.Compile(h,schema);
\r
143 foreach(XmlSchemaObject obj in Attributes)
\r
145 if(obj is XmlSchemaAttribute)
\r
147 XmlSchemaAttribute attr = (XmlSchemaAttribute) obj;
\r
148 errorCount += attr.Compile(h,schema);
\r
150 else if(obj is XmlSchemaAttributeGroupRef)
\r
152 XmlSchemaAttributeGroupRef atgrp = (XmlSchemaAttributeGroupRef) obj;
\r
153 errorCount += atgrp.Compile(h,schema);
\r
156 error(h,obj.GetType() +" is not valid in this place::SimpleContentRestriction");
\r
159 //TODO: Compile Facets: Looks like they are a part of datatypes. So we'll do them with the datatypes
\r
162 XmlSchemaUtil.CompileID(Id,this,schema.IDCollection,h);
\r
163 this.CompilationId = schema.CompilationId;
\r
167 internal override XmlQualifiedName GetBaseTypeName ()
\r
169 return baseTypeName;
\r
172 internal override XmlSchemaParticle GetParticle ()
\r
177 internal override int Validate(ValidationEventHandler h, XmlSchema schema)
\r
179 if (IsValidated (schema.ValidationId))
\r
182 if (baseType != null) {
\r
183 baseType.Validate (h, schema);
\r
184 actualBaseSchemaType = baseType;
\r
186 else if (baseTypeName != XmlQualifiedName.Empty) {
\r
187 XmlSchemaType st = schema.SchemaTypes [baseTypeName] as XmlSchemaType;
\r
189 st.Validate (h, schema);
\r
190 actualBaseSchemaType = st;
\r
191 } else if (baseTypeName == XmlSchemaComplexType.AnyTypeName) {
\r
192 actualBaseSchemaType = XmlSchemaComplexType.AnyType;
193 } else if (XmlSchemaUtil.IsBuiltInDatatypeName (baseTypeName)) {
\r
194 actualBaseSchemaType = XmlSchemaDatatype.FromName (baseTypeName);
\r
195 if (actualBaseSchemaType == null)
\r
196 error (h, "Invalid schema datatype name is specified.");
\r
198 // otherwise, it might be missing sub components.
\r
199 else if (!schema.IsNamespaceAbsent (baseTypeName.Namespace))
\r
200 error (h, "Referenced base schema type " + baseTypeName + " was not found in the corresponding schema.");
\r
203 ValidationId = schema.ValidationId;
\r
210 //{any attributes with non-schema namespace . . .}>
\r
211 //Content: (annotation?, (simpleType?, (minExclusive | minInclusive | maxExclusive | maxInclusive | totalDigits | fractionDigits | length | minLength | maxLength | enumeration | whiteSpace | pattern)*)?, ((attribute | attributeGroup)*, anyAttribute?))
\r
213 internal static XmlSchemaSimpleContentRestriction Read(XmlSchemaReader reader, ValidationEventHandler h)
\r
215 XmlSchemaSimpleContentRestriction restriction = new XmlSchemaSimpleContentRestriction();
\r
216 reader.MoveToElement();
\r
218 if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
\r
220 error(h,"Should not happen :1: XmlSchemaComplexContentRestriction.Read, name="+reader.Name,null);
\r
221 reader.SkipToEnd();
\r
225 restriction.LineNumber = reader.LineNumber;
\r
226 restriction.LinePosition = reader.LinePosition;
\r
227 restriction.SourceUri = reader.BaseURI;
\r
229 while(reader.MoveToNextAttribute())
\r
231 if(reader.Name == "base")
\r
234 restriction.baseTypeName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
\r
235 if(innerex != null)
\r
236 error(h, reader.Value + " is not a valid value for base attribute",innerex);
\r
238 else if(reader.Name == "id")
\r
240 restriction.Id = reader.Value;
\r
242 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
\r
244 error(h,reader.Name + " is not a valid attribute for restriction",null);
\r
248 XmlSchemaUtil.ReadUnhandledAttribute(reader,restriction);
\r
252 reader.MoveToElement();
\r
253 if(reader.IsEmptyElement)
\r
254 return restriction;
\r
256 //Content: 1.annotation?,
\r
258 // 3.(minExclusive |...| enumeration | whiteSpace | pattern)*,
\r
259 // 4.(attribute | attributeGroup)*,
\r
262 while(reader.ReadNextElement())
\r
264 if(reader.NodeType == XmlNodeType.EndElement)
\r
266 if(reader.LocalName != xmlname)
\r
267 error(h,"Should not happen :2: XmlSchemaSimpleContentRestriction.Read, name="+reader.Name,null);
\r
270 if(level <= 1 && reader.LocalName == "annotation")
\r
272 level = 2; //Only one annotation
\r
273 XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
\r
274 if(annotation != null)
\r
275 restriction.Annotation = annotation;
\r
278 if(level <=2 && reader.LocalName == "simpleType")
\r
281 XmlSchemaSimpleType stype = XmlSchemaSimpleType.Read(reader,h);
\r
283 restriction.baseType = stype;
\r
288 if(reader.LocalName == "minExclusive")
\r
291 XmlSchemaMinExclusiveFacet minex = XmlSchemaMinExclusiveFacet.Read(reader,h);
\r
293 restriction.facets.Add(minex);
\r
296 else if(reader.LocalName == "minInclusive")
\r
299 XmlSchemaMinInclusiveFacet mini = XmlSchemaMinInclusiveFacet.Read(reader,h);
\r
301 restriction.facets.Add(mini);
\r
304 else if(reader.LocalName == "maxExclusive")
\r
307 XmlSchemaMaxExclusiveFacet maxex = XmlSchemaMaxExclusiveFacet.Read(reader,h);
\r
309 restriction.facets.Add(maxex);
\r
312 else if(reader.LocalName == "maxInclusive")
\r
315 XmlSchemaMaxInclusiveFacet maxi = XmlSchemaMaxInclusiveFacet.Read(reader,h);
\r
317 restriction.facets.Add(maxi);
\r
320 else if(reader.LocalName == "totalDigits")
\r
323 XmlSchemaTotalDigitsFacet total = XmlSchemaTotalDigitsFacet.Read(reader,h);
\r
325 restriction.facets.Add(total);
\r
328 else if(reader.LocalName == "fractionDigits")
\r
331 XmlSchemaFractionDigitsFacet fraction = XmlSchemaFractionDigitsFacet.Read(reader,h);
\r
332 if(fraction != null)
\r
333 restriction.facets.Add(fraction);
\r
336 else if(reader.LocalName == "length")
\r
339 XmlSchemaLengthFacet length = XmlSchemaLengthFacet.Read(reader,h);
\r
341 restriction.facets.Add(length);
\r
344 else if(reader.LocalName == "minLength")
\r
347 XmlSchemaMinLengthFacet minlen = XmlSchemaMinLengthFacet.Read(reader,h);
\r
349 restriction.facets.Add(minlen);
\r
352 else if(reader.LocalName == "maxLength")
\r
355 XmlSchemaMaxLengthFacet maxlen = XmlSchemaMaxLengthFacet.Read(reader,h);
\r
357 restriction.facets.Add(maxlen);
\r
360 else if(reader.LocalName == "enumeration")
\r
363 XmlSchemaEnumerationFacet enumeration = XmlSchemaEnumerationFacet.Read(reader,h);
\r
364 if(enumeration != null)
\r
365 restriction.facets.Add(enumeration);
\r
368 else if(reader.LocalName == "whiteSpace")
\r
371 XmlSchemaWhiteSpaceFacet ws = XmlSchemaWhiteSpaceFacet.Read(reader,h);
\r
373 restriction.facets.Add(ws);
\r
376 else if(reader.LocalName == "pattern")
\r
379 XmlSchemaPatternFacet pattern = XmlSchemaPatternFacet.Read(reader,h);
\r
380 if(pattern != null)
\r
381 restriction.facets.Add(pattern);
\r
387 if(reader.LocalName == "attribute")
\r
390 XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader,h);
\r
392 restriction.Attributes.Add(attr);
\r
395 if(reader.LocalName == "attributeGroup")
\r
398 XmlSchemaAttributeGroupRef attr = XmlSchemaAttributeGroupRef.Read(reader,h);
\r
400 restriction.attributes.Add(attr);
\r
404 if(level <= 5 && reader.LocalName == "anyAttribute")
\r
407 XmlSchemaAnyAttribute anyattr = XmlSchemaAnyAttribute.Read(reader,h);
\r
408 if(anyattr != null)
\r
409 restriction.AnyAttribute = anyattr;
\r
412 reader.RaiseInvalidElementError();
\r
414 return restriction;
\r