2004-05-06 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.XML / System.Xml.Schema / XmlSchemaIdentityConstraint.cs
1 // Author: Dwivedi, Ajay kumar\r
2 //            Adwiv@Yahoo.com\r
3 using System;\r
4 using System.Collections;\r
5 using System.Xml;\r
6 using System.Xml.Serialization;\r
7 using Mono.Xml.Schema;\r
8 \r
9 namespace System.Xml.Schema\r
10 {\r
11         /// <summary>\r
12         /// Summary description for XmlSchemaIdentityConstraint.\r
13         /// </summary>\r
14         public class XmlSchemaIdentityConstraint : XmlSchemaAnnotated\r
15         {\r
16                 private XmlSchemaObjectCollection fields;\r
17                 private string name;\r
18                 private XmlQualifiedName qName;\r
19                 private XmlSchemaXPath selector;\r
20 \r
21                 private XsdIdentitySelector compiledSelector;\r
22 //              ArrayList compiledFields;\r
23 \r
24                 public XmlSchemaIdentityConstraint()\r
25                 {\r
26                         fields = new XmlSchemaObjectCollection();\r
27                         qName = XmlQualifiedName.Empty;\r
28                 }\r
29                 \r
30                 [System.Xml.Serialization.XmlAttribute("name")]\r
31                 public string Name \r
32                 {\r
33                         get{ return  name; } \r
34                         set{ name = value; }\r
35                 }\r
36 \r
37                 [XmlElement("selector",typeof(XmlSchemaXPath),Namespace=XmlSchema.Namespace)]\r
38                 public XmlSchemaXPath Selector \r
39                 {\r
40                         get{ return  selector; } \r
41                         set{ selector = value; }\r
42                 }\r
43 \r
44                 [XmlElement("field",typeof(XmlSchemaXPath),Namespace=XmlSchema.Namespace)]\r
45                 public XmlSchemaObjectCollection Fields \r
46                 {\r
47                         get{ return fields; }\r
48                 }\r
49                 \r
50                 [XmlIgnore]\r
51                 public XmlQualifiedName QualifiedName \r
52                 {\r
53                         get{ return  qName; }\r
54                 }\r
55 \r
56                 internal XsdIdentitySelector CompiledSelector {\r
57                         get { return compiledSelector; }\r
58                 }\r
59 \r
60                 /// <remarks>\r
61                 /// 1. name must be present\r
62                 /// 2. selector and field must be present\r
63                 /// </remarks>\r
64                 internal override int Compile(ValidationEventHandler h, XmlSchema schema)\r
65                 {\r
66                         // If this is already compiled this time, simply skip.\r
67                         if (this.IsComplied (schema.CompilationId))\r
68                                 return 0;\r
69 \r
70                         if(Name == null)\r
71                                 error(h,"Required attribute name must be present");\r
72                         else if(!XmlSchemaUtil.CheckNCName(this.name)) \r
73                                 error(h,"attribute name must be NCName");\r
74                         else {\r
75                                 this.qName = new XmlQualifiedName(Name,schema.TargetNamespace);\r
76                                 if (schema.NamedIdentities.Contains (qName))\r
77                                         error(h,"There is already same named identity constraint in this namespace.");\r
78                                 else\r
79                                         schema.NamedIdentities.Add (qName, this);\r
80                         }\r
81 \r
82                         if(Selector == null)\r
83                                 error(h,"selector must be present");\r
84                         else\r
85                         {\r
86                                 Selector.isSelector = true;\r
87                                 errorCount += Selector.Compile(h,schema);\r
88                                 if (selector.errorCount == 0)\r
89                                         compiledSelector = new XsdIdentitySelector (Selector);\r
90                         }\r
91                         if (errorCount > 0)\r
92                                 return errorCount; // fatal\r
93 \r
94                         if(Fields.Count == 0)\r
95                                 error(h,"atleast one field value must be present");\r
96                         else\r
97                         {\r
98                                 for (int i = 0; i < Fields.Count; i++)\r
99                                 {\r
100                                         XmlSchemaXPath field = Fields [i] as XmlSchemaXPath;\r
101                                         if(field != null)\r
102                                         {\r
103                                                 errorCount += field.Compile(h,schema);\r
104                                                 if (field.errorCount == 0)\r
105                                                         this.compiledSelector.AddField (new XsdIdentityField (field, i));\r
106                                         }\r
107                                         else\r
108                                                 error (h, "Object of type " + Fields [i].GetType() + " is invalid in the Fields Collection");\r
109                                 }\r
110                         }\r
111                         XmlSchemaUtil.CompileID(Id,this,schema.IDCollection,h);\r
112 \r
113                         this.CompilationId = schema.CompilationId;\r
114                         return errorCount;\r
115                 }\r
116         }\r
117 }\r