Add MIT license to System.XML.DLL
[mono.git] / mcs / class / System.XML / System.Xml.Schema / XmlSchemaAnnotation.cs
1 // Author: Dwivedi, Ajay kumar\r
2 //            Adwiv@Yahoo.com\r
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;\r
25 using System.Collections;\r
26 using System.Xml;\r
27 using System.Xml.Serialization;\r
28 \r
29 namespace System.Xml.Schema\r
30 {\r
31         /// <summary>\r
32         /// Summary description for XmlSchemaAnnotation.\r
33         /// </summary>\r
34         public class XmlSchemaAnnotation : XmlSchemaObject\r
35         {\r
36                 private string id;\r
37                 private XmlSchemaObjectCollection items;\r
38                 private XmlAttribute[] unhandledAttributes;\r
39                 const string xmlname = "annotation";\r
40 \r
41                 public XmlSchemaAnnotation()\r
42                 {\r
43                         items = new XmlSchemaObjectCollection();\r
44                 }\r
45 \r
46                 [System.Xml.Serialization.XmlAttribute("id")]\r
47                 public string Id \r
48                 {\r
49                         get{ return  id; } \r
50                         set{ id = value; }\r
51                 }\r
52                 \r
53                 [XmlElement("appinfo",typeof(XmlSchemaAppInfo),Namespace=XmlSchema.Namespace)]\r
54                 [XmlElement("documentation",typeof(XmlSchemaDocumentation),Namespace=XmlSchema.Namespace)]\r
55                 public XmlSchemaObjectCollection Items\r
56                 {\r
57                         get{ return items; }\r
58                 }\r
59                 \r
60                 [XmlAnyAttribute]\r
61                 public XmlAttribute[] UnhandledAttributes \r
62                 {\r
63                         get\r
64                         {\r
65                                 if(unhandledAttributeList != null)\r
66                                 {\r
67                                         unhandledAttributes = (XmlAttribute[]) unhandledAttributeList.ToArray(typeof(XmlAttribute));\r
68                                         unhandledAttributeList = null;\r
69                                 }\r
70                                 return unhandledAttributes;\r
71                         }\r
72                         set\r
73                         { \r
74                                 unhandledAttributes = value; \r
75                                 unhandledAttributeList = null;\r
76                         }\r
77                 }\r
78 \r
79                 internal override int Compile(ValidationEventHandler h, XmlSchema schema)\r
80                 {\r
81                         // If this is already compiled this time, simply skip.\r
82                         if (this.IsComplied (schema.CompilationId))\r
83                                 return 0;\r
84 \r
85                         this.CompilationId = schema.CompilationId;\r
86                         return 0;\r
87                 }\r
88                 \r
89                 internal override int Validate(ValidationEventHandler h, XmlSchema schema)\r
90                 {\r
91                         return 0;\r
92                 }\r
93 \r
94                 //<annotation\r
95                 //  id = ID\r
96                 //  {any attributes with non-schema namespace . . .}>\r
97                 //  Content: (appinfo | documentation)*\r
98                 //</annotation>\r
99                 internal static XmlSchemaAnnotation Read(XmlSchemaReader reader, ValidationEventHandler h)\r
100                 {\r
101                         XmlSchemaAnnotation annotation = new XmlSchemaAnnotation();\r
102                         reader.MoveToElement();\r
103 \r
104                         if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)\r
105                         {\r
106                                 error(h,"Should not happen :1: XmlSchemaAnnotation.Read, name="+reader.Name,null);\r
107                                 reader.SkipToEnd();\r
108                                 return null;\r
109                         }\r
110 \r
111                         annotation.LineNumber = reader.LineNumber;\r
112                         annotation.LinePosition = reader.LinePosition;\r
113                         annotation.SourceUri = reader.BaseURI;\r
114 \r
115                         //Read Attributes\r
116                         while(reader.MoveToNextAttribute())\r
117                         {\r
118                                 if(reader.Name == "id")\r
119                                 {\r
120                                         annotation.Id = reader.Value;\r
121                                 }\r
122                                 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)\r
123                                 {\r
124                                         error(h,reader.Name + " is not a valid attribute for annotation",null);\r
125                                 }\r
126                                 else\r
127                                 {\r
128                                         XmlSchemaUtil.ReadUnhandledAttribute(reader,annotation);\r
129                                 }\r
130                         }\r
131                         \r
132                         reader.MoveToElement();\r
133                         if(reader.IsEmptyElement)\r
134                                 return annotation;\r
135 \r
136                         //Content: (appinfo | documentation)*\r
137                         bool skip = false;\r
138                         string expectedEnd = null;\r
139                         while(!reader.EOF)\r
140                         {\r
141                                 if(skip) \r
142                                         skip=false;\r
143                                 else \r
144                                         reader.ReadNextElement();\r
145 \r
146                                 if(reader.NodeType == XmlNodeType.EndElement)\r
147                                 {\r
148                                         bool end = true;\r
149                                         string expected = xmlname;\r
150                                         if(expectedEnd != null)\r
151                                         {\r
152                                                 expected = expectedEnd;\r
153                                                 expectedEnd = null;\r
154                                                 end = false;\r
155                                         }\r
156                                         if(reader.LocalName != expected)\r
157                                                 error(h,"Should not happen :2: XmlSchemaAnnotation.Read, name="+reader.Name+",expected="+expected,null);\r
158                                         if (end)\r
159                                                 break;\r
160                                         else\r
161                                                 continue;\r
162                                 }\r
163                                 if(reader.LocalName == "appinfo")\r
164                                 {\r
165                                         XmlSchemaAppInfo appinfo = XmlSchemaAppInfo.Read(reader,h,out skip);\r
166                                         if(appinfo != null)\r
167                                                 annotation.items.Add(appinfo);\r
168                                         continue;\r
169                                 }\r
170                                 if(reader.LocalName == "documentation")\r
171                                 {\r
172                                         XmlSchemaDocumentation documentation = XmlSchemaDocumentation.Read(reader,h, out skip);\r
173                                         if(documentation != null)\r
174                                                 annotation.items.Add(documentation);\r
175                                         continue;\r
176                                 }\r
177                                 reader.RaiseInvalidElementError();\r
178                         }\r
179                         return annotation;\r
180                 }\r
181         }\r
182 }\r