Example #1
0
 /// <summary>
 /// Creates a new Attribute Event from an XML Attribute
 /// </summary>
 /// <param name="qname">QName of the Attribute</param>
 /// <param name="value">Value of the Attribute</param>
 /// <param name="sourceXml">Source XML of the Attribute</param>
 /// <param name="pos">Position Info</param>
 public AttributeEvent(String qname, String value, String sourceXml, PositionInfo pos)
     : base(RdfXmlEvent.Attribute, sourceXml, pos)
 {
     this._value = value;
     if (qname.Contains(':'))
     {
         //Has a Namespace
         //Split the QName into Namespace and Local Name
         String[] parts = qname.Split(':');
         this._namespace = parts[0];
         this._localname = parts[1];
     }
     else
     {
         //Is in the Default Namespace
         this._namespace = String.Empty;
         this._localname = qname;
     }
 }
Example #2
0
 /// <summary>
 /// Creates a new Plain Literal Event
 /// </summary>
 /// <param name="value">Value of the Literal</param>
 /// <param name="language">Language Specifier of the Literal</param>
 /// <param name="sourceXml">Source XML of the Event</param>
 /// <param name="pos">Position Info</param>
 public PlainLiteralEvent(String value, String language, String sourceXml, PositionInfo pos)
     : base(RdfXmlEvent.Literal, sourceXml, pos)
 {
     this._value = value;
     this._language = language;
 }
Example #3
0
 /// <summary>
 /// Creates a new Parse Type Attribute Event
 /// </summary>
 /// <param name="type">Parse Type</param>
 /// <param name="sourceXml">Source XML</param>
 /// <param name="pos">Position Info</param>
 public ParseTypeAttributeEvent(RdfXmlParseType type, String sourceXml, PositionInfo pos)
     : base(RdfXmlEvent.ParseTypeAttribute, sourceXml, pos)
 {
     this._type = type;
 }
