private static void HandleTriple(IRdfHandler handler, string subject, string predicate, string obj, string datatype, bool isLiteral) { INode subjectNode = (subject.StartsWith("_") ? (INode)handler.CreateBlankNode(subject.Substring(2)) : handler.CreateUriNode(new Uri(subject))); INode predicateNode = handler.CreateUriNode(new Uri(predicate)); INode objNode; if (isLiteral) { if (datatype == "http://www.w3.org/2001/XMLSchema#boolean") { obj = obj.ToLowerInvariant(); } objNode = (datatype == null ? handler.CreateLiteralNode(obj) : handler.CreateLiteralNode(obj, new Uri(datatype))); } else { objNode = (obj.StartsWith("_") ? (INode)handler.CreateBlankNode(obj.Substring(2)) : handler.CreateUriNode(new Uri(obj))); } if (!handler.HandleTriple(new Triple(subjectNode, predicateNode, objNode))) { throw new InvalidOperationException(String.Format("Could not add triple {0} {1} {2} .", subjectNode, predicateNode, objNode)); } }
private void TryParseTriple(IRdfHandler handler, IToken s, IToken p, IToken o, Uri graphUri) { INode subj, pred, obj; switch (s.TokenType) { case Token.BLANKNODEWITHID: subj = handler.CreateBlankNode(s.Value.Substring(2)); break; case Token.URI: subj = ParserHelper.TryResolveUri(handler, s); break; default: throw ParserHelper.Error("Unexpected Token '" + s.GetType().ToString() + "' encountered, expected a Blank Node/URI as the Subject of a Triple", s); } switch (p.TokenType) { case Token.URI: pred = ParserHelper.TryResolveUri(handler, p); break; default: throw ParserHelper.Error("Unexpected Token '" + p.GetType().ToString() + "' encountered, expected a URI as the Predicate of a Triple", p); } switch (o.TokenType) { case Token.BLANKNODEWITHID: obj = handler.CreateBlankNode(o.Value.Substring(2)); break; case Token.LITERAL: obj = handler.CreateLiteralNode(o.Value); break; case Token.LITERALWITHDT: String dtUri = ((LiteralWithDataTypeToken)o).DataType; obj = handler.CreateLiteralNode(o.Value, new Uri(dtUri.Substring(1, dtUri.Length - 2))); break; case Token.LITERALWITHLANG: obj = handler.CreateLiteralNode(o.Value, ((LiteralWithLanguageSpecifierToken)o).Language); break; case Token.URI: obj = ParserHelper.TryResolveUri(handler, o); break; default: throw ParserHelper.Error("Unexpected Token '" + o.GetType().ToString() + "' encountered, expected a Blank Node/Literal/URI as the Object of a Triple", o); } if (!handler.HandleTriple(new Triple(subj, pred, obj, graphUri))) { ParserHelper.Stop(); } }
void IXmlNodeProcessor <INode> .ProcessDocumentType(string publicId, string systemId, string internalSubset, bool useAsNamespace, INode baseNode, ref INode defaultNamespace) { INode baseType = null; if (publicId != null) { var publicNode = rdf.CreateUriNode(UriTools.CreatePublicId(publicId)); rdf.HandleTriple(publicNode, label, rdf.CreateLiteralNode(publicId, xpublic)); if (systemId != null) { rdf.HandleTriple(publicNode, isDefinedBy, MakeAbsoluteNode(baseNode, systemId)); } if (useAsNamespace && UseDtdAsDefaultNamespace) { defaultNamespace = defaultNamespace ?? publicNode; } baseType = publicNode; } else if (systemId != null) { var systemNode = MakeAbsoluteNode(baseNode, systemId); baseType = BlankNode("doctype"); rdf.HandleTriple(baseType, isDefinedBy, systemNode); //if(useAsNamespace && UseDtdAsDefaultNamespace) defaultNamespace = defaultNamespace ?? systemNode; } INode subsetType = null; if (!String.IsNullOrWhiteSpace(internalSubset)) { subsetType = rdf.CreateBlankNode("dtd"); rdf.HandleTriple(subsetType, value, rdf.CreateLiteralNode(internalSubset)); if (baseType != null) { rdf.HandleTriple(subsetType, subClassOf, baseType); } baseType = subsetType; } if (baseType != null) { rdf.HandleTriple(baseNode, a, baseType); } }
/// <summary> /// Creates and handles a triple /// </summary> /// <param name="handler">Handler</param> /// <param name="subject">Subject</param> /// <param name="predicate">Predicate</param> /// <param name="obj">Object</param> /// <param name="datatype">Object Datatype</param> /// <param name="isLiteral">isLiteral Object</param> /// <returns>True if parsing should continue, false otherwise</returns> bool HandleTriple(IRdfHandler handler, string subject, string predicate, string obj, string datatype, bool isLiteral) { INode subjectNode; if (subject.StartsWith("_")) { string nodeId = subject.Substring(subject.IndexOf(":") + 1); subjectNode = handler.CreateBlankNode(nodeId); } else { subjectNode = handler.CreateUriNode(new Uri(subject)); } INode predicateNode = handler.CreateUriNode(new Uri(predicate)); INode objNode; if (isLiteral) { if (datatype == "http://www.w3.org/2001/XMLSchema#boolean") { // sometimes newtonsoft.json appears to return boolean as string True and dotNetRdf doesn't appear to recognize that obj = ((string)obj).ToLowerInvariant(); } objNode = (datatype == null) ? handler.CreateLiteralNode((string)obj) : handler.CreateLiteralNode((string)obj, new Uri(datatype)); } else { if (obj.StartsWith("_")) { string nodeId = obj.Substring(obj.IndexOf(":") + 1); objNode = handler.CreateBlankNode(nodeId); } else { objNode = handler.CreateUriNode(new Uri(obj)); } } return(handler.HandleTriple(new Triple(subjectNode, predicateNode, objNode))); }
private void TryParseTriple(IRdfHandler handler, IToken s, IToken p, IToken o, Uri graphUri) { INode subj, pred, obj; switch (s.TokenType) { case Token.BLANKNODEWITHID: subj = handler.CreateBlankNode(s.Value.Substring(2)); break; case Token.URI: subj = ParserHelper.TryResolveUri(handler, s); break; default: throw ParserHelper.Error("Unexpected Token '" + s.GetType().ToString() + "' encountered, expected a Blank Node/URI as the Subject of a Triple", s); } switch (p.TokenType) { case Token.URI: pred = ParserHelper.TryResolveUri(handler, p); break; default: throw ParserHelper.Error("Unexpected Token '" + p.GetType().ToString() + "' encountered, expected a URI as the Predicate of a Triple", p); } switch (o.TokenType) { case Token.BLANKNODEWITHID: obj = handler.CreateBlankNode(o.Value.Substring(2)); break; case Token.LITERAL: obj = handler.CreateLiteralNode(o.Value); break; case Token.LITERALWITHDT: String dtUri = ((LiteralWithDataTypeToken)o).DataType; obj = handler.CreateLiteralNode(o.Value, new Uri(dtUri.Substring(1, dtUri.Length - 2))); break; case Token.LITERALWITHLANG: obj = handler.CreateLiteralNode(o.Value, ((LiteralWithLanguageSpecifierToken)o).Language); break; case Token.URI: obj = ParserHelper.TryResolveUri(handler, o); break; default: throw ParserHelper.Error("Unexpected Token '" + o.GetType().ToString() + "' encountered, expected a Blank Node/Literal/URI as the Object of a Triple", o); } if (!handler.HandleTriple(new Triple(subj, pred, obj, graphUri))) throw ParserHelper.Stop(); }
/// <summary> /// Creates and handles a triple /// </summary> /// <param name="handler">Handler</param> /// <param name="subject">Subject</param> /// <param name="predicate">Predicate</param> /// <param name="obj">Object</param> /// <param name="datatype">Object Datatype</param> /// <returns>True if parsing should continue, false otherwise</returns> bool HandleTriple(IRdfHandler handler, string subject, string predicate, string obj, string datatype, bool isLiteral) { INode subjectNode; if (subject.StartsWith("_")) { string nodeId = subject.Substring(subject.IndexOf(":") + 1); subjectNode = handler.CreateBlankNode(nodeId); } else { subjectNode = handler.CreateUriNode(new Uri(subject)); } INode predicateNode = handler.CreateUriNode(new Uri(predicate)); INode objNode; if (isLiteral) { objNode = (datatype == null) ? handler.CreateLiteralNode((string)obj) : handler.CreateLiteralNode((string)obj, new Uri(datatype)); } else { if (obj.StartsWith("_")) { string nodeId = obj.Substring(obj.IndexOf(":") + 1); objNode = handler.CreateBlankNode(nodeId); } else { objNode = handler.CreateUriNode(new Uri(obj)); } } return handler.HandleTriple(new Triple(subjectNode, predicateNode, objNode)); }
/// <summary> /// Creates and handles a triple /// </summary> /// <param name="handler">Handler</param> /// <param name="subject">Subject</param> /// <param name="predicate">Predicate</param> /// <param name="obj">Object</param> /// <param name="datatype">Object Datatype</param> /// <param name="isLiteral">isLiteral Object</param> /// <returns>True if parsing should continue, false otherwise</returns> bool HandleTriple(IRdfHandler handler, string subject, string predicate, string obj, string datatype, bool isLiteral) { INode subjectNode; if (subject.StartsWith("_")) { string nodeId = subject.Substring(subject.IndexOf(":") + 1); subjectNode = handler.CreateBlankNode(nodeId); } else { subjectNode = handler.CreateUriNode(new Uri(subject)); } INode predicateNode = handler.CreateUriNode(new Uri(predicate)); INode objNode; if (isLiteral) { if (datatype == "http://www.w3.org/2001/XMLSchema#boolean") { // sometimes newtonsoft.json appears to return boolean as string True and dotNetRdf doesn't appear to recognize that obj = ((string)obj).ToLowerInvariant(); } objNode = (datatype == null) ? handler.CreateLiteralNode((string)obj) : handler.CreateLiteralNode((string)obj, new Uri(datatype)); } else { if (obj.StartsWith("_")) { string nodeId = obj.Substring(obj.IndexOf(":") + 1); objNode = handler.CreateBlankNode(nodeId); } else { objNode = handler.CreateUriNode(new Uri(obj)); } } return handler.HandleTriple(new Triple(subjectNode, predicateNode, objNode)); }
private Uri TryParseContext(IRdfHandler handler, ITokenQueue tokens) { IToken next = tokens.Dequeue(); if (next.TokenType == Token.DOT) { return(null); } INode context; switch (next.TokenType) { case Token.BLANKNODEWITHID: context = handler.CreateBlankNode(next.Value.Substring(2)); break; case Token.URI: context = TryParseUri(handler, next.Value); break; case Token.LITERAL: if (this.Syntax != NQuadsSyntax.Original) { throw new RdfParseException("Only a Blank Node/URI may be used as the graph name in RDF NQuads 1.1"); } //Check for Datatype/Language IToken temp = tokens.Peek(); switch (temp.TokenType) { case Token.LANGSPEC: tokens.Dequeue(); context = handler.CreateLiteralNode(next.Value, temp.Value); break; case Token.DATATYPE: tokens.Dequeue(); context = handler.CreateLiteralNode(next.Value, ((IUriNode)TryParseUri(handler, temp.Value.Substring(1, temp.Value.Length - 2))).Uri); break; default: context = handler.CreateLiteralNode(next.Value); break; } break; default: throw ParserHelper.Error("Unexpected Token '" + next.GetType().ToString() + "' encountered, expected a Blank Node/Literal/URI as the Context of the Triple", next); } //Ensure we then see a . to terminate the Quad next = tokens.Dequeue(); if (next.TokenType != Token.DOT) { throw ParserHelper.Error("Unexpected Token '" + next.GetType().ToString() + "' encountered, expected a Dot Token (Line Terminator) to terminate a Triple", next); } //Finally return the Context URI if (context.NodeType == NodeType.Uri) { return(((IUriNode)context).Uri); } else if (context.NodeType == NodeType.Blank) { return(UriFactory.Create("nquads:bnode:" + context.GetHashCode())); } else if (context.NodeType == NodeType.Literal) { return(UriFactory.Create("nquads:literal:" + context.GetHashCode())); } else { throw ParserHelper.Error("Cannot turn a Node of type '" + context.GetType().ToString() + "' into a Context URI for a Triple", next); } }
private INode TryParseNode(XmlReader reader, IRdfHandler handler, TripleSegment segment) { //Only need to Read() if getting the Subject //The previous calls will have resulted in us already reading to the start element for this node if (segment == TripleSegment.Subject) { reader.Read(); } if (reader.NodeType != XmlNodeType.Element) { if (reader.NodeType == XmlNodeType.EndElement) { throw Error("Unexpected end element while trying to parse the nodes of a <triple> element", reader); } } if (reader.Name.Equals("uri")) { return(handler.CreateUriNode(new Uri(reader.ReadInnerXml()))); } else if (reader.Name.Equals("id")) { if (segment == TripleSegment.Predicate) { throw Error("Unexpected element <" + reader.Name + "> encountered, expected a <uri> element as the Predicate of a Triple", reader); } return(handler.CreateBlankNode(reader.ReadInnerXml())); } else if (reader.Name.Equals("plainLiteral")) { if (segment == TripleSegment.Subject) { throw Error("Unexpected element <" + reader.Name + "> encountered, expected a <id>/<uri> element as the Subject of a Triple", reader); } if (reader.AttributeCount > 0) { String lang = String.Empty; for (int i = 0; i < reader.AttributeCount; i++) { reader.MoveToNextAttribute(); if (reader.Name.Equals("xml:lang")) { lang = reader.Value; } } reader.MoveToContent(); if (!lang.Equals(String.Empty)) { return(handler.CreateLiteralNode(reader.ReadElementContentAsString(), lang)); } else { return(handler.CreateLiteralNode(reader.ReadElementContentAsString())); } } else { return(handler.CreateLiteralNode(reader.ReadElementContentAsString())); } } else if (reader.Name.Equals("typedLiteral")) { if (reader.AttributeCount > 0) { Uri dtUri = null; for (int i = 0; i < reader.AttributeCount; i++) { reader.MoveToNextAttribute(); if (reader.Name.Equals("datatype")) { dtUri = new Uri(reader.Value); } } if (dtUri == null) { throw Error("<typedLiteral> element does not have the required datatype attribute", reader); } reader.MoveToContent(); return(handler.CreateLiteralNode(reader.ReadInnerXml(), dtUri)); } else { throw Error("<typedLiteral> element does not have the required datatype attribute", reader); } } else { throw Error("Unexpected element <" + reader.Name + "> encountered, expected a <id>/<uri>/<plainLiteral>/<typedLiteral> element as part of a Triple", reader); } }
private void TryParseTriple(XmlNode tripleEl, IRdfHandler handler, Uri graphUri) { //Verify Node Name if (!tripleEl.Name.Equals("triple")) { throw new RdfParseException("Unexpected Element <" + tripleEl.Name + "> encountered, only an optional <uri> element followed by zero/more <triple> elements are permitted within a <graph> element"); } //Verify number of Child Nodes if (!tripleEl.HasChildNodes) { throw new RdfParseException("<triple> element has no child nodes - 3 child nodes are expected"); } if (tripleEl.ChildNodes.Count < 3) { throw new RdfParseException("<triple> element has too few child nodes (" + tripleEl.ChildNodes.Count + ") - 3 child nodes are expected"); } if (tripleEl.ChildNodes.Count > 3) { throw new RdfParseException("<triple> element has too many child nodes (" + tripleEl.ChildNodes.Count + ") - 3 child nodes are expected"); } //Get the 3 Child Nodes XmlNode subjEl, predEl, objEl; subjEl = tripleEl.ChildNodes[0]; predEl = tripleEl.ChildNodes[1]; objEl = tripleEl.ChildNodes[2]; //Parse XML Nodes into RDF Nodes INode subj, pred, obj; if (subjEl.Name.Equals("uri")) { subj = handler.CreateUriNode(UriFactory.Create(subjEl.InnerText)); } else if (subjEl.Name.Equals("id")) { subj = handler.CreateBlankNode(subjEl.InnerText); } else { throw Error("Unexpected element <" + subjEl.Name + "> encountered, expected a <id>/<uri> element as the Subject of a Triple", subjEl); } if (predEl.Name.Equals("uri")) { pred = handler.CreateUriNode(UriFactory.Create(predEl.InnerText)); } else { throw Error("Unexpected element <" + predEl.Name + "> encountered, expected a <uri> element as the Predicate of a Triple", subjEl); } if (objEl.Name.Equals("uri")) { obj = handler.CreateUriNode(UriFactory.Create(objEl.InnerText)); } else if (objEl.Name.Equals("id")) { obj = handler.CreateBlankNode(objEl.InnerText); } else if (objEl.Name.Equals("plainLiteral")) { if (objEl.Attributes.GetNamedItem("xml:lang") != null) { obj = handler.CreateLiteralNode(objEl.InnerText, objEl.Attributes["xml:lang"].Value); } else { obj = handler.CreateLiteralNode(objEl.InnerText); } } else if (objEl.Name.Equals("typedLiteral")) { if (objEl.Attributes.GetNamedItem("datatype") != null) { Uri dtUri = UriFactory.Create(objEl.Attributes["datatype"].Value); if (objEl.FirstChild.NodeType == XmlNodeType.Text) { obj = handler.CreateLiteralNode(objEl.InnerText, dtUri); } else if (objEl.FirstChild.NodeType == XmlNodeType.CDATA) { obj = handler.CreateLiteralNode(objEl.FirstChild.InnerXml, dtUri); } else { obj = handler.CreateLiteralNode(objEl.InnerText, dtUri); } } else { throw Error("<typedLiteral> element does not have the required datatype attribute", objEl); } } else { throw Error("Unexpected element <" + objEl.Name + "> encountered, expected a <id>/<uri>/<plainLiteral>/<typedLiteral> element as the Object of a Triple", subjEl); } //Assert the resulting Triple if (!handler.HandleTriple(new Triple(subj, pred, obj, graphUri))) { ParserHelper.Stop(); } ; }
private INode TryParseNode(XmlReader reader, IRdfHandler handler, TripleSegment segment) { //Only need to Read() if getting the Subject //The previous calls will have resulted in us already reading to the start element for this node if (segment == TripleSegment.Subject) reader.Read(); if (reader.NodeType != XmlNodeType.Element) { if (reader.NodeType == XmlNodeType.EndElement) throw Error("Unexpected end element while trying to parse the nodes of a <triple> element", reader); } if (reader.Name.Equals("uri")) { return handler.CreateUriNode(new Uri(reader.ReadInnerXml())); } else if (reader.Name.Equals("id")) { if (segment == TripleSegment.Predicate) throw Error("Unexpected element <" + reader.Name + "> encountered, expected a <uri> element as the Predicate of a Triple", reader); return handler.CreateBlankNode(reader.ReadInnerXml()); } else if (reader.Name.Equals("plainLiteral")) { if (segment == TripleSegment.Subject) throw Error("Unexpected element <" + reader.Name + "> encountered, expected a <id>/<uri> element as the Subject of a Triple", reader); if (reader.AttributeCount > 0) { String lang = String.Empty; for (int i = 0; i < reader.AttributeCount; i++) { reader.MoveToNextAttribute(); if (reader.Name.Equals("xml:lang")) lang = reader.Value; } reader.MoveToContent(); if (!lang.Equals(String.Empty)) { return handler.CreateLiteralNode(reader.ReadInnerXml(), lang); } else { return handler.CreateLiteralNode(reader.ReadInnerXml()); } } else { return handler.CreateLiteralNode(reader.ReadInnerXml()); } } else if (reader.Name.Equals("typedLiteral")) { if (reader.AttributeCount > 0) { Uri dtUri = null; for (int i = 0; i < reader.AttributeCount; i++) { reader.MoveToNextAttribute(); if (reader.Name.Equals("datatype")) dtUri = new Uri(reader.Value); } if (dtUri == null) throw Error("<typedLiteral> element does not have the required datatype attribute", reader); reader.MoveToContent(); return handler.CreateLiteralNode(reader.ReadInnerXml(), dtUri); } else { throw Error("<typedLiteral> element does not have the required datatype attribute", reader); } } else { throw Error("Unexpected element <" + reader.Name + "> encountered, expected a <id>/<uri>/<plainLiteral>/<typedLiteral> element as part of a Triple", reader); } }
private void TryParseTriple(XmlNode tripleEl, IRdfHandler handler, Uri graphUri) { //Verify Node Name if (!tripleEl.Name.Equals("triple")) { throw new RdfParseException("Unexpected Element <" + tripleEl.Name + "> encountered, only an optional <uri> element followed by zero/more <triple> elements are permitted within a <graph> element"); } //Verify number of Child Nodes if (!tripleEl.HasChildNodes) throw new RdfParseException("<triple> element has no child nodes - 3 child nodes are expected"); if (tripleEl.ChildNodes.Count < 3) throw new RdfParseException("<triple> element has too few child nodes (" + tripleEl.ChildNodes.Count + ") - 3 child nodes are expected"); if (tripleEl.ChildNodes.Count > 3) throw new RdfParseException("<triple> element has too many child nodes (" + tripleEl.ChildNodes.Count + ") - 3 child nodes are expected"); //Get the 3 Child Nodes XmlNode subjEl, predEl, objEl; subjEl = tripleEl.ChildNodes[0]; predEl = tripleEl.ChildNodes[1]; objEl = tripleEl.ChildNodes[2]; //Parse XML Nodes into RDF Nodes INode subj, pred, obj; if (subjEl.Name.Equals("uri")) { subj = handler.CreateUriNode(new Uri(subjEl.InnerText)); } else if (subjEl.Name.Equals("id")) { subj = handler.CreateBlankNode(subjEl.InnerText); } else { throw Error("Unexpected element <" + subjEl.Name + "> encountered, expected a <id>/<uri> element as the Subject of a Triple", subjEl); } if (predEl.Name.Equals("uri")) { pred = handler.CreateUriNode(new Uri(predEl.InnerText)); } else { throw Error("Unexpected element <" + predEl.Name + "> encountered, expected a <uri> element as the Predicate of a Triple", subjEl); } if (objEl.Name.Equals("uri")) { obj = handler.CreateUriNode(new Uri(objEl.InnerText)); } else if (objEl.Name.Equals("id")) { obj = handler.CreateBlankNode(objEl.InnerText); } else if (objEl.Name.Equals("plainLiteral")) { if (objEl.Attributes.GetNamedItem("xml:lang") != null) { obj = handler.CreateLiteralNode(objEl.InnerText, objEl.Attributes["xml:lang"].Value); } else { obj = handler.CreateLiteralNode(objEl.InnerText); } } else if (objEl.Name.Equals("typedLiteral")) { if (objEl.Attributes.GetNamedItem("datatype") != null) { Uri dtUri = new Uri(objEl.Attributes["datatype"].Value); if (objEl.FirstChild.NodeType == XmlNodeType.Text) { obj = handler.CreateLiteralNode(objEl.InnerText, dtUri); } else if (objEl.FirstChild.NodeType == XmlNodeType.CDATA) { obj = handler.CreateLiteralNode(objEl.FirstChild.InnerXml, dtUri); } else { obj = handler.CreateLiteralNode(objEl.InnerText, dtUri); } } else { throw Error("<typedLiteral> element does not have the required datatype attribute", objEl); } } else { throw Error("Unexpected element <" + objEl.Name + "> encountered, expected a <id>/<uri>/<plainLiteral>/<typedLiteral> element as the Object of a Triple", subjEl); } //Assert the resulting Triple if (!handler.HandleTriple(new Triple(subj, pred, obj, graphUri))) throw ParserHelper.Stop(); ; }
private INode TryParseNode(XmlReader reader, IRdfHandler handler, TripleSegment segment) { // Only need to Read() if getting the Subject // The previous calls will have resulted in us already reading to the start element for this node if (segment == TripleSegment.Subject) { reader.Read(); } ValidateNamespace(reader); if (reader.NamespaceURI != TriXNamespaceURI) { throw Error("Unexpected element namespace " + reader.NamespaceURI + ".", reader); } if (reader.LocalName.Equals("uri")) { return(handler.CreateUriNode(new Uri(reader.ReadInnerXml()))); } if (reader.LocalName.Equals("id")) { if (segment == TripleSegment.Predicate) { throw Error("Unexpected element <" + reader.Name + "> encountered, expected a <uri> element as the Predicate of a Triple", reader); } return(handler.CreateBlankNode(reader.ReadInnerXml())); } if (reader.LocalName.Equals("plainLiteral")) { if (segment == TripleSegment.Subject) { throw Error("Unexpected element <" + reader.Name + "> encountered, expected a <id>/<uri> element as the Subject of a Triple", reader); } if (reader.AttributeCount > 0) { var lang = string.Empty; for (var i = 0; i < reader.AttributeCount; i++) { reader.MoveToNextAttribute(); if (reader.Name.Equals("xml:lang")) { lang = reader.Value; } } reader.MoveToContent(); if (!lang.Equals(string.Empty)) { return(handler.CreateLiteralNode(reader.ReadElementContentAsString(), lang)); } return(handler.CreateLiteralNode(reader.ReadElementContentAsString())); } return(handler.CreateLiteralNode(reader.ReadElementContentAsString())); } if (reader.LocalName.Equals("typedLiteral")) { if (reader.AttributeCount > 0) { Uri dtUri = null; for (var i = 0; i < reader.AttributeCount; i++) { reader.MoveToNextAttribute(); if (reader.Name.Equals("datatype")) { dtUri = new Uri(reader.Value); } } if (dtUri == null) { throw Error("<typedLiteral> element does not have the required datatype attribute", reader); } reader.MoveToContent(); // KA: Why ReadInnerXml here and not in other places? // return handler.CreateLiteralNode(reader.ReadInnerXml(), dtUri); return(handler.CreateLiteralNode(reader.ReadElementContentAsString(), dtUri)); } throw Error("<typedLiteral> element does not have the required datatype attribute", reader); } throw Error("Unexpected element <" + reader.Name + "> encountered, expected a <id>/<uri>/<plainLiteral>/<typedLiteral> element as part of a Triple", reader); }
private static INode MakeNode(IRdfHandler handler, JToken token, bool allowRelativeIri = false) { if (token is JValue) { var stringValue = token.Value <string>(); if (JsonLdProcessor.IsBlankNodeIdentifier(stringValue)) { return(handler.CreateBlankNode(stringValue.Substring(2))); } if (Uri.TryCreate(stringValue, allowRelativeIri ? UriKind.RelativeOrAbsolute : UriKind.Absolute, out Uri iri)) { return(handler.CreateUriNode(iri)); } return(null); } else if (JsonLdProcessor.IsValueObject(token)) { string literalValue = null; var valueObject = token as JObject; var value = valueObject["@value"]; var datatype = valueObject.Property("@type")?.Value.Value <string>(); var language = valueObject.Property("@language")?.Value.Value <string>(); if (value.Type == JTokenType.Boolean) { literalValue = value.Value <bool>() ? "true" : "false"; if (datatype == null) { datatype = XsdNs + "boolean"; } } else if (value.Type == JTokenType.Float || value.Type == JTokenType.Integer && datatype != null && datatype.Equals(XsdNs + "double")) { literalValue = value.Value <double>().ToString("E15", CultureInfo.InvariantCulture); literalValue = ExponentialFormatMatcher.Replace(literalValue, "$1E"); if (literalValue.EndsWith("E")) { literalValue = literalValue + "0"; } if (datatype == null) { datatype = XsdNs + "double"; } } else if (value.Type == JTokenType.Integer || value.Type == JTokenType.Float && datatype != null && datatype.Equals(XsdNs + "integer")) { literalValue = value.Value <long>().ToString("D", CultureInfo.InvariantCulture); if (datatype == null) { datatype = XsdNs + "integer"; } } else { literalValue = value.Value <string>(); if (datatype == null && language == null) { datatype = XsdNs + "string"; } } return(language == null?handler.CreateLiteralNode(literalValue, new Uri(datatype)) : handler.CreateLiteralNode(literalValue, language)); } else if (JsonLdProcessor.IsListObject(token)) { var listArray = token["@list"] as JArray; return(MakeRdfList(handler, listArray)); } else if ((token as JObject)?.Property("@id") != null) { // Must be a node object var nodeObject = (JObject)token; return(MakeNode(handler, nodeObject["@id"])); } return(null); }
private INode MakeNode(IRdfHandler handler, JToken token, Uri graphIri, bool allowRelativeIri = false) { if (token is JValue) { var stringValue = token.Value <string>(); if (JsonLdUtils.IsBlankNodeIdentifier(stringValue)) { return(handler.CreateBlankNode(stringValue.Substring(2))); } if (Uri.TryCreate(stringValue, allowRelativeIri ? UriKind.RelativeOrAbsolute : UriKind.Absolute, out Uri iri)) { if (!Uri.IsWellFormedUriString(stringValue, allowRelativeIri ? UriKind.RelativeOrAbsolute : UriKind.Absolute)) { return(null); } return(handler.CreateUriNode(iri)); } return(null); } if (JsonLdUtils.IsValueObject(token)) { string literalValue = null; var valueObject = token as JObject; var value = valueObject["@value"]; var datatype = valueObject.Property("@type")?.Value.Value <string>(); var language = valueObject.Property("@language")?.Value.Value <string>(); if (datatype == "@json") { datatype = RdfNs + "JSON"; var serializer = new JsonLiteralSerializer(); literalValue = serializer.Serialize(value); } else if (value.Type == JTokenType.Boolean) { literalValue = value.Value <bool>() ? "true" : "false"; if (datatype == null) { datatype = XsdNs + "boolean"; } } else if (value.Type == JTokenType.Float || value.Type == JTokenType.Integer && datatype != null && datatype.Equals(XsdNs + "double")) { var doubleValue = value.Value <double>(); var roundedValue = Math.Round(doubleValue); if (doubleValue.Equals(roundedValue) && doubleValue < 1000000000000000000000.0 && datatype == null) { // Integer values up to 10^21 should be rendered as a fixed-point integer literalValue = roundedValue.ToString("F0"); datatype = XsdNs + "integer"; } else { literalValue = value.Value <double>().ToString("E15", CultureInfo.InvariantCulture); literalValue = ExponentialFormatMatcher.Replace(literalValue, "$1E"); if (literalValue.EndsWith("E")) { literalValue = literalValue + "0"; } if (datatype == null) { datatype = XsdNs + "double"; } } } else if (value.Type == JTokenType.Integer || value.Type == JTokenType.Float && datatype != null && datatype.Equals(XsdNs + "integer")) { literalValue = value.Value <long>().ToString("D", CultureInfo.InvariantCulture); if (datatype == null) { datatype = XsdNs + "integer"; } } else if (valueObject.ContainsKey("@direction") && ParserOptions.RdfDirection.HasValue) { literalValue = value.Value <string>(); var direction = valueObject["@direction"].Value <string>(); language = valueObject.ContainsKey("@language") ? valueObject["@language"].Value <string>().ToLowerInvariant() : string.Empty; if (ParserOptions.RdfDirection == JsonLdRdfDirectionMode.I18NDatatype) { datatype = "https://www.w3.org/ns/i18n#" + language + "_" + direction; return(handler.CreateLiteralNode(literalValue, new Uri(datatype))); } // Otherwise direction mode is CompoundLiteral var literalNode = handler.CreateBlankNode(); var xsdString = UriFactory.Create(XmlSpecsHelper.XmlSchemaDataTypeString); handler.HandleTriple(new Triple( literalNode, handler.CreateUriNode(UriFactory.Create(RdfSpecsHelper.RdfValue)), handler.CreateLiteralNode(literalValue, xsdString), graphIri)); if (!string.IsNullOrEmpty(language)) { handler.HandleTriple(new Triple( literalNode, handler.CreateUriNode(UriFactory.Create(RdfSpecsHelper.RdfLanguage)), handler.CreateLiteralNode(language, xsdString), graphIri)); } handler.HandleTriple(new Triple( literalNode, handler.CreateUriNode(UriFactory.Create(RdfSpecsHelper.RdfDirection)), handler.CreateLiteralNode(direction, xsdString), graphIri)); return(literalNode); } else { literalValue = value.Value <string>(); if (datatype == null && language == null) { datatype = XsdNs + "string"; } } if (language != null) { return(LanguageTag.IsWellFormed(language) ? handler.CreateLiteralNode(literalValue, language) : null); } return(handler.CreateLiteralNode(literalValue, new Uri(datatype))); } if (JsonLdUtils.IsListObject(token)) { var listArray = token["@list"] as JArray; return(MakeRdfList(handler, listArray, graphIri)); } if ((token as JObject)?.Property("@id") != null) { // Must be a node object var nodeObject = (JObject)token; return(MakeNode(handler, nodeObject["@id"], graphIri)); } return(null); }