New test.
[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                 ///<remarks>
55                 /// 1. Content must be present and one of restriction or extention
56                 ///</remarks>
57                 internal override int Compile(ValidationEventHandler h, XmlSchema schema)
58                 {
59                         // If this is already compiled this time, simply skip.
60                         if (CompilationId == schema.CompilationId)
61                                 return 0;
62
63                         if(Content == null)
64                         {
65                                 error(h, "Content must be present in a simpleContent");
66                         }
67                         else
68                         {
69 #if NET_2_0
70                                 Content.Parent = this;
71 #endif
72                                 if(Content is XmlSchemaSimpleContentRestriction)
73                                 {
74                                         XmlSchemaSimpleContentRestriction xscr = (XmlSchemaSimpleContentRestriction) Content;
75                                         errorCount += xscr.Compile(h, schema);
76                                 }
77                                 else if(Content is XmlSchemaSimpleContentExtension)
78                                 {
79                                         XmlSchemaSimpleContentExtension xsce = (XmlSchemaSimpleContentExtension) Content;
80                                         errorCount += xsce.Compile(h, schema);
81                                 }
82                                 else
83                                         error(h,"simpleContent can't have any value other than restriction or extention");
84                         }
85                         
86                         XmlSchemaUtil.CompileID(Id,this, schema.IDCollection,h);
87                         this.CompilationId = schema.CompilationId;
88                         return errorCount;
89                 }
90                 
91                 internal override int Validate(ValidationEventHandler h, XmlSchema schema)
92                 {
93                         if (IsValidated (schema.ValidationId))
94                                 return errorCount;
95
96                         errorCount += this.Content.Validate (h, schema);
97
98                         ValidationId = schema.ValidationId;
99                         return errorCount;
100                 }
101                 //<simpleContent 
102                 //  id = ID 
103                 //  {any attributes with non-schema namespace . . .}>
104                 //  Content: (annotation?, (restriction | extension))
105                 //</simpleContent>
106                 internal static XmlSchemaSimpleContent Read(XmlSchemaReader reader, ValidationEventHandler h)
107                 {
108                         XmlSchemaSimpleContent simple = new XmlSchemaSimpleContent();
109                         reader.MoveToElement();
110
111                         if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
112                         {
113                                 error(h,"Should not happen :1: XmlSchemaComplexContent.Read, name="+reader.Name,null);
114                                 reader.SkipToEnd();
115                                 return null;
116                         }
117
118                         simple.LineNumber = reader.LineNumber;
119                         simple.LinePosition = reader.LinePosition;
120                         simple.SourceUri = reader.BaseURI;
121
122                         while(reader.MoveToNextAttribute())
123                         {
124                                 if(reader.Name == "id")
125                                 {
126                                         simple.Id = reader.Value;
127                                 }
128                                 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
129                                 {
130                                         error(h,reader.Name + " is not a valid attribute for simpleContent",null);
131                                 }
132                                 else
133                                 {
134                                         XmlSchemaUtil.ReadUnhandledAttribute(reader,simple);
135                                 }
136                         }
137                         
138                         reader.MoveToElement();
139                         if(reader.IsEmptyElement)
140                                 return simple;
141                         //Content: (annotation?, (restriction | extension))
142                         int level = 1;
143                         while(reader.ReadNextElement())
144                         {
145                                 if(reader.NodeType == XmlNodeType.EndElement)
146                                 {
147                                         if(reader.LocalName != xmlname)
148                                                 error(h,"Should not happen :2: XmlSchemaSimpleContent.Read, name="+reader.Name,null);
149                                         break;
150                                 }
151                                 if(level <= 1 && reader.LocalName == "annotation")
152                                 {
153                                         level = 2; //Only one annotation
154                                         XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
155                                         if(annotation != null)
156                                                 simple.Annotation = annotation;
157                                         continue;
158                                 }
159                                 if(level <=2)
160                                 {
161                                         if(reader.LocalName == "restriction")
162                                         {
163                                                 level = 3;
164                                                 XmlSchemaSimpleContentRestriction restriction = XmlSchemaSimpleContentRestriction.Read(reader,h);
165                                                 if(restriction != null)
166                                                         simple.content = restriction;
167                                                 continue;
168                                         }
169                                         if(reader.LocalName == "extension")
170                                         {
171                                                 level = 3;
172                                                 XmlSchemaSimpleContentExtension extension = XmlSchemaSimpleContentExtension.Read(reader,h);
173                                                 if(extension != null)
174                                                         simple.content = extension;
175                                                 continue;
176                                         }
177                                 }
178                                 reader.RaiseInvalidElementError();
179                         }
180                         return simple;
181                 }
182
183         }
184 }