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; }
54 internal override void SetParent (XmlSchemaObject parent)
56 base.SetParent (parent);
58 Content.SetParent (this);
62 /// 1. Content must be present and one of restriction or extention
64 internal override int Compile(ValidationEventHandler h, XmlSchema schema)
66 // If this is already compiled this time, simply skip.
67 if (CompilationId == schema.CompilationId)
72 error(h, "Content must be present in a simpleContent");
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);
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;
95 internal override int Validate(ValidationEventHandler h, XmlSchema schema)
97 if (IsValidated (schema.ValidationId))
100 errorCount += this.Content.Validate (h, schema);
102 ValidationId = schema.ValidationId;
107 // {any attributes with non-schema namespace . . .}>
108 // Content: (annotation?, (restriction | extension))
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);
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);
138 XmlSchemaUtil.ReadUnhandledAttribute(reader,simple);
142 reader.MoveToElement();
143 if(reader.IsEmptyElement)
145 //Content: (annotation?, (restriction | extension))
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);
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;
165 if(reader.LocalName == "restriction")
168 XmlSchemaSimpleContentRestriction restriction = XmlSchemaSimpleContentRestriction.Read(reader,h);
169 if(restriction != null)
170 simple.content = restriction;
173 if(reader.LocalName == "extension")
176 XmlSchemaSimpleContentExtension extension = XmlSchemaSimpleContentExtension.Read(reader,h);
177 if(extension != null)
178 simple.content = extension;
182 reader.RaiseInvalidElementError();