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