2002-12-24 Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
[mono.git] / mcs / class / System.XML / System.Xml.Schema / XmlSchemaAll.cs
1 // Author: Dwivedi, Ajay kumar\r
2 //            Adwiv@Yahoo.com\r
3 using System;\r
4 using System.Xml;\r
5 using System.Xml.Serialization;\r
6 \r
7 namespace System.Xml.Schema\r
8 {\r
9         /// <summary>\r
10         /// Summary description for XmlSchemaAll.\r
11         /// </summary>\r
12         public class XmlSchemaAll : XmlSchemaGroupBase\r
13         {\r
14                 private XmlSchemaObjectCollection items;\r
15                 private static string xmlname = "all";\r
16 \r
17                 public XmlSchemaAll()\r
18                 {\r
19                         items = new XmlSchemaObjectCollection();\r
20                 }\r
21 \r
22                 [XmlElement("element",typeof(XmlSchemaElement),Namespace="http://www.w3.org/2001/XMLSchema")]\r
23                 public override XmlSchemaObjectCollection Items \r
24                 {\r
25                         get{ return items; }\r
26                 }\r
27 \r
28                 /// <remarks>\r
29                 /// 1. MaxOccurs must be one. (default is also one)\r
30                 /// 2. MinOccurs must be zero or one.\r
31                 /// </remarks>\r
32                 [MonoTODO]\r
33                 internal int Compile(ValidationEventHandler h, XmlSchemaInfo info)\r
34                 {\r
35                         //FIXME: Should we reset the values on error??\r
36                         if(MaxOccurs != Decimal.One)\r
37                                 error(h,"maxOccurs must be 1");\r
38                         if(MinOccurs != Decimal.One && MinOccurs != Decimal.Zero)\r
39                                 error(h,"minOccurs must be 0 or 1");\r
40 \r
41                         XmlSchemaUtil.CompileID(Id, this, info.IDCollection, h);\r
42 \r
43                         foreach(XmlSchemaObject obj in Items)\r
44                         {\r
45                                 if(obj is XmlSchemaElement)\r
46                                 {\r
47                                         XmlSchemaElement elem = (XmlSchemaElement)obj;\r
48                                         if(elem.MaxOccurs != Decimal.One && elem.MaxOccurs != Decimal.Zero)\r
49                                         {\r
50                                                 elem.error(h,"The {max occurs} of all the elements of 'all' must be 0 or 1. ");\r
51                                         }\r
52                                         errorCount += elem.Compile(h,info);\r
53                                 }\r
54                                 else\r
55                                 {\r
56                                         error(h,"XmlSchemaAll can only contain Items of type Element");\r
57                                 }\r
58                         }\r
59 \r
60                         return errorCount;\r
61                 }\r
62 \r
63                 [MonoTODO]\r
64                 internal int Validate(ValidationEventHandler h)\r
65                 {\r
66                         return errorCount;\r
67                 }\r
68                 //<all\r
69                 //  id = ID\r
70                 //  maxOccurs = 1 : 1\r
71                 //  minOccurs = (0 | 1) : 1\r
72                 //  {any attributes with non-schema namespace . . .}>\r
73                 //  Content: (annotation?, element*)\r
74                 //</all>\r
75                 internal static XmlSchemaAll Read(XmlSchemaReader reader, ValidationEventHandler h)\r
76                 {\r
77                         XmlSchemaAll all = new XmlSchemaAll();\r
78                         reader.MoveToElement();\r
79 \r
80                         if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)\r
81                         {\r
82                                 error(h,"Should not happen :1: XmlSchemaAll.Read, name="+reader.Name,null);\r
83                                 reader.SkipToEnd();\r
84                                 return null;\r
85                         }\r
86                         \r
87                         all.LineNumber = reader.LineNumber;\r
88                         all.LinePosition = reader.LinePosition;\r
89                         all.SourceUri = reader.BaseURI;\r
90 \r
91                         //Read Attributes\r
92                         while(reader.MoveToNextAttribute())\r
93                         {\r
94                                 if(reader.Name == "id")\r
95                                 {\r
96                                         all.Id = reader.Value;\r
97                                 }\r
98                                 else if(reader.Name == "maxOccurs")\r
99                                 {\r
100                                         try\r
101                                         {\r
102                                                 all.MaxOccursString = reader.Value;\r
103                                         }\r
104                                         catch(Exception e)\r
105                                         {\r
106                                                 error(h,reader.Value + " is an invalid value for maxOccurs",e);\r
107                                         }\r
108                                 }\r
109                                 else if(reader.Name == "minOccurs")\r
110                                 {\r
111                                         try\r
112                                         {\r
113                                                 all.MinOccursString = reader.Value;\r
114                                         }\r
115                                         catch(Exception e)\r
116                                         {\r
117                                                 error(h,reader.Value + " is an invalid value for minOccurs",e);\r
118                                         }\r
119                                 }\r
120                                 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)\r
121                                 {\r
122                                         error(h,reader.Name + " is not a valid attribute for all",null);\r
123                                 }\r
124                                 else\r
125                                 {\r
126                                         XmlSchemaUtil.ReadUnhandledAttribute(reader,all);\r
127                                 }\r
128                         }\r
129                         \r
130                         reader.MoveToElement();\r
131                         if(reader.IsEmptyElement)\r
132                                 return all;\r
133 \r
134                         //Content: (annotation?, element*)\r
135                         int level = 1;\r
136                         while(reader.ReadNextElement())\r
137                         {\r
138                                 if(reader.NodeType == XmlNodeType.EndElement)\r
139                                 {\r
140                                         if(reader.LocalName != xmlname)\r
141                                                 error(h,"Should not happen :2: XmlSchemaAll.Read, name="+reader.Name,null);\r
142                                         break;\r
143                                 }\r
144                                 if(level <= 1 && reader.LocalName == "annotation")\r
145                                 {\r
146                                         level = 2;      //Only one annotation\r
147                                         XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);\r
148                                         if(annotation != null)\r
149                                                 all.Annotation = annotation;\r
150                                         continue;\r
151                                 }\r
152                                 if(level <=2 && reader.LocalName == "element")\r
153                                 {\r
154                                         level = 2;\r
155                                         XmlSchemaElement element = XmlSchemaElement.Read(reader,h);\r
156                                         if(element != null)\r
157                                                 all.items.Add(element);\r
158                                         continue;\r
159                                 }\r
160                                 reader.RaiseInvalidElementError();\r
161                         }\r
162                         return all;\r
163                 }\r
164         }\r
165 }\r