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