XmlSchemaAll.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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))]
  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. #if NET_2_0
  75. obj.Parent = this;
  76. #endif
  77. XmlSchemaElement elem = obj as XmlSchemaElement;
  78. if(elem != null)
  79. {
  80. if(elem.ValidatedMaxOccurs != Decimal.One && elem.ValidatedMaxOccurs != Decimal.Zero)
  81. {
  82. elem.error (h,"The {max occurs} of all the elements of 'all' must be 0 or 1. ");
  83. }
  84. errorCount += elem.Compile(h, schema);
  85. }
  86. else
  87. {
  88. error(h,"XmlSchemaAll can only contain Items of type Element");
  89. }
  90. }
  91. this.CompilationId = schema.CompilationId;
  92. return errorCount;
  93. }
  94. internal override XmlSchemaParticle GetOptimizedParticle (bool isTop)
  95. {
  96. if (OptimizedParticle != null)
  97. return OptimizedParticle;
  98. if (Items.Count == 0 || ValidatedMaxOccurs == 0) {
  99. OptimizedParticle = XmlSchemaParticle.Empty;
  100. return OptimizedParticle;
  101. }
  102. else if (Items.Count == 1) {
  103. if (ValidatedMinOccurs == 1 && ValidatedMaxOccurs == 1) {
  104. XmlSchemaSequence seq = new XmlSchemaSequence ();
  105. this.CopyInfo (seq);
  106. XmlSchemaParticle p = (XmlSchemaParticle) Items [0];
  107. p = p.GetOptimizedParticle (false);
  108. if (p == XmlSchemaParticle.Empty)
  109. OptimizedParticle = p;
  110. else {
  111. seq.Items.Add (p);
  112. seq.CompiledItems.Add (p);
  113. seq.Compile (null, schema);
  114. OptimizedParticle = seq;
  115. }
  116. return OptimizedParticle;
  117. }
  118. }
  119. XmlSchemaAll all = new XmlSchemaAll ();
  120. CopyInfo (all);
  121. CopyOptimizedItems (all);
  122. OptimizedParticle = all;
  123. all.ComputeEmptiable ();
  124. return OptimizedParticle;
  125. }
  126. internal override int Validate(ValidationEventHandler h, XmlSchema schema)
  127. {
  128. if (IsValidated (schema.CompilationId))
  129. return errorCount;
  130. // 3.8.6 All Group Limited :: 1.
  131. // Beware that this section was corrected: E1-26 of http://www.w3.org/2001/05/xmlschema-errata#Errata1
  132. if (!this.parentIsGroupDefinition && ValidatedMaxOccurs != 1)
  133. error (h, "-all- group is limited to be content of a model group, or that of a complex type with maxOccurs to be 1.");
  134. CompiledItems.Clear ();
  135. foreach (XmlSchemaParticle obj in Items) {
  136. errorCount += obj.Validate (h, schema);
  137. if (obj.ValidatedMaxOccurs != 0 &&
  138. obj.ValidatedMaxOccurs != 1)
  139. error (h, "MaxOccurs of a particle inside -all- compositor must be either 0 or 1.");
  140. CompiledItems.Add (obj);
  141. }
  142. ComputeEmptiable ();
  143. ValidationId = schema.ValidationId;
  144. return errorCount;
  145. }
  146. private void ComputeEmptiable ()
  147. {
  148. emptiable = true;
  149. for (int i = 0; i < Items.Count; i++) {
  150. if (((XmlSchemaParticle) Items [i]).ValidatedMinOccurs > 0) {
  151. emptiable = false;
  152. break;
  153. }
  154. }
  155. }
  156. internal override bool ValidateDerivationByRestriction (XmlSchemaParticle baseParticle,
  157. ValidationEventHandler h, XmlSchema schema, bool raiseError)
  158. {
  159. XmlSchemaAny any = baseParticle as XmlSchemaAny;
  160. XmlSchemaAll derivedAll = baseParticle as XmlSchemaAll;
  161. if (any != null) {
  162. // NSRecurseCheckCardinality
  163. return ValidateNSRecurseCheckCardinality (any, h, schema, raiseError);
  164. } else if (derivedAll != null) {
  165. // Recurse
  166. if (!ValidateOccurenceRangeOK (derivedAll, h, schema, raiseError))
  167. return false;
  168. return ValidateRecurse (derivedAll, h, schema, raiseError);
  169. }
  170. else {
  171. if (raiseError)
  172. error (h, "Invalid -all- particle derivation was found.");
  173. return false;
  174. }
  175. }
  176. internal override decimal GetMinEffectiveTotalRange ()
  177. {
  178. return GetMinEffectiveTotalRangeAllAndSequence ();
  179. }
  180. internal override void ValidateUniqueParticleAttribution (XmlSchemaObjectTable qnames, ArrayList nsNames,
  181. ValidationEventHandler h, XmlSchema schema)
  182. {
  183. foreach (XmlSchemaElement el in this.Items)
  184. el.ValidateUniqueParticleAttribution (qnames, nsNames, h, schema);
  185. }
  186. internal override void ValidateUniqueTypeAttribution (XmlSchemaObjectTable labels,
  187. ValidationEventHandler h, XmlSchema schema)
  188. {
  189. foreach (XmlSchemaElement el in this.Items)
  190. el.ValidateUniqueTypeAttribution (labels, h, schema);
  191. }
  192. //<all
  193. // id = ID
  194. // maxOccurs = 1 : 1
  195. // minOccurs = (0 | 1) : 1
  196. // {any attributes with non-schema namespace . . .}>
  197. // Content: (annotation?, element*)
  198. //</all>
  199. internal static XmlSchemaAll Read(XmlSchemaReader reader, ValidationEventHandler h)
  200. {
  201. XmlSchemaAll all = new XmlSchemaAll();
  202. reader.MoveToElement();
  203. if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
  204. {
  205. error(h,"Should not happen :1: XmlSchemaAll.Read, name="+reader.Name,null);
  206. reader.SkipToEnd();
  207. return null;
  208. }
  209. all.LineNumber = reader.LineNumber;
  210. all.LinePosition = reader.LinePosition;
  211. all.SourceUri = reader.BaseURI;
  212. //Read Attributes
  213. while(reader.MoveToNextAttribute())
  214. {
  215. if(reader.Name == "id")
  216. {
  217. all.Id = reader.Value;
  218. }
  219. else if(reader.Name == "maxOccurs")
  220. {
  221. try
  222. {
  223. all.MaxOccursString = reader.Value;
  224. }
  225. catch(Exception e)
  226. {
  227. error(h,reader.Value + " is an invalid value for maxOccurs",e);
  228. }
  229. }
  230. else if(reader.Name == "minOccurs")
  231. {
  232. try
  233. {
  234. all.MinOccursString = reader.Value;
  235. }
  236. catch(Exception e)
  237. {
  238. error(h,reader.Value + " is an invalid value for minOccurs",e);
  239. }
  240. }
  241. else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  242. {
  243. error(h,reader.Name + " is not a valid attribute for all",null);
  244. }
  245. else
  246. {
  247. XmlSchemaUtil.ReadUnhandledAttribute(reader,all);
  248. }
  249. }
  250. reader.MoveToElement();
  251. if(reader.IsEmptyElement)
  252. return all;
  253. //Content: (annotation?, element*)
  254. int level = 1;
  255. while(reader.ReadNextElement())
  256. {
  257. if(reader.NodeType == XmlNodeType.EndElement)
  258. {
  259. if(reader.LocalName != xmlname)
  260. error(h,"Should not happen :2: XmlSchemaAll.Read, name="+reader.Name,null);
  261. break;
  262. }
  263. if(level <= 1 && reader.LocalName == "annotation")
  264. {
  265. level = 2; //Only one annotation
  266. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  267. if(annotation != null)
  268. all.Annotation = annotation;
  269. continue;
  270. }
  271. if(level <=2 && reader.LocalName == "element")
  272. {
  273. level = 2;
  274. XmlSchemaElement element = XmlSchemaElement.Read(reader,h);
  275. if(element != null)
  276. all.items.Add(element);
  277. continue;
  278. }
  279. reader.RaiseInvalidElementError();
  280. }
  281. return all;
  282. }
  283. }
  284. }