/// <summary> /// Generates the OWL graph into an XmlDocument object /// </summary> /// <param name="graph">The graph which needs to be generated</param> /// <param name="doc">The XmlDocument object used as a destination for the graph</param> public void GenerateOwl(IOwlGraph graph, XmlDocument doc) { if (doc == null) { throw (new ArgumentNullException("The specified XmlDocument object is a null reference")); } Warnings.Clear(); Errors.Clear(); _visited.Clear(); XmlElement root = _owlDocument.CreateElement("rdf:RDF", OwlNamespaceCollection.RdfNamespace); _owlDocument.AppendChild(root); //Added by HM // Create an XML declaration. XmlDeclaration xmldecl = _owlDocument.CreateXmlDeclaration("1.0", null, null); xmldecl.Encoding = "UTF-8"; xmldecl.Standalone = "yes"; // Add the new node to the document. _owlDocument.InsertBefore(xmldecl, root); //End of HM addition _baseUri = graph.NameSpaces["xml:base"]; IDictionaryEnumerator nsEnumerator = (IDictionaryEnumerator)graph.NameSpaces.GetEnumerator(); while (nsEnumerator.MoveNext()) { // Write all the namespaces to the document XmlAttribute nsAttribute = _owlDocument.CreateAttribute((nsEnumerator.Key).ToString()); nsAttribute.Value = (nsEnumerator.Value).ToString(); root.Attributes.Append(nsAttribute); // Also insert the reversed namespaces into the a local variable _namespaces[nsEnumerator.Value.ToString()] = nsEnumerator.Key.ToString(); } IDictionaryEnumerator nEnumerator = (IDictionaryEnumerator)graph.Nodes.GetEnumerator(); while (nEnumerator.MoveNext()) { OwlNode node = (OwlNode)graph.Nodes[(nEnumerator.Key).ToString()]; if (!node.IsAnonymous()) { node.Accept(this, root); } } XmlComment comment = _owlDocument.CreateComment("This file has been generated by the OwlDotNetApi."); _owlDocument.AppendChild(comment); doc = _owlDocument; }
/// <summary> /// The general implementation of a regular OwlNode. A node is only of type OwlNode in some special cases. /// </summary> /// <param name="node">The actual node to visit</param> /// <param name="parent">The parent object of the node</param> public override void Visit(OwlNode node, Object parent) { XmlElement parentElement = parent as XmlElement; if (parentElement != null) { // Here we will only visit the blank node, because these are // the only ones we need. The others are the standard nodes // representing datatypes or owl, rdf, rdfs predefined nodes. if (node.IsAnonymous()) { XmlComment comment = _owlDocument.CreateComment("Visiting a regular node: " + node.GetType()); parentElement.AppendChild(comment); _visited.Add(node); } } }
/// <summary> /// The general implementation of a regular OwlNode. A node is only of type OwlNode in some special cases. /// </summary> /// <param name="node">The actual node to visit</param> /// <param name="parent">The parent object of the node</param> public override void Visit(OwlNode node, Object parent) { XmlElement parentElement = parent as XmlElement; if(parentElement != null) { // Here we will only visit the blank node, because these are // the only ones we need. The others are the standard nodes // representing datatypes or owl, rdf, rdfs predefined nodes. if(node.IsAnonymous()) { XmlComment comment = _owlDocument.CreateComment("Visiting a regular node: " + node.GetType()); parentElement.AppendChild(comment); node.Visited = true; } } }
/// <summary> /// Implementation of an OwlEdge. /// </summary> /// <param name="edge">The actual edge to be visited</param> /// <param name="parent">The parent object (node) of the edge</param> public override void Visit(OwlEdge edge, Object parent) { XmlElement parentElement = parent as XmlElement; if (parentElement != null) { Uri uri = new Uri(edge.ID); // Retrieve the local name from the uri string localName = uri.Fragment.Substring(1, uri.Fragment.Length - 1); // Get the path, up to the local name, from the uri string path = uri.GetLeftPart(UriPartial.Path) + "#"; string prefix; // Check for the namespace of the edge in order to get the correct prefix if (path == OwlNamespaceCollection.OwlNamespace) { prefix = OwlNamespaceCollection.OwlNamespacePrefix; } else if (path == OwlNamespaceCollection.RdfNamespace) { prefix = OwlNamespaceCollection.RdfNamespacePrefix; } else if (path == OwlNamespaceCollection.RdfSchemaNamespace) { prefix = OwlNamespaceCollection.RdfSchemaNamespacePrefix; } else { // If the namespace of the edge is something else, then look for it prefix = ""; if (path != _baseUri) { // Search for the prefix of this individual int pos = _namespaces[path].IndexOf(':'); if (pos != -1) { prefix = _namespaces[path].Substring(++pos); } } } string qualifiedName = (prefix.Length != 0) ? (prefix + ":" + localName) : localName; // At this point we will not generate the rdf:type edges. XmlElement edgeElement = _owlDocument.CreateElement(qualifiedName, path); // Check to see if the child node is an anonymous one or not. If it is, then further // process that node, otherwise just reference it by name. OwlNode node = (OwlNode)edge.ChildNode; if (node.IsAnonymous() || node.IsLiteral()) { node.Accept(this, edgeElement); } else { // Add the name as one of its attributes XmlAttribute nameAttribute = _owlDocument.CreateAttribute("rdf", "resource", OwlNamespaceCollection.RdfNamespace); nameAttribute.Value = GetNodeReference(node); edgeElement.Attributes.Append(nameAttribute); } parentElement.AppendChild(edgeElement); } }