System.XML: Remove more NET_2_0 directives
[mono.git] / mcs / class / System.XML / System.Xml.Schema / XmlSchemaKeyref.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 using System.Xml.Serialization;
27
28 namespace System.Xml.Schema
29 {
30         /// <summary>
31         /// Summary description for XmlSchemaKeyref.
32         /// </summary>
33         public class XmlSchemaKeyref : XmlSchemaIdentityConstraint
34         {
35                 private XmlQualifiedName refer;
36                 const string xmlname = "keyref";
37                 private XmlSchemaIdentityConstraint target;
38
39                 public XmlSchemaKeyref()
40                 {
41                         refer = XmlQualifiedName.Empty;
42                 }
43
44                 [System.Xml.Serialization.XmlAttribute("refer")]
45                 public XmlQualifiedName Refer
46                 {
47                         get{ return  refer; } 
48                         set{ refer = value; }
49                 }
50
51                 internal XmlSchemaIdentityConstraint Target
52                 {
53                         get { return target; }
54                 }
55
56                 /// <remarks>
57                 /// 1. name must be present
58                 /// 2. selector and field must be present
59                 /// 3. refer must be present
60                 /// </remarks>
61                 internal override int Compile(ValidationEventHandler h, XmlSchema schema)
62                 {
63                         base.Compile(h, schema);
64
65                         if(refer == null || refer.IsEmpty)
66                                 error(h,"refer must be present");
67                         else if(!XmlSchemaUtil.CheckQName(refer))
68                                 error(h,"Refer is not a valid XmlQualifiedName");
69
70                         return errorCount;
71                 }
72                 
73                 internal override int Validate (ValidationEventHandler h, XmlSchema schema)
74                 {
75                         // Find target key
76                         XmlSchemaIdentityConstraint target = schema.NamedIdentities [this.Refer] as XmlSchemaIdentityConstraint;
77                         if (target == null)
78                                 error (h, "Target key was not found.");
79                         else if (target is XmlSchemaKeyref)
80                                 error (h, "Target identity constraint was keyref.");
81                         else if (target.Fields.Count != this.Fields.Count)
82                                 error (h, "Target identity constraint has different number of fields.");
83                         else
84                                 this.target = target;
85                         return errorCount;
86                 }
87
88                 /*
89                 internal new void error(ValidationEventHandler handle, string message)
90                 {
91                         errorCount++;
92                         ValidationHandler.RaiseValidationError(handle, this, message);
93                 }
94                 */
95                 //<key 
96                 //  id = ID 
97                 //  name = NCName 
98                 //  refer = QName 
99                 //  {any attributes with non-schema namespace . . .}>
100                 //  Content: (annotation?, (selector, field+))
101                 //</key>
102                 internal static XmlSchemaKeyref Read(XmlSchemaReader reader, ValidationEventHandler h)
103                 {
104                         XmlSchemaKeyref keyref = new XmlSchemaKeyref();
105                         reader.MoveToElement();
106
107                         if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
108                         {
109                                 error(h,"Should not happen :1: XmlSchemaKeyref.Read, name="+reader.Name,null);
110                                 reader.Skip();
111                                 return null;
112                         }
113
114                         keyref.LineNumber = reader.LineNumber;
115                         keyref.LinePosition = reader.LinePosition;
116                         keyref.SourceUri = reader.BaseURI;
117
118                         while(reader.MoveToNextAttribute())
119                         {
120                                 if(reader.Name == "id")
121                                 {
122                                         keyref.Id = reader.Value;
123                                 }
124                                 else if(reader.Name == "name")
125                                 {
126                                         keyref.Name = reader.Value;
127                                 }
128                                 else if(reader.Name == "refer")
129                                 {
130                                         Exception innerex;
131                                         keyref.refer = XmlSchemaUtil.ReadQNameAttribute(reader,out innerex);
132                                         if(innerex != null)
133                                                 error(h, reader.Value + " is not a valid value for refer attribute",innerex);
134                                 }
135                                 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
136                                 {
137                                         error(h,reader.Name + " is not a valid attribute for keyref",null);
138                                 }
139                                 else
140                                 {
141                                         XmlSchemaUtil.ReadUnhandledAttribute(reader,keyref);
142                                 }
143                         }
144                         
145                         reader.MoveToElement();
146                         if(reader.IsEmptyElement)
147                                 return keyref;
148
149                         //  Content: annotation?, selector, field+
150                         int level = 1;
151                         while(reader.ReadNextElement())
152                         {
153                                 if(reader.NodeType == XmlNodeType.EndElement)
154                                 {
155                                         if(reader.LocalName != xmlname)
156                                                 error(h,"Should not happen :2: XmlSchemaKeyref.Read, name="+reader.Name,null);
157                                         break;
158                                 }
159                                 if(level <= 1 && reader.LocalName == "annotation")
160                                 {
161                                         level = 2; //Only one annotation
162                                         XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
163                                         if(annotation != null)
164                                                 keyref.Annotation = annotation;
165                                         continue;
166                                 }
167                                 if(level <= 2 && reader.LocalName == "selector")
168                                 {
169                                         level = 3;
170                                         XmlSchemaXPath selector = XmlSchemaXPath.Read(reader,h,"selector");
171                                         if(selector != null)
172                                                 keyref.Selector = selector;
173                                         continue;
174                                 }
175                                 if(level <= 3 && reader.LocalName == "field")
176                                 {
177                                         level = 3;
178                                         if(keyref.Selector == null)
179                                                 error(h,"selector must be defined before field declarations",null);
180                                         XmlSchemaXPath field = XmlSchemaXPath.Read(reader,h,"field");
181                                         if(field != null)
182                                                 keyref.Fields.Add(field);
183                                         continue;
184                                 }
185                                 reader.RaiseInvalidElementError();
186                         }
187                         return keyref;
188                 }
189         }
190 }