Removal of NET_1_1 defines and some NET_2_0; Both defines are true these days in...
[mono.git] / mcs / class / System.XML / System.Xml.Schema / XmlSchemaSimpleContent.cs
1 //
2 // System.Xml.Schema.XmlSchemaSimpleContent.cs
3 //
4 // Author:
5 //      Dwivedi, Ajay kumar  Adwiv@Yahoo.com
6 //      Atsushi Enomoto  ginga@kit.hi-ho.ne.jp
7 //
8
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29 using System;
30 using System.Xml.Serialization;
31 using System.Xml;
32
33 namespace System.Xml.Schema
34 {
35         /// <summary>
36         /// Summary description for XmlSchemaSimpleContent.
37         /// </summary>
38         public class XmlSchemaSimpleContent : XmlSchemaContentModel
39         {
40                 private XmlSchemaContent content;
41                 const string xmlname = "simpleContent";
42                 public XmlSchemaSimpleContent()
43                 {
44                 }
45
46                 [XmlElement("restriction",typeof(XmlSchemaSimpleContentRestriction))]
47                 [XmlElement("extension",typeof(XmlSchemaSimpleContentExtension))]
48                 public override XmlSchemaContent Content 
49                 {
50                         get{ return  content; } 
51                         set{ content = value; }
52                 }
53
54                 internal override void SetParent (XmlSchemaObject parent)
55                 {
56                         base.SetParent (parent);
57                         if (Content != null)
58                                 Content.SetParent (this);
59                 }
60
61                 ///<remarks>
62                 /// 1. Content must be present and one of restriction or extention
63                 ///</remarks>
64                 internal override int Compile(ValidationEventHandler h, XmlSchema schema)
65                 {
66                         // If this is already compiled this time, simply skip.
67                         if (CompilationId == schema.CompilationId)
68                                 return 0;
69
70                         if(Content == null)
71                         {
72                                 error(h, "Content must be present in a simpleContent");
73                         }
74                         else
75                         {
76                                 if(Content is XmlSchemaSimpleContentRestriction)
77                                 {
78                                         XmlSchemaSimpleContentRestriction xscr = (XmlSchemaSimpleContentRestriction) Content;
79                                         errorCount += xscr.Compile(h, schema);
80                                 }
81                                 else if(Content is XmlSchemaSimpleContentExtension)
82                                 {
83                                         XmlSchemaSimpleContentExtension xsce = (XmlSchemaSimpleContentExtension) Content;
84                                         errorCount += xsce.Compile(h, schema);
85                                 }
86                                 else
87                                         error(h,"simpleContent can't have any value other than restriction or extention");
88                         }
89                         
90                         XmlSchemaUtil.CompileID(Id,this, schema.IDCollection,h);
91                         this.CompilationId = schema.CompilationId;
92                         return errorCount;
93                 }
94                 
95                 internal override int Validate(ValidationEventHandler h, XmlSchema schema)
96                 {
97                         if (IsValidated (schema.ValidationId))
98                                 return errorCount;
99
100                         errorCount += this.Content.Validate (h, schema);
101
102                         ValidationId = schema.ValidationId;
103                         return errorCount;
104                 }
105                 //<simpleContent 
106                 //  id = ID 
107                 //  {any attributes with non-schema namespace . . .}>
108                 //  Content: (annotation?, (restriction | extension))
109                 //</simpleContent>
110                 internal static XmlSchemaSimpleContent Read(XmlSchemaReader reader, ValidationEventHandler h)
111                 {
112                         XmlSchemaSimpleContent simple = new XmlSchemaSimpleContent();
113                         reader.MoveToElement();
114
115                         if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
116                         {
117                                 error(h,"Should not happen :1: XmlSchemaComplexContent.Read, name="+reader.Name,null);
118                                 reader.SkipToEnd();
119                                 return null;
120                         }
121
122                         simple.LineNumber = reader.LineNumber;
123                         simple.LinePosition = reader.LinePosition;
124                         simple.SourceUri = reader.BaseURI;
125
126                         while(reader.MoveToNextAttribute())
127                         {
128                                 if(reader.Name == "id")
129                                 {
130                                         simple.Id = reader.Value;
131                                 }
132                                 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
133                                 {
134                                         error(h,reader.Name + " is not a valid attribute for simpleContent",null);
135                                 }
136                                 else
137                                 {
138                                         XmlSchemaUtil.ReadUnhandledAttribute(reader,simple);
139                                 }
140                         }
141                         
142                         reader.MoveToElement();
143                         if(reader.IsEmptyElement)
144                                 return simple;
145                         //Content: (annotation?, (restriction | extension))
146                         int level = 1;
147                         while(reader.ReadNextElement())
148                         {
149                                 if(reader.NodeType == XmlNodeType.EndElement)
150                                 {
151                                         if(reader.LocalName != xmlname)
152                                                 error(h,"Should not happen :2: XmlSchemaSimpleContent.Read, name="+reader.Name,null);
153                                         break;
154                                 }
155                                 if(level <= 1 && reader.LocalName == "annotation")
156                                 {
157                                         level = 2; //Only one annotation
158                                         XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
159                                         if(annotation != null)
160                                                 simple.Annotation = annotation;
161                                         continue;
162                                 }
163                                 if(level <=2)
164                                 {
165                                         if(reader.LocalName == "restriction")
166                                         {
167                                                 level = 3;
168                                                 XmlSchemaSimpleContentRestriction restriction = XmlSchemaSimpleContentRestriction.Read(reader,h);
169                                                 if(restriction != null)
170                                                         simple.content = restriction;
171                                                 continue;
172                                         }
173                                         if(reader.LocalName == "extension")
174                                         {
175                                                 level = 3;
176                                                 XmlSchemaSimpleContentExtension extension = XmlSchemaSimpleContentExtension.Read(reader,h);
177                                                 if(extension != null)
178                                                         simple.content = extension;
179                                                 continue;
180                                         }
181                                 }
182                                 reader.RaiseInvalidElementError();
183                         }
184                         return simple;
185                 }
186
187         }
188 }