Add MIT license to System.XML.DLL
[mono.git] / mcs / class / System.XML / System.Xml.Schema / XmlSchemaSet.cs
1 //
2 // XmlSchemaSet.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
6 //
7 // (C)2003 Atsushi Enomoto
8 // (C)2004 Novell Inc.
9 //
10
11 //
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:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
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.
30 //
31 using System;
32 using System.Collections;
33 using System.Collections.Specialized;
34 using System.ComponentModel;
35 using System.IO;
36 using System.Security.Policy;
37 using System.Xml.Schema;
38 using System.Xml.XPath;
39
40 namespace System.Xml.Schema
41 {
42 #if NET_2_0
43         public class XmlSchemaSet
44 #else
45         internal class XmlSchemaSet
46 #endif
47         {
48                 XmlNameTable nameTable;
49                 XmlResolver xmlResolver;
50
51                 Hashtable schemas;
52                 XmlSchemaObjectTable attributes;
53                 XmlSchemaObjectTable elements;
54                 XmlSchemaObjectTable types;
55                 XmlSchemaCollection col;
56
57                 bool isCompiled;
58
59                 internal Guid CompilationId;\r
60
61                 public XmlSchemaSet () : this (new NameTable ())
62                 {
63                 }
64
65                 public XmlSchemaSet (XmlNameTable nameTable)
66                 {
67                         this.nameTable = nameTable;
68                         schemas = new Hashtable ();
69                         attributes = new XmlSchemaObjectTable ();
70                         elements = new XmlSchemaObjectTable ();
71                         types = new XmlSchemaObjectTable ();
72                         CompilationId = Guid.NewGuid ();
73                 }
74
75                 public event ValidationEventHandler ValidationEventHandler;
76
77                 public int Count {
78                         get { return schemas.Count; }
79                 }
80
81                 public XmlSchemaObjectTable GlobalAttributes {
82                         get { return attributes; }
83                 }
84
85                 public XmlSchemaObjectTable GlobalElements {
86                         get { return elements; }
87                 }
88
89                 public XmlSchemaObjectTable GlobalTypes { 
90                         get { return types; }
91                 }
92
93                 public bool IsCompiled { 
94                         get { return isCompiled; }
95                 }
96
97                 public XmlNameTable NameTable { 
98                         get { return nameTable; }
99                 }
100
101                 // This is mainly used for event delegating
102                 internal XmlSchemaCollection SchemaCollection {
103                         get { return col; }
104                         set { col = value; }
105                 }
106
107                 public XmlResolver XmlResolver { 
108                         set { xmlResolver = value; }
109                 }
110
111                 public XmlSchema Add (string targetNamespace, string url)
112                 {
113                         XmlTextReader r = null;
114                         try {
115                                 r = new XmlTextReader (url);
116                                 return Add (targetNamespace, r);
117                         } finally {
118                                 if (r != null)
119                                         r.Close ();
120                         }
121                 }
122
123                 [MonoTODO ("Check how targetNamespace is used")]
124                 public XmlSchema Add (string targetNamespace, XmlReader reader)
125                 {
126                         return Add (XmlSchema.Read (reader, null));
127                 }
128
129                 [MonoTODO ("Check the exact behavior when namespaces are in conflict")]
130                 public void Add (XmlSchemaSet schemaSet)
131                 {
132                         foreach (XmlSchema schema in schemaSet.schemas)
133                                 schemas.Add (schema.TargetNamespace, schema);
134                 }
135
136                 [MonoTODO ("Check the exact behavior when namespaces are in conflict")]
137                 public XmlSchema Add (XmlSchema schema)
138                 {
139                         XmlSchema existing = schemas [GetSafeNs (schema.TargetNamespace)] as XmlSchema;
140                         if (existing != null)
141                                 return existing;
142                         schemas.Add (GetSafeNs (schema.TargetNamespace), schema);
143                         return schema;
144                 }
145
146                 [MonoTODO]
147                 public void Compile ()
148                 {
149                         throw new NotImplementedException ();
150                 }
151
152                 public bool Contains (string targetNamespace)
153                 {
154                         return schemas.ContainsKey (targetNamespace);
155                 }
156
157                 public bool Contains (XmlSchema targetNamespace)
158                 {
159                         return schemas.Contains (targetNamespace);
160                 }
161
162                 public void CopyTo (XmlSchema[] array, int index)
163                 {
164                         schemas.CopyTo (array, index);
165                 }
166
167                 internal void CopyTo (Array array, int index)
168                 {
169                         schemas.CopyTo (array, index);
170                 }
171
172                 internal XmlSchema Get (string ns)
173                 {
174                         return (XmlSchema) schemas [GetSafeNs (ns)];
175                 }
176
177                 internal IEnumerator GetEnumerator ()\r
178                 {\r
179                         return schemas.GetEnumerator ();\r
180                 }\r
181 \r
182                 string GetSafeNs (string ns)\r
183                 {\r
184                         return ns == null ? "" : ns;\r
185                 }\r
186 \r
187                 internal void OnValidationError (object o, ValidationEventArgs e)
188                 {
189                         if (col != null)
190                                 col.OnValidationError (o, e);
191                         if (ValidationEventHandler != null)
192                                 ValidationEventHandler (o, e);
193                         else if (e.Severity == XmlSeverityType.Error)
194                                 throw e.Exception;
195                 }
196
197                 [MonoTODO ("Check exact behavior")]
198                 public XmlSchema Remove (XmlSchema schema)
199                 {
200                         schemas.Remove (schema);
201                         return schema;
202                 }
203
204                 [MonoTODO]
205                 public bool RemoveRecursive (XmlSchema schema)
206                 {
207                         throw new NotImplementedException ();
208                 }
209
210                 [MonoTODO]
211                 public XmlSchema Reprocess (XmlSchema schema)
212                 {
213                         throw new NotImplementedException ();
214                 }
215
216                 public ArrayList Schemas ()
217                 {
218                         return new ArrayList (schemas.Values);
219                 }
220
221                 [MonoTODO]
222                 public ArrayList Schemas (string targetNamespace)
223                 {
224                         throw new NotImplementedException ();
225                 }
226         }
227 }