2004-05-06 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.XML / System.Xml.Schema / XmlSchemaSimpleTypeList.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 \r
8 namespace System.Xml.Schema\r
9 {\r
10         /// <summary>\r
11         /// Summary description for XmlSchemaSimpleTypeList.\r
12         /// </summary>\r
13         public class XmlSchemaSimpleTypeList : XmlSchemaSimpleTypeContent\r
14         {\r
15                 private XmlSchemaSimpleType itemType;\r
16                 private XmlQualifiedName itemTypeName;\r
17                 const string xmlname = "list";\r
18                 private object validatedListItemType;\r
19 \r
20                 public XmlSchemaSimpleTypeList()\r
21                 {\r
22                         this.ItemTypeName = XmlQualifiedName.Empty;\r
23                 }\r
24 \r
25                 [System.Xml.Serialization.XmlAttribute("itemType")]\r
26                 public XmlQualifiedName ItemTypeName\r
27                 {\r
28                         get{ return itemTypeName; } \r
29                         set\r
30                         {\r
31                                 itemTypeName = value;\r
32                         }\r
33                 }\r
34 \r
35                 [XmlElement("simpleType",Namespace=XmlSchema.Namespace)]\r
36                 public XmlSchemaSimpleType ItemType \r
37                 {\r
38                         get{ return itemType; } \r
39                         set\r
40                         {\r
41                                 itemType = value;\r
42                         }\r
43                 }\r
44                 internal object ValidatedListItemType\r
45                 {\r
46                         get { return validatedListItemType; }\r
47                 }\r
48 \r
49                 /// <remarks>\r
50                 /// 1. One of itemType or a <simpleType> must be present, but not both.\r
51                 /// 2. id must be of type ID\r
52                 /// </remarks>\r
53                 internal override int Compile(ValidationEventHandler h, XmlSchema schema)\r
54                 {\r
55                         // If this is already compiled this time, simply skip.\r
56                         if (this.IsComplied (schema.CompilationId))\r
57                                 return 0;\r
58 \r
59                         errorCount = 0;\r
60 \r
61                         if(ItemType != null && !ItemTypeName.IsEmpty)\r
62                                 error(h, "both itemType and simpletype can't be present");\r
63                         if(ItemType == null && ItemTypeName.IsEmpty)\r
64                                 error(h, "one of itemType or simpletype must be present");\r
65                         if(ItemType != null)\r
66                         {\r
67                                 errorCount += ItemType.Compile(h,schema);\r
68                         }\r
69                         if(!XmlSchemaUtil.CheckQName(ItemTypeName))\r
70                                 error(h,"BaseTypeName must be a XmlQualifiedName");\r
71                         \r
72                         XmlSchemaUtil.CompileID(Id,this,schema.IDCollection,h);\r
73 \r
74                         this.CompilationId = schema.CompilationId;\r
75                         return errorCount;\r
76                 }\r
77                 \r
78                 internal override int Validate(ValidationEventHandler h, XmlSchema schema)\r
79                 {\r
80                         if (IsValidated (schema.ValidationId))\r
81                                 return errorCount;\r
82 \r
83                         // ListItemType\r
84                         XmlSchemaSimpleType type = itemType;\r
85                         if (type == null)\r
86                                 type = schema.SchemaTypes [itemTypeName] as XmlSchemaSimpleType;\r
87                         if (type != null) {\r
88                                 errorCount += type.Validate (h, schema);\r
89                                 validatedListItemType = type;\r
90                         } else if (itemTypeName == XmlSchemaComplexType.AnyTypeName) {\r
91                                 validatedListItemType = XmlSchemaSimpleType.AnySimpleType;
92                         } else if (XmlSchemaUtil.IsBuiltInDatatypeName (itemTypeName)) {\r
93                                 validatedListItemType = XmlSchemaDatatype.FromName (itemTypeName);\r
94                                 if (validatedListItemType == null)\r
95                                         error (h, "Invalid schema type name was specified: " + itemTypeName);\r
96                         }\r
97                         // otherwise, it might be missing sub components.\r
98                         else if (!schema.IsNamespaceAbsent (itemTypeName.Namespace))\r
99                                 error (h, "Referenced base list item schema type " + itemTypeName + " was not found.");\r
100 \r
101                         ValidationId = schema.ValidationId;\r
102                         return errorCount;\r
103                 }\r
104 \r
105                 //<list \r
106                 //  id = ID \r
107                 //  itemType = QName \r
108                 //  {any attributes with non-schema namespace . . .}>\r
109                 //  Content: (annotation?, (simpleType?))\r
110                 //</list>\r
111                 internal static XmlSchemaSimpleTypeList Read(XmlSchemaReader reader, ValidationEventHandler h)\r
112                 {\r
113                         XmlSchemaSimpleTypeList list = new XmlSchemaSimpleTypeList();\r
114                         reader.MoveToElement();\r
115 \r
116                         if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)\r
117                         {\r
118                                 error(h,"Should not happen :1: XmlSchemaSimpleTypeList.Read, name="+reader.Name,null);\r
119                                 reader.Skip();\r
120                                 return null;\r
121                         }\r
122 \r
123                         list.LineNumber = reader.LineNumber;\r
124                         list.LinePosition = reader.LinePosition;\r
125                         list.SourceUri = reader.BaseURI;\r
126 \r
127                         while(reader.MoveToNextAttribute())\r
128                         {\r
129                                 if(reader.Name == "id")\r
130                                 {\r
131                                         list.Id = reader.Value;\r
132                                 }\r
133                                 else if(reader.Name == "itemType")\r
134                                 {\r
135                                         Exception innerex;\r
136                                         list.ItemTypeName = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);\r
137                                         if(innerex != null)\r
138                                                 error(h, reader.Value + " is not a valid value for itemType attribute",innerex);\r
139                                 }\r
140                                 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)\r
141                                 {\r
142                                         error(h,reader.Name + " is not a valid attribute for list",null);\r
143                                 }\r
144                                 else\r
145                                 {\r
146                                         XmlSchemaUtil.ReadUnhandledAttribute(reader,list);\r
147                                 }\r
148                         }\r
149                         \r
150                         reader.MoveToElement();\r
151                         if(reader.IsEmptyElement)\r
152                                 return list;\r
153                         //  Content: annotation?, simpleType?\r
154                         int level = 1;\r
155                         while(reader.ReadNextElement())\r
156                         {\r
157                                 if(reader.NodeType == XmlNodeType.EndElement)\r
158                                 {\r
159                                         if(reader.LocalName != xmlname)\r
160                                                 error(h,"Should not happen :2: XmlSchemaSimpleTypeList.Read, name="+reader.Name,null);\r
161                                         break;\r
162                                 }\r
163                                 if(level <= 1 && reader.LocalName == "annotation")\r
164                                 {\r
165                                         level = 2; //Only one annotation\r
166                                         XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);\r
167                                         if(annotation != null)\r
168                                                 list.Annotation = annotation;\r
169                                         continue;\r
170                                 }\r
171                                 if(level <= 2 && reader.LocalName == "simpleType")\r
172                                 {\r
173                                         level = 3;\r
174                                         XmlSchemaSimpleType stype = XmlSchemaSimpleType.Read(reader,h);\r
175                                         if(stype != null)\r
176                                                 list.itemType = stype;\r
177                                         continue;\r
178                                 }\r
179                                 reader.RaiseInvalidElementError();\r
180                         }\r
181                         return list;\r
182                 }\r
183         }\r
184 }\r