/// <summary>
        /// Gets the next event from the XML stream
        /// </summary>
        /// <returns></returns>
        public IRdfXmlEvent GetNextEvent()
        {
            if (this._stop) return null;

            //If the Root Element is filled then return it
            if (this._rootEl != null)
            {
                IRdfXmlEvent temp = this._rootEl;
                this._rootEl = null;
                return temp;
            }

            //If Literal Parsing flag is set then we've just read an element which had rdf:parseType="Literal"
            if (this._parseLiteral)
            {
                this._requireEndElement = true;
                this._parseLiteral = false;
                this._noRead = true;
                this._reader.MoveToContent();
                String data = this._reader.ReadInnerXml();
                return new TypedLiteralEvent(data, RdfSpecsHelper.RdfXmlLiteral, data, this.GetPosition());
            }

            //If we need to return an end element then do so
            if (this._requireEndElement)
            {
                this._requireEndElement = false;
                this._currentBaseUri = this._baseUris.Pop();
                return new EndElementEvent(this.GetPosition());
            }

            //If at EOF throw an error
            if (this._reader.EOF) throw new RdfParseException("Unable to read further events as the end of the stream has already been reached");

            //Otherwise attempt to read the next node
            bool read = true;
            if (!this._noRead)
            {
                read = this._reader.Read();
            }
            else
            {
                this._noRead = false;
            }
            if (read)
            {
                //Return the appropriate event for the Node Type
                switch (this._reader.NodeType)
                {
                    case XmlNodeType.Element:
                        //Element
                        if (this._first)
                        {
                            this._first = false;
                            this._rdfRootSeen = this.IsName("RDF", NamespaceMapper.RDF);
                            this._rootEl = this.GetElement();
                            RootEvent root = new RootEvent(this.GetBaseUri(), this._reader.Value, this.GetPosition());
                            root.Children.Add((ElementEvent)this._rootEl);
                            return root;
                        }
                        else
                        {
                            if (!this._first && this.IsName("RDF", NamespaceMapper.RDF))
                            {
                                if (this._rdfRootSeen) throw new RdfParseException("Unexpected nested rdf:RDF node encountered, this is not valid RDF/XML syntax");
                                this._noRead = true;
                                this._first = true;
                                return new ClearQueueEvent();
                            }
                            return this.GetElement();
                        }

                    case XmlNodeType.EndElement:
                        //End of an Element
                        this._currentBaseUri = this._baseUris.Pop();
                        if (this.IsName("RDF", NamespaceMapper.RDF))
                        {
                            this._stop = true;
                        }
                        return new EndElementEvent(this.GetPosition());

                    case XmlNodeType.Attribute:
                        //Attribute
                        throw new RdfParseException("Unexpected Attribute Node encountered");

                    case XmlNodeType.Text:
                        return new TextEvent(this._reader.Value, this._reader.Value, this.GetPosition());

                    case XmlNodeType.CDATA:
                        return new TextEvent(this._reader.Value, this._reader.Value, this.GetPosition());

                    case XmlNodeType.Document:
                    case XmlNodeType.DocumentType:
                    case XmlNodeType.XmlDeclaration:
                    case XmlNodeType.Comment:
                    case XmlNodeType.ProcessingInstruction:
                    case XmlNodeType.Notation:
                    case XmlNodeType.Whitespace:
                        //Node Types that don't generate events and just indicate to continue reading
                        return this.GetNextEvent();

                    default:
                        throw new RdfParseException("Unexpected XML Node Type " + this._reader.NodeType.ToString() + " encountered");
                }
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// Given an XML Node that is the Root of the RDF/XML section of the Document Tree creates the RootEvent and generates the rest of the Event Tree by recursive calls to the <see cref="GenerateEvents">GenerateEvents</see> method
        /// </summary>
        /// <param name="context">Parser Context</param>
        /// <param name="docEl">XML Node that is the Root of the RDF/XML section of the Document Tree</param>
        /// <returns></returns>
        private RootEvent GenerateEventTree(RdfXmlParserContext context, XmlNode docEl)
        {
            //Get the Document Element
            //XmlNode docEl = document.DocumentElement;

            //Generate a Root Event and Element Event from it
            RootEvent root = new RootEvent(docEl.BaseURI, docEl.OuterXml);
            if (docEl.BaseURI.Equals(String.Empty))
            {
                if (context.BaseUri != null)
                {
                    root.BaseUri = context.BaseUri.ToString();
                }
            }
            ElementEvent element = new ElementEvent(docEl.LocalName, docEl.Prefix, root.BaseUri, docEl.OuterXml);

            //Initialise Language Settings
            root.Language = String.Empty;
            element.Language = root.Language;

            //Set as Document Element and add as only Child
            root.DocumentElement = element;
            root.Children.Add(element);

            #region Attribute Processing

            //Go through attributes looking for XML Namespace Declarations
            foreach (XmlAttribute attr in docEl.Attributes)
            {
                if (attr.Prefix.Equals("xmlns") || attr.Name == "xmlns")
                {
                    //Define a Namespace
                    String prefix = attr.LocalName;
                    if (prefix.Equals("xmlns")) prefix = String.Empty;
                    String uri;
                    if (attr.Value.StartsWith("http://"))
                    {
                        //Absolute Uri
                        uri = attr.Value;
                    }
                    else if (!root.BaseUri.Equals(String.Empty))
                    {
                        //Relative Uri with a Base Uri to resolve against
                        //uri = root.BaseUri + attr.Value;
                        uri = Tools.ResolveUri(attr.Value, root.BaseUri);
                    }
                    else
                    {
                        //Relative Uri with no Base Uri
                        throw new RdfParseException("Cannot resolve a Relative Namespace URI since there is no in-scope Base URI");
                    }
                    context.Namespaces.AddNamespace(prefix, new Uri(uri));
                }
                else if (attr.Name == "xml:base")
                {
                    //Set the Base Uri
                    String baseUri = attr.Value;

                    if (RdfXmlSpecsHelper.IsAbsoluteURI(baseUri))
                    {
                        //Absolute Uri
                        root.BaseUri = baseUri;
                    }
                    else if (!element.BaseUri.Equals(String.Empty))
                    {
                        //Relative Uri with a Base Uri to resolve against
                        //root.BaseUri += baseUri;
                        root.BaseUri = Tools.ResolveUri(baseUri, root.BaseUri);
                    }
                    else
                    {
                        //Relative Uri with no Base Uri
                        throw new RdfParseException("Cannot resolve a Relative Base URI since there is no in-scope Base URI");
                    }
                    element.BaseUri = root.BaseUri;
                }
            }

            #endregion

            //Iterate over Children
            foreach (XmlNode child in docEl.ChildNodes)
            {
                //Ignore Irrelevant Node Types
                if (this.IsIgnorableNode(child))
                {
                    continue;
                }

                //Generate an Event for the Child Node
                ElementEvent childEvent = this.GenerateEvents(context, child, element);
                element.Children.Add(childEvent);
            }

            //Return the Root Event
            return root;
        }
Example #3
0
        /// <summary>
        /// Implementation of the RDF/XML Grammar Production 'doc'
        /// </summary>
        /// <param name="context">Parser Context</param>
        /// <param name="root">Root Event to start applying Productions from</param>
        private void GrammarProductionDoc(RdfXmlParserContext context, RootEvent root)
        {
            //Tracing
            if (this._traceparsing)
            {
                this.ProductionTrace("Doc");
            }

            //Call the RDF Production on the first child if it's an rdf:RDF element
            if (root.Children[0].QName.Equals("rdf:RDF") || root.Children[0].QName.Equals(":RDF"))
            {
                this.GrammarProductionRDF(context, root.Children[0]);
            }
            else
            {
                //No rdf:RDF element
                //Drop first element from Queue (which will be a RootEvent)
                //Skip straight to NodeElementList production
                //context.Events.Dequeue();
                this.GrammarProductionNodeElementList(context, context.Events);
            }
        }