* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / System.XML / System.Xml.Schema / XmlSchemaSimpleTypeList.cs
1 // Author: Dwivedi, Ajay kumar\r
2 //            Adwiv@Yahoo.com\r
3
4 //
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:
12 // 
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
15 // 
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.
23 //
24 using System;\r
25 using System.Xml;\r
26 using System.Xml.Serialization;\r
27 \r
28 \r
29 namespace System.Xml.Schema\r
30 {\r
31         /// <summary>\r
32         /// Summary description for XmlSchemaSimpleTypeList.\r
33         /// </summary>\r
34         public class XmlSchemaSimpleTypeList : XmlSchemaSimpleTypeContent\r
35         {\r
36                 private XmlSchemaSimpleType itemType;\r
37                 private XmlQualifiedName itemTypeName;\r
38                 const string xmlname = "list";\r
39                 private object validatedListItemType;\r
40 #if NET_2_0\r
41                 private XmlSchemaSimpleType validatedListItemSchemaType;\r
42 #endif\r
43 \r
44                 public XmlSchemaSimpleTypeList()\r
45                 {\r
46                         this.ItemTypeName = XmlQualifiedName.Empty;\r
47                 }\r
48 \r
49                 [System.Xml.Serialization.XmlAttribute("itemType")]\r
50                 public XmlQualifiedName ItemTypeName\r
51                 {\r
52                         get{ return itemTypeName; } \r
53                         set\r
54                         {\r
55                                 itemTypeName = value;\r
56                         }\r
57                 }\r
58 \r
59                 [XmlElement("simpleType", Type=typeof (XmlSchemaSimpleType))]\r
60                 public XmlSchemaSimpleType ItemType \r
61                 {\r
62                         get{ return itemType; } \r
63                         set\r
64                         {\r
65                                 itemType = value;\r
66                         }\r
67                 }\r
68 \r
69 #if NET_2_0\r
70                 // LAMESPEC: this name is really ambiguous. Actually it just\r
71                 // holds compiled itemType, not baseType of the itemType.\r
72                 [XmlIgnore]\r
73                 public XmlSchemaSimpleType BaseItemType {\r
74                         get { return validatedListItemSchemaType; }\r
75                         // LAMESPEC: Since it is "post compilation information"\r
76                         // this setter should not exist. Here I just ignore\r
77                         // to not break the whole API. If this setter means \r
78                         // something, then it means that MS implementation is\r
79                         // buggy.\r
80                         set { }\r
81                 }\r
82 #endif\r
83 \r
84                 internal object ValidatedListItemType\r
85                 {\r
86                         get { return validatedListItemType; }\r
87                 }\r
88 \r
89                 /// <remarks>\r
90                 /// 1. One of itemType or a <simpleType> must be present, but not both.\r
91                 /// 2. id must be of type ID\r
92                 /// </remarks>\r
93                 internal override int Compile(ValidationEventHandler h, XmlSchema schema)\r
94                 {\r
95                         // If this is already compiled this time, simply skip.\r
96                         if (CompilationId == schema.CompilationId)\r
97                                 return 0;\r
98 \r
99 #if NET_2_0\r
100                         if (ItemType != null)\r
101                                 ItemType.Parent = this;\r
102 #endif\r
103 \r
104                         errorCount = 0;\r
105 \r
106                         if(ItemType != null && !ItemTypeName.IsEmpty)\r
107                                 error(h, "both itemType and simpletype can't be present");\r
108                         if(ItemType == null && ItemTypeName.IsEmpty)\r
109                                 error(h, "one of itemType or simpletype must be present");\r
110                         if(ItemType != null)\r
111                         {\r
112                                 errorCount += ItemType.Compile(h,schema);\r
113                         }\r
114                         if(!XmlSchemaUtil.CheckQName(ItemTypeName))\r
115                                 error(h,"BaseTypeName must be a XmlQualifiedName");\r
116                         \r
117                         XmlSchemaUtil.CompileID(Id,this,schema.IDCollection,h);\r
118 \r
119                         this.CompilationId = schema.CompilationId;\r
120                         return errorCount;\r
121                 }\r
122                 \r
123                 internal override int Validate(ValidationEventHandler h, XmlSchema schema)\r
124                 {\r
125                         if (IsValidated (schema.ValidationId))\r
126                                 return errorCount;\r
127 \r
128                         // ListItemType\r
129                         XmlSchemaSimpleType type = itemType;\r
130                         if (type == null)\r
131                                 type = schema.SchemaTypes [itemTypeName] as XmlSchemaSimpleType;\r
132                         if (type != null) {\r
133                                 errorCount += type.Validate (h, schema);\r
134                                 validatedListItemType = type;\r
135                         } else if (itemTypeName == XmlSchemaComplexType.AnyTypeName) {\r
136                                 validatedListItemType = XmlSchemaSimpleType.AnySimpleType;
137                         } else if (XmlSchemaUtil.IsBuiltInDatatypeName (itemTypeName)) {\r
138                                 validatedListItemType = XmlSchemaDatatype.FromName (itemTypeName);\r
139                                 if (validatedListItemType == null)\r
140                                         error (h, "Invalid schema type name was specified: " + itemTypeName);\r
141                         }\r
142                         // otherwise, it might be missing sub components.\r
143                         else if (!schema.IsNamespaceAbsent (itemTypeName.Namespace))\r
144                                 error (h, "Referenced base list item schema type " + itemTypeName + " was not found.");\r
145 \r
146 #if NET_2_0\r
147                         XmlSchemaSimpleType st = validatedListItemType as XmlSchemaSimpleType;\r
148                         if (st == null && validatedListItemType != null)\r
149                                 st = XmlSchemaType.GetBuiltInSimpleType (((XmlSchemaDatatype) validatedListItemType).TypeCode);\r
150                         validatedListItemSchemaType = st;\r
151 #endif\r
152 \r
153                         ValidationId = schema.ValidationId;\r
154                         return errorCount;\r
155                 }\r
156 \r
157                 //<list \r
158                 //  id = ID \r
159                 //  itemType = QName \r
160                 //  {any attributes with non-schema namespace . . .}>\r
161                 //  Content: (annotation?, (simpleType?))\r
162                 //</list>\r
163                 internal static XmlSchemaSimpleTypeList Read(XmlSchemaReader reader, ValidationEventHandler h)\r
164                 {\r
165                         XmlSchemaSimpleTypeList list = new XmlSchemaSimpleTypeList();\r
166                         reader.MoveToElement();\r
167 \r
168                         if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)\r
169                         {\r
170                                 error(h,"Should not happen :1: XmlSchemaSimpleTypeList.Read, name="+reader.Name,null);\r
171                                 reader.Skip();\r
172                                 return null;\r
173                         }\r
174 \r
175                         list.LineNumber = reader.LineNumber;\r
176                         list.LinePosition = reader.LinePosition;\r
177                         list.SourceUri = reader.BaseURI;\r
178 \r
179                         while(reader.MoveToNextAttribute())\r
180                         {\r
181                                 if(reader.Name == "id")\r
182                                 {\r
183                                         list.Id = reader.Value;\r
184                                 }\r
185                                 else if(reader.Name == "itemType")\r
186                                 {\r
187                                         Exception innerex;\r
188                                         list.ItemTypeName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);\r
189                                         if(innerex != null)\r
190                                                 error(h, reader.Value + " is not a valid value for itemType attribute",innerex);\r
191                                 }\r
192                                 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)\r
193                                 {\r
194                                         error(h,reader.Name + " is not a valid attribute for list",null);\r
195                                 }\r
196                                 else\r
197                                 {\r
198                                         XmlSchemaUtil.ReadUnhandledAttribute(reader,list);\r
199                                 }\r
200                         }\r
201                         \r
202                         reader.MoveToElement();\r
203                         if(reader.IsEmptyElement)\r
204                                 return list;\r
205                         //  Content: annotation?, simpleType?\r
206                         int level = 1;\r
207                         while(reader.ReadNextElement())\r
208                         {\r
209                                 if(reader.NodeType == XmlNodeType.EndElement)\r
210                                 {\r
211                                         if(reader.LocalName != xmlname)\r
212                                                 error(h,"Should not happen :2: XmlSchemaSimpleTypeList.Read, name="+reader.Name,null);\r
213                                         break;\r
214                                 }\r
215                                 if(level <= 1 && reader.LocalName == "annotation")\r
216                                 {\r
217                                         level = 2; //Only one annotation\r
218                                         XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);\r
219                                         if(annotation != null)\r
220                                                 list.Annotation = annotation;\r
221                                         continue;\r
222                                 }\r
223                                 if(level <= 2 && reader.LocalName == "simpleType")\r
224                                 {\r
225                                         level = 3;\r
226                                         XmlSchemaSimpleType stype = XmlSchemaSimpleType.Read(reader,h);\r
227                                         if(stype != null)\r
228                                                 list.itemType = stype;\r
229                                         continue;\r
230                                 }\r
231                                 reader.RaiseInvalidElementError();\r
232                         }\r
233                         return list;\r
234                 }\r
235         }\r
236 }\r