1 // Author: Dwivedi, Ajay kumar
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:
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
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.
26 using System
.Xml
.Serialization
;
28 namespace System
.Xml
.Schema
31 /// Summary description for XmlSchemaKeyref.
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
51 internal XmlSchemaIdentityConstraint Target
53 get { return target; }
57 /// 1. name must be present
58 /// 2. selector and field must be present
59 /// 3. refer must be present
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");
73 internal override int Validate (ValidationEventHandler h
, XmlSchema schema
)
76 XmlSchemaIdentityConstraint target
= schema
.NamedIdentities
[this.Refer
] as XmlSchemaIdentityConstraint
;
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.");
89 internal new void error(ValidationEventHandler handle, string message)
92 ValidationHandler.RaiseValidationError(handle, this, message);
99 // {any attributes with non-schema namespace . . .}>
100 // Content: (annotation?, (selector, field+))
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);
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")
131 keyref
.refer
= XmlSchemaUtil
.ReadQNameAttribute(reader
,out innerex
);
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);
141 XmlSchemaUtil
.ReadUnhandledAttribute(reader
,keyref
);
145 reader
.MoveToElement();
146 if(reader
.IsEmptyElement
)
149 // Content: annotation?, selector, field+
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);
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
;
167 if(level
<= 2 && reader
.LocalName
== "selector")
170 XmlSchemaXPath selector
= XmlSchemaXPath
.Read(reader
,h
,"selector");
172 keyref
.Selector
= selector
;
175 if(level
<= 3 && reader
.LocalName
== "field")
178 if(keyref
.Selector
== null)
179 error(h
,"selector must be defined before field declarations",null);
180 XmlSchemaXPath field
= XmlSchemaXPath
.Read(reader
,h
,"field");
182 keyref
.Fields
.Add(field
);
185 reader
.RaiseInvalidElementError();