| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353 |
- //
- // XmlSchemaSet.cs
- //
- // Author:
- // Atsushi Enomoto <[email protected]>
- //
- // (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 ();
- foreach (XmlSchema schema in al)
- if (!schema.IsCompiled)
- schema.CompileSubset (ValidationEventHandler, this, xmlResolver);
- 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;
- }
- }
- }
|