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; }
74 internal override void SetParent (XmlSchemaObject parent
)
76 base.SetParent (parent
);
77 foreach (XmlSchemaObject obj
in BaseTypes
)
82 /// 1. Circular union type definition is disallowed. (WTH is this?)
83 /// 2. id must be a valid ID
85 internal override int Compile(ValidationEventHandler h
, XmlSchema schema
)
87 // If this is already compiled this time, simply skip.
88 if (CompilationId
== schema
.CompilationId
)
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
);
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
;
119 count
+= MemberTypes
.Length
;
125 error(h
, "Atleast one simpletype or membertype must be present");
127 XmlSchemaUtil
.CompileID(Id
,this,schema
.IDCollection
,h
);
131 this.CompilationId
= schema
.CompilationId
;
135 internal override int Validate(ValidationEventHandler h
, XmlSchema schema
)
137 if (IsValidated (schema
.ValidationId
))
140 ArrayList al
= new ArrayList ();
141 // Validate MemberTypes
142 if (MemberTypes
!= null) {
143 foreach (XmlQualifiedName memberTypeName
in MemberTypes
) {
145 XmlSchemaType xstype
= schema
.FindSchemaType (memberTypeName
) as XmlSchemaSimpleType
;
146 if (xstype
!= null) {
147 errorCount
+= xstype
.Validate (h
, schema
);
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
);
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.");
164 if (BaseTypes
!= null) {
165 foreach (XmlSchemaSimpleType st
in BaseTypes
) {
166 st
.Validate (h
, schema
);
170 this.validatedTypes
= al
.ToArray ();
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
;
185 ValidationId
= schema
.ValidationId
;
191 // memberTypes = List of QName
192 // {any attributes with non-schema namespace . . .}>
193 // Content: (annotation?, (simpleType*))
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);
207 union
.LineNumber
= reader
.LineNumber
;
208 union
.LinePosition
= reader
.LinePosition
;
209 union
.SourceUri
= reader
.BaseURI
;
212 while(reader
.MoveToNextAttribute())
214 if(reader
.Name
== "id")
216 union
.Id
= reader
.Value
;
218 else if(reader
.Name
== "memberTypes")
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
);
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);
236 XmlSchemaUtil
.ReadUnhandledAttribute(reader
,union
);
240 reader
.MoveToElement();
241 if(reader
.IsEmptyElement
)
244 // Content: annotation?, simpleType*
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);
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
;
262 if(level
<=2 && reader
.LocalName
== "simpleType")
265 XmlSchemaSimpleType stype
= XmlSchemaSimpleType
.Read(reader
,h
);
267 union
.baseTypes
.Add(stype
);
270 reader
.RaiseInvalidElementError();