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