Handle more type conversion.
[mono.git] / mcs / class / System.XML / System.Xml.Schema / XmlSchemaUnique.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
27 namespace System.Xml.Schema
28 {
29         /// <summary>
30         /// Summary description for XmlSchemaUnique.
31         /// </summary>
32         public class XmlSchemaUnique : XmlSchemaIdentityConstraint
33         {
34                 const string xmlname = "unique";
35
36                 public XmlSchemaUnique()
37                 {
38                 }
39                 /// <remarks>
40                 /// 1. name must be present
41                 /// 2. selector and field must be present
42                 /// </remarks>
43                 internal override int Compile(ValidationEventHandler h, XmlSchema schema)
44                 {
45                         return base.Compile(h,schema);
46                 }
47                 
48                 internal override int Validate(ValidationEventHandler h, XmlSchema schema)
49                 {
50                         return errorCount;
51                 }
52
53                 /*
54                 internal new void error(ValidationEventHandler handle, string message)
55                 {
56                         errorCount++;
57                         ValidationHandler.RaiseValidationError(handle, this, message);
58                 }
59                 */
60                 //<unique 
61                 //  id = ID 
62                 //  name = NCName 
63                 //  {any attributes with non-schema namespace . . .}>
64                 //  Content: (annotation?, (selector, field+))
65                 //</unique>
66                 internal static XmlSchemaUnique Read(XmlSchemaReader reader, ValidationEventHandler h)
67                 {
68                         XmlSchemaUnique unique = new XmlSchemaUnique();
69                         reader.MoveToElement();
70
71                         if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
72                         {
73                                 error(h,"Should not happen :1: XmlSchemaUnique.Read, name="+reader.Name,null);
74                                 reader.Skip();
75                                 return null;
76                         }
77
78                         unique.LineNumber = reader.LineNumber;
79                         unique.LinePosition = reader.LinePosition;
80                         unique.SourceUri = reader.BaseURI;
81
82                         while(reader.MoveToNextAttribute())
83                         {
84                                 if(reader.Name == "id")
85                                 {
86                                         unique.Id = reader.Value;
87                                 }
88                                 else if(reader.Name == "name")
89                                 {
90                                         unique.Name = reader.Value;
91                                 }
92                                 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
93                                 {
94                                         error(h,reader.Name + " is not a valid attribute for unique",null);
95                                 }
96                                 else
97                                 {
98                                         XmlSchemaUtil.ReadUnhandledAttribute(reader,unique);
99                                 }
100                         }
101                         
102                         reader.MoveToElement();
103                         if(reader.IsEmptyElement)
104                                 return unique;
105
106                         //  Content: annotation?, selector, field+
107                         int level = 1;
108                         while(reader.ReadNextElement())
109                         {
110                                 if(reader.NodeType == XmlNodeType.EndElement)
111                                 {
112                                         if(reader.LocalName != xmlname)
113                                                 error(h,"Should not happen :2: XmlSchemaUnion.Read, name="+reader.Name,null);
114                                         break;
115                                 }
116                                 if(level <= 1 && reader.LocalName == "annotation")
117                                 {
118                                         level = 2; //Only one annotation
119                                         XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
120                                         if(annotation != null)
121                                                 unique.Annotation = annotation;
122                                         continue;
123                                 }
124                                 if(level <= 2 && reader.LocalName == "selector")
125                                 {
126                                         level = 3;
127                                         XmlSchemaXPath selector = XmlSchemaXPath.Read(reader,h,"selector");
128                                         if(selector != null)
129                                                 unique.Selector = selector;
130                                         continue;
131                                 }
132                                 if(level <= 3 && reader.LocalName == "field")
133                                 {
134                                         level = 3;
135                                         if(unique.Selector == null)
136                                                 error(h,"selector must be defined before field declarations",null);
137                                         XmlSchemaXPath field = XmlSchemaXPath.Read(reader, h, "field");
138                                         if(field != null)
139                                                 unique.Fields.Add(field);
140                                         continue;
141                                 }
142                                 reader.RaiseInvalidElementError();
143                         }
144                         return unique;
145                 }
146         }
147 }