5 // Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
7 // (C)2003 Atsushi Enomoto
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Collections;
33 using System.Collections.Specialized;
34 using System.ComponentModel;
36 using System.Security.Policy;
37 using System.Xml.Schema;
38 using System.Xml.XPath;
40 namespace System.Xml.Schema
43 public class XmlSchemaSet
45 internal sealed class XmlSchemaSet
48 XmlNameTable nameTable;
49 XmlResolver xmlResolver = new XmlUrlResolver ();
52 XmlSchemaObjectTable attributes;
53 XmlSchemaObjectTable elements;
54 XmlSchemaObjectTable types;
55 // XmlSchemaObjectTable attributeGroups;
56 // XmlSchemaObjectTable groups;
58 XmlSchemaCollection col;
59 ValidationEventHandler handler;
63 internal Guid CompilationId;
65 public XmlSchemaSet ()
66 : this (new NameTable ())
68 handler = new ValidationEventHandler (this.OnValidationError);
71 public XmlSchemaSet (XmlNameTable nameTable)
73 if (nameTable == null)
74 throw new ArgumentNullException ("nameTable");
76 this.nameTable = nameTable;
77 schemas = new ArrayList ();
78 CompilationId = Guid.NewGuid ();
81 public event ValidationEventHandler ValidationEventHandler;
84 get { return schemas.Count; }
87 public XmlSchemaObjectTable GlobalAttributes {
89 if (attributes == null)
90 attributes = new XmlSchemaObjectTable ();
95 public XmlSchemaObjectTable GlobalElements {
98 elements = new XmlSchemaObjectTable ();
103 public XmlSchemaObjectTable GlobalTypes {
106 types = new XmlSchemaObjectTable ();
111 public bool IsCompiled {
112 get { return isCompiled; }
115 public XmlNameTable NameTable {
116 get { return nameTable; }
119 // This is mainly used for event delegating
120 internal XmlSchemaCollection SchemaCollection {
124 col = new XmlSchemaCollection ();
125 foreach (XmlSchema s in schemas)
133 public XmlResolver XmlResolver {
134 set { xmlResolver = value; }
136 internal get { return xmlResolver; }
140 public XmlSchema Add (string targetNamespace, string url)
142 targetNamespace = GetSafeNs (targetNamespace);
143 XmlTextReader r = null;
145 r = new XmlTextReader (url, nameTable);
146 return Add (targetNamespace, r);
153 [MonoTODO ("It has weird namespace duplication check that is different from Add(XmlSchema).")]
154 public XmlSchema Add (string targetNamespace, XmlReader reader)
156 XmlSchema schema = XmlSchema.Read (reader, handler);
157 if (targetNamespace != null
158 && targetNamespace.Length > 0)
159 schema.TargetNamespace = targetNamespace;
164 [MonoTODO ("Check the exact behavior when namespaces are in conflict (but it would be preferable to wait for 2.0 RTM).")]
165 public void Add (XmlSchemaSet schemaSet)
167 ArrayList al = new ArrayList ();
168 foreach (XmlSchema schema in schemaSet.schemas) {
169 if (!schemas.Contains (schema))
172 AddGlobalComponents (schema);
174 foreach (XmlSchema schema in al)
178 [MonoTODO ("We need to research more about the expected behavior")]
179 public XmlSchema Add (XmlSchema schema)
181 schemas.Add (schema);
182 AddGlobalComponents (schema);
186 [MonoTODO ("It should be the actual compilation engine.")]
187 public void Compile ()
189 ClearGlobalComponents ();
190 ArrayList al = new ArrayList ();
191 al.AddRange (schemas);
192 foreach (XmlSchema schema in al) {
193 if (!schema.IsCompiled)
194 schema.Compile (handler, this, xmlResolver);
195 AddGlobalComponents (schema);
200 private void ClearGlobalComponents ()
202 GlobalElements.Clear ();
203 GlobalAttributes.Clear ();
204 GlobalTypes.Clear ();
205 // GlobalAttributeGroups.Clear ();
206 // GlobalGroups.Clear ();
209 private void AddGlobalComponents (XmlSchema schema)
211 foreach (XmlSchemaElement el in schema.Elements.Values)
212 GlobalElements.Add (el.QualifiedName, el);
213 foreach (XmlSchemaAttribute a in schema.Attributes.Values)
214 GlobalAttributes.Add (a.QualifiedName, a);
215 foreach (XmlSchemaType t in schema.SchemaTypes.Values)
216 GlobalTypes.Add (t.QualifiedName, t);
219 public bool Contains (string targetNamespace)
221 targetNamespace = GetSafeNs (targetNamespace);
222 foreach (XmlSchema schema in schemas)
223 if (GetSafeNs (schema.TargetNamespace) == targetNamespace)
228 public bool Contains (XmlSchema targetNamespace)
230 foreach (XmlSchema schema in schemas)
231 if (schema == targetNamespace)
236 public void CopyTo (XmlSchema [] array, int index)
238 schemas.CopyTo (array, index);
241 internal void CopyTo (Array array, int index)
243 schemas.CopyTo (array, index);
246 string GetSafeNs (string ns)
248 return ns == null ? "" : ns;
251 internal void OnValidationError (object o, ValidationEventArgs e)
254 col.OnValidationError (o, e);
255 if (ValidationEventHandler != null)
256 ValidationEventHandler (o, e);
257 else if (e.Severity == XmlSeverityType.Error)
261 [MonoTODO ("Check exact behavior")]
262 public XmlSchema Remove (XmlSchema schema)
265 throw new ArgumentNullException ("schema");
266 ArrayList al = new ArrayList ();
267 al.AddRange (schemas);
268 if (!al.Contains (schema))
270 // FIXME: I have no idea why Remove() might throw
271 // XmlSchemaException, except for the case it compiles.
272 if (!schema.IsCompiled)
273 schema.Compile (handler, this, xmlResolver);
274 schemas.Remove (schema);
276 ClearGlobalComponents ();
280 [MonoTODO ("Check exact behavior")]
281 public bool RemoveRecursive (XmlSchema schema)
284 throw new ArgumentNullException ("schema");
285 ArrayList al = new ArrayList ();
286 al.AddRange (schemas);
287 if (!al.Contains (schema))
290 schemas.Remove (schema);
291 ClearGlobalComponents ();
292 foreach (XmlSchema s in al) {
294 AddGlobalComponents (schema);
299 public XmlSchema Reprocess (XmlSchema schema)
302 throw new ArgumentNullException ("schema");
303 ArrayList al = new ArrayList ();
304 al.AddRange (schemas);
305 if (!al.Contains (schema))
306 throw new ArgumentException ("Target schema is not contained in the schema set.");
307 ClearGlobalComponents ();
308 foreach (XmlSchema s in al) {
310 schema.Compile (handler, this, xmlResolver);
312 AddGlobalComponents (schema);
314 return schema.IsCompiled ? schema : null;
317 public ICollection Schemas ()
323 public ICollection Schemas (string targetNamespace)
325 targetNamespace = GetSafeNs (targetNamespace);
326 ArrayList al = new ArrayList ();
327 foreach (XmlSchema schema in schemas)
328 if (GetSafeNs (schema.TargetNamespace) == targetNamespace)
333 internal bool MissedSubComponents (string targetNamespace)
335 foreach (XmlSchema s in Schemas (targetNamespace))
336 if (s.missedSubComponents)