1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
|
//
// XmlSchemaSet.cs
//
// Author:
// Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
//
// (C)2003 Atsushi Enomoto
// (C)2004 Novell Inc.
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.IO;
using System.Security.Policy;
using System.Xml.Schema;
using System.Xml.XPath;
namespace System.Xml.Schema
{
#if NET_2_0
public class XmlSchemaSet
#else
internal sealed class XmlSchemaSet
#endif
{
XmlNameTable nameTable;
XmlResolver xmlResolver = new XmlUrlResolver ();
ArrayList schemas;
XmlSchemaObjectTable attributes;
XmlSchemaObjectTable elements;
XmlSchemaObjectTable types;
// XmlSchemaObjectTable attributeGroups;
// XmlSchemaObjectTable groups;
Hashtable idCollection;
XmlSchemaObjectTable namedIdentities;
XmlSchemaCompilationSettings settings =
new XmlSchemaCompilationSettings ();
bool isCompiled;
internal Guid CompilationId;
public XmlSchemaSet ()
: this (new NameTable ())
{
}
public XmlSchemaSet (XmlNameTable nameTable)
{
if (nameTable == null)
throw new ArgumentNullException ("nameTable");
this.nameTable = nameTable;
schemas = new ArrayList ();
CompilationId = Guid.NewGuid ();
}
public event ValidationEventHandler ValidationEventHandler;
public int Count {
get { return schemas.Count; }
}
public XmlSchemaObjectTable GlobalAttributes {
get {
if (attributes == null)
attributes = new XmlSchemaObjectTable ();
return attributes;
}
}
public XmlSchemaObjectTable GlobalElements {
get {
if (elements == null)
elements = new XmlSchemaObjectTable ();
return elements;
}
}
public XmlSchemaObjectTable GlobalTypes {
get {
if (types == null)
types = new XmlSchemaObjectTable ();
return types;
}
}
public bool IsCompiled {
get { return isCompiled; }
}
public XmlNameTable NameTable {
get { return nameTable; }
}
public XmlSchemaCompilationSettings CompilationSettings {
get { return settings; }
set { settings = value; }
}
public XmlResolver XmlResolver {
set { xmlResolver = value; }
#if NET_2_0
internal get { return xmlResolver; }
#else
get { return xmlResolver; }
#endif
}
internal Hashtable IDCollection {
get {
if (idCollection == null)
idCollection = new Hashtable ();
return idCollection;
}
}
internal XmlSchemaObjectTable NamedIdentities {
get {
if (namedIdentities == null)
namedIdentities = new XmlSchemaObjectTable();
return namedIdentities;
}
}
public XmlSchema Add (string targetNamespace, string url)
{
XmlTextReader r = null;
try {
r = new XmlTextReader (url, nameTable);
return Add (targetNamespace, r);
} finally {
if (r != null)
r.Close ();
}
}
public XmlSchema Add (string targetNamespace, XmlReader reader)
{
XmlSchema schema = XmlSchema.Read (reader, ValidationEventHandler);
if (schema.TargetNamespace == null)
schema.TargetNamespace = targetNamespace;
else if (targetNamespace != null && schema.TargetNamespace != targetNamespace)
throw new XmlSchemaException ("The actual targetNamespace in the schema does not match the parameter.");
Add (schema);
return schema;
}
[MonoTODO]
// FIXME: Check the exact behavior when namespaces are in conflict (but it would be preferable to wait for 2.0 RTM)
public void Add (XmlSchemaSet schemaSet)
{
ArrayList al = new ArrayList ();
foreach (XmlSchema schema in schemaSet.schemas) {
if (!schemas.Contains (schema))
al.Add (schema);
}
foreach (XmlSchema schema in al)
Add (schema);
}
public XmlSchema Add (XmlSchema schema)
{
schemas.Add (schema);
ResetCompile ();
return schema;
}
// FIXME: It should be the actual compilation engine.
public void Compile ()
{
ClearGlobalComponents ();
ArrayList al = new ArrayList ();
al.AddRange (schemas);
IDCollection.Clear ();
NamedIdentities.Clear ();
Hashtable handledUris = new Hashtable ();
foreach (XmlSchema schema in al)
if (!schema.IsCompiled)
schema.CompileSubset (ValidationEventHandler, this, xmlResolver, handledUris);
// Process substitutionGroup first, as this process
// involves both substituted and substituting elements
// and hence it needs to be done before actual
// validation (by current design of conformance checker).
foreach (XmlSchema schema in al)
foreach (XmlSchemaElement elem in schema.Elements.Values)
elem.FillSubstitutionElementInfo ();
foreach (XmlSchema schema in al)
schema.Validate (ValidationEventHandler);
foreach (XmlSchema schema in al)
AddGlobalComponents (schema);
isCompiled = true;
}
private void ClearGlobalComponents ()
{
GlobalElements.Clear ();
GlobalAttributes.Clear ();
GlobalTypes.Clear ();
// GlobalAttributeGroups.Clear ();
// GlobalGroups.Clear ();
}
private void AddGlobalComponents (XmlSchema schema)
{
foreach (XmlSchemaElement el in schema.Elements.Values)
GlobalElements.Add (el.QualifiedName, el);
foreach (XmlSchemaAttribute a in schema.Attributes.Values)
GlobalAttributes.Add (a.QualifiedName, a);
foreach (XmlSchemaType t in schema.SchemaTypes.Values)
GlobalTypes.Add (t.QualifiedName, t);
}
public bool Contains (string targetNamespace)
{
targetNamespace = GetSafeNs (targetNamespace);
foreach (XmlSchema schema in schemas)
if (GetSafeNs (schema.TargetNamespace) == targetNamespace)
return true;
return false;
}
public bool Contains (XmlSchema targetNamespace)
{
foreach (XmlSchema schema in schemas)
if (schema == targetNamespace)
return true;
return false;
}
public void CopyTo (XmlSchema [] array, int index)
{
schemas.CopyTo (array, index);
}
internal void CopyTo (Array array, int index)
{
schemas.CopyTo (array, index);
}
string GetSafeNs (string ns)
{
return ns == null ? "" : ns;
}
[MonoTODO]
// FIXME: Check exact behavior
public XmlSchema Remove (XmlSchema schema)
{
if (schema == null)
throw new ArgumentNullException ("schema");
ArrayList al = new ArrayList ();
al.AddRange (schemas);
if (!al.Contains (schema))
return null;
// FIXME: I have no idea why Remove() might throw
// XmlSchemaException, except for the case it compiles.
if (!schema.IsCompiled)
schema.CompileSubset (ValidationEventHandler, this, xmlResolver);
schemas.Remove (schema);
ResetCompile ();
return schema;
}
void ResetCompile ()
{
isCompiled = false;
ClearGlobalComponents ();
}
public bool RemoveRecursive (XmlSchema schema)
{
if (schema == null)
throw new ArgumentNullException ("schema");
ArrayList al = new ArrayList ();
al.AddRange (schemas);
if (!al.Contains (schema))
return false;
al.Remove (schema);
schemas.Remove (schema);
if (!IsCompiled)
return true;
ClearGlobalComponents ();
foreach (XmlSchema s in al) {
if (s.IsCompiled)
AddGlobalComponents (schema);
}
return true;
}
public XmlSchema Reprocess (XmlSchema schema)
{
if (schema == null)
throw new ArgumentNullException ("schema");
ArrayList al = new ArrayList ();
al.AddRange (schemas);
if (!al.Contains (schema))
throw new ArgumentException ("Target schema is not contained in the schema set.");
ClearGlobalComponents ();
foreach (XmlSchema s in al) {
if (schema == s)
schema.CompileSubset (ValidationEventHandler, this, xmlResolver);
if (s.IsCompiled)
AddGlobalComponents (schema);
}
return schema.IsCompiled ? schema : null;
}
public ICollection Schemas ()
{
return schemas;
}
public ICollection Schemas (string targetNamespace)
{
targetNamespace = GetSafeNs (targetNamespace);
ArrayList al = new ArrayList ();
foreach (XmlSchema schema in schemas)
if (GetSafeNs (schema.TargetNamespace) == targetNamespace)
al.Add (schema);
return al;
}
internal bool MissedSubComponents (string targetNamespace)
{
foreach (XmlSchema s in Schemas (targetNamespace))
if (s.missedSubComponents)
return true;
return false;
}
}
}
|