2 // System.Xml.Schema.XmlSchemaSimpleContent.cs
5 // Dwivedi, Ajay kumar Adwiv@Yahoo.com
6 // Atsushi Enomoto ginga@kit.hi-ho.ne.jp
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
30 using System.Xml.Serialization;
33 namespace System.Xml.Schema
36 /// Summary description for XmlSchemaSimpleContent.
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; }
55 /// 1. Content must be present and one of restriction or extention
57 internal override int Compile(ValidationEventHandler h, XmlSchema schema)
59 // If this is already compiled this time, simply skip.
60 if (CompilationId == schema.CompilationId)
65 error(h, "Content must be present in a simpleContent");
70 Content.Parent = this;
72 if(Content is XmlSchemaSimpleContentRestriction)
74 XmlSchemaSimpleContentRestriction xscr = (XmlSchemaSimpleContentRestriction) Content;
75 errorCount += xscr.Compile(h, schema);
77 else if(Content is XmlSchemaSimpleContentExtension)
79 XmlSchemaSimpleContentExtension xsce = (XmlSchemaSimpleContentExtension) Content;
80 errorCount += xsce.Compile(h, schema);
83 error(h,"simpleContent can't have any value other than restriction or extention");
86 XmlSchemaUtil.CompileID(Id,this, schema.IDCollection,h);
87 this.CompilationId = schema.CompilationId;
91 internal override int Validate(ValidationEventHandler h, XmlSchema schema)
93 if (IsValidated (schema.ValidationId))
96 errorCount += this.Content.Validate (h, schema);
98 ValidationId = schema.ValidationId;
103 // {any attributes with non-schema namespace . . .}>
104 // Content: (annotation?, (restriction | extension))
106 internal static XmlSchemaSimpleContent Read(XmlSchemaReader reader, ValidationEventHandler h)
108 XmlSchemaSimpleContent simple = new XmlSchemaSimpleContent();
109 reader.MoveToElement();
111 if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
113 error(h,"Should not happen :1: XmlSchemaComplexContent.Read, name="+reader.Name,null);
118 simple.LineNumber = reader.LineNumber;
119 simple.LinePosition = reader.LinePosition;
120 simple.SourceUri = reader.BaseURI;
122 while(reader.MoveToNextAttribute())
124 if(reader.Name == "id")
126 simple.Id = reader.Value;
128 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
130 error(h,reader.Name + " is not a valid attribute for simpleContent",null);
134 XmlSchemaUtil.ReadUnhandledAttribute(reader,simple);
138 reader.MoveToElement();
139 if(reader.IsEmptyElement)
141 //Content: (annotation?, (restriction | extension))
143 while(reader.ReadNextElement())
145 if(reader.NodeType == XmlNodeType.EndElement)
147 if(reader.LocalName != xmlname)
148 error(h,"Should not happen :2: XmlSchemaSimpleContent.Read, name="+reader.Name,null);
151 if(level <= 1 && reader.LocalName == "annotation")
153 level = 2; //Only one annotation
154 XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
155 if(annotation != null)
156 simple.Annotation = annotation;
161 if(reader.LocalName == "restriction")
164 XmlSchemaSimpleContentRestriction restriction = XmlSchemaSimpleContentRestriction.Read(reader,h);
165 if(restriction != null)
166 simple.content = restriction;
169 if(reader.LocalName == "extension")
172 XmlSchemaSimpleContentExtension extension = XmlSchemaSimpleContentExtension.Read(reader,h);
173 if(extension != null)
174 simple.content = extension;
178 reader.RaiseInvalidElementError();