Ejemplo n.º 1
0
        private void SplitFile(string filePath, IEnumerable <string> lines, TripleSegment tripleSegment)
        {
            Log.LogDebug("SplitFile: {0}", filePath);
            _splitDirPath = filePath.Substring(0, filePath.IndexOf('.'));
            if (!Directory.Exists(Path.Combine(_baseDirectory.FullName, _splitDirPath)))
            {
                Directory.CreateDirectory(Path.Combine(_baseDirectory.FullName, _splitDirPath));
                var prefixString =
                    _splitDirPath.Replace(Path.DirectorySeparatorChar.ToString(), "")
                    .Replace(Path.AltDirectorySeparatorChar.ToString(), "");
                _directoryMap.Add(prefixString, _splitDirPath);
            }

            var handler = new ImportHandler(this, tripleSegment);

            _lineReader.Load(handler, new StringReader(string.Join("\n", lines)));

            // Drop the old file
            _cache.Remove(filePath);
            var deletePath = Path.Combine(_baseDirectory.FullName, filePath);

            if (File.Exists(deletePath))
            {
                File.Delete(deletePath);
            }
        }
Ejemplo n.º 2
0
        private string GenerateNodeOutput(INode node, TripleSegment segment)
        {
            switch (node.NodeType)
            {
            case NodeType.Blank:
                if (segment == TripleSegment.Predicate)
                {
                    throw new RdfOutputException(WriterErrorMessages.BlankPredicatesUnserializable("Turtle"));
                }

                return(Format(node, segment));

            case NodeType.Uri:
                return(Format(node, segment));

            case NodeType.Literal:
                if (segment == TripleSegment.Subject)
                {
                    throw new RdfOutputException(WriterErrorMessages.LiteralSubjectsUnserializable("Turtle"));
                }
                if (segment == TripleSegment.Predicate)
                {
                    throw new RdfOutputException(WriterErrorMessages.LiteralPredicatesUnserializable("Turtle"));
                }
                return(Format(node, segment));

            case NodeType.GraphLiteral:
                throw new RdfOutputException(WriterErrorMessages.GraphLiteralsUnserializable("Turtle"));

            default:
                throw new RdfOutputException(WriterErrorMessages.UnknownNodeTypeUnserializable("Turtle"));
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Converts a Node into relevant NTriples Syntax
        /// </summary>
        /// <param name="n">Node to convert</param>
        /// <param name="context">Writer Context</param>
        /// <param name="segment">Triple Segment being written</param>
        /// <returns></returns>
        private String NodeToNTriples(NTriplesWriterContext context, INode n, TripleSegment segment)
        {
            switch (n.NodeType)
            {
            case NodeType.Blank:
                if (segment == TripleSegment.Predicate)
                {
                    throw new RdfOutputException(WriterErrorMessages.BlankPredicatesUnserializable("NQuads"));
                }
                break;

            case NodeType.Literal:
                if (segment == TripleSegment.Subject)
                {
                    throw new RdfOutputException(WriterErrorMessages.LiteralSubjectsUnserializable("NQuads"));
                }
                if (segment == TripleSegment.Predicate)
                {
                    throw new RdfOutputException(WriterErrorMessages.LiteralPredicatesUnserializable("NQuads"));
                }
                break;

            case NodeType.Uri:
                break;

            case NodeType.GraphLiteral:
                throw new RdfOutputException(WriterErrorMessages.GraphLiteralsUnserializable("NQuads"));

            default:
                throw new RdfOutputException(WriterErrorMessages.UnknownNodeTypeUnserializable("NQuads"));
            }

            return(context.NodeFormatter.Format(n));
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Formats a URI Node
 /// </summary>
 /// <param name="u">URI Node</param>
 /// <param name="segment">Triple Segment</param>
 /// <returns></returns>
 protected override string FormatUriNode(IUriNode u, TripleSegment? segment)
 {
     StringBuilder output = new StringBuilder();
     output.Append('<');
     output.Append(this.FormatUri(u.Uri));
     output.Append('>');
     return output.ToString();
 }
Ejemplo n.º 5
0
        private bool HandleTriple(Triple t, TripleSegment tripleSegment)
        {
            var line           = FormatTriple(t.Subject, t.Predicate, t.Object, t.GraphUri);
            var targetFileName = GetTripleSegmentFileName(t, tripleSegment);
            var targetPath     = GetFilePath(targetFileName);

            Assert(tripleSegment, line, targetPath, true);
            return(true);
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Formats Literals for CSV output
 /// </summary>
 /// <param name="l">Literal</param>
 /// <param name="segment">Triple Segment</param>
 /// <returns></returns>
 protected override string FormatLiteralNode(ILiteralNode l, TripleSegment? segment)
 {
     String value = l.Value;
     if (value.Contains('"') || value.Contains(',') || value.Contains('\n') || value.Contains('\r'))
     {
         return '"' + value.Replace("\"", "\"\"") + '"';
     }
     else
     {
         return value;
     }
 }
Ejemplo n.º 7
0
        /// <summary>
        /// Formats a Graph Literal Node for Notation 3
        /// </summary>
        /// <param name="glit">Graph Literal</param>
        /// <param name="segment">Triple Segment</param>
        /// <returns></returns>
        protected override string FormatGraphLiteralNode(IGraphLiteralNode glit, TripleSegment? segment)
        {
            if (segment == TripleSegment.Predicate) throw new RdfOutputException(WriterErrorMessages.GraphLiteralPredicatesUnserializable(this.FormatName));

            StringBuilder output = new StringBuilder();
            output.Append("{");
            foreach (Triple t in glit.SubGraph.Triples)
            {
                output.Append(this.Format(t));
            }
            output.Append("}");
            return output.ToString();
        }
Ejemplo n.º 8
0
        private bool Assert(TripleSegment segment, string line, string storePath, bool flushing = false)
        {
            Log.LogTrace("Assert: path={0}, data={1}", storePath, line);
            var lines = GetFileContent(storePath, flushing);
            var ix    = lines.BinarySearch(line);

            if (ix >= 0)
            {
                return(false);
            }
            lines.Insert(~ix, line);
            return(true);
        }
Ejemplo n.º 9
0
        private static INode GetNode(Triple t, TripleSegment segment)
        {
            switch (segment)
            {
            case TripleSegment.Subject:
                return(t.Subject);

            case TripleSegment.Object:
                return(t.Object);

            default:
                return(null);
            }
        }
Ejemplo n.º 10
0
        // KA - Regex-based checking is slightly faster than using the DNR parser, but the parser is less likely to be wrong :-)

        /*
         * private Regex SplitRegex = new Regex("(?<s>(_:[^\\s]+)|(<[^>]+>))\\s+(?<p>(_:[^\\s]+)|(<[^>]+>))\\s+(?<o>(_:[^\\s]+)|(<[^>]+>)|\"[^\"]*\"(@\\S+)?(\\^\\^<[^>]+>)?)\\s+(?<g>(_:[^\\s]+)|(<[^>]+>))\\s*\\.");
         *
         * private bool CanSplitRegex(IReadOnlyList<string> lines, TripleSegment segment)
         * {
         *  var segmentGroup = segment == TripleSegment.Subject ? "s" : segment == TripleSegment.Predicate ? "p" : "o";
         *  var firstNode = SplitRegex.Match(lines[0]).Groups[segmentGroup].Value;
         *  return lines.Skip(1).Any(l => SplitRegex.Match(l).Groups[segmentGroup].Value != firstNode);
         * }
         */

        private bool CanSplitParser(IReadOnlyList <string> lines, TripleSegment segment)
        {
            var singleLineHandler = new SingleLineHandler();

            _lineReader.Load(singleLineHandler, new StringReader(lines[0]));
            var firstNode = segment == TripleSegment.Subject
                ? singleLineHandler.LastTriple.Subject
                : segment == TripleSegment.Predicate
                    ? singleLineHandler.LastTriple.Predicate
                    : singleLineHandler.LastTriple.Object;
            var matchHandler = new WhileMatchHandler(segment, firstNode);

            _lineReader.Load(matchHandler, new StringReader(string.Join("\n", lines)));
            return(!matchHandler.Match);
        }
Ejemplo n.º 11
0
        private static INode GetNode(Triple triple, TripleSegment segment)
        {
            switch (segment)
            {
            case TripleSegment.Subject:
                return(triple.Subject);

            case TripleSegment.Predicate:
                return(triple.Predicate);

            case TripleSegment.Object:
                return(triple.Object);

            default:
                throw new InvalidOperationException();
            }
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Generates Output for the given Node
        /// </summary>
        /// <param name="context">Writer Context</param>
        /// <param name="n">Node</param>
        /// <param name="segment">Triple Context</param>
        private void GenerateNodeOutput(BaseWriterContext context, INode n, TripleSegment segment)
        {
            switch (n.NodeType)
            {
            case NodeType.GraphLiteral:
                throw new RdfOutputException(WriterErrorMessages.GraphLiteralsUnserializable("TSV"));

            case NodeType.Blank:
            case NodeType.Literal:
            case NodeType.Uri:
                context.Output.Write(this._formatter.Format(n));
                break;

            default:
                throw new RdfOutputException(WriterErrorMessages.UnknownNodeTypeUnserializable("TSV"));
            }
        }
Ejemplo n.º 13
0
        private string GetTripleSegmentFileName(Triple t, TripleSegment segment)
        {
            switch (segment)
            {
            case TripleSegment.Subject:
                return(GetSubjectFileName(t.Subject));

            case TripleSegment.Predicate:
                return(GetPredicateFileName(t.Predicate));

            case TripleSegment.Object:
                return(GetObjectFileName(t.Object));

            default:
                throw new ArgumentException("Segment must be subject, predicate or object", nameof(segment));
            }
        }
Ejemplo n.º 14
0
        private INode TryParseNode(JsonParserContext context, out TripleSegment segment)
        {
            if (context.Input.Read())
            {
                if (context.Input.TokenType == JsonToken.PropertyName)
                {
                    //Determine the Triple Segment
                    switch (context.Input.Value.ToString())
                    {
                    case "subject":
                        segment = TripleSegment.Subject;
                        break;

                    case "predicate":
                        segment = TripleSegment.Predicate;
                        break;

                    case "object":
                        segment = TripleSegment.Object;
                        break;

                    default:
                        throw Error(context, "Unexpected Property '" + context.Input.Value.ToString() + "' encountered, expected one of 'subject', 'predicate' or 'object'");
                    }

                    if (context.Input.Read())
                    {
                        String value = context.Input.Value.ToString();
                        return(this.TryParseNodeValue(context, value));
                    }
                    else
                    {
                        throw Error(context, "Unexpected End of Input when a Value for a Node of a Triple was expected");
                    }
                }
                else
                {
                    throw Error(context, "Unexpected Token '" + context.Input.TokenType.ToString() + "' encountered, expected a Property Name for the node of a Triple");
                }
            }
            else
            {
                throw Error(context, "Unexpected End of Input when a Property Value pair for a Node of a Triple was expected");
            }
        }
Ejemplo n.º 15
0
 /// <summary>
 /// Formats a Node as a String
 /// </summary>
 /// <param name="n">Node</param>
 /// <param name="segment">Triple Segment</param>
 /// <returns></returns>
 public virtual String Format(INode n, TripleSegment? segment)
 {
     switch (n.NodeType)
     {
         case NodeType.Blank:
             return this.FormatBlankNode((IBlankNode)n, segment);
         case NodeType.GraphLiteral:
             return this.FormatGraphLiteralNode((IGraphLiteralNode)n, segment);
         case NodeType.Literal:
             return this.FormatLiteralNode((ILiteralNode)n, segment);
         case NodeType.Uri:
             return this.FormatUriNode((IUriNode)n, segment);
         case NodeType.Variable:
             return this.FormatVariableNode((IVariableNode)n, segment);
         default:
             throw new RdfOutputException(WriterErrorMessages.UnknownNodeTypeUnserializable(this._format));
     }
 }
Ejemplo n.º 16
0
        private static string ProcessNode(Triple t, TripleSegment segment, BaseWriterContext context, bool collapseLiterals)
        {
            var node = GraphVizWriter.GetNode(t, segment);

            switch (node)
            {
            case ILiteralNode literalnode:
                return(GraphVizWriter.WriteLiteralNode(literalnode, t, context, collapseLiterals));

            case IUriNode uriNode:
                return(GraphVizWriter.ReduceToQName(uriNode.Uri, context));

            case IBlankNode blankNode:
                return(blankNode.ToString());

            default:
                throw new RdfOutputException("Only Uri nodes, literal nodes and blank nodes can be converted to GraphViz DOT Format.");
            }
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Generates Output for the given Node.
        /// </summary>
        /// <param name="context">Writer Context.</param>
        /// <param name="n">Node.</param>
        /// <param name="segment">Triple Segment.</param>
        private void GenerateNodeOutput(BaseWriterContext context, INode n, TripleSegment segment)
        {
            switch (n.NodeType)
            {
            case NodeType.Blank:
                if (segment == TripleSegment.Predicate)
                {
                    throw new RdfOutputException(WriterErrorMessages.BlankPredicatesUnserializable("CSV"));
                }

                context.Output.Write(_formatter.Format(n));
                break;

            case NodeType.GraphLiteral:
                throw new RdfOutputException(WriterErrorMessages.GraphLiteralsUnserializable("CSV"));

            case NodeType.Literal:
                if (segment == TripleSegment.Subject)
                {
                    throw new RdfOutputException(WriterErrorMessages.LiteralSubjectsUnserializable("CSV"));
                }
                if (segment == TripleSegment.Predicate)
                {
                    throw new RdfOutputException(WriterErrorMessages.LiteralPredicatesUnserializable("CSV"));
                }

                context.Output.Write(_formatter.Format(n));
                break;

            case NodeType.Uri:
                context.Output.Write(_formatter.Format(n));
                break;

            default:
                throw new RdfOutputException(WriterErrorMessages.UnknownNodeTypeUnserializable("CSV"));
            }
        }
Ejemplo n.º 18
0
        private INode TryParseNode(JsonParserContext context, out TripleSegment segment)
        {
            if (context.Input.Read())
            {
                if (context.Input.TokenType == JsonToken.PropertyName)
                {
                    //Determine the Triple Segment
                    switch (context.Input.Value.ToString())
                    {
                        case "subject":
                            segment = TripleSegment.Subject;
                            break;
                        case "predicate":
                            segment = TripleSegment.Predicate;
                            break;
                        case "object":
                            segment = TripleSegment.Object;
                            break;
                        default:
                            throw Error(context, "Unexpected Property '" + context.Input.Value.ToString() + "' encountered, expected one of 'subject', 'predicate' or 'object'");
                    }

                    if (context.Input.Read())
                    {
                        String value = context.Input.Value.ToString();
                        return this.TryParseNodeValue(context, value);
                    }
                    else
                    {
                        throw Error(context, "Unexpected End of Input when a Value for a Node of a Triple was expected");
                    }
                }
                else
                {
                    throw Error(context, "Unexpected Token '" + context.Input.TokenType.ToString() + "' encountered, expected a Property Name for the node of a Triple");
                }
            }
            else
            {
                throw Error(context, "Unexpected End of Input when a Property Value pair for a Node of a Triple was expected");
            }
        }
Ejemplo n.º 19
0
        /// <summary>
        /// Generates Output for Nodes in Notation 3 syntax.
        /// </summary>
        /// <param name="context">Writer Context.</param>
        /// <param name="n">Node to generate output for.</param>
        /// <param name="segment">Segment of the Triple being output.</param>
        /// <param name="indent">Indent to use for pretty printing.</param>
        /// <returns></returns>
        private String GenerateNodeOutput(CompressingTurtleWriterContext context, INode n, TripleSegment segment, int indent)
        {
            StringBuilder output = new StringBuilder();

            switch (n.NodeType)
            {
            case NodeType.Blank:
                if (context.Collections.ContainsKey(n))
                {
                    output.Append(GenerateCollectionOutput(context, context.Collections[n], indent));
                }
                else
                {
                    return(context.NodeFormatter.Format(n, segment));
                }
                break;

            case NodeType.GraphLiteral:
                if (segment == TripleSegment.Predicate)
                {
                    throw new RdfOutputException(WriterErrorMessages.GraphLiteralPredicatesUnserializable("Notation 3"));
                }

                output.Append("{");
                IGraphLiteralNode glit = (IGraphLiteralNode)n;

                StringBuilder temp = new StringBuilder();
                CompressingTurtleWriterContext subcontext = new CompressingTurtleWriterContext(glit.SubGraph, new System.IO.StringWriter(temp));
                subcontext.NodeFormatter = context.NodeFormatter;
                bool contextWritten = false;

                // Write Triples 1 at a Time on a single line
                foreach (Triple t in subcontext.Graph.Triples)
                {
                    if (!contextWritten && t.Context != null && t.Context is VariableContext)
                    {
                        contextWritten = GenerateVariableQuantificationOutput(subcontext, (VariableContext)t.Context);
                        if (contextWritten)
                        {
                            output.Append(temp.ToString());
                        }
                    }

                    output.Append(GenerateNodeOutput(subcontext, t.Subject, TripleSegment.Subject, 0));
                    output.Append(" ");
                    output.Append(GenerateNodeOutput(subcontext, t.Predicate, TripleSegment.Predicate, 0));
                    output.Append(" ");
                    output.Append(GenerateNodeOutput(subcontext, t.Object, TripleSegment.Object, 0));
                    output.Append(". ");
                }

                output.Append("}");
                break;

            case NodeType.Literal:
                if (segment == TripleSegment.Predicate)
                {
                    throw new RdfOutputException(WriterErrorMessages.LiteralPredicatesUnserializable("Notation 3"));
                }
                return(context.NodeFormatter.Format(n, segment));

            case NodeType.Uri:
                return(context.NodeFormatter.Format(n, segment));

            case NodeType.Variable:
                return(context.NodeFormatter.Format(n, segment));

            default:
                throw new RdfOutputException(WriterErrorMessages.UnknownNodeTypeUnserializable("Notation 3"));
            }

            return(output.ToString());
        }
Ejemplo n.º 20
0
 /// <summary>
 /// Formats a Blank Node as a String
 /// </summary>
 /// <param name="b">Blank Node</param>
 /// <param name="segment">Triple Segment</param>
 /// <returns></returns>
 protected override string FormatBlankNode(IBlankNode b, TripleSegment? segment)
 {
     return "_:" + this._bnodeMapper.GetOutputID(b.InternalID);
 }
Ejemplo n.º 21
0
        /// <summary>
        /// Generates the Output for a Node in Turtle Syntax
        /// </summary>
        /// <param name="context">Context for writing the Graph</param>
        /// <param name="n">Node to generate Output for</param>
        /// <param name="segment">Segment of the Triple being written</param>
        /// <returns></returns>
        private String GenerateNodeOutput(TurtleWriterContext context, INode n, TripleSegment segment)
        {
            switch (n.NodeType)
            {
                case NodeType.Blank:
                    if (segment == TripleSegment.Predicate) throw new RdfOutputException(WriterErrorMessages.BlankPredicatesUnserializable("Turtle"));
                    return context.NodeFormatter.Format(n, segment);

                case NodeType.GraphLiteral:
                    throw new RdfOutputException(WriterErrorMessages.GraphLiteralsUnserializable("Turtle"));

                case NodeType.Literal:
                    if (segment == TripleSegment.Subject) throw new RdfOutputException(WriterErrorMessages.LiteralSubjectsUnserializable("Turtle"));
                    if (segment == TripleSegment.Predicate) throw new RdfOutputException(WriterErrorMessages.LiteralPredicatesUnserializable("Turtle"));
                    return context.NodeFormatter.Format(n, segment);

                case NodeType.Uri:
                    return context.NodeFormatter.Format(n, segment);

                default:
                    throw new RdfOutputException(WriterErrorMessages.UnknownNodeTypeUnserializable("Turtle"));
            }
        }
Ejemplo n.º 22
0
        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);
            }
        }
Ejemplo n.º 23
0
 /// <summary>
 /// Formats a Blank Node as a String for the given Format
 /// </summary>
 /// <param name="b">Blank Node</param>
 /// <param name="segment">Triple Segment</param>
 /// <returns></returns>
 protected virtual String FormatBlankNode(IBlankNode b, TripleSegment? segment)
 {
     if (segment == TripleSegment.Predicate) throw new RdfOutputException(WriterErrorMessages.BlankPredicatesUnserializable(this._format));
     return b.ToString();
 }
Ejemplo n.º 24
0
        /// <summary>
        /// Generates Output for Nodes in Turtle syntax
        /// </summary>
        /// <param name="context">Writer Context</param>
        /// <param name="n">Node to generate output for</param>
        /// <param name="segment">Segment of the Triple being written</param>
        /// <param name="indent">Indentation</param>
        /// <returns></returns>
        private String GenerateNodeOutput(CompressingTurtleWriterContext context, INode n, TripleSegment segment, int indent)
        {
            StringBuilder output = new StringBuilder();

            switch (n.NodeType)
            {
            case NodeType.Blank:
                if (segment == TripleSegment.Predicate)
                {
                    throw new RdfOutputException(WriterErrorMessages.BlankPredicatesUnserializable("Turtle"));
                }

                if (context.Collections.ContainsKey(n))
                {
                    output.Append(this.GenerateCollectionOutput(context, context.Collections[n], indent));
                }
                else
                {
                    return(context.NodeFormatter.Format(n, segment));
                }
                break;

            case NodeType.GraphLiteral:
                throw new RdfOutputException(WriterErrorMessages.GraphLiteralsUnserializable("Turtle"));

            case NodeType.Literal:
                if (segment == TripleSegment.Subject)
                {
                    throw new RdfOutputException(WriterErrorMessages.LiteralSubjectsUnserializable("Turtle"));
                }
                if (segment == TripleSegment.Predicate)
                {
                    throw new RdfOutputException(WriterErrorMessages.LiteralPredicatesUnserializable("Turtle"));
                }
                return(context.NodeFormatter.Format(n, segment));

            case NodeType.Uri:
                return(context.NodeFormatter.Format(n, segment));

            default:
                throw new RdfOutputException(WriterErrorMessages.UnknownNodeTypeUnserializable("Turtle"));
            }

            return(output.ToString());
        }
Ejemplo n.º 25
0
 /// <summary>
 /// Formats a Graph Literal Node as a String for the given Format
 /// </summary>
 /// <param name="glit">Graph Literal</param>
 /// <param name="segment">Triple Segment</param>
 /// <returns></returns>
 protected virtual String FormatGraphLiteralNode(IGraphLiteralNode glit, TripleSegment? segment)
 {
     throw new RdfOutputException(WriterErrorMessages.GraphLiteralsUnserializable(this._format));
 }
Ejemplo n.º 26
0
 /// <summary>
 /// Formats a URI Node as a String for the given Format
 /// </summary>
 /// <param name="u">URI Node</param>
 /// <param name="segment">Triple Segment</param>
 /// <returns></returns>
 protected abstract String FormatUriNode(IUriNode u, TripleSegment? segment);
Ejemplo n.º 27
0
        /// <summary>
        /// Formats a URI Node using QName compression if possible
        /// </summary>
        /// <param name="u">URI</param>
        /// <param name="segment">Triple Segment</param>
        /// <returns></returns>
        protected override string FormatUriNode(IUriNode u, TripleSegment? segment)
        {
            StringBuilder output = new StringBuilder();
            String qname;

            if (this._allowAKeyword && segment == TripleSegment.Predicate && u.Uri.ToString().Equals(RdfSpecsHelper.RdfType))
            {
                output.Append('a');
            }
            else if (this._qnameMapper.ReduceToQName(u.Uri.ToString(), out qname))
            {
                if (this.IsValidQName(qname))
                {
                    output.Append(qname);
                }
                else
                {
                    output.Append('<');
                    output.Append(this.FormatUri(u.Uri));
                    output.Append('>');
                }
            }
            else
            {
                output.Append('<');
                output.Append(this.FormatUri(u.Uri));
                output.Append('>');
            }
            return output.ToString();
        }
Ejemplo n.º 28
0
 /// <summary>
 /// Formats URIs for CSV output
 /// </summary>
 /// <param name="u">URI</param>
 /// <param name="segment">Triple Segment</param>
 /// <returns></returns>
 protected override string FormatUriNode(IUriNode u, TripleSegment? segment)
 {
     return this.FormatUri(u.Uri);
 }
Ejemplo n.º 29
0
 /// <summary>
 /// Gets the String representation of the Node formatted with the given Node formatter
 /// </summary>
 /// <param name="formatter">Formatter</param>
 /// <param name="segment">Triple Segment</param>
 /// <returns></returns>
 public virtual String ToString(INodeFormatter formatter, TripleSegment segment)
 {
     return formatter.Format(this, segment);
 }
Ejemplo n.º 30
0
 /// <summary>
 /// Formats a Variable Node as a String for the given Format
 /// </summary>
 /// <param name="v">Variable Name</param>
 /// <param name="segment">Triple Segment</param>
 /// <returns></returns>
 protected virtual String FormatVariableNode(IVariableNode v, TripleSegment? segment)
 {
     throw new RdfOutputException(WriterErrorMessages.VariableNodesUnserializable(this._format));
 }
Ejemplo n.º 31
0
 private bool CanSplit(IReadOnlyList <string> lines, TripleSegment segment)
 {
     return(lines.Count >= _splitThreshold && CanSplitParser(lines, segment));
     //return CanSplitRegex(lines, segment);
 }
Ejemplo n.º 32
0
 /// <summary>
 /// Formats a URI Node
 /// </summary>
 /// <param name="u">URI Node</param>
 /// <param name="segment">Triple Segment</param>
 /// <returns></returns>
 protected override string FormatUriNode(IUriNode u, TripleSegment? segment)
 {
     StringBuilder output = new StringBuilder();
     if (this._uriStartChar != null) output.Append(this._uriStartChar);
     if (this._uriEndChar != null)
     {
         output.Append(this.FormatUri(u.Uri));
         output.Append(this._uriEndChar);
     }
     else
     {
         output.Append(this.FormatUri(u.Uri));
     }
     return output.ToString();
 }
Ejemplo n.º 33
0
        /// <summary>
        /// Formats a Literal Node
        /// </summary>
        /// <param name="l">Literal Node</param>
        /// <param name="segment">Triple Segment</param>
        /// <returns></returns>
        protected override string FormatLiteralNode(ILiteralNode l, TripleSegment? segment)
        {
            StringBuilder output = new StringBuilder();
            String value;

            output.Append('"');
            value = l.Value;

            if (!value.IsFullyEscaped(this._validEscapes, this._litEscapes))
            {
                //This first replace escapes all back slashes for good measure
                value = value.EscapeBackslashes(this._validEscapes);

                //Then these escape characters that can't occur in a NTriples literal
                value = value.Replace("\n", "\\n");
                value = value.Replace("\r", "\\r");
                value = value.Replace("\t", "\\t");
                value = value.Escape('"');

                //Then remove null character since it doesn't change the meaning of the Literal
                value = value.Replace("\0", "");
            }

            foreach (char c in value.ToCharArray())
            {
                output.Append(this.FormatChar(c));
            }
            output.Append('"');

            if (!l.Language.Equals(String.Empty))
            {
                output.Append('@');
                output.Append(l.Language.ToLower());
            }
            else if (l.DataType != null)
            {
                output.Append("^^<");
                foreach (char c in this.FormatUri(l.DataType))
                {
                    output.Append(this.FormatChar(c));
                }
                output.Append('>');
            }

            return output.ToString();
        }
Ejemplo n.º 34
0
        /// <summary>
        /// Generates Node Output for the given Node
        /// </summary>
        /// <param name="output">Text Writer</param>
        /// <param name="n">Node</param>
        /// <param name="segment">Triple Segment</param>
        private void GenerateNodeOutput(TextWriter output, INode n, TripleSegment segment)
        {
            switch (n.NodeType)
            {
                case NodeType.Blank:
                    if (segment == TripleSegment.Predicate) throw new RdfOutputException(WriterErrorMessages.BlankPredicatesUnserializable("CSV"));

                    output.Write(this._formatter.Format(n));
                    break;

                case NodeType.GraphLiteral:
                    throw new RdfOutputException(WriterErrorMessages.GraphLiteralsUnserializable("CSV"));

                case NodeType.Literal:
                    if (segment == TripleSegment.Subject) throw new RdfOutputException(WriterErrorMessages.LiteralSubjectsUnserializable("CSV"));
                    if (segment == TripleSegment.Predicate) throw new RdfOutputException(WriterErrorMessages.LiteralPredicatesUnserializable("CSV"));

                    output.Write(this._formatter.Format(n));
                    break;

                case NodeType.Uri:
                    output.Write(this._formatter.Format(n));
                    break;

                default:
                    throw new RdfOutputException(WriterErrorMessages.UnknownNodeTypeUnserializable("CSV"));
            }
        }
Ejemplo n.º 35
0
 private string Format(INode node, TripleSegment segment)
 {
     return(_turtleFormatter.Format(node, segment));
 }
Ejemplo n.º 36
0
 public ImportHandler(DynamicFileStore fileStore, TripleSegment importSegment)
 {
     _fileStore     = fileStore;
     _importSegment = importSegment;
 }
Ejemplo n.º 37
0
        private static void WriteNode(XmlWriter writer, Triple triple, Dictionary <IUriNode, string> labelMapping, TripleSegment segment)
        {
            var node = HtmlWriter.GetNode(triple, segment);

            if (node is IUriNode uriNode)
            {
                var uri = uriNode.Uri.AbsoluteUri;

                if (segment == TripleSegment.Object && HtmlWriter.IsPhoto(triple.Predicate))
                {
                    var id = Global.InstanceUri.MakeRelativeUri(uriNode.Uri);

                    writer.WriteStartElement("a");
                    writer.WriteAttributeString("href", "resource?uri=" + WebUtility.UrlEncode(uri));
                    writer.WriteStartElement("data");
                    writer.WriteAttributeString("value", uri);
                    writer.WriteStartElement("img");
                    writer.WriteAttributeString("src", $"https://api.parliament.uk/photo/{id}.jpeg?crop=MCU_3:2&width=260&quality=80");
                    writer.WriteEndElement(); // data
                    writer.WriteEndElement(); // img
                    writer.WriteEndElement(); // a

                    return;
                }

                if (!labelMapping.TryGetValue(uriNode, out string label))
                {
                    label = uri
                            .Replace(Global.SchemaUri.AbsoluteUri, string.Empty)
                            .Replace(Global.InstanceUri.AbsoluteUri, string.Empty)
                            .Replace(RdfSpecsHelper.RdfType, "a")
                            .Replace("http://example.com/", string.Empty)
                            .Replace("http://www.w3.org/2000/01/rdf-schema#", string.Empty)
                            .Replace("http://www.w3.org/2002/07/owl#", string.Empty)
                            .Replace("http://www.w3.org/1999/02/22-rdf-syntax-ns#", string.Empty);
                }

                writer.WriteStartElement("div");
                writer.WriteStartElement("a");
                writer.WriteAttributeString("href", "resource?uri=" + WebUtility.UrlEncode(uri));

                writer.WriteStartElement("data");
                writer.WriteAttributeString("value", uri);
                writer.WriteString(label);
                writer.WriteEndElement(); // data

                writer.WriteEndElement(); // a
                writer.WriteEndElement(); // div

                return;
            }

            if (node is IBlankNode blankNode)
            {
                if (segment == TripleSegment.Subject)
                {
                    writer.WriteStartElement("div");
                    writer.WriteStartElement("a");
                    writer.WriteAttributeString("name", blankNode.InternalID);
                    writer.WriteFullEndElement(); // a

                    writer.WriteString(blankNode.InternalID);
                    writer.WriteEndElement(); // div

                    return;
                }

                if (segment == TripleSegment.Object)
                {
                    writer.WriteStartElement("a");
                    writer.WriteAttributeString("href", "#" + blankNode.InternalID);
                    writer.WriteString(blankNode.InternalID);
                    writer.WriteEndElement(); // a

                    return;
                }
            }

            if (node is ILiteralNode literalNode)
            {
                var datatype = literalNode.DataType?.AbsoluteUri;

                if (datatype == XmlSpecsHelper.XmlSchemaDataTypeDate)
                {
                    if (DateTimeOffset.TryParse(literalNode.Value, out DateTimeOffset dto))
                    {
                        writer.WriteStartElement("time");
                        writer.WriteString(dto.ToString("yyyy-MM-dd"));
                        writer.WriteEndElement(); // time

                        return;
                    }
                }

                if (datatype == "http://www.opengis.net/ont/geosparql#wktLiteral")
                {
                    if (HtmlWriter.IsGeography(triple.Predicate))
                    {
                        writer.WriteStartElement("data");
                        writer.WriteAttributeString("class", "map");
                        writer.WriteAttributeString("value", literalNode.Value);
                        writer.WriteEndElement(); // data

                        return;
                    }
                }

                writer.WriteString(literalNode.Value);

                return;
            }

            writer.WriteString(node.ToString());
        }
Ejemplo n.º 38
0
        /// <summary>
        /// Formats a Pattern Item in nicely formatted SPARQL syntax
        /// </summary>
        /// <param name="item">Pattern Item</param>
        /// <param name="segment">Triple Pattern Segment</param>
        /// <returns></returns>
        public virtual String Format(PatternItem item, TripleSegment? segment)
        {
            if (item is VariablePattern)
            {
                return item.ToString();
            }
            else if (item is NodeMatchPattern)
            {
                NodeMatchPattern match = (NodeMatchPattern)item;
                return this.Format(match.Node, segment);
            }
            else if (item is FixedBlankNodePattern)
            {
                if (segment != null)
                {
                    if (segment == TripleSegment.Predicate) throw new RdfOutputException("Cannot format a Fixed Blank Node Pattern Item as the Predicate of a Triple Pattern as Blank Nodes are not permitted as Predicates");
                }

                return item.ToString();
            }
            else if (item is BlankNodePattern)
            {
                return item.ToString();
            }
            else
            {
                throw new RdfOutputException("Unable to Format an unknown PatternItem implementation as a String");
            }
        }
Ejemplo n.º 39
0
        /// <summary>
        /// Formats a Literal Node as a String
        /// </summary>
        /// <param name="l">Literal Node</param>
        /// <param name="segment">Triple Segment</param>
        /// <returns></returns>
        protected override string FormatLiteralNode(ILiteralNode l, TripleSegment? segment)
        {
            StringBuilder output = new StringBuilder();
            String value, qname;
            bool longlit = false, plainlit = false;

            longlit = TurtleSpecsHelper.IsLongLiteral(l.Value);
            plainlit = TurtleSpecsHelper.IsValidPlainLiteral(l.Value, l.DataType);

            if (plainlit)
            {
                if (TurtleSpecsHelper.IsValidDecimal(l.Value) && l.Value.EndsWith("."))
                {
                    //Ensure we strip the trailing dot of any xsd:decimal and add a datatype definition
                    output.Append('"');
                    output.Append(l.Value.Substring(0, l.Value.Length - 1));
                    output.Append("\"^^<");
                    output.Append(this.FormatUri(XmlSpecsHelper.XmlSchemaDataTypeDecimal));
                    output.Append('>');
                }
                else
                {
                    //Otherwise just write out the value
                    output.Append(l.Value);
                }
                //For integers ensure we insert a space after the literal to ensure it can't ever be confused with a decimal
                if (TurtleSpecsHelper.IsValidInteger(l.Value))
                {
                    output.Append(' ');
                }
            }
            else
            {
                output.Append('"');
                if (longlit) output.Append("\"\"");

                value = l.Value;

                bool fullyEscaped = (longlit) ? value.IsFullyEscaped(this._validEscapes, this._longLitMustEscape) : value.IsFullyEscaped(this._validEscapes, this._litMustEscape);

                if (!fullyEscaped)
                {
                    //This first replace escapes all back slashes for good measure
                    value = value.EscapeBackslashes(this._validEscapes);

                    //Then remove null character since it doesn't change the meaning of the Literal
                    value = value.Replace("\0", "");

                    //Don't need all the other escapes for long literals as the characters that would be escaped are permitted in long literals
                    //Need to escape " still
                    value = value.Escape('"');

                    if (!longlit)
                    {
                        //Then if we're not a long literal we'll escape tabs
                        value = value.Replace("\t", "\\t");
                    }
                }
                output.Append(value);
                output.Append('"');
                if (longlit) output.Append("\"\"");

                if (!l.Language.Equals(String.Empty))
                {
                    output.Append('@');
                    output.Append(l.Language.ToLower());
                }
                else if (l.DataType != null)
                {
                    output.Append("^^");
                    if (this._qnameMapper.ReduceToQName(l.DataType.ToString(), out qname))
                    {
                        if (TurtleSpecsHelper.IsValidQName(qname))
                        {
                            output.Append(qname);
                        }
                        else
                        {
                            output.Append('<');
                            output.Append(this.FormatUri(l.DataType));
                            output.Append('>');
                        }
                    }
                    else
                    {
                        output.Append('<');
                        output.Append(this.FormatUri(l.DataType));
                        output.Append('>');
                    }
                }
            }

            return output.ToString();
        }
Ejemplo n.º 40
0
        /// <summary>
        /// Generates Output for Nodes in Notation 3 syntax
        /// </summary>
        /// <param name="context">Writer Context</param>
        /// <param name="n">Node to generate output for</param>
        /// <param name="segment">Segment of the Triple being output</param>
        /// <param name="indent">Indent to use for pretty printing</param>
        /// <returns></returns>
        private String GenerateNodeOutput(CompressingTurtleWriterContext context, INode n, TripleSegment segment, int indent)
        {
            StringBuilder output = new StringBuilder();

            switch (n.NodeType)
            {
                case NodeType.Blank:
                    if (context.Collections.ContainsKey(n))
                    {
                        output.Append(this.GenerateCollectionOutput(context, context.Collections[n], indent));
                    }
                    else
                    {
                        return context.NodeFormatter.Format(n, segment);
                    }
                    break;

                case NodeType.GraphLiteral:
                    if (segment == TripleSegment.Predicate) throw new RdfOutputException(WriterErrorMessages.GraphLiteralPredicatesUnserializable("Notation 3"));

                    output.Append("{");
                    IGraphLiteralNode glit = (IGraphLiteralNode)n;

                    StringBuilder temp = new StringBuilder();
                    CompressingTurtleWriterContext subcontext = new CompressingTurtleWriterContext(glit.SubGraph, new System.IO.StringWriter(temp));
                    subcontext.NodeFormatter = context.NodeFormatter;
                    bool contextWritten = false;

                    //Write Triples 1 at a Time on a single line
                    foreach (Triple t in subcontext.Graph.Triples)
                    {
                        if (!contextWritten && t.Context != null && t.Context is VariableContext)
                        {
                            contextWritten = this.GenerateVariableQuantificationOutput(subcontext, (VariableContext)t.Context);
                            if (contextWritten) output.Append(temp.ToString());
                        }

                        output.Append(this.GenerateNodeOutput(subcontext, t.Subject, TripleSegment.Subject, 0));
                        output.Append(" ");
                        output.Append(this.GenerateNodeOutput(subcontext, t.Predicate, TripleSegment.Predicate, 0));
                        output.Append(" ");
                        output.Append(this.GenerateNodeOutput(subcontext, t.Object, TripleSegment.Object, 0));
                        output.Append(". ");
                    }

                    output.Append("}");
                    break;

                case NodeType.Literal:
                    if (segment == TripleSegment.Predicate) throw new RdfOutputException(WriterErrorMessages.LiteralPredicatesUnserializable("Notation 3"));
                    return context.NodeFormatter.Format(n, segment);

                case NodeType.Uri:
                    return context.NodeFormatter.Format(n, segment);

                case NodeType.Variable:
                    return context.NodeFormatter.Format(n, segment);

                default:
                    throw new RdfOutputException(WriterErrorMessages.UnknownNodeTypeUnserializable("Notation 3"));
            }

            return output.ToString();
        }
Ejemplo n.º 41
0
        /// <summary>
        /// Generates Output for Nodes in Turtle syntax
        /// </summary>
        /// <param name="context">Writer Context</param>
        /// <param name="n">Node to generate output for</param>
        /// <param name="segment">Segment of the Triple being written</param>
        /// <param name="indent">Indentation</param>
        /// <returns></returns>
        private String GenerateNodeOutput(CompressingTurtleWriterContext context, INode n, TripleSegment segment, int indent)
        {
            StringBuilder output = new StringBuilder();

            switch (n.NodeType)
            {
                case NodeType.Blank:
                    if (segment == TripleSegment.Predicate) throw new RdfOutputException(WriterErrorMessages.BlankPredicatesUnserializable("Turtle"));

                    if (context.Collections.ContainsKey(n))
                    {
                        output.Append(this.GenerateCollectionOutput(context, context.Collections[n], indent));
                    }
                    else
                    {
                        return context.NodeFormatter.Format(n, segment);
                    }
                    break;

                case NodeType.GraphLiteral:
                    throw new RdfOutputException(WriterErrorMessages.GraphLiteralsUnserializable("Turtle"));

                case NodeType.Literal:
                    if (segment == TripleSegment.Subject) throw new RdfOutputException(WriterErrorMessages.LiteralSubjectsUnserializable("Turtle"));
                    if (segment == TripleSegment.Predicate) throw new RdfOutputException(WriterErrorMessages.LiteralPredicatesUnserializable("Turtle"));
                    return context.NodeFormatter.Format(n, segment);

                case NodeType.Uri:
                    return context.NodeFormatter.Format(n, segment);

                default:
                    throw new RdfOutputException(WriterErrorMessages.UnknownNodeTypeUnserializable("Turtle"));
            }

            return output.ToString();
        }
Ejemplo n.º 42
0
 /// <inheritdoc/>
 public string ToString(INodeFormatter formatter, TripleSegment segment)
 {
     return(Node.ToString(formatter, segment));
 }
Ejemplo n.º 43
0
        private static void WriteCell(XmlWriter writer, Triple t, Dictionary <IUriNode, string> labelMapping, TripleSegment segment, int tripleCount)
        {
            writer.WriteStartElement("td");

            if (tripleCount > 1)
            {
                writer.WriteAttributeString("rowspan", tripleCount.ToString());
            }

            HtmlWriter.WriteNode(writer, t, labelMapping, segment);
            writer.WriteEndElement(); // td
        }
Ejemplo n.º 44
0
 /// <summary>
 /// Generates Output for the given Node
 /// </summary>
 /// <param name="context">Writer Context</param>
 /// <param name="n">Node</param>
 /// <param name="segment">Triple Context</param>
 private void GenerateNodeOutput(BaseWriterContext context, INode n, TripleSegment segment)
 {
     switch (n.NodeType)
     {
         case NodeType.GraphLiteral:
             throw new RdfOutputException(WriterErrorMessages.GraphLiteralsUnserializable("TSV"));
         case NodeType.Blank:
         case NodeType.Literal:
         case NodeType.Uri:
             context.Output.Write(this._formatter.Format(n));
             break;
         default:
             throw new RdfOutputException(WriterErrorMessages.UnknownNodeTypeUnserializable("TSV"));
     }
 }
Ejemplo n.º 45
0
        /// <summary>
        /// Converts a Node into relevant NTriples Syntax
        /// </summary>
        /// <param name="n">Node to convert</param>
        /// <param name="context">Writer Context</param>
        /// <param name="segment">Triple Segment being written</param>
        /// <returns></returns>
        private String NodeToNTriples(NTriplesWriterContext context, INode n, TripleSegment segment)
        {
            switch (n.NodeType)
            {
                case NodeType.Blank:
                    if (segment == TripleSegment.Predicate) throw new RdfOutputException(WriterErrorMessages.BlankPredicatesUnserializable("NQuads"));
                    break;
                case NodeType.Literal:
                    if (segment == TripleSegment.Subject) throw new RdfOutputException(WriterErrorMessages.LiteralSubjectsUnserializable("NQuads"));
                    if (segment == TripleSegment.Predicate) throw new RdfOutputException(WriterErrorMessages.LiteralPredicatesUnserializable("NQuads"));
                    break;
                case NodeType.Uri:
                    break;
                case NodeType.GraphLiteral:
                    throw new RdfOutputException(WriterErrorMessages.GraphLiteralsUnserializable("NQuads"));
                default:
                    throw new RdfOutputException(WriterErrorMessages.UnknownNodeTypeUnserializable("NQuads"));
            }

            return context.NodeFormatter.Format(n);
        }
Ejemplo n.º 46
0
 /// <summary>
 /// Formats a Variable Node in SPARQL Syntax
 /// </summary>
 /// <param name="v">Variable Node</param>
 /// <param name="segment">Triple Segment</param>
 /// <returns></returns>
 protected override string FormatVariableNode(IVariableNode v, TripleSegment? segment)
 {
     return v.ToString();
 }
Ejemplo n.º 47
0
 /// <summary>
 /// Formats a Literal Node using QName compression for the datatype if possible
 /// </summary>
 /// <param name="l">Literal Node</param>
 /// <param name="segment">Triple Segment</param>
 /// <returns></returns>
 protected abstract override string FormatLiteralNode(ILiteralNode l, TripleSegment? segment);
Ejemplo n.º 48
0
 public WhileMatchHandler(TripleSegment segment, INode node)
 {
     _segmentToMatch = segment;
     _nodeToMatch    = node;
 }
Ejemplo n.º 49
0
 public string ToString(INodeFormatter formatter, TripleSegment segment)
 {
     return(formatter.Format(this, segment));
 }
Ejemplo n.º 50
0
        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);
        }
Ejemplo n.º 51
0
        /// <summary>
        /// Formats a Literal Node
        /// </summary>
        /// <param name="lit">Literal Node</param>
        /// <param name="segment">Triple Segment</param>
        /// <returns></returns>
        protected override string FormatLiteralNode(ILiteralNode lit, TripleSegment? segment)
        {
            StringBuilder output = new StringBuilder();
            if (TurtleSpecsHelper.IsValidPlainLiteral(lit.Value, lit.DataType))
            {
                output.Append(lit.Value);
            }
            else
            {
                String value = lit.Value;

                if (TurtleSpecsHelper.IsLongLiteral(value))
                {
                    value = value.Replace("\n", "\\n");
                    value = value.Replace("\r", "\\r");
                    value = value.Escape('"');
                    value = value.Replace("\t", "\\t");

                    //If there are no wrapper characters then we must escape the deliminator
                    if (value.Contains(this._deliminatorChar))
                    {
                        if (this._literalWrapperChar == null && this._longLiteralWrapperChar == null)
                        {
                            //Replace the deliminator
                            value = value.Replace(new String(new char[] { this._deliminatorChar }), new String(new char[] { this._escapeChar, this._deliminatorChar }));
                        }
                    }

                    //Apply appropriate wrapper characters
                    if (this._longLiteralWrapperChar != null)
                    {
                        output.Append(this._longLiteralWrapperChar + value + this._longLiteralWrapperChar);
                    }
                    else if (this._literalWrapperChar != null)
                    {
                        output.Append(this._literalWrapperChar + value + this._literalWrapperChar);
                    }
                    else
                    {
                        output.Append(value);
                    }
                }
                else
                {
                    //Replace the deliminator
                    value = value.Replace(new String(new char[] { this._deliminatorChar }), new String(new char[] { this._escapeChar, this._deliminatorChar }));

                    //Apply appropriate wrapper characters
                    if (this._literalWrapperChar != null)
                    {
                        output.Append(this._literalWrapperChar + value + this._literalWrapperChar);
                    }
                    else
                    {
                        output.Append(value);
                    }
                }

                if (this._fullLiteralOutput)
                {
                    if (!lit.Language.Equals(String.Empty))
                    {
                        output.Append("@" + lit.Language.ToLower());
                    }
                    else if (lit.DataType != null)
                    {
                        output.Append("^^");
                        if (this._uriStartChar != null) output.Append(this._uriStartChar);
                        if (this._uriEndChar != null)
                        {
                            output.Append(this.FormatUri(lit.DataType));
                            output.Append(this._uriEndChar);
                        }
                        else
                        {
                            output.Append(this.FormatUri(lit.DataType));
                        }
                    }
                }
            }
            return output.ToString();
        }
Ejemplo n.º 52
0
        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);
            }
        }