2010-04-06 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System.XML / System.Xml.Schema / XmlSchemaSimpleContent.cs
blobb3c1502734407065d3da84f737a85915cdcbbe5f
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 //
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.
29 using System;
30 using System.Xml.Serialization;
31 using System.Xml;
33 namespace System.Xml.Schema
35 /// <summary>
36 /// Summary description for XmlSchemaSimpleContent.
37 /// </summary>
38 public class XmlSchemaSimpleContent : XmlSchemaContentModel
40 private XmlSchemaContent content;
41 const string xmlname = "simpleContent";
42 public XmlSchemaSimpleContent()
46 [XmlElement("restriction",typeof(XmlSchemaSimpleContentRestriction))]
47 [XmlElement("extension",typeof(XmlSchemaSimpleContentExtension))]
48 public override XmlSchemaContent Content
50 get{ return content; }
51 set{ content = value; }
54 internal override void SetParent (XmlSchemaObject parent)
56 base.SetParent (parent);
57 if (Content != null)
58 Content.SetParent (this);
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)
66 // If this is already compiled this time, simply skip.
67 if (CompilationId == schema.CompilationId)
68 return 0;
70 if(Content == null)
72 error(h, "Content must be present in a simpleContent");
74 else
76 if(Content is XmlSchemaSimpleContentRestriction)
78 XmlSchemaSimpleContentRestriction xscr = (XmlSchemaSimpleContentRestriction) Content;
79 errorCount += xscr.Compile(h, schema);
81 else if(Content is XmlSchemaSimpleContentExtension)
83 XmlSchemaSimpleContentExtension xsce = (XmlSchemaSimpleContentExtension) Content;
84 errorCount += xsce.Compile(h, schema);
86 else
87 error(h,"simpleContent can't have any value other than restriction or extention");
90 XmlSchemaUtil.CompileID(Id,this, schema.IDCollection,h);
91 this.CompilationId = schema.CompilationId;
92 return errorCount;
95 internal override int Validate(ValidationEventHandler h, XmlSchema schema)
97 if (IsValidated (schema.ValidationId))
98 return errorCount;
100 errorCount += this.Content.Validate (h, schema);
102 ValidationId = schema.ValidationId;
103 return errorCount;
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)
112 XmlSchemaSimpleContent simple = new XmlSchemaSimpleContent();
113 reader.MoveToElement();
115 if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
117 error(h,"Should not happen :1: XmlSchemaComplexContent.Read, name="+reader.Name,null);
118 reader.SkipToEnd();
119 return null;
122 simple.LineNumber = reader.LineNumber;
123 simple.LinePosition = reader.LinePosition;
124 simple.SourceUri = reader.BaseURI;
126 while(reader.MoveToNextAttribute())
128 if(reader.Name == "id")
130 simple.Id = reader.Value;
132 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
134 error(h,reader.Name + " is not a valid attribute for simpleContent",null);
136 else
138 XmlSchemaUtil.ReadUnhandledAttribute(reader,simple);
142 reader.MoveToElement();
143 if(reader.IsEmptyElement)
144 return simple;
145 //Content: (annotation?, (restriction | extension))
146 int level = 1;
147 while(reader.ReadNextElement())
149 if(reader.NodeType == XmlNodeType.EndElement)
151 if(reader.LocalName != xmlname)
152 error(h,"Should not happen :2: XmlSchemaSimpleContent.Read, name="+reader.Name,null);
153 break;
155 if(level <= 1 && reader.LocalName == "annotation")
157 level = 2; //Only one annotation
158 XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
159 if(annotation != null)
160 simple.Annotation = annotation;
161 continue;
163 if(level <=2)
165 if(reader.LocalName == "restriction")
167 level = 3;
168 XmlSchemaSimpleContentRestriction restriction = XmlSchemaSimpleContentRestriction.Read(reader,h);
169 if(restriction != null)
170 simple.content = restriction;
171 continue;
173 if(reader.LocalName == "extension")
175 level = 3;
176 XmlSchemaSimpleContentExtension extension = XmlSchemaSimpleContentExtension.Read(reader,h);
177 if(extension != null)
178 simple.content = extension;
179 continue;
182 reader.RaiseInvalidElementError();
184 return simple;