Example #4
0
 /// <summary>
 /// Creates a new Attribute Event from an XML Attribute
 /// </summary>
 /// <param name="localname">Local Name of the Attribute</param>
 /// <param name="ns">Namespace Prefix of the Attribute</param>
 /// <param name="value">Value of the Attribute</param>
 /// <param name="sourceXml">Source XML of the Attribute</param>
 /// <param name="pos">Position Info</param>
 public AttributeEvent(String localname, String ns, String value, String sourceXml, PositionInfo pos)
     : base(RdfXmlEvent.Attribute, sourceXml, pos)
 {
     this._value = value;
     this._localname = localname;
     this._namespace = ns;
 }
        /// <summary>
        /// Parser method which parses Json Objects reprsenting Object Nodes.
        /// </summary>
        /// <param name="context">Parser Context.</param>
        /// <param name="subj">Subject of Triples which comes from the Great-Grandparent Json Object.</param>
        /// <param name="pred">Predicate of Triples which comes form the Grandparent Json Object.</param>
        private void ParseObject(JsonParserContext context, INode subj, INode pred)
        {
            String token, nodeValue, nodeType, nodeLang, nodeDatatype;

            nodeValue = nodeType = nodeLang = nodeDatatype = null;

            PositionInfo startPos = context.CurrentPosition;

            if (context.Input.Read())
            {
                if (context.Input.TokenType == JsonToken.StartObject)
                {
                    context.Input.Read();
                    while (context.Input.TokenType != JsonToken.EndObject)
                    {
                        if (context.Input.TokenType == JsonToken.PropertyName)
                        {
                            token = context.Input.Value.ToString().ToLower();

                            // Check that we get a Property Value as a String
                            context.Input.Read();
                            if (context.Input.TokenType != JsonToken.String)
                            {
                                if (token.Equals("value") && context.Input.TokenType == JsonToken.Integer || context.Input.TokenType == JsonToken.Float)
                                {
                                    RaiseWarning("Unexpected Token '" + context.Input.TokenType + "' encountered. Expected a string value. Value will be converted to a string for parsing.");
                                }
                                else
                                {
                                    throw Error(context, "Unexpected Token '" + context.Input.TokenType.ToString() + "' encountered, expected a Property Value describing one of the properties of an Object Node", startPos);
                                }
                            }

                            // Extract the Information from the Object
                            if (token.Equals("value"))
                            {
                                if (context.Input.TokenType == JsonToken.String)
                                {
                                    nodeValue = context.Input.Value.ToString();
                                }
                                else if (context.Input.TokenType == JsonToken.Float ||
                                         context.Input.TokenType == JsonToken.Integer)
                                {
                                    nodeValue = Convert.ToString(context.Input.Value, CultureInfo.InvariantCulture);
                                }
                                else
                                {
                                    // Shouldn't end up here as it should have been caught in the initial token type check
                                    throw Error(context,
                                                "Unexpected Token '" + context.Input.TokenType.ToString() +
                                                "' encountered, expected a Property Value describing one of the properties of an Object Node",
                                                startPos);
                                }
                            }
                            else if (token.Equals("type"))
                            {
                                nodeType = context.Input.Value.ToString().ToLower();
                            }
                            else if (token.Equals("lang") || token.Equals("xml:lang"))
                            {
                                if (nodeLang == null && nodeDatatype == null)
                                {
                                    nodeLang = context.Input.Value.ToString();
                                }
                                else
                                {
                                    throw Error(context, "Unexpected Language Property specified for an Object Node where a Language or Datatype has already been specified", startPos);
                                }
                            }
                            else if (token.Equals("datatype"))
                            {
                                if (nodeDatatype == null && nodeLang == null)
                                {
                                    nodeDatatype = context.Input.Value.ToString();
                                }
                                else
                                {
                                    throw Error(context, "Unexpected Datatype Property specified for an Object Node where a Language or Datatype has already been specified", startPos);
                                }
                            }
                            else
                            {
                                throw Error(context, "Unexpected Property '" + token + "' specified for an Object Node, only 'value', 'type', 'lang' and 'datatype' are valid properties", startPos);
                            }
                        }
                        else
                        {
                            throw Error(context, "Unexpected Token '" + context.Input.TokenType.ToString() + "' encountered, expected a Property Name describing one of the properties of an Object Node", startPos);
                        }

                        context.Input.Read();
                    }

                    // Validate the Information
                    if (nodeType == null)
                    {
                        throw Error(context, "Cannot parse an Object Node from the JSON where no 'type' property was specified in the JSON Object representing the Node", startPos);
                    }
                    if (nodeValue == null)
                    {
                        throw Error(context, "Cannot parse an Object Node from the JSON where no 'value' property was specified in the JSON Object representing the Node", startPos);
                    }

                    // Turn this information into a Node
                    INode obj;
                    if (nodeType.Equals("uri"))
                    {
                        obj = context.Handler.CreateUriNode(UriFactory.Create(nodeValue));
                    }
                    else if (nodeType.Equals("bnode"))
                    {
                        obj = context.Handler.CreateBlankNode(nodeValue.Substring(nodeValue.IndexOf(':') + 1));
                    }
                    else if (nodeType.Equals("literal"))
                    {
                        if (nodeLang != null)
                        {
                            obj = context.Handler.CreateLiteralNode(nodeValue, nodeLang);
                        }
                        else if (nodeDatatype != null)
                        {
                            obj = context.Handler.CreateLiteralNode(nodeValue, UriFactory.Create(nodeDatatype));
                        }
                        else
                        {
                            obj = context.Handler.CreateLiteralNode(nodeValue);
                        }
                    }
                    else
                    {
                        throw Error(context, "Cannot parse an Object Node from the JSON where the 'type' property is not set to one of the permitted values 'uri', 'bnode' or 'literal' in the JSON Object representing the Node", startPos);
                    }

                    // Assert as a Triple
                    if (!context.Handler.HandleTriple(new Triple(subj, pred, obj)))
                    {
                        ParserHelper.Stop();
                    }
                }
            }
            else
            {
                throw Error(context, "Unexpected End of Input while trying to parse an Object Node from the JSON", startPos);
            }
        }
Example #6
0
 /// <summary>
 /// Creates a new RDF Parse Exception which contains Position Information
 /// </summary>
 /// <param name="errorMsg">Error Message</param>
 /// <param name="position">Position Information</param>
 public RdfParseException(String errorMsg, PositionInfo position)
     : this(errorMsg, position, null) { }
