/// <summary> /// Formats a Graph Header by creating an <strong><rdf:RDF></strong> element and adding namespace definitions. /// </summary> /// <param name="g">Graph.</param> /// <returns></returns> public string FormatGraphHeader(IGraph g) { _mapper = new QNameOutputMapper(g.NamespaceMap); StringBuilder output = new StringBuilder(); output.Append(GetGraphHeaderBase()); foreach (String prefix in g.NamespaceMap.Prefixes) { if (!prefix.Equals("rdf")) { if (prefix.Equals(String.Empty)) { output.Append(" xmlns=\"" + WriterHelper.EncodeForXml(g.NamespaceMap.GetNamespaceUri(prefix).AbsoluteUri) + "\""); } else { output.Append(" xmlns:" + prefix + "=\"" + WriterHelper.EncodeForXml(g.NamespaceMap.GetNamespaceUri(prefix).AbsoluteUri) + "\""); } } } if (g.BaseUri != null) { output.Append(" xml:base=\"" + WriterHelper.EncodeForXml(g.BaseUri.AbsoluteUri) + "\""); } output.Append(">"); return(output.ToString()); }
/// <summary> /// Formats a Graph Header by creating an <strong><rdf:RDF></strong> element and adding namespace definitions /// </summary> /// <param name="g">Graph</param> /// <returns></returns> public string FormatGraphHeader(IGraph g) { this._mapper = new QNameOutputMapper(g.NamespaceMap); StringBuilder output = new StringBuilder(); output.Append(this.GetGraphHeaderBase()); foreach (String prefix in g.NamespaceMap.Prefixes) { if (!prefix.Equals("rdf")) { if (prefix.Equals(String.Empty)) { output.Append(" xmlns=\"" + WriterHelper.EncodeForXml(g.NamespaceMap.GetNamespaceUri(prefix).ToString()) + "\""); } else { output.Append(" xmlns:" + prefix + "=\"" + WriterHelper.EncodeForXml(g.NamespaceMap.GetNamespaceUri(prefix).ToString()) + "\""); } } } if (g.BaseUri != null) { output.Append(" xml:base=\"" + WriterHelper.EncodeForXml(g.BaseUri.ToString()) + "\""); } output.Append(">"); return output.ToString(); }
/// <summary> /// Creates a new QName Formatter /// </summary> /// <param name="formatName">Format Name</param> /// <param name="qnameMapper">QName Map</param> public QNameFormatter(String formatName, QNameOutputMapper qnameMapper) : base(formatName) { this._qnameMapper = qnameMapper; foreach (String prefix in this._qnameMapper.Prefixes.ToList()) { if (!this.IsValidQName(prefix + ":")) { this._qnameMapper.RemoveNamespace(prefix); } } }
/// <summary> /// Formats a Graph Header by creating an <strong><rdf:RDF></strong> element and adding namespace definitions /// </summary> /// <param name="namespaces">Namespaces</param> /// <returns></returns> public string FormatGraphHeader(INamespaceMapper namespaces) { this._mapper = new QNameOutputMapper(namespaces); StringBuilder output = new StringBuilder(); output.Append(this.GetGraphHeaderBase()); foreach (String prefix in namespaces.Prefixes) { if (!prefix.Equals("rdf")) { if (prefix.Equals(String.Empty)) { output.Append(" xmlns=\"" + WriterHelper.EncodeForXml(namespaces.GetNamespaceUri(prefix).ToString()) + "\""); } else { output.Append(" xmlns:" + prefix + "=\"" + WriterHelper.EncodeForXml(namespaces.GetNamespaceUri(prefix).ToString()) + "\""); } } } output.Append(">"); return(output.ToString()); }
/// <summary> /// Creates a new Base Writer Context with default settings /// </summary> /// <param name="g">Graph being written</param> /// <param name="output">TextWriter being written to</param> public BaseWriterContext(IGraph g, TextWriter output) { this._g = g; this._output = output; this._qnameMapper = new QNameOutputMapper(this._g.NamespaceMap); }
/// <summary> /// Creates a new Turtle Formatter /// </summary> /// <param name="formatName">Format Name</param> /// <param name="qnameMapper">QName Map</param> protected TurtleFormatter(String formatName, QNameOutputMapper qnameMapper) : base(formatName, qnameMapper) { }
/// <summary> /// Creates a new Turtle Formatter that uses the given QName mapper /// </summary> /// <param name="qnameMapper">QName Mapper</param> public TurtleFormatter(QNameOutputMapper qnameMapper) : base("Turtle", qnameMapper) { }
/// <summary> /// Creates a new Turtle Formatter that uses the given QName mapper /// </summary> /// <param name="qnameMapper">QName Mapper</param> public TurtleW3CFormatter(QNameOutputMapper qnameMapper) : base("Turtle (W3C)", qnameMapper) { }
/// <summary> /// Internal method which generates the HTML Output for the Sparql Results /// </summary> /// <param name="results"></param> /// <param name="output"></param> private void GenerateOutput(SparqlResultSet results, TextWriter output) { HtmlTextWriter writer = new HtmlTextWriter(output); QNameOutputMapper qnameMapper = new QNameOutputMapper(this._namespaces != null ? this._namespaces : new NamespaceMapper(true)); //Page Header writer.Write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"); writer.RenderBeginTag(HtmlTextWriterTag.Html); writer.RenderBeginTag(HtmlTextWriterTag.Head); writer.RenderBeginTag(HtmlTextWriterTag.Title); writer.WriteEncodedText("SPARQL Query Results"); writer.RenderEndTag(); if (!this.Stylesheet.Equals(String.Empty)) { writer.AddAttribute(HtmlTextWriterAttribute.Href, this.Stylesheet); writer.AddAttribute(HtmlTextWriterAttribute.Type, "text/css"); writer.AddAttribute(HtmlTextWriterAttribute.Rel, "stylesheet"); writer.RenderBeginTag(HtmlTextWriterTag.Link); writer.RenderEndTag(); } //TODO: Add <meta> for charset? writer.RenderEndTag(); //Start Body writer.RenderBeginTag(HtmlTextWriterTag.Body); if (results.ResultsType == SparqlResultsType.VariableBindings) { //Create a Table for the results writer.AddAttribute(HtmlTextWriterAttribute.Width, "100%"); writer.RenderBeginTag(HtmlTextWriterTag.Table); //Create a Table Header with the Variable Names writer.RenderBeginTag(HtmlTextWriterTag.Thead); writer.RenderBeginTag(HtmlTextWriterTag.Tr); foreach (String var in results.Variables) { writer.RenderBeginTag(HtmlTextWriterTag.Th); writer.WriteEncodedText(var); writer.RenderEndTag(); } writer.RenderEndTag(); writer.RenderEndTag(); #if !NO_WEB writer.WriteLine(); #endif //Create a Table Body for the Results writer.RenderBeginTag(HtmlTextWriterTag.Tbody); //Create a Column for each Binding foreach (SparqlResult result in results) { //Start Row writer.RenderBeginTag(HtmlTextWriterTag.Tr); foreach (String var in results.Variables) { //Start Column writer.RenderBeginTag(HtmlTextWriterTag.Td); if (result.HasValue(var)) { INode value = result[var]; if (value != null) { switch (value.NodeType) { case NodeType.Blank: writer.AddAttribute(HtmlTextWriterAttribute.Class, this.CssClassBlankNode); writer.RenderBeginTag(HtmlTextWriterTag.Span); writer.WriteEncodedText(value.ToString()); writer.RenderEndTag(); break; case NodeType.Literal: ILiteralNode lit = (ILiteralNode)value; writer.AddAttribute(HtmlTextWriterAttribute.Class, this.CssClassLiteral); writer.RenderBeginTag(HtmlTextWriterTag.Span); if (lit.DataType != null) { writer.WriteEncodedText(lit.Value); writer.RenderEndTag(); writer.WriteEncodedText("^^"); writer.AddAttribute(HtmlTextWriterAttribute.Href, this._formatter.FormatUri(lit.DataType.ToString())); writer.AddAttribute(HtmlTextWriterAttribute.Class, this.CssClassDatatype); writer.RenderBeginTag(HtmlTextWriterTag.A); writer.WriteEncodedText(lit.DataType.ToString()); writer.RenderEndTag(); } else { writer.WriteEncodedText(lit.Value); if (!lit.Language.Equals(String.Empty)) { writer.RenderEndTag(); writer.WriteEncodedText("@"); writer.AddAttribute(HtmlTextWriterAttribute.Class, this.CssClassLangSpec); writer.RenderBeginTag(HtmlTextWriterTag.Span); writer.WriteEncodedText(lit.Language); writer.RenderEndTag(); } else { writer.RenderEndTag(); } } break; case NodeType.GraphLiteral: //Error throw new RdfOutputException("Result Sets which contain Graph Literal Nodes cannot be serialized in the HTML Format"); case NodeType.Uri: writer.AddAttribute(HtmlTextWriterAttribute.Class, this.CssClassUri); writer.AddAttribute(HtmlTextWriterAttribute.Href, this._formatter.FormatUri(this.UriPrefix + value.ToString())); writer.RenderBeginTag(HtmlTextWriterTag.A); String qname; if (qnameMapper.ReduceToQName(value.ToString(), out qname)) { writer.WriteEncodedText(qname); } else { writer.WriteEncodedText(value.ToString()); } writer.RenderEndTag(); break; default: throw new RdfOutputException("Result Sets which contain Unknown Node Types cannot be serialized in the HTML Format"); } } else { writer.WriteEncodedText(" "); } } else { writer.WriteEncodedText(" "); } //End Column writer.RenderEndTag(); } //End Row writer.RenderEndTag(); #if !NO_WEB writer.WriteLine(); #endif } //End Table Body writer.RenderEndTag(); //End Table writer.RenderEndTag(); } else { //Show a Header and a Boolean value writer.RenderBeginTag(HtmlTextWriterTag.H3); writer.WriteEncodedText("ASK Query Result"); writer.RenderEndTag(); writer.RenderBeginTag(HtmlTextWriterTag.P); writer.WriteEncodedText(results.Result.ToString()); writer.RenderEndTag(); } //End of Page writer.RenderEndTag(); //End Body writer.RenderEndTag(); //End Html }
/// <summary> /// Creates a new Turtle Formatter. /// </summary> /// <param name="formatName">Format Name.</param> /// <param name="qnameMapper">QName Map.</param> protected TurtleW3CFormatter(string formatName, QNameOutputMapper qnameMapper) : base(formatName, qnameMapper) { }
/// <summary> /// Creates a new Turtle Formatter that uses the given QName mapper. /// </summary> /// <param name="qnameMapper">QName Mapper.</param> public TurtleW3CFormatter(QNameOutputMapper qnameMapper) : base("Turtle (W3C)", qnameMapper) { }
/// <summary> /// Creates a new Turtle Formatter that uses the given QName mapper. /// </summary> /// <param name="qnameMapper">QName Mapper.</param> public TurtleFormatter(QNameOutputMapper qnameMapper) : base("Turtle", qnameMapper) { }
/// <summary> /// Formats a Graph Footer by closing the <strong><rdf:RDF></strong> element /// </summary> /// <returns></returns> public string FormatGraphFooter() { this._mapper = null; return "</rdf:RDF>"; }
/// <summary> /// Creates a new QName Formatter /// </summary> /// <param name="formatName">Format Name</param> /// <param name="qnameMapper">QName Map</param> /// <param name="allowAKeyword">Whether the 'a' keyword can be used for the RDF type predicate</param> public QNameFormatter(String formatName, QNameOutputMapper qnameMapper, bool allowAKeyword) : this(formatName, qnameMapper) { this._allowAKeyword = allowAKeyword; }
/// <summary> /// Creates a new Base Writer Context with default settings. /// </summary> /// <param name="g">Graph being written.</param> /// <param name="output">TextWriter being written to.</param> public BaseWriterContext(IGraph g, TextWriter output) { _g = g; _output = output; _qnameMapper = new QNameOutputMapper(_g.NamespaceMap); }
/// <summary> /// Formats a Graph Footer by closing the <strong><rdf:RDF></strong> element. /// </summary> /// <returns></returns> public string FormatGraphFooter() { _mapper = null; return("</rdf:RDF>"); }