public void WritingCollectionCompressionCyclic3() { Graph g = new Graph(); g.NamespaceMap.AddNamespace("ex", new Uri("http://example.org/")); g.NamespaceMap.AddNamespace("dnr", new Uri(ConfigurationLoader.ConfigurationNamespace)); INode a = g.CreateBlankNode(); INode b = g.CreateBlankNode(); INode c = g.CreateBlankNode(); INode d = g.CreateBlankNode(); INode e = g.CreateBlankNode(); INode pred = g.CreateUriNode("ex:pred"); g.Assert(d, pred, a); g.Assert(d, pred, g.CreateLiteralNode("D")); g.Assert(a, pred, b); g.Assert(a, pred, g.CreateLiteralNode("A")); g.Assert(b, pred, c); g.Assert(b, pred, g.CreateLiteralNode("B")); g.Assert(c, pred, a); g.Assert(c, pred, g.CreateLiteralNode("C")); g.Assert(e, pred, g.CreateLiteralNode("E")); CompressingTurtleWriterContext context = new CompressingTurtleWriterContext(g, Console.Out); WriterHelper.FindCollections(context); Assert.Equal(3, context.Collections.Count); this.CheckCompressionRoundTrip(g); }
public void WritingCollectionCompressionCyclic() { Graph g = new Graph(); g.NamespaceMap.AddNamespace("ex", new Uri("http://example.org/")); g.NamespaceMap.AddNamespace("dnr", new Uri(ConfigurationLoader.ConfigurationNamespace)); INode a = g.CreateBlankNode(); INode b = g.CreateBlankNode(); INode c = g.CreateBlankNode(); INode pred = g.CreateUriNode("ex:pred"); g.Assert(a, pred, b); g.Assert(a, pred, g.CreateLiteralNode("Value for A")); g.Assert(b, pred, c); g.Assert(b, pred, g.CreateLiteralNode("Value for B")); g.Assert(c, pred, a); g.Assert(c, pred, g.CreateLiteralNode("Value for C")); CompressingTurtleWriterContext context = new CompressingTurtleWriterContext(g, Console.Out); WriterHelper.FindCollections(context); Assert.AreEqual(2, context.Collections.Count, "Expected 2 collections (one should be eliminated to break the cycle)"); this.CheckCompressionRoundTrip(g); }
public void WritingXmlAmpersandEscaping() { List <String> inputs = new List <string>() { "&value", "&", "&", "&value&next", "& &squot; < > ' \"", new String('&', 1000) }; List <String> outputs = new List <string>() { "&value", "&", "&", "&value&next", "& &squot; < > ' "" }; StringBuilder temp = new StringBuilder(); for (int i = 0; i < 1000; i++) { temp.Append("&"); } outputs.Add(temp.ToString()); for (int i = 0; i < inputs.Count; i++) { Console.WriteLine("Input: " + inputs[i] + " - Expected Output: " + outputs[i] + " - Actual Output: " + WriterHelper.EncodeForXml(inputs[i])); Assert.AreEqual(outputs[i], WriterHelper.EncodeForXml(inputs[i]), "Ampersands should have been encoded correctly"); } }
public void WritingCollectionCompressionComplex2() { Graph g = new Graph(); g.LoadFromFile("resources\\complex-collections.nt"); CompressingTurtleWriterContext context = new CompressingTurtleWriterContext(g, Console.Out); WriterHelper.FindCollections(context); NTriplesFormatter formatter = new NTriplesFormatter(); foreach (KeyValuePair <INode, OutputRdfCollection> kvp in context.Collections) { _output.WriteLine("Collection Root - " + kvp.Key.ToString(formatter)); _output.WriteLine("Collection Triples (" + kvp.Value.Triples.Count + ")"); foreach (Triple t in kvp.Value.Triples) { _output.WriteLine(t.ToString(formatter)); } _output.WriteLine(""); } this.CheckCompressionRoundTrip(g); }
private void GenerateLiteralOutput(RdfXmlWriterContext context, ILiteralNode lit, XmlElement pred, XmlDocument doc) { pred.InnerText = WriterHelper.EncodeForXml(lit.Value); //pred.InnerText = XmlConvert.ToString(lit.Value); if (!lit.Language.Equals(String.Empty)) { XmlAttribute lang = doc.CreateAttribute("xml:lang"); lang.Value = lit.Language; pred.Attributes.Append(lang); } else if (lit.DataType != null) { if (RdfSpecsHelper.RdfXmlLiteral.Equals(lit.DataType.ToString())) { XmlAttribute parseType = doc.CreateAttribute("rdf:parseType"); parseType.Value = "Literal"; pred.Attributes.Append(parseType); pred.InnerText = String.Empty; XmlDocumentFragment fragment = doc.CreateDocumentFragment(); fragment.InnerXml = lit.Value; pred.AppendChild(fragment); } else { XmlAttribute dt = doc.CreateAttribute("rdf:datatype"); dt.Value = Uri.EscapeUriString(lit.DataType.ToString()); pred.Attributes.Append(dt); } } }
private void GenerateUriOutput(RdfXmlWriterContext context, IUriNode u, String attribute) { // Get a Uri Reference if the Uri can be reduced UriRefType rtype; String uriref = this.GenerateUriRef(context, u.Uri, UriRefType.UriRef, out rtype); if (attribute.Contains(':')) { context.Writer.WriteStartAttribute(attribute.Substring(0, attribute.IndexOf(':')), attribute.Substring(attribute.IndexOf(':') + 1), NamespaceMapper.RDF); if (rtype == UriRefType.UriRef) { context.Writer.WriteRaw(WriterHelper.EncodeForXml(uriref));//Uri.EscapeUriString(WriterHelper.EncodeForXml(uriref))); } else { context.Writer.WriteString(uriref);//Uri.EscapeUriString(uriref)); } context.Writer.WriteEndAttribute(); } else { context.Writer.WriteStartAttribute(attribute); if (rtype == UriRefType.UriRef) { context.Writer.WriteRaw(WriterHelper.EncodeForXml(uriref));//Uri.EscapeUriString(WriterHelper.EncodeForXml(uriref))); } else { context.Writer.WriteString(uriref);//Uri.EscapeUriString(uriref)); } context.Writer.WriteEndAttribute(); } }
public void WritingCollectionCompressionNamedListNodes2() { Graph g = new Graph(); g.NamespaceMap.AddNamespace("ex", new Uri("http://example.org/")); INode n = g.CreateUriNode("ex:listRoot"); INode m = g.CreateUriNode("ex:listItem"); INode rdfType = g.CreateUriNode("rdf:type"); INode rdfFirst = g.CreateUriNode("rdf:first"); INode rdfRest = g.CreateUriNode("rdf:rest"); INode rdfNil = g.CreateUriNode("rdf:nil"); g.Assert(g.CreateUriNode("ex:subj"), g.CreateUriNode("ex:pred"), n); g.Assert(n, rdfFirst, g.CreateLiteralNode("first")); g.Assert(n, rdfRest, m); g.Assert(m, rdfFirst, g.CreateLiteralNode("second")); g.Assert(m, rdfRest, rdfNil); CompressingTurtleWriterContext context = new CompressingTurtleWriterContext(g, Console.Out); WriterHelper.FindCollections(context); Assert.Equal(0, context.Collections.Count); this.CheckCompressionRoundTrip(g); }
/// <summary> /// Generates the Output for a Graph as a String in CSV syntax /// </summary> /// <param name="globalContext">Context for writing the Store</param> /// <param name="context">Context for writing the Graph</param> /// <returns></returns> private String GenerateGraphOutput(ThreadedStoreWriterContext globalContext, BaseWriterContext context) { if (!WriterHelper.IsDefaultGraph(context.Graph.BaseUri)) { //Named Graphs have a fourth context field added foreach (Triple t in context.Graph.Triples) { this.GenerateNodeOutput(context, t.Subject, TripleSegment.Subject); context.Output.Write(','); this.GenerateNodeOutput(context, t.Predicate, TripleSegment.Predicate); context.Output.Write(','); this.GenerateNodeOutput(context, t.Object, TripleSegment.Object); context.Output.Write(','); context.Output.Write(this._formatter.FormatUri(context.Graph.BaseUri)); context.Output.Write("\r\n"); } } else { //Default Graph has an empty field added foreach (Triple t in context.Graph.Triples) { this.GenerateNodeOutput(context, t.Subject, TripleSegment.Subject); context.Output.Write(','); this.GenerateNodeOutput(context, t.Predicate, TripleSegment.Predicate); context.Output.Write(','); this.GenerateNodeOutput(context, t.Object, TripleSegment.Object); context.Output.Write(','); context.Output.Write("\r\n"); } } return(context.Output.ToString()); }
private Dictionary <INode, OutputRdfCollection> FindCollections(IGraph g) { var sw = new System.IO.StringWriter(); var context = new CompressingTurtleWriterContext(g, sw); _output.WriteLine(sw.ToString()); WriterHelper.FindCollections(context); return(context.Collections); }
/// <summary> /// Thread Worker method which writes Graphs to the output /// </summary> /// <param name="globalContext">Context for writing the Store</param> private void SaveGraphs(TriGWriterContext globalContext) { try { Uri u = globalContext.GetNextUri(); while (u != null) { //Get the Graph from the Store if (WriterHelper.IsDefaultGraph(u) && !globalContext.Store.HasGraph(u)) { u = null; } IGraph g = globalContext.Store.Graphs[u]; //Generate the Graph Output and add to Stream TurtleWriterContext context = new TurtleWriterContext(g, new System.IO.StringWriter(), globalContext.PrettyPrint, globalContext.HighSpeedModePermitted); if (globalContext.CompressionLevel > WriterCompressionLevel.None) { context.NodeFormatter = new TurtleFormatter(globalContext.QNameMapper); } else { context.NodeFormatter = new UncompressedTurtleFormatter(); } String graphContent = this.GenerateGraphOutput(globalContext, context); try { Monitor.Enter(globalContext.Output); globalContext.Output.WriteLine(graphContent); globalContext.Output.Flush(); } catch { throw; } finally { Monitor.Exit(globalContext.Output); } //Get the Next Uri u = globalContext.GetNextUri(); } } catch (ThreadAbortException) { //We've been terminated, don't do anything #if !SILVERLIGHT Thread.ResetAbort(); #endif } catch (Exception ex) { throw new RdfStorageException("Error in Threaded Writer in Thread ID " + Thread.CurrentThread.ManagedThreadId, ex); } }
private Dictionary <INode, String> FindTypeReferences(RdfXmlWriterContext context) { // LINQ query to find all Triples which define the rdf:type of a Uri/BNode as a Uri IUriNode rdfType = context.Graph.CreateUriNode(UriFactory.Create(NamespaceMapper.RDF + "type")); IEnumerable <Triple> ts = from t in context.Graph.Triples where (t.Subject.NodeType == NodeType.Blank || t.Subject.NodeType == NodeType.Uri) && t.Predicate.Equals(rdfType) && t.Object.NodeType == NodeType.Uri && !context.TriplesDone.Contains(t) select t; Dictionary <INode, String> typerefs = new Dictionary <INode, string>(); foreach (Triple t in ts) { if (!typerefs.ContainsKey(t.Subject)) { String typeref; UriRefType rtype; typeref = this.GenerateUriRef(context, ((IUriNode)t.Object).Uri, UriRefType.QName, out rtype); if (rtype != UriRefType.QName) { // Generate a Temporary Namespace for the QName Type Reference String prefix, uri; this.GenerateTemporaryNamespace(context, (IUriNode)t.Object, out prefix, out uri); // Add to current XML Element context.Writer.WriteStartAttribute("xmlns", prefix, null); context.Writer.WriteRaw(Uri.EscapeUriString(WriterHelper.EncodeForXml(uri))); context.Writer.WriteEndAttribute(); typeref = this.GenerateUriRef(context, ((IUriNode)t.Object).Uri, UriRefType.QName, out rtype); if (rtype == UriRefType.QName) { // Got a QName Type Reference in the Temporary Namespace OK typerefs.Add(t.Subject, typeref); if (context.Graph.Triples.WithSubject(t.Subject).Count() > 1) { context.TriplesDone.Add(t); } } } else { // Got a QName Type Reference OK typerefs.Add(t.Subject, typeref); if (context.Graph.Triples.WithSubject(t.Subject).Count() > 1) { context.TriplesDone.Add(t); } } } } return(typerefs); }
private String EncodeStyle(String value) { value = WriterHelper.EncodeForXml(value); if (value.EndsWith("&")) { value += "amp;"; } value = value.Replace("\"", "'"); value = value.Replace("<", "<"); value = value.Replace(">", ">"); return(value); }
private void GenerateUriOutput(RdfXmlWriterContext context, IUriNode u, String attribute, List <String> tempNamespaceIDs, XmlElement node, XmlDocument doc) { //Create an attribute XmlAttribute attr = doc.CreateAttribute(attribute, NamespaceMapper.RDF); //Get a Uri Reference if the Uri can be reduced UriRefType rtype; String uriref = this.GenerateUriRef(context, u, UriRefType.UriRef, tempNamespaceIDs, out rtype); attr.InnerXml = Uri.EscapeUriString(WriterHelper.EncodeForXml(uriref)); //Append the attribute node.Attributes.Append(attr); }
private void NodeToTriX(INode n, XmlWriter writer) { switch (n.NodeType) { case NodeType.Blank: writer.WriteStartElement("id"); writer.WriteRaw(WriterHelper.EncodeForXml(((IBlankNode)n).InternalID)); writer.WriteEndElement(); break; case NodeType.GraphLiteral: throw new RdfOutputException(WriterErrorMessages.GraphLiteralsUnserializable("TriX")); case NodeType.Literal: ILiteralNode lit = (ILiteralNode)n; if (lit.DataType != null) { writer.WriteStartElement("typedLiteral"); writer.WriteStartAttribute("datatype"); writer.WriteRaw(WriterHelper.EncodeForXml(lit.DataType.AbsoluteUri)); writer.WriteEndAttribute(); if (lit.DataType.AbsoluteUri.Equals(RdfSpecsHelper.RdfXmlLiteral)) { writer.WriteCData(lit.Value); } else { writer.WriteRaw(WriterHelper.EncodeForXml(lit.Value)); } writer.WriteEndElement(); } else { writer.WriteStartElement("plainLiteral"); if (!lit.Language.Equals(String.Empty)) { writer.WriteAttributeString("xml", "lang", null, lit.Language); } writer.WriteRaw(WriterHelper.EncodeForXml(lit.Value)); writer.WriteEndElement(); } break; case NodeType.Uri: writer.WriteStartElement("uri"); writer.WriteRaw(WriterHelper.EncodeForXml(((IUriNode)n).Uri.AbsoluteUri)); writer.WriteEndElement(); break; default: throw new RdfOutputException(WriterErrorMessages.UnknownNodeTypeUnserializable("TriX")); } }
/// <summary> /// Thread Worker method which writes Graphs to the output /// </summary> /// <param name="globalContext">Context for writing the Store</param> private void SaveGraphs(ThreadedStoreWriterContext globalContext) { try { Uri u = globalContext.GetNextUri(); while (u != null) { //Get the Graph from the Store if (WriterHelper.IsDefaultGraph(u) && !globalContext.Store.HasGraph(u)) { u = null; } IGraph g = globalContext.Store.Graphs[u]; //Generate the Graph Output and add to Stream NTriplesWriterContext context = new NTriplesWriterContext(g, new System.IO.StringWriter(), globalContext.PrettyPrint, globalContext.HighSpeedModePermitted); String graphContent = this.GraphToNQuads(globalContext, context); if (!graphContent.Equals(String.Empty)) { try { Monitor.Enter(globalContext.Output); globalContext.Output.WriteLine(graphContent); globalContext.Output.Flush(); } catch { throw; } finally { Monitor.Exit(globalContext.Output); } } //Get the Next Uri u = globalContext.GetNextUri(); } } catch (ThreadAbortException) { //We've been terminated, don't do anything #if !SILVERLIGHT Thread.ResetAbort(); #endif } catch (Exception ex) { throw new RdfStorageException("Error in Threaded Writer in Thread ID " + Thread.CurrentThread.ManagedThreadId, ex); } }
private void GeneratePredicateNode(RdfXmlWriterContext context, INode p) { switch (p.NodeType) { case NodeType.GraphLiteral: throw new RdfOutputException(WriterErrorMessages.GraphLiteralsUnserializable("RDF/XML")); case NodeType.Blank: throw new RdfOutputException(WriterErrorMessages.BlankPredicatesUnserializable("RDF/XML")); case NodeType.Literal: throw new RdfOutputException(WriterErrorMessages.LiteralPredicatesUnserializable("RDF/XML")); case NodeType.Uri: //OK UriRefType rtype; String predRef = this.GenerateUriRef(context, ((IUriNode)p).Uri, UriRefType.QName, out rtype); String prefix, uri; prefix = uri = null; if (rtype != UriRefType.QName) { this.GenerateTemporaryNamespace(context, (IUriNode)p, out prefix, out uri); predRef = this.GenerateUriRef(context, ((IUriNode)p).Uri, UriRefType.QName, out rtype); if (rtype != UriRefType.QName) { throw new RdfOutputException(WriterErrorMessages.UnreducablePropertyURIUnserializable + " - '" + p.ToString() + "'"); } } this.GenerateElement(context, predRef); //Add Temporary Namespace to current XML Element if (prefix != null && uri != null) { context.Writer.WriteStartAttribute("xmlns", prefix, null); context.Writer.WriteRaw(Uri.EscapeUriString(WriterHelper.EncodeForXml(uri))); context.Writer.WriteEndAttribute(); } break; default: throw new RdfOutputException(WriterErrorMessages.UnknownNodeTypeUnserializable("RDF/XML")); } //Write the Predicate }
public void WritingCollectionCompressionEmpty2() { Graph g = new Graph(); g.NamespaceMap.AddNamespace("ex", new Uri("http://example.org/")); INode rdfType = g.CreateUriNode("rdf:type"); g.Assert(g.CreateUriNode("ex:subj"), g.CreateUriNode("ex:pred"), g.CreateUriNode("rdf:nil")); CompressingTurtleWriterContext context = new CompressingTurtleWriterContext(g, Console.Out); WriterHelper.FindCollections(context); Assert.Equal(0, context.Collections.Count); this.CheckCompressionRoundTrip(g); }
public void WritingCollectionCompressionEmpty1() { Graph g = new Graph(); g.NamespaceMap.AddNamespace("ex", new Uri("http://example.org/")); INode n = g.CreateBlankNode(); g.Assert(g.CreateUriNode("ex:subj"), g.CreateUriNode("ex:pred"), n); CompressingTurtleWriterContext context = new CompressingTurtleWriterContext(g, Console.Out); WriterHelper.FindCollections(context); Assert.Single(context.Collections); Assert.Empty(context.Collections.First().Value.Triples); this.CheckCompressionRoundTrip(g); }
private String GraphToNQuads(ThreadedStoreWriterContext globalContext, NTriplesWriterContext context) { if (context.Graph.IsEmpty) { return(String.Empty); } if (context.PrettyPrint && !WriterHelper.IsDefaultGraph(context.Graph.BaseUri)) { context.Output.WriteLine("# Graph: " + context.Graph.BaseUri.ToString()); } foreach (Triple t in context.Graph.Triples) { context.Output.WriteLine(this.TripleToNQuads(context, t)); } context.Output.WriteLine(); return(context.Output.ToString()); }
public void WritingCollectionCompressionEmpty1() { Graph g = new Graph(); g.NamespaceMap.AddNamespace("ex", new Uri("http://example.org/")); INode n = g.CreateBlankNode(); INode rdfType = g.CreateUriNode("rdf:type"); g.Assert(g.CreateUriNode("ex:subj"), g.CreateUriNode("ex:pred"), n); CompressingTurtleWriterContext context = new CompressingTurtleWriterContext(g, Console.Out); WriterHelper.FindCollections(context); Assert.AreEqual(1, context.Collections.Count, "Expected 1 Collection to be found"); Assert.AreEqual(0, context.Collections.First().Value.Triples.Count, "Expected no Triples to be in the collection"); this.CheckCompressionRoundTrip(g); }
public void WritingCollectionCompressionSimple6() { Graph g = new Graph(); g.NamespaceMap.AddNamespace("ex", new Uri("http://example.org/")); INode n = g.CreateBlankNode(); INode rdfType = g.CreateUriNode("rdf:type"); g.Assert(n, rdfType, g.CreateUriNode("ex:Obj")); CompressingTurtleWriterContext context = new CompressingTurtleWriterContext(g, Console.Out); WriterHelper.FindCollections(context); Assert.Equal(1, context.Collections.Count); Assert.Equal(0, context.Collections.First().Value.Triples.Count); this.CheckCompressionRoundTrip(g); }
/// <summary> /// Converts a Triple into relevant NQuads Syntax /// </summary> /// <param name="context">Writer Context</param> /// <param name="t">Triple to convert</param> /// <returns></returns> private String TripleToNQuads(NTriplesWriterContext context, Triple t) { StringBuilder output = new StringBuilder(); output.Append(this.NodeToNTriples(context, t.Subject, TripleSegment.Subject)); output.Append(" "); output.Append(this.NodeToNTriples(context, t.Predicate, TripleSegment.Predicate)); output.Append(" "); output.Append(this.NodeToNTriples(context, t.Object, TripleSegment.Object)); if (t.GraphUri != null && !WriterHelper.IsDefaultGraph(t.GraphUri)) { output.Append(" <"); output.Append(context.UriFormatter.FormatUri(t.GraphUri)); output.Append(">"); } output.Append(" ."); return(output.ToString()); }
public void WritingCollectionCompressionSimple4() { Graph g = new Graph(); g.NamespaceMap.AddNamespace("ex", new Uri("http://example.org/")); INode n = g.CreateBlankNode(); INode rdfType = g.CreateUriNode("rdf:type"); INode rdfFirst = g.CreateUriNode("rdf:first"); INode rdfRest = g.CreateUriNode("rdf:rest"); INode rdfNil = g.CreateUriNode("rdf:nil"); g.Assert(n, rdfFirst, g.CreateLiteralNode("first")); g.Assert(n, rdfRest, rdfNil); CompressingTurtleWriterContext context = new CompressingTurtleWriterContext(g, Console.Out); WriterHelper.FindCollections(context); Assert.Empty(context.Collections); this.CheckCompressionRoundTrip(g); }
private void GraphToTriX(IGraph g, XmlWriter writer) { // Create the <graph> element writer.WriteStartElement("graph"); // Is the Graph Named? if (g.BaseUri != null) { if (!g.BaseUri.AbsoluteUri.StartsWith("trix:local:")) { writer.WriteStartElement("uri"); writer.WriteRaw(WriterHelper.EncodeForXml(g.BaseUri.AbsoluteUri)); writer.WriteEndElement(); } else { writer.WriteStartElement("id"); writer.WriteRaw(WriterHelper.EncodeForXml(g.BaseUri.AbsoluteUri.Substring(11))); writer.WriteEndElement(); } } // Output the Triples foreach (Triple t in g.Triples) { writer.WriteStartElement("triple"); this.NodeToTriX(t.Subject, writer); this.NodeToTriX(t.Predicate, writer); this.NodeToTriX(t.Object, writer); // </triple> writer.WriteEndElement(); } // </graph> writer.WriteEndElement(); }
public void WritingCollectionCompressionComplex1() { SparqlConnector connector = new SparqlConnector(new VDS.RDF.Query.SparqlRemoteEndpoint(new Uri("http://dbpedia.org/sparql"))); Graph g = new Graph(); g.NamespaceMap.AddNamespace("ex", new Uri("http://example.org/")); g.NamespaceMap.AddNamespace("dnr", new Uri(ConfigurationLoader.ConfigurationNamespace)); INode n = g.CreateBlankNode(); g.Assert(g.CreateUriNode("ex:subj"), g.CreateUriNode("dnr:genericManager"), n); ConfigurationSerializationContext sContext = new ConfigurationSerializationContext(g); sContext.NextSubject = n; connector.SerializeConfiguration(sContext); CompressingTurtleWriterContext context = new CompressingTurtleWriterContext(g, Console.Out); WriterHelper.FindCollections(context); Assert.Equal(2, context.Collections.Count); this.CheckCompressionRoundTrip(g); }
/// <summary> /// Generates the Output for a Graph as a String in TriG syntax /// </summary> /// <param name="globalContext">Context for writing the Store</param> /// <param name="context">Context for writing the Graph</param> /// <returns></returns> private String GenerateGraphOutput(TriGWriterContext globalContext, TurtleWriterContext context) { if (!WriterHelper.IsDefaultGraph(context.Graph.BaseUri)) { //Named Graph String gname; String sep = (globalContext.N3CompatabilityMode) ? " = " : " "; if (globalContext.CompressionLevel > WriterCompressionLevel.None && globalContext.QNameMapper.ReduceToQName(context.Graph.BaseUri.ToString(), out gname)) { if (TurtleSpecsHelper.IsValidQName(gname)) { context.Output.WriteLine(gname + sep + "{"); } else { context.Output.WriteLine("<" + context.UriFormatter.FormatUri(context.Graph.BaseUri) + ">" + sep + "{"); } } else { context.Output.WriteLine("<" + context.UriFormatter.FormatUri(context.Graph.BaseUri) + ">" + sep + "{"); } } else { context.Output.WriteLine("{"); } //Generate Triples this.GenerateTripleOutput(globalContext, context); //Close the Graph context.Output.WriteLine("}"); return(context.Output.ToString()); }
/// <summary> /// Internal method which generates the RDF/Json Output for a Graph. /// </summary> /// <param name="g">Graph to save.</param> /// <param name="output">Stream to save to.</param> private void GenerateOutput(IGraph g, TextWriter output) { // Always force RDF Namespace to be correctly defined g.NamespaceMap.Import(_defaultNamespaces); g.NamespaceMap.AddNamespace("rdf", UriFactory.Create(NamespaceMapper.RDF)); // Create our Writer Context and start the XML Document RdfXmlWriterContext context = new RdfXmlWriterContext(g, output); context.CompressionLevel = _compressionLevel; context.UseDtd = _useDTD; context.UseAttributes = _useAttributes; context.Writer.WriteStartDocument(); if (context.UseDtd) { // Create the DOCTYPE declaration StringBuilder entities = new StringBuilder(); String uri; entities.Append('\n'); foreach (String prefix in context.NamespaceMap.Prefixes) { uri = context.NamespaceMap.GetNamespaceUri(prefix).AbsoluteUri; if (!uri.Equals(context.NamespaceMap.GetNamespaceUri(prefix).ToString())) { context.UseDtd = false; break; } if (!prefix.Equals(String.Empty)) { entities.AppendLine("\t<!ENTITY " + prefix + " '" + uri + "'>"); } } if (context.UseDtd) { context.Writer.WriteDocType("rdf:RDF", null, null, entities.ToString()); } } // Create the rdf:RDF element context.Writer.WriteStartElement("rdf", "RDF", NamespaceMapper.RDF); if (context.Graph.BaseUri != null) { context.Writer.WriteAttributeString("xml", "base", null, context.Graph.BaseUri.AbsoluteUri); } // Add all the existing Namespace Definitions here context.NamespaceMap.IncrementNesting(); foreach (String prefix in context.NamespaceMap.Prefixes) { if (prefix.Equals("rdf")) { continue; } if (!prefix.Equals(String.Empty)) { context.Writer.WriteStartAttribute("xmlns", prefix, null); context.Writer.WriteString(context.NamespaceMap.GetNamespaceUri(prefix).AbsoluteUri);//Uri.EscapeUriString(context.NamespaceMap.GetNamespaceUri(prefix).AbsoluteUri)); context.Writer.WriteEndAttribute(); } else { context.Writer.WriteStartAttribute("xmlns"); context.Writer.WriteString(context.NamespaceMap.GetNamespaceUri(prefix).AbsoluteUri);//Uri.EscapeUriString(context.NamespaceMap.GetNamespaceUri(prefix).AbsoluteUri)); context.Writer.WriteEndAttribute(); } } // Find the Collections and Type References if (context.CompressionLevel >= WriterCompressionLevel.More) { WriterHelper.FindCollections(context, CollectionSearchMode.All); } // Get the Triples as a Sorted List List <Triple> ts = context.Graph.Triples.Where(t => !context.TriplesDone.Contains(t)).ToList(); ts.Sort(new RdfXmlTripleComparer()); INode lastSubj = null; List <Triple> sameSubject = new List <Triple>(); for (int i = 0; i < ts.Count; i++) { // Find the first group of Triples with the same subject if (lastSubj == null) { // Start of new set of Triples with the same subject lastSubj = ts[i].Subject; sameSubject.Add(ts[i]); if (lastSubj.NodeType == NodeType.GraphLiteral) { throw new RdfOutputException(WriterErrorMessages.GraphLiteralsUnserializable("RDF/XML")); } else if (lastSubj.NodeType == NodeType.Variable) { throw new RdfOutputException(WriterErrorMessages.VariableNodesUnserializable("RDF/XML")); } } else { if (ts[i].Subject.Equals(lastSubj)) { // Still finding Triples with same subject sameSubject.Add(ts[i]); } else { // Found the end of current set of Triples with same subject GenerateSubjectOutput(context, sameSubject, true); // Reset so we'll start from a new subject on next iteration sameSubject.Clear(); lastSubj = null; i--; } } } // Ensure last set of Triples with same subject gets written if (sameSubject.Count > 0) { GenerateSubjectOutput(context, sameSubject, true); } // Take care of any collections that weren't yet written foreach (KeyValuePair <INode, OutputRdfCollection> kvp in context.Collections) { if (!kvp.Value.HasBeenWritten) { // Generate a rdf:Description node and then write the collection context.Writer.WriteStartElement("rdf", "Description", NamespaceMapper.RDF); if (kvp.Value.Triples.Count > 0 || !kvp.Value.IsExplicit) { context.Writer.WriteAttributeString("rdf", "nodeID", NamespaceMapper.RDF, context.BlankNodeMapper.GetOutputID(((IBlankNode)kvp.Key).InternalID)); } GenerateCollectionOutput(context, kvp.Key); context.Writer.WriteEndElement(); } } context.NamespaceMap.DecrementNesting(); context.Writer.WriteEndDocument(); // Save to the Output Stream context.Writer.Flush(); }
/// <summary> /// Method which generates the Sparql Query Results XML Format serialization of the Result Set /// </summary> /// <returns></returns> protected void GenerateOutput(SparqlResultSet resultSet, XmlWriter writer) { // XML Declaration writer.WriteStartDocument(); // <sparql> element writer.WriteStartElement("sparql", SparqlSpecsHelper.SparqlNamespace); // <head> element writer.WriteStartElement("head"); // Variables in the Header? if (resultSet.ResultsType == SparqlResultsType.VariableBindings) { foreach (String var in resultSet.Variables) { // <variable> element writer.WriteStartElement("variable"); writer.WriteAttributeString("name", var); writer.WriteEndElement(); } // </head> Element writer.WriteEndElement(); // <results> Element writer.WriteStartElement("results"); foreach (SparqlResult r in resultSet.Results) { // <result> Element writer.WriteStartElement("result"); foreach (String var in resultSet.Variables) { if (r.HasValue(var)) { INode n = r.Value(var); if (n == null) { continue; //NULLs don't get serialized in the XML Format } // <binding> Element writer.WriteStartElement("binding"); writer.WriteAttributeString("name", var); switch (n.NodeType) { case NodeType.Blank: // <bnode> element writer.WriteStartElement("bnode"); writer.WriteRaw(((IBlankNode)n).InternalID); writer.WriteEndElement(); break; case NodeType.GraphLiteral: // Error! throw new RdfOutputException("Result Sets which contain Graph Literal Nodes cannot be serialized in the SPARQL Query Results XML Format"); case NodeType.Literal: // <literal> element writer.WriteStartElement("literal"); ILiteralNode l = (ILiteralNode)n; if (!l.Language.Equals(String.Empty)) { writer.WriteStartAttribute("xml", "lang", XmlSpecsHelper.NamespaceXml); writer.WriteRaw(l.Language); writer.WriteEndAttribute(); } else if (l.DataType != null) { writer.WriteStartAttribute("datatype"); writer.WriteRaw(WriterHelper.EncodeForXml(l.DataType.AbsoluteUri)); writer.WriteEndAttribute(); } // Write the Value and the </literal> writer.WriteRaw(WriterHelper.EncodeForXml(l.Value)); writer.WriteEndElement(); break; case NodeType.Uri: // <uri> element writer.WriteStartElement("uri"); writer.WriteRaw(WriterHelper.EncodeForXml(((IUriNode)n).Uri.AbsoluteUri)); writer.WriteEndElement(); break; default: throw new RdfOutputException("Result Sets which contain Nodes of unknown Type cannot be serialized in the SPARQL Query Results XML Format"); } // </binding> element writer.WriteEndElement(); } } // </result> element writer.WriteEndElement(); } // </results> writer.WriteEndElement(); } else { // </head> writer.WriteEndElement(); // <boolean> element writer.WriteStartElement("boolean"); writer.WriteRaw(resultSet.Result.ToString().ToLower()); writer.WriteEndElement(); } // </sparql> element writer.WriteEndElement(); // End Document writer.WriteEndDocument(); writer.Flush(); writer.Close(); }
/// <summary> /// Internal method which generates the RDF/Json Output for a Graph /// </summary> /// <param name="g">Graph to save</param> /// <param name="output">Stream to save to</param> private void GenerateOutput(IGraph g, TextWriter output) { // Always force RDF Namespace to be correctly defined g.NamespaceMap.Import(this._defaultNamespaces); g.NamespaceMap.AddNamespace("rdf", UriFactory.Create(NamespaceMapper.RDF)); // Create our Writer Context and start the XML Document RdfXmlWriterContext context = new RdfXmlWriterContext(g, output); context.CompressionLevel = this._compressionLevel; context.UseDtd = this._useDTD; context.Writer.WriteStartDocument(); if (context.UseDtd) { // Create the DOCTYPE declaration StringBuilder entities = new StringBuilder(); String uri; entities.Append('\n'); foreach (String prefix in context.NamespaceMap.Prefixes) { uri = context.NamespaceMap.GetNamespaceUri(prefix).AbsoluteUri; if (!uri.Equals(context.NamespaceMap.GetNamespaceUri(prefix).ToString())) { context.UseDtd = false; break; } if (!prefix.Equals(String.Empty)) { entities.AppendLine("\t<!ENTITY " + prefix + " '" + uri + "'>"); } } if (context.UseDtd) { context.Writer.WriteDocType("rdf:RDF", null, null, entities.ToString()); } } // Create the rdf:RDF element context.Writer.WriteStartElement("rdf", "RDF", NamespaceMapper.RDF); if (context.Graph.BaseUri != null) { context.Writer.WriteAttributeString("xml", "base", null, context.Graph.BaseUri.AbsoluteUri);//Uri.EscapeUriString(context.Graph.BaseUri.ToString())); } context.NamespaceMap.IncrementNesting(); foreach (String prefix in context.NamespaceMap.Prefixes) { if (prefix.Equals("rdf")) { continue; } if (!prefix.Equals(String.Empty)) { context.Writer.WriteStartAttribute("xmlns", prefix, null); // String nsRef = "&" + prefix + ";"; // context.Writer.WriteRaw(nsRef); // context.Writer.WriteEntityRef(prefix); context.Writer.WriteRaw(WriterHelper.EncodeForXml(context.NamespaceMap.GetNamespaceUri(prefix).AbsoluteUri));//Uri.EscapeUriString(WriterHelper.EncodeForXml(context.NamespaceMap.GetNamespaceUri(prefix).AbsoluteUri))); context.Writer.WriteEndAttribute(); } else { context.Writer.WriteStartAttribute("xmlns"); context.Writer.WriteRaw(WriterHelper.EncodeForXml(context.NamespaceMap.GetNamespaceUri(prefix).AbsoluteUri));//Uri.EscapeUriString(WriterHelper.EncodeForXml(context.NamespaceMap.GetNamespaceUri(prefix).AbsoluteUri))); context.Writer.WriteEndAttribute(); } } // Find the Collections and Type References if (context.CompressionLevel >= WriterCompressionLevel.More) { WriterHelper.FindCollections(context, CollectionSearchMode.ImplicitOnly); // WriterHelper.FindCollections(context, CollectionSearchMode.All); } Dictionary <INode, String> typerefs = this.FindTypeReferences(context); // Get the Triples as a Sorted List List <Triple> ts = context.Graph.Triples.Where(t => !context.TriplesDone.Contains(t)).ToList(); ts.Sort(new RdfXmlTripleComparer()); // Variables we need to track our writing INode lastSubj, lastPred, lastObj; lastSubj = lastPred = lastObj = null; for (int i = 0; i < ts.Count; i++) { Triple t = ts[i]; if (context.TriplesDone.Contains(t)) { continue; //Skip if already done } if (lastSubj == null || !t.Subject.Equals(lastSubj)) { // Start a new set of Triples if (lastSubj != null) { context.NamespaceMap.DecrementNesting(); context.Writer.WriteEndElement(); } if (lastPred != null) { context.NamespaceMap.DecrementNesting(); context.Writer.WriteEndElement(); } // Write out the Subject // Validate Subject // Use a Type Reference if applicable context.NamespaceMap.IncrementNesting(); if (typerefs.ContainsKey(t.Subject)) { String tref = typerefs[t.Subject]; if (tref.StartsWith(":")) { context.Writer.WriteStartElement(tref.Substring(1)); } else if (tref.Contains(":")) { context.Writer.WriteStartElement(tref.Substring(0, tref.IndexOf(':')), tref.Substring(tref.IndexOf(':') + 1), null); } else { context.Writer.WriteStartElement(tref); } } else { context.Writer.WriteStartElement("rdf", "Description", NamespaceMapper.RDF); } lastSubj = t.Subject; // Apply appropriate attributes switch (t.Subject.NodeType) { case NodeType.GraphLiteral: throw new RdfOutputException(WriterErrorMessages.GraphLiteralsUnserializable("RDF/XML")); case NodeType.Literal: throw new RdfOutputException(WriterErrorMessages.LiteralSubjectsUnserializable("RDF/XML")); case NodeType.Blank: if (context.Collections.ContainsKey(t.Subject)) { this.GenerateCollectionOutput(context, t.Subject); } else { context.Writer.WriteAttributeString("rdf", "nodeID", null, context.BlankNodeMapper.GetOutputID(((IBlankNode)t.Subject).InternalID)); } break; case NodeType.Uri: this.GenerateUriOutput(context, (IUriNode)t.Subject, "rdf:about"); break; default: throw new RdfOutputException(WriterErrorMessages.UnknownNodeTypeUnserializable("RDF/XML")); } // Write the Predicate context.NamespaceMap.IncrementNesting(); this.GeneratePredicateNode(context, t.Predicate); lastPred = t.Predicate; lastObj = null; } else if (lastPred == null || !t.Predicate.Equals(lastPred)) { if (lastPred != null) { context.NamespaceMap.DecrementNesting(); context.Writer.WriteEndElement(); } // Write the Predicate context.NamespaceMap.IncrementNesting(); this.GeneratePredicateNode(context, t.Predicate); lastPred = t.Predicate; lastObj = null; } // Write the Object if (lastObj != null) { // Terminate the previous Predicate Node context.NamespaceMap.DecrementNesting(); context.Writer.WriteEndElement(); // Start a new Predicate Node context.NamespaceMap.DecrementNesting(); context.Writer.WriteEndElement(); context.NamespaceMap.IncrementNesting(); this.GeneratePredicateNode(context, t.Predicate); } // Create an Object for the Object switch (t.Object.NodeType) { case NodeType.Blank: if (context.Collections.ContainsKey(t.Object)) { // Output a Collection this.GenerateCollectionOutput(context, t.Object); } else { // Terminate the Blank Node triple by adding a rdf:nodeID attribute context.Writer.WriteAttributeString("rdf", "nodeID", null, context.BlankNodeMapper.GetOutputID(((IBlankNode)t.Object).InternalID)); } break; case NodeType.GraphLiteral: throw new RdfOutputException(WriterErrorMessages.GraphLiteralsUnserializable("RDF/XML")); case NodeType.Literal: ILiteralNode lit = (ILiteralNode)t.Object; this.GenerateLiteralOutput(context, lit); break; case NodeType.Uri: this.GenerateUriOutput(context, (IUriNode)t.Object, "rdf:resource"); break; default: throw new RdfOutputException(WriterErrorMessages.UnknownNodeTypeUnserializable("RDF/XML")); } lastObj = t.Object; // Force a new Predicate Node context.NamespaceMap.DecrementNesting(); context.Writer.WriteEndElement(); lastPred = null; context.TriplesDone.Add(t); } // Check we haven't failed to output any collections foreach (KeyValuePair <INode, OutputRdfCollection> pair in context.Collections) { if (pair.Value.Triples.Count > 0) { if (typerefs.ContainsKey(pair.Key)) { String tref = typerefs[pair.Key]; context.NamespaceMap.IncrementNesting(); if (tref.StartsWith(":")) { context.Writer.WriteStartElement(tref.Substring(1)); } else if (tref.Contains(":")) { context.Writer.WriteStartElement(tref.Substring(0, tref.IndexOf(':')), tref.Substring(tref.IndexOf(':') + 1), null); } else { context.Writer.WriteStartElement(tref); } this.GenerateCollectionOutput(context, pair.Key); context.Writer.WriteEndElement(); } else { context.Writer.WriteStartElement("rdf", "Description", NamespaceMapper.RDF); context.Writer.WriteAttributeString("rdf", "nodeID", NamespaceMapper.RDF, context.BlankNodeMapper.GetOutputID(((IBlankNode)pair.Key).InternalID)); this.GenerateCollectionOutput(context, pair.Key); context.Writer.WriteEndElement(); // throw new RdfOutputException("Failed to output a Collection due to an unknown error"); } } } context.NamespaceMap.DecrementNesting(); context.Writer.WriteEndDocument(); // Save to the Output Stream context.Writer.Flush(); }
/// <summary> /// Generates the Turtle Syntax for the Graph /// </summary> private void GenerateOutput(CompressingTurtleWriterContext context) { //Create the Header //Base Directive if (context.Graph.BaseUri != null) { context.Output.WriteLine("@base <" + context.UriFormatter.FormatUri(context.Graph.BaseUri) + ">."); context.Output.WriteLine(); } //Prefix Directives foreach (String prefix in context.Graph.NamespaceMap.Prefixes) { if (TurtleSpecsHelper.IsValidQName(prefix + ":")) { if (!prefix.Equals(String.Empty)) { context.Output.WriteLine("@prefix " + prefix + ": <" + context.UriFormatter.FormatUri(context.Graph.NamespaceMap.GetNamespaceUri(prefix)) + ">."); } else { context.Output.WriteLine("@prefix : <" + context.UriFormatter.FormatUri(context.Graph.NamespaceMap.GetNamespaceUri(String.Empty)) + ">."); } } } context.Output.WriteLine(); //Decide on the Write Mode to use bool hiSpeed = false; double subjNodes = context.Graph.Triples.SubjectNodes.Count(); double triples = context.Graph.Triples.Count; if ((subjNodes / triples) > 0.75) { hiSpeed = true; } if (context.CompressionLevel == WriterCompressionLevel.None || (hiSpeed && context.HighSpeedModePermitted)) { this.RaiseWarning("High Speed Write Mode in use - minimal syntax compression will be used"); context.CompressionLevel = WriterCompressionLevel.Minimal; context.NodeFormatter = new UncompressedTurtleFormatter(); foreach (Triple t in context.Graph.Triples) { context.Output.WriteLine(this.GenerateTripleOutput(context, t)); } } else { if (context.CompressionLevel >= WriterCompressionLevel.More) { WriterHelper.FindCollections(context); } //Get the Triples as a Sorted List List <Triple> ts = context.Graph.Triples.Where(t => !context.TriplesDone.Contains(t)).ToList(); ts.Sort(new FullTripleComparer(new FastNodeComparer())); //Variables we need to track our writing INode lastSubj, lastPred; lastSubj = lastPred = null; int subjIndent = 0, predIndent = 0; String temp; for (int i = 0; i < ts.Count; i++) { Triple t = ts[i]; if (lastSubj == null || !t.Subject.Equals(lastSubj)) { //Terminate previous Triples if (lastSubj != null) { context.Output.WriteLine("."); } //Start a new set of Triples temp = this.GenerateNodeOutput(context, t.Subject, TripleSegment.Subject, 0); context.Output.Write(temp); context.Output.Write(" "); if (temp.Contains('\n')) { subjIndent = temp.Split('\n').Last().Length + 1; } else { subjIndent = temp.Length + 1; } lastSubj = t.Subject; //Write the first Predicate temp = this.GenerateNodeOutput(context, t.Predicate, TripleSegment.Predicate, subjIndent); context.Output.Write(temp); context.Output.Write(" "); predIndent = temp.Length + 1; lastPred = t.Predicate; } else if (lastPred == null || !t.Predicate.Equals(lastPred)) { //Terminate previous Predicate Object list context.Output.WriteLine(";"); if (context.PrettyPrint) { context.Output.Write(new String(' ', subjIndent)); } //Write the next Predicate temp = this.GenerateNodeOutput(context, t.Predicate, TripleSegment.Predicate, subjIndent); context.Output.Write(temp); context.Output.Write(" "); predIndent = temp.Length + 1; lastPred = t.Predicate; } else { //Continue Object List context.Output.WriteLine(","); if (context.PrettyPrint) { context.Output.Write(new String(' ', subjIndent + predIndent)); } } //Write the Object context.Output.Write(this.GenerateNodeOutput(context, t.Object, TripleSegment.Object, subjIndent + predIndent)); } //Terminate Triples if (ts.Count > 0) { context.Output.WriteLine("."); } return; } }