Example #7
0
        /// <summary>
        /// Creates a new Element Event
        /// </summary>
        /// <param name="qname">QName of the XML Node</param>
        /// <param name="baseUri">Base Uri of the XML Node</param>
        /// <param name="sourceXml">Source XML of the XML Node</param>
        /// <param name="pos">Position Info</param>
        public ElementEvent(String qname, String baseUri, String sourceXml, PositionInfo pos)
            : base(RdfXmlEvent.Element, sourceXml, pos)
        {
            this._baseuri = baseUri;

            if (qname.Contains(':'))
            {
                //Has a Namespace
                //Split the QName into Namespace and Local Name
                String[] parts = qname.Split(':');
                this._namespace = parts[0];
                this._localname = parts[1];
            }
            else
            {
                //Is in the Default Namespace
                this._namespace = String.Empty;
                this._localname = qname;
            }
        }
Example #8
0
 /// <summary>
 /// Creates a new RDF Parse Exception which contains Position Information
 /// </summary>
 /// <param name="errorMsg">Error Message</param>
 /// <param name="position">Position Information</param>
 /// <param name="cause">Error that caused this exception</param>
 public RdfParseException(String errorMsg, PositionInfo position, Exception cause)
     : this(errorMsg, position.StartLine, position.EndLine, position.StartPosition, position.EndPosition, cause)
 {
 }
Example #9
0
 /// <summary>
 /// Creates a new Blank Node ID Event for a named Blank Node
 /// </summary>
 /// <param name="identifier">Node ID for the Blank Node</param>
 /// <param name="sourceXml">Source XML</param>
 /// <param name="pos">Position Info</param>
 public BlankNodeIDEvent(String identifier, String sourceXml, PositionInfo pos)
     : base(RdfXmlEvent.BlankNodeID, sourceXml, pos)
 {
     this._id = identifier;
 }
Example #10
0
 /// <summary>
 /// Creates a new Blank Node ID Event for an anonymous Blank Node
 /// </summary>
 /// <param name="sourceXml">Source XML</param>
 /// <param name="pos">Position Info</param>
 public BlankNodeIDEvent(String sourceXml, PositionInfo pos)
     : base(RdfXmlEvent.BlankNodeID, sourceXml, pos)
 {
     this._id = String.Empty;
 }
Example #11
0
 /// <summary>
 /// Creates a new XML Base Attribute
 /// </summary>
 /// <param name="baseUri">Base URI</param>
 /// <param name="sourceXml">Source XML</param>
 /// <param name="pos">Position Info</param>
 public XmlBaseAttributeEvent(String baseUri, String sourceXml, PositionInfo pos)
     : base(RdfXmlEvent.XmlBaseAttribute, sourceXml, pos)
 {
     this._baseUri = baseUri;
 }
Example #12
0
 /// <summary>
 /// Creates a new URIRef Event from a URIRef in an XML Attribute value or similar
 /// </summary>
 /// <param name="identifier">URIRef</param>
 /// <param name="sourceXml">Source XML of the URIRef</param>
 /// <param name="pos">Position Info</param>
 public UriReferenceEvent(String identifier, String sourceXml, PositionInfo pos)
     : base(RdfXmlEvent.UriReference, sourceXml, pos)
 {
     this._id = identifier;
 }
Example #13
0
 /// <summary>
 /// Creates a new Typed Literal Event
 /// </summary>
 /// <param name="value">Value of the Literal</param>
 /// <param name="datatype">DataType Uri of the Literal</param>
 /// <param name="sourceXml">Source XML of the Event</param>
 /// <param name="pos">Position Info</param>
 public TypedLiteralEvent(String value, String datatype, String sourceXml, PositionInfo pos)
     : base(RdfXmlEvent.TypedLiteral, sourceXml, pos)
 {
     this._value = value;
     this._datatype = datatype;
 }
Example #14
0
 /// <summary>
 /// Creates a new QName Event
 /// </summary>
 /// <param name="qname">QName</param>
 /// <param name="sourceXml">Source XML of the QName</param>
 /// <param name="pos">Position Info</param>
 public QNameEvent(String qname, String sourceXml, PositionInfo pos)
     : base(RdfXmlEvent.QName, sourceXml, pos)
 {
     this._qname = qname;
 }
