error messages review
[mono.git] / mcs / class / System.XML / System.Xml.Schema / XmlSchemaIdentityConstraint.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.Collections;\r
26 using System.Xml;\r
27 using System.Xml.Serialization;\r
28 using Mono.Xml.Schema;\r
29 \r
30 namespace System.Xml.Schema\r
31 {\r
32         /// <summary>\r
33         /// Summary description for XmlSchemaIdentityConstraint.\r
34         /// </summary>\r
35         public class XmlSchemaIdentityConstraint : XmlSchemaAnnotated\r
36         {\r
37                 private XmlSchemaObjectCollection fields;\r
38                 private string name;\r
39                 private XmlQualifiedName qName;\r
40                 private XmlSchemaXPath selector;\r
41 \r
42                 private XsdIdentitySelector compiledSelector;\r
43 //              ArrayList compiledFields;\r
44 \r
45                 public XmlSchemaIdentityConstraint()\r
46                 {\r
47                         fields = new XmlSchemaObjectCollection();\r
48                         qName = XmlQualifiedName.Empty;\r
49                 }\r
50                 \r
51                 [System.Xml.Serialization.XmlAttribute("name")]\r
52                 public string Name \r
53                 {\r
54                         get{ return  name; } \r
55                         set{ name = value; }\r
56                 }\r
57 \r
58                 [XmlElement("selector",typeof(XmlSchemaXPath),Namespace=XmlSchema.Namespace)]\r
59                 public XmlSchemaXPath Selector \r
60                 {\r
61                         get{ return  selector; } \r
62                         set{ selector = value; }\r
63                 }\r
64 \r
65                 [XmlElement("field",typeof(XmlSchemaXPath),Namespace=XmlSchema.Namespace)]\r
66                 public XmlSchemaObjectCollection Fields \r
67                 {\r
68                         get{ return fields; }\r
69                 }\r
70                 \r
71                 [XmlIgnore]\r
72                 public XmlQualifiedName QualifiedName \r
73                 {\r
74                         get{ return  qName; }\r
75                 }\r
76 \r
77                 internal XsdIdentitySelector CompiledSelector {\r
78                         get { return compiledSelector; }\r
79                 }\r
80 \r
81                 /// <remarks>\r
82                 /// 1. name must be present\r
83                 /// 2. selector and field must be present\r
84                 /// </remarks>\r
85                 internal override int Compile(ValidationEventHandler h, XmlSchema schema)\r
86                 {\r
87                         // If this is already compiled this time, simply skip.\r
88                         if (this.IsComplied (schema.CompilationId))\r
89                                 return 0;\r
90 \r
91 #if NET_2_0\r
92                         if (Selector != null)\r
93                                 Selector.Parent = this;\r
94                         foreach (XmlSchemaObject obj in Fields)\r
95                                 obj.Parent = this;\r
96 #endif\r
97 \r
98                         if(Name == null)\r
99                                 error(h,"Required attribute name must be present");\r
100                         else if(!XmlSchemaUtil.CheckNCName(this.name)) \r
101                                 error(h,"attribute name must be NCName");\r
102                         else {\r
103                                 this.qName = new XmlQualifiedName(Name,schema.TargetNamespace);\r
104                                 if (schema.NamedIdentities.Contains (qName))\r
105                                         error(h,"There is already same named identity constraint in this namespace.");\r
106                                 else\r
107                                         schema.NamedIdentities.Add (qName, this);\r
108                         }\r
109 \r
110                         if(Selector == null)\r
111                                 error(h,"selector must be present");\r
112                         else\r
113                         {\r
114                                 Selector.isSelector = true;\r
115                                 errorCount += Selector.Compile(h,schema);\r
116                                 if (selector.errorCount == 0)\r
117                                         compiledSelector = new XsdIdentitySelector (Selector);\r
118                         }\r
119                         if (errorCount > 0)\r
120                                 return errorCount; // fatal\r
121 \r
122                         if(Fields.Count == 0)\r
123                                 error(h,"atleast one field value must be present");\r
124                         else\r
125                         {\r
126                                 for (int i = 0; i < Fields.Count; i++)\r
127                                 {\r
128                                         XmlSchemaXPath field = Fields [i] as XmlSchemaXPath;\r
129                                         if(field != null)\r
130                                         {\r
131                                                 errorCount += field.Compile(h,schema);\r
132                                                 if (field.errorCount == 0)\r
133                                                         this.compiledSelector.AddField (new XsdIdentityField (field, i));\r
134                                         }\r
135                                         else\r
136                                                 error (h, "Object of type " + Fields [i].GetType() + " is invalid in the Fields Collection");\r
137                                 }\r
138                         }\r
139                         XmlSchemaUtil.CompileID(Id,this,schema.IDCollection,h);\r
140 \r
141                         this.CompilationId = schema.CompilationId;\r
142                         return errorCount;\r
143                 }\r
144         }\r
145 }\r