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