/// <summary> /// Must lock (nm) and probably graph or it may throw a InvalidOperationException /// </summary> /// <param name="graph"></param> /// <param name="nm"></param> private static void EnsureBaseURIMapped(IGraph graph, INamespaceMapper nm) { bool hsBlank = nm.HasNamespace(""); var BaseUri = graph.BaseUri; if (!hsBlank) { if (BaseUri != null) { //nm.AddNamespace("", BaseUri); return; } var newIdea = UriFactory.Create(RoboKindURI); graph.BaseUri = newIdea; //nm.AddNamespace("", newIdea); return; } Uri previosuBlankURI = nm.GetNamespaceUri(""); var previousBlank = previosuBlankURI.AbsoluteUri; if (BaseUri != null) { if (previousBlank != BaseUri.AbsoluteUri) { } ; //nm.AddNamespace("", BaseUri); return; } var newIdea2 = UriFactory.Create(RoboKindURI); //nm.AddNamespace("", newIdea2); }
/// <summary> /// Checks whether an attribute is an property attribute. /// </summary> /// <param name="attr">Attribute to Test.</param> /// <param name="nsMapper">The namespace prefix mappings to use when expanding the namespace prefix of the attribute.</param> /// <returns>True if is an property attribute.</returns> public static bool IsPropertyAttribute(AttributeEvent attr, INamespaceMapper nsMapper) { // QName must be a valid Property Attribute Uri // Any string value allowed so if Uri test is true then we're a property Attribute return(nsMapper.HasNamespace(attr.Namespace) && IsPropertyAttributeURI(nsMapper.GetNamespaceUri(attr.Namespace), attr.LocalName)); }
private static string Uri2QName(SNamespaceMap mapLocal, string uri, INamespaceMapper globalNamespaceMaper) { string qname; if (mapLocal.ReduceToQName(uri, out qname)) { return(qname); } string shortName; Uri urlNs; SplitUrl(uri, out shortName, out urlNs); var prefix = globalNamespaceMaper.GetPrefix(urlNs); if (prefix == null) { string p = "ns"; int i = 0; while (mapLocal.HasNamespace(prefix = p + i + ":") || globalNamespaceMaper.HasNamespace(prefix)) { i++; } } mapLocal.AddNamespace(prefix, urlNs); return(prefix + shortName); }
/// <summary> /// Resolves a QName into a Uri using the Namespace Mapper and Base Uri provided. /// </summary> /// <param name="qname">QName to resolve.</param> /// <param name="nsmap">Namespace Map to resolve against.</param> /// <param name="baseUri">Base Uri to resolve against.</param> /// <param name="allowDefaultPrefixFallback">Whether when the default prefix is used but not defined it can fallback to Base URI.</param> /// <returns></returns> public static String ResolveQName(String qname, INamespaceMapper nsmap, Uri baseUri, bool allowDefaultPrefixFallback) { String output; if (qname.StartsWith(":")) { // QName in Default Namespace if (nsmap.HasNamespace(String.Empty)) { // Default Namespace Defined output = nsmap.GetNamespaceUri(String.Empty).AbsoluteUri + qname.Substring(1); } else if (allowDefaultPrefixFallback) { // No Default Namespace so use Base Uri // These type of QNames are scoped to the local Uri regardless of the type of the Base Uri // i.e. these always result in Hash URIs if (baseUri != null) { output = baseUri.AbsoluteUri; if (output.EndsWith("#")) { output += qname.Substring(1); } else { output += "#" + qname.Substring(1); } } else { throw new RdfParseException("Cannot resolve the QName '" + qname + "' in the Default Namespace when there is no in-scope Base URI and no Default Namespace defined. Did you forget to define a namespace for the : prefix?"); } } else { throw new RdfParseException("Cannot resolve the QName '" + qname + "' in the Default Namespace since the namespace is not defined. Did you to forget to define a namespace for the : prefix?"); } } else { // QName in some other Namespace String[] parts = qname.Split(new char[] { ':' }, 2); if (parts.Length == 1) { output = nsmap.GetNamespaceUri(String.Empty).AbsoluteUri + parts[0]; } else { output = nsmap.GetNamespaceUri(parts[0]).AbsoluteUri + parts[1]; } } return(output); }
private static void EnsureReaderNamespaces(IGraph graph) { lock (graph) { INamespaceMapper nm = graph.NamespaceMap; lock (nm) if (!nm.HasNamespace(RoboKindPrefix)) { lock (rdfDefNS) nm.Import(rdfDefNS); } lock (nm) EnsureBaseURIMapped(graph, nm); } }
/// <summary> /// Resolves a QName into a Uri using the Namespace Mapper and Base Uri provided /// </summary> /// <param name="qname">QName to resolve</param> /// <param name="nsmap">Namespace Map to resolve against</param> /// <param name="baseUri">Base Uri to resolve against</param> /// <returns></returns> public static String ResolveQName(String qname, INamespaceMapper nsmap, Uri baseUri) { String output; if (qname.StartsWith(":")) { //QName in Default Namespace if (nsmap.HasNamespace(String.Empty)) { //Default Namespace Defined output = nsmap.GetNamespaceUri(String.Empty).ToString() + qname.Substring(1); } else { //No Default Namespace so use Base Uri //These type of QNames are scoped to the local Uri regardless of the type of the Base Uri //i.e. these always result in Hash URIs if (baseUri != null) { output = baseUri.ToString(); if (output.EndsWith("#")) { output += qname.Substring(1); } else { output += "#" + qname.Substring(1); } } else { throw new RdfParseException("Cannot resolve a QName in the Default Namespace when there is no in-scope Base URI and no Default Namespace defined"); } } } else { //QName in some other Namespace String[] parts = qname.Split(new char[] { ':' }, 2); if (parts.Length == 1) { output = nsmap.GetNamespaceUri(String.Empty).ToString() + parts[0]; } else { output = nsmap.GetNamespaceUri(parts[0]).ToString() + parts[1]; } } return(output); }
/// <summary> /// Checks whether an attribute is an rdf:resource attribute. /// </summary> /// <param name="attr">Attribute to Test.</param> /// <param name="nsMapper">The namespace prefix mappings to use when expanding the namespace prefix of the attribute.</param> /// <returns>True if is an rdf:resource attribute.</returns> public static bool IsResourceAttribute(AttributeEvent attr, INamespaceMapper nsMapper) { // QName must be rdf:resource if (nsMapper.HasNamespace(attr.Namespace) && nsMapper.GetNamespaceUri(attr.Namespace).ToString().Equals(NamespaceMapper.RDF) && attr.LocalName.Equals("resource")) { // Must be a valid RDF Uri Reference return(IsRdfUriReference(attr.Value)); } else { return(false); } }
/// <summary> /// Checks whether a given element is a valid property element. /// </summary> /// <param name="e">The element to test.</param> /// <param name="nsMapper">The namespace mappings to use when expanding element QName prefixes.</param> /// <returns>True if the element is valid.</returns> public static bool IsPropertyElement(ElementEvent e, INamespaceMapper nsMapper) { if (nsMapper.HasNamespace(e.Namespace)) { var nsUri = nsMapper.GetNamespaceUri(e.Namespace); // Not allowed to be a Core Syntax Term, rdf:Description or an Old Syntax Term if (IsCoreSyntaxTerm(nsUri, e.LocalName) || IsOldTerm(nsUri, e.LocalName) || nsUri.ToString().Equals(NamespaceMapper.RDF) && e.LocalName.Equals("Description")) { return(false); } } // Any other URIs are allowed return(true); }
/// <summary> /// Checks whether an attribute is an rdf:nodeID attribute. /// </summary> /// <param name="attr">Attribute to Test.</param> /// <param name="nsMapper">The namespace prefix mappings to use when expanding the namespace prefix of the attribute.</param> /// <returns>True if is an rdf:nodeID attribute.</returns> /// <remarks>Does some validation on ID value but other validation occurs at other points in the Parsing.</remarks> public static bool IsNodeIDAttribute(AttributeEvent attr, INamespaceMapper nsMapper) { // QName must be rdf:nodeID if (nsMapper.HasNamespace(attr.Namespace) && nsMapper.GetNamespaceUri(attr.Namespace).ToString().Equals(NamespaceMapper.RDF) && attr.LocalName.Equals("nodeID")) { // Must be a valid RDF ID if (IsRdfID(attr.Value)) { // OK return(true); } else { // Invalid RDF ID so Error throw ParserHelper.Error("The value '" + attr.Value + "' for rdf:id is not valid, RDF IDs can only be valid NCNames as defined by the W3C XML Namespaces specification", attr); } } else { return(false); } }
/// <summary> /// Checks whether an attribute is an rdf:type attribute. /// </summary> /// <param name="attr">The attribute to check.</param> /// <param name="nsMapper">The namespace prefix mappings to use to expand QNames.</param> /// <returns>True if the attribute is and rdf:type attribute, false otherwise.</returns> public static bool IsTypeAttribute(AttributeEvent attr, INamespaceMapper nsMapper) { return(nsMapper.HasNamespace(attr.Namespace) && IsRdfNamespace(nsMapper.GetNamespaceUri(attr.Namespace)) && attr.LocalName.Equals("type")); }
/// <summary> /// Checks whether a give element is an rdf:li element. /// </summary> /// <param name="e">The element to test.</param> /// <param name="nsMapper">The namespace mappings to use when expanding element QName prefixes.</param> /// <returns>True if the element is an rdf:li element, false otherwise.</returns> public static bool IsLiElement(ElementEvent e, INamespaceMapper nsMapper) { return(nsMapper.HasNamespace(e.Namespace) && IsRdfNamespace(nsMapper.GetNamespaceUri(e.Namespace)) && e.LocalName.Equals("li")); }
/// <summary> /// Resolves a QName into a Uri using the Namespace Mapper and Base Uri provided /// </summary> /// <param name="qname">QName to resolve</param> /// <param name="nsmap">Namespace Map to resolve against</param> /// <param name="baseUri">Base Uri to resolve against</param> /// <returns></returns> public static String ResolveQName(String qname, INamespaceMapper nsmap, Uri baseUri) { String output; if (qname.StartsWith(":")) { //QName in Default Namespace if (nsmap.HasNamespace(String.Empty)) { //Default Namespace Defined output = nsmap.GetNamespaceUri(String.Empty).ToString() + qname.Substring(1); } else { //No Default Namespace so use Base Uri //These type of QNames are scoped to the local Uri regardless of the type of the Base Uri //i.e. these always result in Hash URIs if (baseUri != null) { output = baseUri.ToString(); if (output.EndsWith("#")) { output += qname.Substring(1); } else { output += "#" + qname.Substring(1); } } else { throw new RdfParseException("Cannot resolve a QName in the Default Namespace when there is no in-scope Base URI and no Default Namespace defined"); } } } else { //QName in some other Namespace String[] parts = qname.Split(':'); if (parts.Length == 1) { output = nsmap.GetNamespaceUri(String.Empty).ToString() + parts[0]; } else { output = nsmap.GetNamespaceUri(parts[0]).ToString() + parts[1]; } } return output; }
public static void LoadGraphPrefixes(INamespaceMapper nm) { if (nm.HasNamespace(RoboKindPrefix)) { return; } string s = @" @prefix : <#> . @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . @prefix daml: <http://www.daml.org/2001/03/daml+oil#> . @prefix log: <http://www.w3.org/2000/10/swap/log.n3#> . @prefix foaf: <http://xmlns.com/foaf/0.1/> . @prefix owl: <http://www.w3.org/2002/07/owl#> . @prefix virtrdf: <http://www.openlinksw.com/schemas/virtrdf#> . @prefix sswap: <http://sswapmeet.sswap.info/sswap/#> . @prefix sioc: <http://rdfs.org/sioc/ns#> . @prefix sioct: <http://rdfs.org/sioc/types#> . @prefix atom: <http://atomowl.org/ontologies/atomrdf#> . @prefix dc: <http://purl.org/dc/elements/1.1/> . @prefix dct: <http://purl.org/dc/terms/> . @prefix skos: <http://www.w3.org/2004/02/skos/core#> . @prefix geo: <http://www.w3.org/2003/01/geo/wgs84_pos#> . @prefix wikiont: <http://sw.deri.org/2005/04/wikipedia/wikiont.owl#> . @prefix aowl: <http://atomowl.org/ontologies/atomrdf#> . @prefix v: <http://www.openlinksw.com/schemas/drupal_v#> . @prefix sd: <http://www.w3.org/ns/sparql-service-description#> . @prefix dbpprop: <http://dbpedia.org/property/> . @prefix dbpedia-owl: <http://dbpedia.org/ontology/> . @prefix dc: <http://purl.org/dc/elements/1.1/> . @prefix grddl: <http://www.w3.org/2003/g/data-view#> . @prefix xml: <http://www.w3.org/XML/1998/namespace> . @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . @prefix dnr: <http://www.dotnetrdf.org/configuration#> . @prefix dotnetrdf: <http://www.dotnetrdf.org/configuration#> . @prefix dc: <http://purl.org/dc/elements/1.1/>. @prefix dcterms: <http://www.purl.org/dc/terms/>. @prefix vann: <http://purl.org/vocab/vann/>. @prefix vs: <http://www.w3.org/2003/06/sw-vocab-status/ns#>. @prefix fmt: <http://www.w3.org/ns/formats/>. @prefix siprolog: <http://" + CogbotServerWithPort + @"/siprolog#> . @prefix robokind: <" + RoboKindURI + @"> . "; string ss = @" fn http://www.w3.org/2005/xpath-functions gmlxbt http://www.opengis.net/gml/3.3/xbt sch http://www.ascc.net/xml/schematron # gml http://www.opengis.net/gml/3.2 gml http://www.opengis.net/gml/_ gmd http://www.isotc211.org/2005/gmd xlink http://www.w3.org/1999/xlink xsl http://www.w3.org/1999/XSL/Transform rdf http://www.w3.org/1999/02/22-rdf-syntax-ns# p3q http://www.w3.org/2004/01/rdxh/p3q-ns-example p3qr http://www.example.org/P3Q-rdf# p3dr http://www.example.org/TR/P3P/base# ont http://www.daml.org/2001/03/daml+oil# s http://schema.org/ xsd http://www.w3.org/2001/XMLSchema# eco http://www.ebusiness-unibw.org/ontologies/eclass/5.1.4/ gr http://purl.org/goodrelations/v1# dc http://purl.org/dc/elements/1.1/ ao http://purl.org/ao/core/ aoa http://purl.org/ao/annotea/ aof http://purl.org/ao/foaf/ aold http://biotea.ws/ontologies/aold/ aos http://purl.org/ao/selectors/ aot http://purl.org/ao/types/ bibo http://purl.org/ontology/bibo/ bif bif: bio2rdf_mesh http://bio2rdf.org/ns/mesh# bio2rdf_ns http://bio2rdf.org/ns/bio2rdf# chebi http://purl.obolibrary.org/obo/CHEBI_ cnt http://www.w3.org/2011/content# dawgt http://www.w3.org/2001/sw/DataAccess/tests/test-dawg# dbpedia http://dbpedia.org/resource/ dbpprop http://dbpedia.org/property/ dc http://purl.org/dc/elements/1.1/ dcterms http://purl.org/dc/terms/ doco http://purl.org/spar/doco/ fma http://purl.org/obo/owl/FMA#FMA_ fn http://www.w3.org/2005/xpath-functions/# foaf http://xmlns.com/foaf/0.1/ geo http://www.w3.org/2003/01/geo/wgs84_pos# go http://purl.org/obo/owl/GO#GO_ gw_property http://genewikiplus.org/wiki/Special:URIResolver/Property-3A gw_wiki http://genewikiplus.org/wiki/Special:URIResolver/ icd9 http://purl.bioontology.org/ontology/ICD9-9/ math http://www.w3.org/2000/10/swap/math# mddb http://purl.bioontology.org/ontology/MDDB/ meddra http://purl.bioontology.org/ontology/MDR/ medline http://purl.bioontology.org/ontology/MEDLINEPLUS/ mesh http://purl.org/commons/record/mesh/ mf http://www.w3.org/2001/sw/DataAccess/tests/test-manifest# mged http://mged.sourceforge.net/ontologies/MGEDOntology.owl# ncbitaxon http://purl.org/obo/owl/NCBITaxon#NCBITaxon_ ncithesaurus http://ncicb.nci.nih.gov/xml/owl/EVS/Thesaurus.owl# nddf http://purl.bioontology.org/ontology/NDDF/ ndfrt http://purl.bioontology.org/ontology/NDFRT/ obi http://purl.obolibrary.org/obo/OBI_ obo http://www.geneontology.org/formats/oboInOwl# omim http://purl.bioontology.org/ontology/OMIM/ owl http://www.w3.org/2002/07/owl# pav http://purl.org/swan/pav/provenance/ po http://purl.bioontology.org/ontology/PO/ product http://www.buy.com/rss/module/productV2/ protseq http://purl.org/science/protein/bysequence/ prov http://www.w3.org/ns/prov# pw http://purl.org/obo/owl/PW#PW_ rdf http://www.w3.org/1999/02/22-rdf-syntax-ns# rdfa http://www.w3.org/ns/rdfa# rdfdf http://www.openlinksw.com/virtrdf-data-formats# rdfs http://www.w3.org/2000/01/rdf-schema# sc http://purl.org/science/owl/sciencecommons/ scovo http://purl.org/NET/scovo# sioc http://rdfs.org/sioc/ns# skos http://www.w3.org/2004/02/skos/core# snomed http://purl.bioontology.org/ontology/SNOMEDCT/ sql sql: swivt http://semantic-mediawiki.org/swivt/1.0# symptom http://purl.org/obo/owl/SYMP#SYMP_ taxonomy http://www.uniprot.org/taxonomy/ umls http://berkeleybop.org/obo/UMLS: uniprot http://purl.uniprot.org/core/ vcard http://www.w3.org/2001/vcard-rdf/3.0# vcard2006 http://www.w3.org/2006/vcard/ns# virtcxml http://www.openlinksw.com/schemas/virtcxml# virtrdf http://www.openlinksw.com/schemas/virtrdf# void http://rdfs.org/ns/void# xf http://www.w3.org/2004/07/xpath-functions xml http://www.w3.org/XML/1998/namespace xsd http://www.w3.org/2001/XMLSchema# xsl10 http://www.w3.org/XSL/Transform/1.0 xsl1999 http://www.w3.org/1999/XSL/Transform xslwd http://www.w3.org/TR/WD-xsl xsp http://www.owl-ontologies.com/2005/08/07/xsp.owl# yago http://dbpedia.org/class/yago/ "; ss = s + ss; foreach (string s00 in ss.Split('\n', '\r')) { if (String.IsNullOrEmpty(s00)) { continue; } var s0 = s00.Replace('\t', ' ').Trim(); if (s0.StartsWith("#")) { continue; } if (s0.StartsWith("@prefix ")) { s0 = s0.Substring("@prefix ".Length).TrimStart(); s0 = s0.TrimEnd('.', ' '); } while (s0.Contains(" ")) { s0 = s0.Replace(" ", " "); } if (String.IsNullOrEmpty(s0)) { continue; } int spc = s0.IndexOf(' '); string prefix = s0.Substring(0, spc).Trim().TrimEnd(' ', ':'); string uri = s0.Substring(spc).Trim(); if (uri.StartsWith("<") && uri.EndsWith(">")) { uri = uri.Substring(1, uri.Length - 2).Trim(); } if (uri == "#") { continue; // uri = graph.BaseUri.AbsoluteUri; } if (String.IsNullOrEmpty(prefix)) { continue; prefix = ":";//"Warn("adding null prefix " + uri); } if (nm.HasNamespace(prefix)) { var prev = nm.GetNamespaceUri(prefix).ToString(); if (prev != uri) { if (uri.Length < prev.Length) { continue; } } } nm.AddNamespace(prefix, new Uri(uri)); } }
/// <summary> /// Get column name by using qname compression and replacing special chars with underscores /// </summary> /// <param name="node">Node</param> /// <param name="namespaces">Namespaces</param> /// <returns></returns> private string GetColumnName(INode node, INamespaceMapper namespaces) { if (node.NodeType != NodeType.Uri) { return(null); } Uri u = ((IUriNode)node).Uri; String qname; if (!namespaces.ReduceToQName(u.AbsoluteUri, out qname)) { // Issue temporary namespaces String tempNsPrefix = "ns" + this._nextTempID; String nsUri = u.AbsoluteUri; if (nsUri.Contains("#")) { //Create a Hash Namespace Uri nsUri = nsUri.Substring(0, nsUri.LastIndexOf("#") + 1); } else if (nsUri.Contains("/")) { //Create a Slash Namespace Uri nsUri = nsUri.Substring(0, nsUri.LastIndexOf("/") + 1); } Uri tempNsUri; if (!Uri.TryCreate(nsUri, UriKind.Absolute, out tempNsUri)) { tempNsUri = u; } while (namespaces.HasNamespace(tempNsPrefix)) { this._nextTempID++; tempNsPrefix = "ns" + this._nextTempID; } namespaces.AddNamespace(tempNsPrefix, tempNsUri); if (!namespaces.ReduceToQName(u.AbsoluteUri, out qname)) { qname = null; } } if (qname == null) { // Issue temporary column name return("entityPredicateColumn" + (++this._nextTempColumnID)); } // Sanitize qname column name qname = qname.StartsWith("_") ? qname.Substring(1) : qname.Replace(':', '_'); if (SparqlSpecsHelper.IsValidVarName(qname)) { return(qname); } if (qname.Length == 0) { // Issue temporary column name return("entityPredicateColumn " + (++this._nextTempColumnID)); } // Clean up illegal characters // First Character must be from PN_CHARS_U or a digit // Add a leading p for invalid first characters char[] cs = qname.ToCharArray(); char first = cs[0]; if (!Char.IsDigit(first) || !SparqlSpecsHelper.IsPNCharsU(first)) { qname = "p" + qname; if (SparqlSpecsHelper.IsValidVarName(qname)) { return(qname); } cs = qname.ToCharArray(); } for (int i = 1; i < cs.Length; i++) { if (i >= cs.Length - 1) { continue; } // Subsequent Chars must be from PN_CHARS (except -) or a '.' // Replace invalid characters with _ if (cs[i] == '.' || cs[i] == '-') { cs[i] = '_'; } else if (!SparqlSpecsHelper.IsPNChars(cs[i])) { cs[i] = '_'; } } // Trim trailing underscores return(new string(cs).TrimEnd('_')); }