New test.
[mono.git] / mcs / class / System.XML / System.Xml.Schema / XmlSchemaSimpleContent.cs
1 //\r
2 // System.Xml.Schema.XmlSchemaSimpleContent.cs\r
3 //\r
4 // Author:\r
5 //      Dwivedi, Ajay kumar  Adwiv@Yahoo.com\r
6 //      Atsushi Enomoto  ginga@kit.hi-ho.ne.jp\r
7 //\r
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;\r
30 using System.Xml.Serialization;\r
31 using System.Xml;\r
32 \r
33 namespace System.Xml.Schema\r
34 {\r
35         /// <summary>\r
36         /// Summary description for XmlSchemaSimpleContent.\r
37         /// </summary>\r
38         public class XmlSchemaSimpleContent : XmlSchemaContentModel\r
39         {\r
40                 private XmlSchemaContent content;\r
41                 const string xmlname = "simpleContent";\r
42                 public XmlSchemaSimpleContent()\r
43                 {\r
44                 }\r
45 \r
46                 [XmlElement("restriction",typeof(XmlSchemaSimpleContentRestriction))]\r
47                 [XmlElement("extension",typeof(XmlSchemaSimpleContentExtension))]\r
48                 public override XmlSchemaContent Content \r
49                 {\r
50                         get{ return  content; } \r
51                         set{ content = value; }\r
52                 }\r
53 \r
54                 ///<remarks>\r
55                 /// 1. Content must be present and one of restriction or extention\r
56                 ///</remarks>\r
57                 internal override int Compile(ValidationEventHandler h, XmlSchema schema)\r
58                 {\r
59                         // If this is already compiled this time, simply skip.\r
60                         if (CompilationId == schema.CompilationId)\r
61                                 return 0;\r
62 \r
63                         if(Content == null)\r
64                         {\r
65                                 error(h, "Content must be present in a simpleContent");\r
66                         }\r
67                         else\r
68                         {\r
69 #if NET_2_0\r
70                                 Content.Parent = this;\r
71 #endif\r
72                                 if(Content is XmlSchemaSimpleContentRestriction)\r
73                                 {\r
74                                         XmlSchemaSimpleContentRestriction xscr = (XmlSchemaSimpleContentRestriction) Content;\r
75                                         errorCount += xscr.Compile(h, schema);\r
76                                 }\r
77                                 else if(Content is XmlSchemaSimpleContentExtension)\r
78                                 {\r
79                                         XmlSchemaSimpleContentExtension xsce = (XmlSchemaSimpleContentExtension) Content;\r
80                                         errorCount += xsce.Compile(h, schema);\r
81                                 }\r
82                                 else\r
83                                         error(h,"simpleContent can't have any value other than restriction or extention");\r
84                         }\r
85                         \r
86                         XmlSchemaUtil.CompileID(Id,this, schema.IDCollection,h);\r
87                         this.CompilationId = schema.CompilationId;\r
88                         return errorCount;\r
89                 }\r
90                 \r
91                 internal override int Validate(ValidationEventHandler h, XmlSchema schema)\r
92                 {\r
93                         if (IsValidated (schema.ValidationId))\r
94                                 return errorCount;\r
95 \r
96                         errorCount += this.Content.Validate (h, schema);\r
97 \r
98                         ValidationId = schema.ValidationId;\r
99                         return errorCount;\r
100                 }\r
101                 //<simpleContent \r
102                 //  id = ID \r
103                 //  {any attributes with non-schema namespace . . .}>\r
104                 //  Content: (annotation?, (restriction | extension))\r
105                 //</simpleContent>\r
106                 internal static XmlSchemaSimpleContent Read(XmlSchemaReader reader, ValidationEventHandler h)\r
107                 {\r
108                         XmlSchemaSimpleContent simple = new XmlSchemaSimpleContent();\r
109                         reader.MoveToElement();\r
110 \r
111                         if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)\r
112                         {\r
113                                 error(h,"Should not happen :1: XmlSchemaComplexContent.Read, name="+reader.Name,null);\r
114                                 reader.SkipToEnd();\r
115                                 return null;\r
116                         }\r
117 \r
118                         simple.LineNumber = reader.LineNumber;\r
119                         simple.LinePosition = reader.LinePosition;\r
120                         simple.SourceUri = reader.BaseURI;\r
121 \r
122                         while(reader.MoveToNextAttribute())\r
123                         {\r
124                                 if(reader.Name == "id")\r
125                                 {\r
126                                         simple.Id = reader.Value;\r
127                                 }\r
128                                 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)\r
129                                 {\r
130                                         error(h,reader.Name + " is not a valid attribute for simpleContent",null);\r
131                                 }\r
132                                 else\r
133                                 {\r
134                                         XmlSchemaUtil.ReadUnhandledAttribute(reader,simple);\r
135                                 }\r
136                         }\r
137                         \r
138                         reader.MoveToElement();\r
139                         if(reader.IsEmptyElement)\r
140                                 return simple;\r
141                         //Content: (annotation?, (restriction | extension))\r
142                         int level = 1;\r
143                         while(reader.ReadNextElement())\r
144                         {\r
145                                 if(reader.NodeType == XmlNodeType.EndElement)\r
146                                 {\r
147                                         if(reader.LocalName != xmlname)\r
148                                                 error(h,"Should not happen :2: XmlSchemaSimpleContent.Read, name="+reader.Name,null);\r
149                                         break;\r
150                                 }\r
151                                 if(level <= 1 && reader.LocalName == "annotation")\r
152                                 {\r
153                                         level = 2; //Only one annotation\r
154                                         XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);\r
155                                         if(annotation != null)\r
156                                                 simple.Annotation = annotation;\r
157                                         continue;\r
158                                 }\r
159                                 if(level <=2)\r
160                                 {\r
161                                         if(reader.LocalName == "restriction")\r
162                                         {\r
163                                                 level = 3;\r
164                                                 XmlSchemaSimpleContentRestriction restriction = XmlSchemaSimpleContentRestriction.Read(reader,h);\r
165                                                 if(restriction != null)\r
166                                                         simple.content = restriction;\r
167                                                 continue;\r
168                                         }\r
169                                         if(reader.LocalName == "extension")\r
170                                         {\r
171                                                 level = 3;\r
172                                                 XmlSchemaSimpleContentExtension extension = XmlSchemaSimpleContentExtension.Read(reader,h);\r
173                                                 if(extension != null)\r
174                                                         simple.content = extension;\r
175                                                 continue;\r
176                                         }\r
177                                 }\r
178                                 reader.RaiseInvalidElementError();\r
179                         }\r
180                         return simple;\r
181                 }\r
182 \r
183         }\r
184 }\r