/// <summary> /// Determines if the <see cref="XmlDocument"/> could be an instance of the /// indicated <see cref="SchemaRelease"/>. /// </summary> /// <param name="release">The potential <see cref="SchemaRelease"/>.</param> /// <param name="document">The <see cref="XmlDocument"/> to be tested.</param> /// <returns><b>true</b> if the <see cref="XmlDocument"/> could be an /// instance of the indicated <see cref="SchemaRelease"/>.</returns> public override bool Recognises(SchemaRelease release, XmlDocument document) { if (release.IsExtensionOnly) { // If the schema is a pure extension then check to see if any // xmlns attribute references its namespace. foreach (XmlAttribute attr in document.DocumentElement.Attributes) { if (attr.Name.StartsWith("xmlns") && attr.Value.Equals(release.NamespaceUri)) { return(true); } } } else { // If the schema declares root elements then check the name and // namespace matches. XmlElement root = document.DocumentElement; if (root != null) { String namespaceUri = root.NamespaceURI; if ((namespaceUri != null) && namespaceUri.Equals(release.NamespaceUri) && release.HasRootElement(root.LocalName)) { return(true); } } } return(false); }
/// <summary> /// Determines if the <see cref="XmlDocument"/> could be an instance of the /// indicated <see cref="SchemaRelease"/>. /// </summary> /// <param name="release">The potential <see cref="SchemaRelease"/>.</param> /// <param name="document">The <see cref="XmlDocument"/> to be tested.</param> /// <returns><b>true</b> if the <see cref="XmlDocument"/> could be an /// instance of the indicated <see cref="SchemaRelease"/>.</returns> public override bool Recognises(SchemaRelease release, XmlDocument document) { if (release.IsExtensionOnly) { // If the schema is a pure extension then check to see if any // xmlns attribute references its namespace. foreach (XmlAttribute attr in document.DocumentElement.Attributes) { if (attr.Name.StartsWith ("xmlns") && attr.Value.Equals (release.NamespaceUri)) return (true); } } else { // If the schema declares root elements then check the name and // namespace matches. XmlElement root = document.DocumentElement; if (root != null) { String namespaceUri = root.NamespaceURI; if ((namespaceUri != null) && namespaceUri.Equals (release.NamespaceUri) && release.HasRootElement (root.LocalName)) return (true); } } return (false); }
/// <summary> /// Initialises a new <see cref="XmlDocument"/> by adding required definitions to /// the structure indicated by its root <see cref="XmlElement"/>. /// </summary> /// <param name="release">The <see cref="SchemaRelease"/> being initialised.</param> /// <param name="root">The root <see cref="XmlElement"/> of the new document.</param> /// <param name="isDefaultNamespace"><b>true</b> if the default namespace is being initialised.</param> public override void Initialise(SchemaRelease release, XmlElement root, bool isDefaultNamespace) { string namespaceUri = release.NamespaceUri; string preferredPrefix = release.PreferredPrefix; string schemaLocation = release.SchemaLocation; XmlAttribute attr; if (isDefaultNamespace) { attr = root.OwnerDocument.CreateAttribute("xmlns", SchemaRelease.NAMESPACES_URL); } else { attr = root.OwnerDocument.CreateAttribute("xmlns", preferredPrefix, SchemaRelease.NAMESPACES_URL); } attr.Value = namespaceUri; root.Attributes.Append(attr); String value = root.GetAttribute("xsi:schemaLocation"); if (value != null) { value += " "; } value += namespaceUri + " " + schemaLocation; root.SetAttribute("xsi:schemaLocation", value); }
/// <summary> /// Connects this schema to any other schemas that it imports. /// </summary> /// <param name="release">The <see cref="SchemaRelease"/> for this schema.</param> /// <param name="context">The context <see cref="XmlElement"/> for the section.</param> /// <param name="loadedSchemas">A dictionay of previous bootstrapped schemas.</param> protected void HandleImports(SchemaRelease release, XmlElement context, Dictionary <string, SchemaRelease> loadedSchemas) { foreach (XmlElement node in XPath.Paths(context, "import")) { XmlAttribute href = node.GetAttributeNode("href"); if (href != null) { release.AddImport((SchemaRelease)loadedSchemas [href.Value]); } } }
/// <summary> /// Extracts the data from the DOM tree below the indicated context /// <see cref="XmlElement"/> and create a suitable <see crel="Release"/> /// structure to add to the indicated <see cref="Specification"/>. /// </summary> /// <param name="specification">The owning <see cref="Specification"/>.</param> /// <param name="context">The context <see cref="XmlElement"/> containing data</param> /// <param name="loadedSchemas">A dictionary of all ready loaded schemas.</param> public virtual void LoadData(Specification specification, XmlElement context, Dictionary<string, SchemaRelease> loadedSchemas) { XmlAttribute id = context.GetAttributeNode ("id"); SchemaRelease release = new SchemaRelease (specification, GetVersion (context), GetNamespaceUri (context), GetSchemaLocation (context), GetPreferredPrefix (context), GetAlternatePrefix (context), GetRootElements (context)); HandleImports (release, context, loadedSchemas); if (id != null) loadedSchemas.Add (id.Value, release); }
/// <summary> /// Attempts to locate a <see cref="SchemaRelease"/> associated with this /// <b>Specification</b> with the indicated version identifier and /// namespace URI. /// </summary> /// <param name="version">The target version identifier.</param> /// <param name="namespaceUri">The target namespace URI.</param> /// <returns>The corresponding <see cref="SchemaRelease"/> instance or <c>null</c> /// if none exists.</returns> public SchemaRelease GetReleaseForVersionAndNamespace(string version, string namespaceUri) { foreach (Release release in releases) { if (release is SchemaRelease) { SchemaRelease schemaRelease = release as SchemaRelease; if (schemaRelease.Version.Equals(version) && schemaRelease.NamespaceUri.Equals(namespaceUri)) { return(schemaRelease); } } } return(null); }
/// <summary> /// Extracts the data from the DOM tree below the indicated context /// <see cref="XmlElement"/> and create a suitable <see crel="Release"/> /// structure to add to the indicated <see cref="Specification"/>. /// </summary> /// <param name="specification">The owning <see cref="Specification"/>.</param> /// <param name="context">The context <see cref="XmlElement"/> containing data</param> /// <param name="loadedSchemas">A dictionary of all ready loaded schemas.</param> public virtual void LoadData(Specification specification, XmlElement context, Dictionary <string, SchemaRelease> loadedSchemas) { XmlAttribute id = context.GetAttributeNode("id"); SchemaRelease release = new SchemaRelease(specification, GetVersion(context), GetNamespaceUri(context), GetSchemaLocation(context), GetPreferredPrefix(context), GetAlternatePrefix(context), GetRootElements(context)); HandleImports(release, context, loadedSchemas); if (id != null) { loadedSchemas.Add(id.Value, release); } }
/// <summary> /// Initialises a new <see cref="XmlDocument"/> by adding required definitions to /// the structure indicated by its root <see cref="XmlElement"/>. /// </summary> /// <param name="release">The <see cref="SchemaRelease"/> being initialised.</param> /// <param name="root">The root <see cref="XmlElement"/> of the new document.</param> /// <param name="isDefaultNamespace"><b>true</b> if the default namespace is being initialised.</param> public override void Initialise(SchemaRelease release, XmlElement root, bool isDefaultNamespace) { string namespaceUri = release.NamespaceUri; string preferredPrefix = release.PreferredPrefix; string schemaLocation = release.SchemaLocation; XmlAttribute attr; if (isDefaultNamespace) attr = root.OwnerDocument.CreateAttribute ("xmlns", SchemaRelease.NAMESPACES_URL); else attr = root.OwnerDocument.CreateAttribute ("xmlns", preferredPrefix, SchemaRelease.NAMESPACES_URL); attr.Value = namespaceUri; root.Attributes.Append (attr); String value = root.GetAttribute ("xsi:schemaLocation"); if (value != null) value += " "; value += namespaceUri + " " + schemaLocation; root.SetAttribute ("xsi:schemaLocation", value); }
/// <summary> /// Creates a new instance the XML grammar represented by this instance /// using the indicated element name as the root element for the document. /// </summary> /// <param name="rootElement">The name of the root element.</param> /// <returns>A new <see cref="XmlDocument"/> instance.</returns> public override XmlDocument NewInstance(string rootElement) { List <SchemaRelease> releases = new List <SchemaRelease> (); SchemaRelease mainSchema = null; FindAllImports(releases); foreach (SchemaRelease release in releases) { if (release.HasRootElement(rootElement)) { if (mainSchema != null) { log.Fatal("Multiple schemas define root element '" + rootElement + "'"); return(null); } mainSchema = release; } } if (mainSchema == null) { log.Fatal("No schema recognised '" + rootElement + "' as a root element."); return(null); } XmlDocument document = new XmlDocument(); XmlElement element = document.CreateElement(rootElement, namespaceUri); element.SetAttribute("xmlns:xsi", INSTANCE_URL); document.AppendChild(element); foreach (SchemaRelease release in releases) { release.initialiser.Initialise(release, element, release == mainSchema); } return(document); }
/// <summary> /// Determines if the <see cref="XmlDocument"/> could be an instance of the /// indicated <see cref="SchemaRelease"/>. /// </summary> /// <param name="release">The potential <see cref="SchemaRelease"/>.</param> /// <param name="document">The <see cref="XmlDocument"/> to be tested.</param> /// <returns><b>true</b> if the <see cref="XmlDocument"/> could be an /// instance of the indicated <see cref="SchemaRelease"/>.</returns> public abstract bool Recognises(SchemaRelease release, XmlDocument document);
/// <summary> /// Initialises a new <see cref="XmlDocument"/> by adding required definitions to /// the structure indicated by its root <see cref="XmlElement"/>. /// </summary> /// <param name="release">The <see cref="SchemaRelease"/> being initialised.</param> /// <param name="root">The root <see cref="XmlElement"/> of the new document.</param> /// <param name="isDefaultNamespace"><b>true</b> if the default namespace is being initialised.</param> public abstract void Initialise(SchemaRelease release, XmlElement root, bool isDefaultNamespace);
/// <summary> /// Resolve the location of the indicated <see cref="SchemaRelease"/> (and /// any that it imports) to the schema set using the given XML catalog to /// resolve the schema location. /// </summary> /// <param name="release">The <see cref="SchemaRelease"/> to be added.</param> /// <param name="catalog">The <see cref="Catalog"/> to resolve with.</param> public void Add(SchemaRelease release, Catalog catalog) { List<SchemaRelease> imports = release.ImportSet; foreach (SchemaRelease schema in imports) { if (!schemas.Contains (schema)) { Uri uri = catalog.ResolveUri (null, schema.NamespaceUri); if (uri != null) { schemaSet.Add (schema.NamespaceUri, Unwrap (uri.LocalPath)); schemas.Add (schema); } else log.Fatal ("Failed to resolve schema for '" + schema.NamespaceUri + "'"); } } }
/// <summary> /// Resolve the location of the indicated <see cref="SchemaRelease"/> (and /// any that it imports) to the schema set using default XML catalog to /// resolve the schema location. /// </summary> /// <param name="release">The <see cref="SchemaRelease"/> to be added.</param> public void Add(SchemaRelease release) { Add (release, XmlUtility.DefaultCatalog); }
/// <summary> /// Connects this schema to any other schemas that it imports. /// </summary> /// <param name="release">The <see cref="SchemaRelease"/> for this schema.</param> /// <param name="context">The context <see cref="XmlElement"/> for the section.</param> /// <param name="loadedSchemas">A dictionay of previous bootstrapped schemas.</param> protected void HandleImports(SchemaRelease release, XmlElement context, Dictionary<string, SchemaRelease> loadedSchemas) { foreach (XmlElement node in XPath.Paths (context, "import")) { XmlAttribute href = node.GetAttributeNode ("href"); if (href != null) release.AddImport ((SchemaRelease) loadedSchemas [href.Value]); } }
/// <summary> /// Breaks the bi-directional reference between this <see cref="SchemaRelease"/> /// and the indicated one. /// </summary> /// <param name="release">The <see cref="SchemaRelease"/> no longer imported.</param> public void RemoveImport(SchemaRelease release) { this.imports.Remove(release); release.importedBy.Remove(this); }
/// <summary> /// Creates a bi-directional reference between this <see cref="SchemaRelease"/> /// and the meta data for other instance that it imports. /// </summary> /// <param name="release">The imported <see cref="SchemaRelease"/>.</param> public void AddImport(SchemaRelease release) { this.imports.Add (release); release.importedBy.Add (this); }
/// <summary> /// Breaks the bi-directional reference between this <see cref="SchemaRelease"/> /// and the indicated one. /// </summary> /// <param name="release">The <see cref="SchemaRelease"/> no longer imported.</param> public void RemoveImport(SchemaRelease release) { this.imports.Remove (release); release.importedBy.Remove (this); }
/// <summary> /// Creates a bi-directional reference between this <see cref="SchemaRelease"/> /// and the meta data for other instance that it imports. /// </summary> /// <param name="release">The imported <see cref="SchemaRelease"/>.</param> public void AddImport(SchemaRelease release) { this.imports.Add(release); release.importedBy.Add(this); }