merge -r 58060:58217
[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                 }\r
76 #endif\r
77 \r
78                 internal object ValidatedListItemType\r
79                 {\r
80                         get { return validatedListItemType; }\r
81                 }\r
82 \r
83                 /// <remarks>\r
84                 /// 1. One of itemType or a <simpleType> must be present, but not both.\r
85                 /// 2. id must be of type ID\r
86                 /// </remarks>\r
87                 internal override int Compile(ValidationEventHandler h, XmlSchema schema)\r
88                 {\r
89                         // If this is already compiled this time, simply skip.\r
90                         if (this.IsComplied (schema.CompilationId))\r
91                                 return 0;\r
92 \r
93 #if NET_2_0\r
94                         if (ItemType != null)\r
95                                 ItemType.Parent = this;\r
96 #endif\r
97 \r
98                         errorCount = 0;\r
99 \r
100                         if(ItemType != null && !ItemTypeName.IsEmpty)\r
101                                 error(h, "both itemType and simpletype can't be present");\r
102                         if(ItemType == null && ItemTypeName.IsEmpty)\r
103                                 error(h, "one of itemType or simpletype must be present");\r
104                         if(ItemType != null)\r
105                         {\r
106                                 errorCount += ItemType.Compile(h,schema);\r
107                         }\r
108                         if(!XmlSchemaUtil.CheckQName(ItemTypeName))\r
109                                 error(h,"BaseTypeName must be a XmlQualifiedName");\r
110                         \r
111                         XmlSchemaUtil.CompileID(Id,this,schema.IDCollection,h);\r
112 \r
113                         this.CompilationId = schema.CompilationId;\r
114                         return errorCount;\r
115                 }\r
116                 \r
117                 internal override int Validate(ValidationEventHandler h, XmlSchema schema)\r
118                 {\r
119                         if (IsValidated (schema.ValidationId))\r
120                                 return errorCount;\r
121 \r
122                         // ListItemType\r
123                         XmlSchemaSimpleType type = itemType;\r
124                         if (type == null)\r
125                                 type = schema.SchemaTypes [itemTypeName] as XmlSchemaSimpleType;\r
126                         if (type != null) {\r
127                                 errorCount += type.Validate (h, schema);\r
128                                 validatedListItemType = type;\r
129                         } else if (itemTypeName == XmlSchemaComplexType.AnyTypeName) {\r
130                                 validatedListItemType = XmlSchemaSimpleType.AnySimpleType;
131                         } else if (XmlSchemaUtil.IsBuiltInDatatypeName (itemTypeName)) {\r
132                                 validatedListItemType = XmlSchemaDatatype.FromName (itemTypeName);\r
133                                 if (validatedListItemType == null)\r
134                                         error (h, "Invalid schema type name was specified: " + itemTypeName);\r
135                         }\r
136                         // otherwise, it might be missing sub components.\r
137                         else if (!schema.IsNamespaceAbsent (itemTypeName.Namespace))\r
138                                 error (h, "Referenced base list item schema type " + itemTypeName + " was not found.");\r
139 \r
140 #if NET_2_0\r
141                         XmlSchemaSimpleType st = validatedListItemType as XmlSchemaSimpleType;\r
142                         if (st == null && validatedListItemType != null)\r
143                                 st = XmlSchemaType.GetBuiltInSimpleType (((XmlSchemaDatatype) validatedListItemType).TypeCode);\r
144                         validatedListItemSchemaType = st;\r
145 #endif\r
146 \r
147                         ValidationId = schema.ValidationId;\r
148                         return errorCount;\r
149                 }\r
150 \r
151                 //<list \r
152                 //  id = ID \r
153                 //  itemType = QName \r
154                 //  {any attributes with non-schema namespace . . .}>\r
155                 //  Content: (annotation?, (simpleType?))\r
156                 //</list>\r
157                 internal static XmlSchemaSimpleTypeList Read(XmlSchemaReader reader, ValidationEventHandler h)\r
158                 {\r
159                         XmlSchemaSimpleTypeList list = new XmlSchemaSimpleTypeList();\r
160                         reader.MoveToElement();\r
161 \r
162                         if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)\r
163                         {\r
164                                 error(h,"Should not happen :1: XmlSchemaSimpleTypeList.Read, name="+reader.Name,null);\r
165                                 reader.Skip();\r
166                                 return null;\r
167                         }\r
168 \r
169                         list.LineNumber = reader.LineNumber;\r
170                         list.LinePosition = reader.LinePosition;\r
171                         list.SourceUri = reader.BaseURI;\r
172 \r
173                         while(reader.MoveToNextAttribute())\r
174                         {\r
175                                 if(reader.Name == "id")\r
176                                 {\r
177                                         list.Id = reader.Value;\r
178                                 }\r
179                                 else if(reader.Name == "itemType")\r
180                                 {\r
181                                         Exception innerex;\r
182                                         list.ItemTypeName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);\r
183                                         if(innerex != null)\r
184                                                 error(h, reader.Value + " is not a valid value for itemType attribute",innerex);\r
185                                 }\r
186                                 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)\r
187                                 {\r
188                                         error(h,reader.Name + " is not a valid attribute for list",null);\r
189                                 }\r
190                                 else\r
191                                 {\r
192                                         XmlSchemaUtil.ReadUnhandledAttribute(reader,list);\r
193                                 }\r
194                         }\r
195                         \r
196                         reader.MoveToElement();\r
197                         if(reader.IsEmptyElement)\r
198                                 return list;\r
199                         //  Content: annotation?, simpleType?\r
200                         int level = 1;\r
201                         while(reader.ReadNextElement())\r
202                         {\r
203                                 if(reader.NodeType == XmlNodeType.EndElement)\r
204                                 {\r
205                                         if(reader.LocalName != xmlname)\r
206                                                 error(h,"Should not happen :2: XmlSchemaSimpleTypeList.Read, name="+reader.Name,null);\r
207                                         break;\r
208                                 }\r
209                                 if(level <= 1 && reader.LocalName == "annotation")\r
210                                 {\r
211                                         level = 2; //Only one annotation\r
212                                         XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);\r
213                                         if(annotation != null)\r
214                                                 list.Annotation = annotation;\r
215                                         continue;\r
216                                 }\r
217                                 if(level <= 2 && reader.LocalName == "simpleType")\r
218                                 {\r
219                                         level = 3;\r
220                                         XmlSchemaSimpleType stype = XmlSchemaSimpleType.Read(reader,h);\r
221                                         if(stype != null)\r
222                                                 list.itemType = stype;\r
223                                         continue;\r
224                                 }\r
225                                 reader.RaiseInvalidElementError();\r
226                         }\r
227                         return list;\r
228                 }\r
229         }\r
230 }\r