2010-06-21 Atsushi Enomoto <atsushi@ximian.com>
[mcs.git] / class / System.XML / System.Xml.Schema / XmlSchemaKeyref.cs
blob746fe488015eb935de3f1388c938fea6bbf42e43
1 // Author: Dwivedi, Ajay kumar
2 // Adwiv@Yahoo.com
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.
24 using System;
25 using System.Xml;
26 using System.Xml.Serialization;
28 namespace System.Xml.Schema
30 /// <summary>
31 /// Summary description for XmlSchemaKeyref.
32 /// </summary>
33 public class XmlSchemaKeyref : XmlSchemaIdentityConstraint
35 private XmlQualifiedName refer;
36 const string xmlname = "keyref";
37 private XmlSchemaIdentityConstraint target;
39 public XmlSchemaKeyref()
41 refer = XmlQualifiedName.Empty;
44 [System.Xml.Serialization.XmlAttribute("refer")]
45 public XmlQualifiedName Refer
47 get{ return refer; }
48 set{ refer = value; }
51 internal XmlSchemaIdentityConstraint Target
53 get { return target; }
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)
63 base.Compile(h, schema);
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");
70 return errorCount;
73 internal override int Validate (ValidationEventHandler h, XmlSchema schema)
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;
89 internal new void error(ValidationEventHandler handle, string message)
91 errorCount++;
92 ValidationHandler.RaiseValidationError(handle, this, message);
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)
104 XmlSchemaKeyref keyref = new XmlSchemaKeyref();
105 reader.MoveToElement();
107 if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
109 error(h,"Should not happen :1: XmlSchemaKeyref.Read, name="+reader.Name,null);
110 reader.Skip();
111 return null;
114 keyref.LineNumber = reader.LineNumber;
115 keyref.LinePosition = reader.LinePosition;
116 keyref.SourceUri = reader.BaseURI;
118 while(reader.MoveToNextAttribute())
120 if(reader.Name == "id")
122 keyref.Id = reader.Value;
124 else if(reader.Name == "name")
126 keyref.Name = reader.Value;
128 else if(reader.Name == "refer")
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);
135 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
137 error(h,reader.Name + " is not a valid attribute for keyref",null);
139 else
141 XmlSchemaUtil.ReadUnhandledAttribute(reader,keyref);
145 reader.MoveToElement();
146 if(reader.IsEmptyElement)
147 return keyref;
149 // Content: annotation?, selector, field+
150 int level = 1;
151 while(reader.ReadNextElement())
153 if(reader.NodeType == XmlNodeType.EndElement)
155 if(reader.LocalName != xmlname)
156 error(h,"Should not happen :2: XmlSchemaKeyref.Read, name="+reader.Name,null);
157 break;
159 if(level <= 1 && reader.LocalName == "annotation")
161 level = 2; //Only one annotation
162 XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
163 if(annotation != null)
164 keyref.Annotation = annotation;
165 continue;
167 if(level <= 2 && reader.LocalName == "selector")
169 level = 3;
170 XmlSchemaXPath selector = XmlSchemaXPath.Read(reader,h,"selector");
171 if(selector != null)
172 keyref.Selector = selector;
173 continue;
175 if(level <= 3 && reader.LocalName == "field")
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;
185 reader.RaiseInvalidElementError();
187 return keyref;