XmlSchemaSimpleContentRestriction.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. // Author: Dwivedi, Ajay kumar
  2. // [email protected]
  3. using System;
  4. using System.Xml;
  5. using System.Xml.Serialization;
  6. namespace System.Xml.Schema
  7. {
  8. /// <summary>
  9. /// Summary description for XmlSchemaSimpleContentRestriction.
  10. /// </summary>
  11. public class XmlSchemaSimpleContentRestriction : XmlSchemaContent
  12. {
  13. private XmlSchemaAnyAttribute any;
  14. private XmlSchemaObjectCollection attributes;
  15. private XmlSchemaSimpleType baseType;
  16. private XmlQualifiedName baseTypeName;
  17. private XmlSchemaObjectCollection facets;
  18. private static string xmlname = "restriction";
  19. public XmlSchemaSimpleContentRestriction()
  20. {
  21. baseTypeName = XmlQualifiedName.Empty;
  22. attributes = new XmlSchemaObjectCollection();
  23. facets = new XmlSchemaObjectCollection();
  24. }
  25. [System.Xml.Serialization.XmlAttribute("base")]
  26. public XmlQualifiedName BaseTypeName
  27. {
  28. get{ return baseTypeName; }
  29. set{ baseTypeName = value; }
  30. }
  31. [XmlElement("simpleType",Namespace="http://www.w3.org/2001/XMLSchema")]
  32. public XmlSchemaSimpleType BaseType
  33. {
  34. get{ return baseType; }
  35. set{ baseType = value; }
  36. }
  37. [XmlElement("minExclusive",typeof(XmlSchemaMinExclusiveFacet),Namespace="http://www.w3.org/2001/XMLSchema")]
  38. [XmlElement("minInclusive",typeof(XmlSchemaMinInclusiveFacet),Namespace="http://www.w3.org/2001/XMLSchema")]
  39. [XmlElement("maxExclusive",typeof(XmlSchemaMaxExclusiveFacet),Namespace="http://www.w3.org/2001/XMLSchema")]
  40. [XmlElement("maxInclusive",typeof(XmlSchemaMaxInclusiveFacet),Namespace="http://www.w3.org/2001/XMLSchema")]
  41. [XmlElement("totalDigits",typeof(XmlSchemaTotalDigitsFacet),Namespace="http://www.w3.org/2001/XMLSchema")]
  42. [XmlElement("fractionDigits",typeof(XmlSchemaFractionDigitsFacet),Namespace="http://www.w3.org/2001/XMLSchema")]
  43. [XmlElement("length",typeof(XmlSchemaLengthFacet),Namespace="http://www.w3.org/2001/XMLSchema")]
  44. [XmlElement("minLength",typeof(XmlSchemaMinLengthFacet),Namespace="http://www.w3.org/2001/XMLSchema")]
  45. [XmlElement("maxLength",typeof(XmlSchemaMaxLengthFacet),Namespace="http://www.w3.org/2001/XMLSchema")]
  46. [XmlElement("enumeration",typeof(XmlSchemaEnumerationFacet),Namespace="http://www.w3.org/2001/XMLSchema")]
  47. [XmlElement("whiteSpace",typeof(XmlSchemaWhiteSpaceFacet),Namespace="http://www.w3.org/2001/XMLSchema")]
  48. [XmlElement("pattern",typeof(XmlSchemaPatternFacet),Namespace="http://www.w3.org/2001/XMLSchema")]
  49. public XmlSchemaObjectCollection Facets
  50. {
  51. get{ return facets; }
  52. }
  53. [XmlElement("attribute",typeof(XmlSchemaAttribute),Namespace="http://www.w3.org/2001/XMLSchema")]
  54. [XmlElement("attributeGroup",typeof(XmlSchemaAttributeGroupRef),Namespace="http://www.w3.org/2001/XMLSchema")]
  55. public XmlSchemaObjectCollection Attributes
  56. {
  57. get{ return attributes; }
  58. }
  59. [XmlElement("anyAttribute",Namespace="http://www.w3.org/2001/XMLSchema")]
  60. public XmlSchemaAnyAttribute AnyAttribute
  61. {
  62. get{ return any; }
  63. set{ any = value; }
  64. }
  65. ///<remarks>
  66. /// 1. Base must be present and a QName
  67. ///</remarks>
  68. [MonoTODO]
  69. internal int Compile(ValidationEventHandler h, XmlSchemaInfo info)
  70. {
  71. if(BaseTypeName == null || BaseTypeName.IsEmpty)
  72. {
  73. error(h, "base must be present and a QName");
  74. }
  75. else if(!XmlSchemaUtil.CheckQName(BaseTypeName))
  76. error(h,"BaseTypeName must be a QName");
  77. if(BaseType != null)
  78. {
  79. errorCount += BaseType.Compile(h,info);
  80. }
  81. if(this.AnyAttribute != null)
  82. {
  83. errorCount += AnyAttribute.Compile(h,info);
  84. }
  85. foreach(XmlSchemaObject obj in Attributes)
  86. {
  87. if(obj is XmlSchemaAttribute)
  88. {
  89. XmlSchemaAttribute attr = (XmlSchemaAttribute) obj;
  90. errorCount += attr.Compile(h,info);
  91. }
  92. else if(obj is XmlSchemaAttributeGroupRef)
  93. {
  94. XmlSchemaAttributeGroupRef atgrp = (XmlSchemaAttributeGroupRef) obj;
  95. errorCount += atgrp.Compile(h,info);
  96. }
  97. else
  98. error(h,obj.GetType() +" is not valid in this place::SimpleContentRestriction");
  99. }
  100. //TODO: Compile Facets: Looks like they are a part of datatypes. So we'll do them with the datatypes
  101. XmlSchemaUtil.CompileID(Id,this,info.IDCollection,h);
  102. return errorCount;
  103. }
  104. [MonoTODO]
  105. internal int Validate(ValidationEventHandler h)
  106. {
  107. return errorCount;
  108. }
  109. //<restriction
  110. //base = QName
  111. //id = ID
  112. //{any attributes with non-schema namespace . . .}>
  113. //Content: (annotation?, (simpleType?, (minExclusive | minInclusive | maxExclusive | maxInclusive | totalDigits | fractionDigits | length | minLength | maxLength | enumeration | whiteSpace | pattern)*)?, ((attribute | attributeGroup)*, anyAttribute?))
  114. //</restriction>
  115. internal static XmlSchemaSimpleContentRestriction Read(XmlSchemaReader reader, ValidationEventHandler h)
  116. {
  117. XmlSchemaSimpleContentRestriction restriction = new XmlSchemaSimpleContentRestriction();
  118. reader.MoveToElement();
  119. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  120. {
  121. error(h,"Should not happen :1: XmlSchemaComplexContentRestriction.Read, name="+reader.Name,null);
  122. reader.SkipToEnd();
  123. return null;
  124. }
  125. restriction.LineNumber = reader.LineNumber;
  126. restriction.LinePosition = reader.LinePosition;
  127. restriction.SourceUri = reader.BaseURI;
  128. while(reader.MoveToNextAttribute())
  129. {
  130. if(reader.Name == "base")
  131. {
  132. Exception innerex;
  133. restriction.baseTypeName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
  134. if(innerex != null)
  135. error(h, reader.Value + " is not a valid value for base attribute",innerex);
  136. }
  137. else if(reader.Name == "id")
  138. {
  139. restriction.Id = reader.Value;
  140. }
  141. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  142. {
  143. error(h,reader.Name + " is not a valid attribute for restriction",null);
  144. }
  145. else
  146. {
  147. XmlSchemaUtil.ReadUnhandledAttribute(reader,restriction);
  148. }
  149. }
  150. reader.MoveToElement();
  151. if(reader.IsEmptyElement)
  152. return restriction;
  153. //Content: 1.annotation?,
  154. // 2.simpleType?,
  155. // 3.(minExclusive |...| enumeration | whiteSpace | pattern)*,
  156. // 4.(attribute | attributeGroup)*,
  157. // 5.anyAttribute?
  158. int level = 1;
  159. while(reader.ReadNextElement())
  160. {
  161. if(reader.NodeType == XmlNodeType.EndElement)
  162. {
  163. if(reader.LocalName != xmlname)
  164. error(h,"Should not happen :2: XmlSchemaSimpleContentRestriction.Read, name="+reader.Name,null);
  165. break;
  166. }
  167. if(level <= 1 && reader.LocalName == "annotation")
  168. {
  169. level = 2; //Only one annotation
  170. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  171. if(annotation != null)
  172. restriction.Annotation = annotation;
  173. continue;
  174. }
  175. if(level <=2 && reader.LocalName == "simpleType")
  176. {
  177. level = 3;
  178. XmlSchemaSimpleType stype = XmlSchemaSimpleType.Read(reader,h);
  179. if(stype != null)
  180. restriction.baseType = stype;
  181. continue;
  182. }
  183. if(level <= 3)
  184. {
  185. if(reader.LocalName == "minExclusive")
  186. {
  187. level = 3;
  188. XmlSchemaMinExclusiveFacet minex = XmlSchemaMinExclusiveFacet.Read(reader,h);
  189. if(minex != null)
  190. restriction.facets.Add(minex);
  191. continue;
  192. }
  193. else if(reader.LocalName == "minInclusive")
  194. {
  195. level = 3;
  196. XmlSchemaMinInclusiveFacet mini = XmlSchemaMinInclusiveFacet.Read(reader,h);
  197. if(mini != null)
  198. restriction.facets.Add(mini);
  199. continue;
  200. }
  201. else if(reader.LocalName == "maxExclusive")
  202. {
  203. level = 3;
  204. XmlSchemaMaxExclusiveFacet maxex = XmlSchemaMaxExclusiveFacet.Read(reader,h);
  205. if(maxex != null)
  206. restriction.facets.Add(maxex);
  207. continue;
  208. }
  209. else if(reader.LocalName == "maxInclusive")
  210. {
  211. level = 3;
  212. XmlSchemaMaxInclusiveFacet maxi = XmlSchemaMaxInclusiveFacet.Read(reader,h);
  213. if(maxi != null)
  214. restriction.facets.Add(maxi);
  215. continue;
  216. }
  217. else if(reader.LocalName == "totalDigits")
  218. {
  219. level = 3;
  220. XmlSchemaTotalDigitsFacet total = XmlSchemaTotalDigitsFacet.Read(reader,h);
  221. if(total != null)
  222. restriction.facets.Add(total);
  223. continue;
  224. }
  225. else if(reader.LocalName == "fractionDigits")
  226. {
  227. level = 3;
  228. XmlSchemaFractionDigitsFacet fraction = XmlSchemaFractionDigitsFacet.Read(reader,h);
  229. if(fraction != null)
  230. restriction.facets.Add(fraction);
  231. continue;
  232. }
  233. else if(reader.LocalName == "length")
  234. {
  235. level = 3;
  236. XmlSchemaLengthFacet length = XmlSchemaLengthFacet.Read(reader,h);
  237. if(length != null)
  238. restriction.facets.Add(length);
  239. continue;
  240. }
  241. else if(reader.LocalName == "minLength")
  242. {
  243. level = 3;
  244. XmlSchemaMinLengthFacet minlen = XmlSchemaMinLengthFacet.Read(reader,h);
  245. if(minlen != null)
  246. restriction.facets.Add(minlen);
  247. continue;
  248. }
  249. else if(reader.LocalName == "maxLength")
  250. {
  251. level = 3;
  252. XmlSchemaMaxLengthFacet maxlen = XmlSchemaMaxLengthFacet.Read(reader,h);
  253. if(maxlen != null)
  254. restriction.facets.Add(maxlen);
  255. continue;
  256. }
  257. else if(reader.LocalName == "enumeration")
  258. {
  259. level = 3;
  260. XmlSchemaEnumerationFacet enumeration = XmlSchemaEnumerationFacet.Read(reader,h);
  261. if(enumeration != null)
  262. restriction.facets.Add(enumeration);
  263. continue;
  264. }
  265. else if(reader.LocalName == "whiteSpace")
  266. {
  267. level = 3;
  268. XmlSchemaWhiteSpaceFacet ws = XmlSchemaWhiteSpaceFacet.Read(reader,h);
  269. if(ws != null)
  270. restriction.facets.Add(ws);
  271. continue;
  272. }
  273. else if(reader.LocalName == "pattern")
  274. {
  275. level = 3;
  276. XmlSchemaPatternFacet pattern = XmlSchemaPatternFacet.Read(reader,h);
  277. if(pattern != null)
  278. restriction.facets.Add(pattern);
  279. continue;
  280. }
  281. }
  282. if(level <= 4)
  283. {
  284. if(reader.LocalName == "attribute")
  285. {
  286. level = 4;
  287. XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader,h);
  288. if(attr != null)
  289. restriction.Attributes.Add(attr);
  290. continue;
  291. }
  292. if(reader.LocalName == "attributeGroup")
  293. {
  294. level = 4;
  295. XmlSchemaAttributeGroupRef attr = XmlSchemaAttributeGroupRef.Read(reader,h);
  296. if(attr != null)
  297. restriction.attributes.Add(attr);
  298. continue;
  299. }
  300. }
  301. if(level <= 5 && reader.LocalName == "anyAttribute")
  302. {
  303. level = 6;
  304. XmlSchemaAnyAttribute anyattr = XmlSchemaAnyAttribute.Read(reader,h);
  305. if(anyattr != null)
  306. restriction.AnyAttribute = anyattr;
  307. continue;
  308. }
  309. reader.RaiseInvalidElementError();
  310. }
  311. return restriction;
  312. }
  313. }
  314. }