XmlSchemaAll.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. //
  2. // System.Xml.Schema.XmlSchemaAll.cs
  3. //
  4. // Author:
  5. // Dwivedi, Ajay kumar [email protected]
  6. // Atsushi Enomoto [email protected]
  7. //
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections;
  30. using System.Xml;
  31. using System.Xml.Serialization;
  32. namespace System.Xml.Schema
  33. {
  34. /// <summary>
  35. /// Summary description for XmlSchemaAll.
  36. /// </summary>
  37. public class XmlSchemaAll : XmlSchemaGroupBase
  38. {
  39. private XmlSchema schema;
  40. private XmlSchemaObjectCollection items;
  41. const string xmlname = "all";
  42. private bool emptiable;
  43. public XmlSchemaAll()
  44. {
  45. items = new XmlSchemaObjectCollection();
  46. }
  47. [XmlElement("element",typeof(XmlSchemaElement),Namespace=XmlSchema.Namespace)]
  48. public override XmlSchemaObjectCollection Items
  49. {
  50. get{ return items; }
  51. }
  52. internal bool Emptiable
  53. {
  54. get { return emptiable; }
  55. }
  56. /// <remarks>
  57. /// 1. MaxOccurs must be one. (default is also one)
  58. /// 2. MinOccurs must be zero or one.
  59. /// </remarks>
  60. internal override int Compile(ValidationEventHandler h, XmlSchema schema)
  61. {
  62. // If this is already compiled this time, simply skip.
  63. if (this.IsComplied (schema.CompilationId))
  64. return 0;
  65. this.schema = schema;
  66. if(MaxOccurs != Decimal.One)
  67. error(h,"maxOccurs must be 1");
  68. if(MinOccurs != Decimal.One && MinOccurs != Decimal.Zero)
  69. error(h,"minOccurs must be 0 or 1");
  70. XmlSchemaUtil.CompileID(Id, this, schema.IDCollection, h);
  71. CompileOccurence (h, schema);
  72. foreach(XmlSchemaObject obj in Items)
  73. {
  74. XmlSchemaElement elem = obj as XmlSchemaElement;
  75. if(elem != null)
  76. {
  77. if(elem.ValidatedMaxOccurs != Decimal.One && elem.ValidatedMaxOccurs != Decimal.Zero)
  78. {
  79. elem.error (h,"The {max occurs} of all the elements of 'all' must be 0 or 1. ");
  80. }
  81. errorCount += elem.Compile(h, schema);
  82. }
  83. else
  84. {
  85. error(h,"XmlSchemaAll can only contain Items of type Element");
  86. }
  87. }
  88. this.CompilationId = schema.CompilationId;
  89. return errorCount;
  90. }
  91. internal override XmlSchemaParticle GetOptimizedParticle (bool isTop)
  92. {
  93. if (OptimizedParticle != null)
  94. return OptimizedParticle;
  95. if (Items.Count == 0 || ValidatedMaxOccurs == 0) {
  96. OptimizedParticle = XmlSchemaParticle.Empty;
  97. return OptimizedParticle;
  98. }
  99. else if (Items.Count == 1) {
  100. if (ValidatedMinOccurs == 1 && ValidatedMaxOccurs == 1) {
  101. XmlSchemaSequence seq = new XmlSchemaSequence ();
  102. this.CopyInfo (seq);
  103. XmlSchemaParticle p = (XmlSchemaParticle) Items [0];
  104. p = p.GetOptimizedParticle (false);
  105. if (p == XmlSchemaParticle.Empty)
  106. OptimizedParticle = p;
  107. else {
  108. seq.Items.Add (p);
  109. seq.CompiledItems.Add (p);
  110. seq.Compile (null, schema);
  111. OptimizedParticle = seq;
  112. }
  113. return OptimizedParticle;
  114. }
  115. }
  116. XmlSchemaAll all = new XmlSchemaAll ();
  117. CopyInfo (all);
  118. CopyOptimizedItems (all);
  119. OptimizedParticle = all;
  120. all.ComputeEmptiable ();
  121. return OptimizedParticle;
  122. }
  123. internal override int Validate(ValidationEventHandler h, XmlSchema schema)
  124. {
  125. if (IsValidated (schema.CompilationId))
  126. return errorCount;
  127. // 3.8.6 All Group Limited :: 1.
  128. // Beware that this section was corrected: E1-26 of http://www.w3.org/2001/05/xmlschema-errata#Errata1
  129. if (!this.parentIsGroupDefinition && ValidatedMaxOccurs != 1)
  130. error (h, "-all- group is limited to be content of a model group, or that of a complex type with maxOccurs to be 1.");
  131. CompiledItems.Clear ();
  132. foreach (XmlSchemaParticle obj in Items) {
  133. errorCount += obj.Validate (h, schema);
  134. if (obj.ValidatedMaxOccurs != 0 &&
  135. obj.ValidatedMaxOccurs != 1)
  136. error (h, "MaxOccurs of a particle inside -all- compositor must be either 0 or 1.");
  137. CompiledItems.Add (obj);
  138. }
  139. ComputeEmptiable ();
  140. ValidationId = schema.ValidationId;
  141. return errorCount;
  142. }
  143. private void ComputeEmptiable ()
  144. {
  145. emptiable = true;
  146. for (int i = 0; i < Items.Count; i++) {
  147. if (((XmlSchemaParticle) Items [i]).ValidatedMinOccurs > 0) {
  148. emptiable = false;
  149. break;
  150. }
  151. }
  152. }
  153. internal override bool ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
  154. ValidationEventHandler h, XmlSchema schema, bool raiseError)
  155. {
  156. XmlSchemaAny any = baseParticle as XmlSchemaAny;
  157. XmlSchemaAll derivedAll = baseParticle as XmlSchemaAll;
  158. if (any != null) {
  159. // NSRecurseCheckCardinality
  160. return ValidateNSRecurseCheckCardinality (any, h, schema, raiseError);
  161. } else if (derivedAll != null) {
  162. // Recurse
  163. if (!ValidateOccurenceRangeOK (derivedAll, h, schema, raiseError))
  164. return false;
  165. return ValidateRecurse (derivedAll, h, schema, raiseError);
  166. }
  167. else {
  168. if (raiseError)
  169. error (h, "Invalid -all- particle derivation was found.");
  170. return false;
  171. }
  172. }
  173. internal override decimal GetMinEffectiveTotalRange ()
  174. {
  175. return GetMinEffectiveTotalRangeAllAndSequence ();
  176. }
  177. internal override void ValidateUniqueParticleAttribution (XmlSchemaObjectTable qnames, ArrayList nsNames,
  178. ValidationEventHandler h, XmlSchema schema)
  179. {
  180. foreach (XmlSchemaElement el in this.Items)
  181. el.ValidateUniqueParticleAttribution (qnames, nsNames, h, schema);
  182. }
  183. internal override void ValidateUniqueTypeAttribution (XmlSchemaObjectTable labels,
  184. ValidationEventHandler h, XmlSchema schema)
  185. {
  186. foreach (XmlSchemaElement el in this.Items)
  187. el.ValidateUniqueTypeAttribution (labels, h, schema);
  188. }
  189. //<all
  190. // id = ID
  191. // maxOccurs = 1 : 1
  192. // minOccurs = (0 | 1) : 1
  193. // {any attributes with non-schema namespace . . .}>
  194. // Content: (annotation?, element*)
  195. //</all>
  196. internal static XmlSchemaAll Read(XmlSchemaReader reader, ValidationEventHandler h)
  197. {
  198. XmlSchemaAll all = new XmlSchemaAll();
  199. reader.MoveToElement();
  200. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  201. {
  202. error(h,"Should not happen :1: XmlSchemaAll.Read, name="+reader.Name,null);
  203. reader.SkipToEnd();
  204. return null;
  205. }
  206. all.LineNumber = reader.LineNumber;
  207. all.LinePosition = reader.LinePosition;
  208. all.SourceUri = reader.BaseURI;
  209. //Read Attributes
  210. while(reader.MoveToNextAttribute())
  211. {
  212. if(reader.Name == "id")
  213. {
  214. all.Id = reader.Value;
  215. }
  216. else if(reader.Name == "maxOccurs")
  217. {
  218. try
  219. {
  220. all.MaxOccursString = reader.Value;
  221. }
  222. catch(Exception e)
  223. {
  224. error(h,reader.Value + " is an invalid value for maxOccurs",e);
  225. }
  226. }
  227. else if(reader.Name == "minOccurs")
  228. {
  229. try
  230. {
  231. all.MinOccursString = reader.Value;
  232. }
  233. catch(Exception e)
  234. {
  235. error(h,reader.Value + " is an invalid value for minOccurs",e);
  236. }
  237. }
  238. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  239. {
  240. error(h,reader.Name + " is not a valid attribute for all",null);
  241. }
  242. else
  243. {
  244. XmlSchemaUtil.ReadUnhandledAttribute(reader,all);
  245. }
  246. }
  247. reader.MoveToElement();
  248. if(reader.IsEmptyElement)
  249. return all;
  250. //Content: (annotation?, element*)
  251. int level = 1;
  252. while(reader.ReadNextElement())
  253. {
  254. if(reader.NodeType == XmlNodeType.EndElement)
  255. {
  256. if(reader.LocalName != xmlname)
  257. error(h,"Should not happen :2: XmlSchemaAll.Read, name="+reader.Name,null);
  258. break;
  259. }
  260. if(level <= 1 && reader.LocalName == "annotation")
  261. {
  262. level = 2; //Only one annotation
  263. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  264. if(annotation != null)
  265. all.Annotation = annotation;
  266. continue;
  267. }
  268. if(level <=2 && reader.LocalName == "element")
  269. {
  270. level = 2;
  271. XmlSchemaElement element = XmlSchemaElement.Read(reader,h);
  272. if(element != null)
  273. all.items.Add(element);
  274. continue;
  275. }
  276. reader.RaiseInvalidElementError();
  277. }
  278. return all;
  279. }
  280. }
  281. }