2004-05-06 Atsushi Enomoto <atsushi@ximian.com>
[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 using System;
11 using System.Collections;
12 using System.Collections.Specialized;
13 using System.ComponentModel;
14 using System.IO;
15 using System.Security.Policy;
16 using System.Xml.Schema;
17 using System.Xml.XPath;
18
19 namespace System.Xml.Schema
20 {
21 #if NET_2_0
22         public class XmlSchemaSet
23 #else
24         internal class XmlSchemaSet
25 #endif
26         {
27                 XmlNameTable nameTable;
28                 XmlResolver xmlResolver;
29
30                 Hashtable schemas;
31                 XmlSchemaObjectTable attributes;
32                 XmlSchemaObjectTable elements;
33                 XmlSchemaObjectTable types;
34                 XmlSchemaCollection col;
35
36                 bool isCompiled;
37
38                 internal Guid CompilationId;\r
39
40                 public XmlSchemaSet () : this (new NameTable ())
41                 {
42                 }
43
44                 public XmlSchemaSet (XmlNameTable nameTable)
45                 {
46                         this.nameTable = nameTable;
47                         schemas = new Hashtable ();
48                         attributes = new XmlSchemaObjectTable ();
49                         elements = new XmlSchemaObjectTable ();
50                         types = new XmlSchemaObjectTable ();
51                         CompilationId = Guid.NewGuid ();
52                 }
53
54                 public event ValidationEventHandler ValidationEventHandler;
55
56                 public int Count {
57                         get { return schemas.Count; }
58                 }
59
60                 public XmlSchemaObjectTable GlobalAttributes {
61                         get { return attributes; }
62                 }
63
64                 public XmlSchemaObjectTable GlobalElements {
65                         get { return elements; }
66                 }
67
68                 public XmlSchemaObjectTable GlobalTypes { 
69                         get { return types; }
70                 }
71
72                 public bool IsCompiled { 
73                         get { return isCompiled; }
74                 }
75
76                 public XmlNameTable NameTable { 
77                         get { return nameTable; }
78                 }
79
80                 // This is mainly used for event delegating
81                 internal XmlSchemaCollection SchemaCollection {
82                         get { return col; }
83                         set { col = value; }
84                 }
85
86                 public XmlResolver XmlResolver { 
87                         set { xmlResolver = value; }
88                 }
89
90                 public XmlSchema Add (string targetNamespace, string url)
91                 {
92                         XmlTextReader r = null;
93                         try {
94                                 r = new XmlTextReader (url);
95                                 return Add (targetNamespace, r);
96                         } finally {
97                                 if (r != null)
98                                         r.Close ();
99                         }
100                 }
101
102                 [MonoTODO ("Check how targetNamespace is used")]
103                 public XmlSchema Add (string targetNamespace, XmlReader reader)
104                 {
105                         return Add (XmlSchema.Read (reader, null));
106                 }
107
108                 [MonoTODO ("Check the exact behavior when namespaces are in conflict")]
109                 public void Add (XmlSchemaSet schemaSet)
110                 {
111                         foreach (XmlSchema schema in schemaSet.schemas)
112                                 schemas.Add (schema.TargetNamespace, schema);
113                 }
114
115                 [MonoTODO ("Check the exact behavior when namespaces are in conflict")]
116                 public XmlSchema Add (XmlSchema schema)
117                 {
118                         XmlSchema existing = schemas [GetSafeNs (schema.TargetNamespace)] as XmlSchema;
119                         if (existing != null)
120                                 return existing;
121                         schemas.Add (GetSafeNs (schema.TargetNamespace), schema);
122                         return schema;
123                 }
124
125                 public void Compile ()
126                 {
127                         throw new NotImplementedException ();
128                 }
129
130                 public bool Contains (string targetNamespace)
131                 {
132                         return schemas.ContainsKey (targetNamespace);
133                 }
134
135                 public bool Contains (XmlSchema targetNamespace)
136                 {
137                         return schemas.Contains (targetNamespace);
138                 }
139
140                 public void CopyTo (XmlSchema[] array, int index)
141                 {
142                         schemas.CopyTo (array, index);
143                 }
144
145                 internal void CopyTo (Array array, int index)
146                 {
147                         schemas.CopyTo (array, index);
148                 }
149
150                 internal XmlSchema Get (string ns)
151                 {
152                         return (XmlSchema) schemas [GetSafeNs (ns)];
153                 }
154
155                 internal IEnumerator GetEnumerator()\r
156                 {\r
157                         return schemas.GetEnumerator();\r
158                 }\r
159 \r
160                 string GetSafeNs (string ns)\r
161                 {\r
162                         return ns == null ? "" : ns;\r
163                 }\r
164 \r
165                 internal void OnValidationError (object o, ValidationEventArgs e)
166                 {
167                         if (col != null)
168                                 col.OnValidationError (o, e);
169                         if (ValidationEventHandler != null)
170                                 ValidationEventHandler (o, e);
171                         else if (e.Severity == XmlSeverityType.Error)
172                                 throw e.Exception;
173                 }
174
175                 [MonoTODO ("Check exact behavior")]
176                 public XmlSchema Remove (XmlSchema schema)
177                 {
178                         schemas.Remove (schema);
179                         return schema;
180                 }
181
182                 public ArrayList Schemas ()
183                 {
184                         return new ArrayList (schemas);
185                 }
186
187                 [MonoTODO]
188                 public ArrayList Schemas (string targetNamespace)
189                 {
190                         throw new NotImplementedException ();
191                 }
192         }
193 }