2010-04-06 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System.XML / System.Xml.Schema / XmlSchemaSimpleTypeUnion.cs
blobf4d1c20759f7b5bc295b2e58cc0a9df5404121e0
1 // Author: Dwivedi, Ajay kumar
2 // Adwiv@Yahoo.com
4 //
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:
12 //
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
15 //
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.
24 using System;
25 using System.Collections;
26 using System.Xml;
27 using System.Xml.Serialization;
29 namespace System.Xml.Schema
31 /// <summary>
32 /// Summary description for XmlSchemaSimpleTypeUnion.
33 /// </summary>
34 public class XmlSchemaSimpleTypeUnion : XmlSchemaSimpleTypeContent
36 private XmlSchemaObjectCollection baseTypes;
37 private XmlQualifiedName[] memberTypes;
38 const string xmlname = "union";
39 private object [] validatedTypes;
40 #if NET_2_0
41 private XmlSchemaSimpleType [] validatedSchemaTypes;
42 #endif
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; }
62 #if NET_2_0
63 [XmlIgnore]
64 public XmlSchemaSimpleType [] BaseMemberTypes {
65 get { return validatedSchemaTypes; }
67 #endif
69 internal object [] ValidatedTypes
71 get { return validatedTypes; }
74 internal override void SetParent (XmlSchemaObject parent)
76 base.SetParent (parent);
77 foreach (XmlSchemaObject obj in BaseTypes)
78 obj.SetParent (this);
81 /// <remarks>
82 /// 1. Circular union type definition is disallowed. (WTH is this?)
83 /// 2. id must be a valid ID
84 /// </remarks>
85 internal override int Compile(ValidationEventHandler h, XmlSchema schema)
87 // If this is already compiled this time, simply skip.
88 if (CompilationId == schema.CompilationId)
89 return 0;
91 errorCount = 0;
93 int count = BaseTypes.Count;
95 foreach(XmlSchemaObject obj in baseTypes)
97 if(obj != null && obj is XmlSchemaSimpleType)
99 XmlSchemaSimpleType stype = (XmlSchemaSimpleType) obj;
100 errorCount += stype.Compile(h, schema);
102 else
104 error(h, "baseTypes can't have objects other than a simpletype");
108 if(memberTypes!=null)
110 for(int i=0; i< memberTypes.Length; i++)
112 if(memberTypes[i] == null || !XmlSchemaUtil.CheckQName(MemberTypes[i]))
114 error (h,"Invalid membertype");
115 memberTypes[i] = XmlQualifiedName.Empty;
117 else
119 count += MemberTypes.Length;
124 if(count == 0)
125 error(h, "Atleast one simpletype or membertype must be present");
127 XmlSchemaUtil.CompileID(Id,this,schema.IDCollection,h);
131 this.CompilationId = schema.CompilationId;
132 return errorCount;
135 internal override int Validate(ValidationEventHandler h, XmlSchema schema)
137 if (IsValidated (schema.ValidationId))
138 return errorCount;
140 ArrayList al = new ArrayList ();
141 // Validate MemberTypes
142 if (MemberTypes != null) {
143 foreach (XmlQualifiedName memberTypeName in MemberTypes) {
144 object type = null;
145 XmlSchemaType xstype = schema.FindSchemaType (memberTypeName) as XmlSchemaSimpleType;
146 if (xstype != null) {
147 errorCount += xstype.Validate (h, schema);
148 type = xstype;
149 } else if (memberTypeName == XmlSchemaComplexType.AnyTypeName) {
150 type = XmlSchemaSimpleType.AnySimpleType;
151 } else if (memberTypeName.Namespace == XmlSchema.Namespace ||
152 memberTypeName.Namespace == XmlSchema.XdtNamespace) {
153 type = XmlSchemaDatatype.FromName (memberTypeName);
154 if (type == null)
155 error (h, "Invalid schema type name was specified: " + memberTypeName);
157 // otherwise, it might be missing sub components.
158 else if (!schema.IsNamespaceAbsent (memberTypeName.Namespace))
159 error (h, "Referenced base schema type " + memberTypeName + " was not found in the corresponding schema.");
161 al.Add (type);
164 if (BaseTypes != null) {
165 foreach (XmlSchemaSimpleType st in BaseTypes) {
166 st.Validate (h, schema);
167 al.Add (st);
170 this.validatedTypes = al.ToArray ();
172 #if NET_2_0
173 if (validatedTypes != null) {
174 validatedSchemaTypes = new XmlSchemaSimpleType [validatedTypes.Length];
175 for (int i = 0; i < validatedTypes.Length; i++) {
176 object t = validatedTypes [i];
177 XmlSchemaSimpleType st = t as XmlSchemaSimpleType;
178 if (st == null && t != null)
179 st = XmlSchemaType.GetBuiltInSimpleType (((XmlSchemaDatatype) t).TypeCode);
180 validatedSchemaTypes [i] = st;
183 #endif
185 ValidationId = schema.ValidationId;
186 return errorCount;
189 //<union
190 // id = ID
191 // memberTypes = List of QName
192 // {any attributes with non-schema namespace . . .}>
193 // Content: (annotation?, (simpleType*))
194 //</union>
195 internal static XmlSchemaSimpleTypeUnion Read(XmlSchemaReader reader, ValidationEventHandler h)
197 XmlSchemaSimpleTypeUnion union = new XmlSchemaSimpleTypeUnion();
198 reader.MoveToElement();
200 if(reader.NamespaceURI != XmlSchema.Namespace || reader.LocalName != xmlname)
202 error(h,"Should not happen :1: XmlSchemaSimpleTypeUnion.Read, name="+reader.Name,null);
203 reader.Skip();
204 return null;
207 union.LineNumber = reader.LineNumber;
208 union.LinePosition = reader.LinePosition;
209 union.SourceUri = reader.BaseURI;
211 //Read Attributes
212 while(reader.MoveToNextAttribute())
214 if(reader.Name == "id")
216 union.Id = reader.Value;
218 else if(reader.Name == "memberTypes")
220 Exception innerEx;
221 string[] names = XmlSchemaUtil.SplitList(reader.Value);
222 union.memberTypes = new XmlQualifiedName[names.Length];
223 for(int i=0;i<names.Length;i++)
225 union.memberTypes[i] = XmlSchemaUtil.ToQName(reader,names[i],out innerEx);
226 if(innerEx != null)
227 error(h,"'"+names[i] + "' is not a valid memberType",innerEx);
230 else if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
232 error(h,reader.Name + " is not a valid attribute for union",null);
234 else
236 XmlSchemaUtil.ReadUnhandledAttribute(reader,union);
240 reader.MoveToElement();
241 if(reader.IsEmptyElement)
242 return union;
244 // Content: annotation?, simpleType*
245 int level = 1;
246 while(reader.ReadNextElement())
248 if(reader.NodeType == XmlNodeType.EndElement)
250 if(reader.LocalName != xmlname)
251 error(h,"Should not happen :2: XmlSchemaSimpleTypeUnion.Read, name="+reader.Name,null);
252 break;
254 if(level <= 1 && reader.LocalName == "annotation")
256 level = 2; //Only one annotation
257 XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
258 if(annotation != null)
259 union.Annotation = annotation;
260 continue;
262 if(level <=2 && reader.LocalName == "simpleType")
264 level = 2;
265 XmlSchemaSimpleType stype = XmlSchemaSimpleType.Read(reader,h);
266 if(stype != null)
267 union.baseTypes.Add(stype);
268 continue;
270 reader.RaiseInvalidElementError();
272 return union;