2005-01-13 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.XML / System.Xml.Schema / XmlSchemaKey.cs
index f89c00594fc6b86363a5680c0e41a088942f3bf9..a70a608c1ead77835a8e83e58160f8a8a63a683e 100755 (executable)
@@ -1,6 +1,28 @@
 // Author: Dwivedi, Ajay kumar\r
 //            Adwiv@Yahoo.com\r
+
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
 using System;\r
+using System.Xml;\r
 \r
 namespace System.Xml.Schema\r
 {\r
@@ -9,8 +31,113 @@ namespace System.Xml.Schema
        /// </summary>\r
        public class XmlSchemaKey : XmlSchemaIdentityConstraint\r
        {\r
+               const string xmlname = "key";\r
+\r
                public XmlSchemaKey()\r
                {\r
                }\r
+               /// <remarks>\r
+               /// 1. name must be present\r
+               /// 2. selector and field must be present\r
+               /// </remarks>\r
+               internal override int Compile(ValidationEventHandler h, XmlSchema schema)\r
+               {\r
+                       return base.Compile(h, schema);\r
+               }\r
+               \r
+               /*\r
+               internal new void error(ValidationEventHandler handle, string message)\r
+               {\r
+                       errorCount++;\r
+                       ValidationHandler.RaiseValidationError(handle, this, message);\r
+               }\r
+               */\r
+\r
+               //<key \r
+               //  id = ID \r
+               //  name = NCName \r
+               //  {any attributes with non-schema namespace . . .}>\r
+               //  Content: (annotation?, (selector, field+))\r
+               //</key>\r
+               internal static XmlSchemaKey Read(XmlSchemaReader reader, ValidationEventHandler h)\r
+               {\r
+                       XmlSchemaKey key = new XmlSchemaKey();\r
+                       reader.MoveToElement();\r
+\r
+                       if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)\r
+                       {\r
+                               error(h,"Should not happen :1: XmlSchemaKey.Read, name="+reader.Name,null);\r
+                               reader.Skip();\r
+                               return null;\r
+                       }\r
+\r
+                       key.LineNumber = reader.LineNumber;\r
+                       key.LinePosition = reader.LinePosition;\r
+                       key.SourceUri = reader.BaseURI;\r
+\r
+                       while(reader.MoveToNextAttribute())\r
+                       {\r
+                               if(reader.Name == "id")\r
+                               {\r
+                                       key.Id = reader.Value;\r
+                               }\r
+                               else if(reader.Name == "name")\r
+                               {\r
+                                       key.Name = reader.Value;\r
+                               }\r
+                               else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)\r
+                               {\r
+                                       error(h,reader.Name + " is not a valid attribute for key",null);\r
+                               }\r
+                               else\r
+                               {\r
+                                       XmlSchemaUtil.ReadUnhandledAttribute(reader,key);\r
+                               }\r
+                       }\r
+                       \r
+                       reader.MoveToElement();\r
+                       if(reader.IsEmptyElement)\r
+                               return key;\r
+\r
+                       //  Content: annotation?, selector, field+\r
+                       int level = 1;\r
+                       while(reader.ReadNextElement())\r
+                       {\r
+                               if(reader.NodeType == XmlNodeType.EndElement)\r
+                               {\r
+                                       if(reader.LocalName != xmlname)\r
+                                               error(h,"Should not happen :2: XmlSchemaKey.Read, name="+reader.Name,null);\r
+                                       break;\r
+                               }\r
+                               if(level <= 1 && reader.LocalName == "annotation")\r
+                               {\r
+                                       level = 2; //Only one annotation\r
+                                       XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);\r
+                                       if(annotation != null)\r
+                                               key.Annotation = annotation;\r
+                                       continue;\r
+                               }\r
+                               if(level <= 2 && reader.LocalName == "selector")\r
+                               {\r
+                                       level = 3;\r
+                                       XmlSchemaXPath selector = XmlSchemaXPath.Read(reader,h,"selector");\r
+                                       if(selector != null)\r
+                                               key.Selector = selector;\r
+                                       continue;\r
+                               }\r
+                               if(level <= 3 && reader.LocalName == "field")\r
+                               {\r
+                                       level = 3;\r
+                                       if(key.Selector == null)\r
+                                               error(h,"selector must be defined before field declarations",null);\r
+                                       XmlSchemaXPath field = XmlSchemaXPath.Read(reader,h,"field");\r
+                                       if(field != null)\r
+                                               key.Fields.Add(field);\r
+                                       continue;\r
+                               }\r
+                               reader.RaiseInvalidElementError();\r
+                       }\r
+                       return key;\r
+               }\r
        }\r
 }\r