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