1 // Author: Dwivedi, Ajay kumar
5 // Permission is hereby granted, free of charge, to any person obtaining
6 // a copy of this software and associated documentation files (the
7 // "Software"), to deal in the Software without restriction, including
8 // without limitation the rights to use, copy, modify, merge, publish,
9 // distribute, sublicense, and/or sell copies of the Software, and to
10 // permit persons to whom the Software is furnished to do so, subject to
11 // the following conditions:
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 using System.Collections;
27 using System.Xml.Serialization;
29 namespace System.Xml.Schema
32 /// Summary description for XmlSchemaSimpleTypeUnion.
34 public class XmlSchemaSimpleTypeUnion : XmlSchemaSimpleTypeContent
36 private XmlSchemaObjectCollection baseTypes;
37 private XmlQualifiedName[] memberTypes;
38 const string xmlname = "union";
39 private object [] validatedTypes;
41 private XmlSchemaSimpleType [] validatedSchemaTypes;
44 public XmlSchemaSimpleTypeUnion()
46 baseTypes = new XmlSchemaObjectCollection();
49 [XmlElement("simpleType",typeof(XmlSchemaSimpleType))]
50 public XmlSchemaObjectCollection BaseTypes
52 get{ return baseTypes; }
55 [System.Xml.Serialization.XmlAttribute("memberTypes")]
56 public XmlQualifiedName[] MemberTypes
58 get{ return memberTypes; }
59 set{ memberTypes = value; }
64 public XmlSchemaSimpleType [] BaseMemberTypes {
65 get { return validatedSchemaTypes; }
69 internal object [] ValidatedTypes
71 get { return validatedTypes; }
75 /// 1. Circular union type definition is disallowed. (WTH is this?)
76 /// 2. id must be a valid ID
78 internal override int Compile(ValidationEventHandler h, XmlSchema schema)
80 // If this is already compiled this time, simply skip.
81 if (CompilationId == schema.CompilationId)
85 foreach (XmlSchemaObject obj in BaseTypes)
91 int count = BaseTypes.Count;
93 foreach(XmlSchemaObject obj in baseTypes)
95 if(obj != null && obj is XmlSchemaSimpleType)
97 XmlSchemaSimpleType stype = (XmlSchemaSimpleType) obj;
98 errorCount += stype.Compile(h, schema);
102 error(h, "baseTypes can't have objects other than a simpletype");
106 if(memberTypes!=null)
108 for(int i=0; i< memberTypes.Length; i++)
110 if(memberTypes[i] == null || !XmlSchemaUtil.CheckQName(MemberTypes[i]))
112 error (h,"Invalid membertype");
113 memberTypes[i] = XmlQualifiedName.Empty;
117 count += MemberTypes.Length;
123 error(h, "Atleast one simpletype or membertype must be present");
125 XmlSchemaUtil.CompileID(Id,this,schema.IDCollection,h);
129 this.CompilationId = schema.CompilationId;
133 internal override int Validate(ValidationEventHandler h, XmlSchema schema)
135 if (IsValidated (schema.ValidationId))
138 ArrayList al = new ArrayList ();
139 // Validate MemberTypes
140 if (MemberTypes != null) {
141 foreach (XmlQualifiedName memberTypeName in MemberTypes) {
143 XmlSchemaType xstype = schema.FindSchemaType (memberTypeName) as XmlSchemaSimpleType;
144 if (xstype != null) {
145 errorCount += xstype.Validate (h, schema);
147 } else if (memberTypeName == XmlSchemaComplexType.AnyTypeName) {
148 type = XmlSchemaSimpleType.AnySimpleType;
149 } else if (memberTypeName.Namespace == XmlSchema.Namespace ||
150 memberTypeName.Namespace == XmlSchema.XdtNamespace) {
151 type = XmlSchemaDatatype.FromName (memberTypeName);
153 error (h, "Invalid schema type name was specified: " + memberTypeName);
155 // otherwise, it might be missing sub components.
156 else if (!schema.IsNamespaceAbsent (memberTypeName.Namespace))
157 error (h, "Referenced base schema type " + memberTypeName + " was not found in the corresponding schema.");
162 if (BaseTypes != null) {
163 foreach (XmlSchemaSimpleType st in BaseTypes) {
164 st.Validate (h, schema);
168 this.validatedTypes = al.ToArray ();
171 if (validatedTypes != null) {
172 validatedSchemaTypes = new XmlSchemaSimpleType [validatedTypes.Length];
173 for (int i = 0; i < validatedTypes.Length; i++) {
174 object t = validatedTypes [i];
175 XmlSchemaSimpleType st = t as XmlSchemaSimpleType;
176 if (st == null && t != null)
177 st = XmlSchemaType.GetBuiltInSimpleType (((XmlSchemaDatatype) t).TypeCode);
178 validatedSchemaTypes [i] = st;
183 ValidationId = schema.ValidationId;
189 // memberTypes = List of QName
190 // {any attributes with non-schema namespace . . .}>
191 // Content: (annotation?, (simpleType*))
193 internal static XmlSchemaSimpleTypeUnion Read(XmlSchemaReader reader, ValidationEventHandler h)
195 XmlSchemaSimpleTypeUnion union = new XmlSchemaSimpleTypeUnion();
196 reader.MoveToElement();
198 if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
200 error(h,"Should not happen :1: XmlSchemaSimpleTypeUnion.Read, name="+reader.Name,null);
205 union.LineNumber = reader.LineNumber;
206 union.LinePosition = reader.LinePosition;
207 union.SourceUri = reader.BaseURI;
210 while(reader.MoveToNextAttribute())
212 if(reader.Name == "id")
214 union.Id = reader.Value;
216 else if(reader.Name == "memberTypes")
219 string[] names = XmlSchemaUtil.SplitList(reader.Value);
220 union.memberTypes = new XmlQualifiedName[names.Length];
221 for(int i=0;i<names.Length;i++)
223 union.memberTypes[i] = XmlSchemaUtil.ToQName(reader,names[i],out innerEx);
225 error(h,"'"+names[i] + "' is not a valid memberType",innerEx);
228 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
230 error(h,reader.Name + " is not a valid attribute for union",null);
234 XmlSchemaUtil.ReadUnhandledAttribute(reader,union);
238 reader.MoveToElement();
239 if(reader.IsEmptyElement)
242 // Content: annotation?, simpleType*
244 while(reader.ReadNextElement())
246 if(reader.NodeType == XmlNodeType.EndElement)
248 if(reader.LocalName != xmlname)
249 error(h,"Should not happen :2: XmlSchemaSimpleTypeUnion.Read, name="+reader.Name,null);
252 if(level <= 1 && reader.LocalName == "annotation")
254 level = 2; //Only one annotation
255 XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
256 if(annotation != null)
257 union.Annotation = annotation;
260 if(level <=2 && reader.LocalName == "simpleType")
263 XmlSchemaSimpleType stype = XmlSchemaSimpleType.Read(reader,h);
265 union.baseTypes.Add(stype);
268 reader.RaiseInvalidElementError();