Example #15
0
 /// <summary>
 /// Creates new Element Event
 /// </summary>
 /// <param name="localname">Local Name of the XML Node</param>
 /// <param name="ns">Namespace Prefix of the XML Node</param>
 /// <param name="baseUri">Base Uri of the XML Node</param>
 /// <param name="sourceXml">Source XML of the XML Node</param>
 /// <param name="pos">Position Info</param>
 public ElementEvent(String localname, String ns, String baseUri, String sourceXml, PositionInfo pos)
     : base(RdfXmlEvent.Element, sourceXml, pos)
 {
     this._baseuri = baseUri;
     this._localname = localname;
     this._namespace = ns;
 }
Example #16
0
 /// <summary>
 /// Creates a new Root Event
 /// </summary>
 /// <param name="baseUri">Base Uri of the Document</param>
 /// <param name="sourceXml">Source XML of the Document</param>
 /// <param name="pos">Position Info</param>
 public RootEvent(String baseUri, String sourceXml, PositionInfo pos)
     : base(RdfXmlEvent.Root, sourceXml, pos)
 {
     this._baseuri = baseUri;
 }
Example #17
0
 /// <summary>
 /// Creates a new EndElementEvent
 /// </summary>
 public EndElementEvent(PositionInfo pos)
     : base(RdfXmlEvent.EndElement, String.Empty, pos)
 {
 }
Example #18
0
 /// <summary>
 /// Creates a new RDF Parse Exception which contains Position Information
 /// </summary>
 /// <param name="errorMsg">Error Message</param>
 /// <param name="position">Position Information</param>
 public RdfParseException(String errorMsg, PositionInfo position)
     : this(errorMsg, position, null)
 {
 }
Example #19
0
 /// <summary>
 /// Creates a new Language Attribute Event
 /// </summary>
 /// <param name="lang">Language</param>
 /// <param name="sourceXml">Source XML</param>
 /// <param name="pos">Position Info</param>
 public LanguageAttributeEvent(String lang, String sourceXml, PositionInfo pos)
     : base(RdfXmlEvent.LanguageAttribute, sourceXml, pos)
 {
     this._lang = lang;
 }
Example #20
0
 /// <summary>
 /// Creates a new RDF Parse Exception which contains Position Information
 /// </summary>
 /// <param name="errorMsg">Error Message</param>
 /// <param name="position">Position Information</param>
 /// <param name="cause">Error that caused this exception</param>
 public RdfParseException(String errorMsg, PositionInfo position, Exception cause)
     : this(errorMsg, position.StartLine, position.EndLine, position.StartPosition, position.EndPosition, cause) { }
Example #21
0
 /// <summary>
 /// Creates a new Namespace Attribute Event
 /// </summary>
 /// <param name="prefix">Namespace Prefix</param>
 /// <param name="uri">Namespace Uri</param>
 /// <param name="sourceXml">Source XML</param>
 /// <param name="pos">Position Info</param>
 public NamespaceAttributeEvent(String prefix, String uri, String sourceXml, PositionInfo pos)
     : base(RdfXmlEvent.NamespaceAttribute, sourceXml, pos)
 {
     this._prefix = prefix;
     this._uri = uri;
 }
Example #22
0
 /// <summary>
 /// Helper method for raising Error messages with attached Position Information
 /// </summary>
 /// <param name="context">Parser Context</param>
 /// <param name="message">Error Message</param>
 /// <param name="startPos">Start Position</param>
 /// <returns></returns>
 private RdfParseException Error(JsonParserContext context, String message, PositionInfo startPos)
 {
     PositionInfo info = context.GetPositionRange(startPos);
     StringBuilder error = new StringBuilder();
     error.Append("[Line " + info.StartLine + " Column " + info.StartPosition + " to Line " + info.EndLine + " Column " + info.EndPosition + "] ");
     error.AppendLine(message);
     throw new RdfParseException(error.ToString(), info);
 }
Example #23
0
 /// <summary>
 /// Creates a new Text Node
 /// </summary>
 /// <param name="value">Textual Content of the XML Text Node</param>
 /// <param name="sourceXml">Source XML of the Node</param>
 /// <param name="pos">Position Info</param>
 public TextEvent(String value, String sourceXml, PositionInfo pos)
     : base(RdfXmlEvent.Text, sourceXml, pos)
 {
     this._value = value;
 }