Removal of NET_1_1 defines and some NET_2_0; Both defines are true these days in...
[mono.git] / mcs / class / System.XML / System.Xml.Schema / XmlSchemaCollection.cs
old mode 100755 (executable)
new mode 100644 (file)
index d5d1fdf..e572e06
@@ -40,6 +40,9 @@ namespace System.Xml.Schema
        /// XmlSchemaCollection is originally designed to be conformant to 
        /// W3C specification).
        /// </summary>
+#if NET_2_0
+       [Obsolete ("Use XmlSchemaSet.")]
+#endif
        public sealed class XmlSchemaCollection : ICollection, IEnumerable
        {
                //private fields
@@ -53,7 +56,7 @@ namespace System.Xml.Schema
                public XmlSchemaCollection (XmlNameTable nameTable)
                        : this (new XmlSchemaSet (nameTable))
                {
-                       this.schemaSet.SchemaCollection = this;
+                       schemaSet.ValidationEventHandler += new ValidationEventHandler (OnValidationError);
                }
 
                internal XmlSchemaCollection (XmlSchemaSet schemaSet)
@@ -75,7 +78,16 @@ namespace System.Xml.Schema
                }
 
                public XmlSchema this [ string ns ] { 
-                       get { return schemaSet.Get (ns); }
+                       get {
+                               ICollection col = schemaSet.Schemas (ns);
+                               if (col == null)
+                                       return null;
+                               IEnumerator e = col.GetEnumerator ();
+                               if (e.MoveNext ())
+                                       return (XmlSchema) e.Current;
+                               else
+                                       return null;
+                       }
                }
 
                // Events
@@ -87,23 +99,24 @@ namespace System.Xml.Schema
                        return Add (ns, reader, new XmlUrlResolver ());
                }
 
-#if NET_1_0
-               internal XmlSchema Add (string ns, XmlReader reader, XmlResolver resolver)
-#else
                public XmlSchema Add (string ns, XmlReader reader, XmlResolver resolver)
-#endif
                {
                        XmlSchema schema = XmlSchema.Read (reader, ValidationEventHandler);
-                       schema.Compile (ValidationEventHandler, this, resolver);
-                       lock (schemaSet) {
-                               return schemaSet.Add (schema);
-                       }
+                       if (schema.TargetNamespace == null)
+                               schema.TargetNamespace = ns;
+                       else if (ns != null && schema.TargetNamespace != ns)
+                               throw new XmlSchemaException ("The actual targetNamespace in the schema does not match the parameter.");
+
+                       return Add (schema);
                }
 
                public XmlSchema Add (string ns, string uri)
                {
-                       lock (schemaSet) {
-                               return schemaSet.Add (ns, uri);
+                       XmlReader reader = new XmlTextReader (uri);
+                       try {
+                               return Add (ns, reader);
+                       } finally {
+                               reader.Close ();
                        }
                }
 
@@ -117,21 +130,19 @@ namespace System.Xml.Schema
                        if (schema == null)
                                throw new ArgumentNullException ("schema");
 
-                       // XmlSchemaCollection.Add() compiles, while XmlSchemaSet.Add() does not
-                       if (!schema.IsCompiled)
-                               schema.Compile (ValidationEventHandler, this, resolver);
+                       XmlSchemaSet xss = new XmlSchemaSet (schemaSet.NameTable);
+                       xss.Add (schemaSet);
 
-                       string ns = GetSafeNs (schema.TargetNamespace);
-                       lock (schemaSet) {
-                               if (schemaSet.Contains (ns))
-                                       schemaSet.Remove (schemaSet.Get (ns));
-                               return schemaSet.Add (schema);
-                       }
-               }
-
-               private string GetSafeNs (string ns)
-               {
-                       return ns != null ? ns : String.Empty;
+                       // FIXME: maybe it requires Reprocess()
+                       xss.Add (schema);
+                       xss.ValidationEventHandler += ValidationEventHandler;
+                       xss.XmlResolver = resolver;
+                       xss.Compile ();
+                       if (!xss.IsCompiled)
+                               return null;
+                       // It is set only when the compilation was successful.
+                       schemaSet = xss;
+                       return schema;
                }
 
                public void Add (XmlSchemaCollection schema)
@@ -139,19 +150,18 @@ namespace System.Xml.Schema
                        if (schema == null)
                                throw new ArgumentNullException ("schema");
 
-                       /*
-                       foreach (XmlSchema s in schema) {
-                               string ns = GetSafeNs (s.TargetNamespace);
-                               lock (schemaSet) {
-                                       if (schemaSet.Contains (ns))
-                                               schemaSet.Remove (schemaSet.Get (ns));
-                                       schemaSet.Add (s);
-                               }
-                       }
-                       */
-                       lock (schemaSet) {
-                               schemaSet.Add (schema.schemaSet);
-                       }
+                       XmlSchemaSet xss = new XmlSchemaSet (schemaSet.NameTable);
+                       xss.Add (schemaSet);
+
+                       // FIXME: maybe it requires Reprocess()
+                       xss.Add (schema.schemaSet);
+                       xss.ValidationEventHandler += ValidationEventHandler;
+                       xss.XmlResolver = schemaSet.XmlResolver;
+                       xss.Compile ();
+                       if (!xss.IsCompiled)
+                               return;
+                       // It is set only when the compilation was successful.
+                       schemaSet = xss;
                }
 
                public bool Contains (string ns)
@@ -177,9 +187,14 @@ namespace System.Xml.Schema
 
                public XmlSchemaCollectionEnumerator GetEnumerator ()
                {
-                       return new XmlSchemaCollectionEnumerator (this);
+                        // The actual collection is schemaSet.Schemas()
+                       return new XmlSchemaCollectionEnumerator(schemaSet.Schemas());
+               }
+
+               int ICollection.Count {
+                       get { return Count; }
                }
-               
+
                // interface Methods
                void ICollection.CopyTo (Array array, int index)
                {
@@ -195,7 +210,7 @@ namespace System.Xml.Schema
 
                IEnumerator IEnumerable.GetEnumerator ()
                {
-                       return schemaSet.GetEnumerator ();
+                       return this.GetEnumerator ();
                }
 
                Object ICollection.SyncRoot
@@ -203,23 +218,7 @@ namespace System.Xml.Schema
                        get { return this; }
                }
 
-               // Internal Methods
-               internal XmlSchemaAttribute FindAttribute (XmlQualifiedName qname)
-               {
-                       return (XmlSchemaAttribute) schemaSet.GlobalAttributes [qname];
-               }
-
-               internal XmlSchemaElement FindElement (XmlQualifiedName qname)
-               {
-                       return (XmlSchemaElement) schemaSet.GlobalElements [qname];
-               }
-
-               internal object FindSchemaType (XmlQualifiedName qname)
-               {
-                       return schemaSet.GlobalTypes [qname];
-               }
-
-               internal void OnValidationError (object o, ValidationEventArgs e)
+               void OnValidationError (object o, ValidationEventArgs e)
                {
                        if (ValidationEventHandler != null)
                                ValidationEventHandler (o, e);