/// <inheritdoc/> public override T Peek() { if (_events.Count > _threshold) { return(_events.Peek()); } throw new InvalidOperationException("Event queue is empty"); }
public void Publish(string pluginName, IEventPublisher callback) { while (ququedEventsQueue.GetCount(pluginName) > 0) { var e = ququedEventsQueue.Peek(pluginName); if (RePublish(pluginName, e, callback)) { ququedEventsQueue.Dequeue(pluginName); } else { logger.Warn("Can not send event {0} for plugin {1} so this loop was break.", e, pluginName); break; } } }
/// <summary> /// Implementation of the RDF/XML Grammar Production 'parseTypeCollectionPropertyElt' /// </summary> /// <param name="context">Parser Context</param> /// <param name="eventlist">Queue of Events that make up the Collection Parse Type Property Element and its Children</param> /// <param name="parent">Parent Event (ie. Node) of the Property Element</param> private void GrammarProductionParseTypeCollectionPropertyElement(RdfXmlParserContext context, IEventQueue<IRdfXmlEvent> eventlist, IRdfXmlEvent parent) { //Tracing if (this._traceparsing) { this.ProductionTracePartial("Parse Type Collection Property Element"); } //Get the first Event, should be an ElementEvent //Type checking is done by the Parent Production IRdfXmlEvent first = eventlist.Dequeue(); ElementEvent element = (ElementEvent)first; if (this._traceparsing) this.ProductionTracePartial(element); //Apply Namespaces this.ApplyNamespaces(context, element); //Validate Attributes String ID = String.Empty; if (element.Attributes.Count > 2) { //Can't be more than 2 Attributes, only allowed an optional rdf:ID and a required rdf:parseType throw ParserHelper.Error("An Property Element with Parse Type 'Collection' was encountered with too many Attributes. Only rdf:ID and rdf:parseType are allowed on Property Elements with Parse Type 'Collection'", "Parse Type Collection Property Element", element); } else { //Check the attributes that do exist foreach (AttributeEvent a in element.Attributes) { if (RdfXmlSpecsHelper.IsIDAttribute(a)) { ID = "#" + a.Value; } else if (a.QName.Equals("rdf:parseType")) { //OK } else { //Invalid Attribute throw ParserHelper.Error("Unexpected Attribute '" + a.QName + "' was encountered on a Property Element with Parse Type 'Collection'. Only rdf:ID and rdf:parseType are allowed on Property Elements with Parse Type 'Collection'", "Parse Type Collection Property Element", element); } } } //Build sequence of Blank Nodes IRdfXmlEvent next; IRdfXmlEvent nodeElement; Queue<ElementEvent> seqNodes = new Queue<ElementEvent>(); while (eventlist.Count > 1) { #region Node Element Processing //Need to process the Node Element first //Create a new Sublist IEventQueue<IRdfXmlEvent> subevents = new EventQueue<IRdfXmlEvent>(); int nesting = 0; nodeElement = eventlist.Peek(); //Add Node Element to sequence seqNodes.Enqueue((ElementEvent)nodeElement); //Gather the Sublist taking account of nesting do { next = eventlist.Dequeue(); subevents.Enqueue(next); if (next is ElementEvent) { nesting++; } else if (next is EndElementEvent) { nesting--; } } while (nesting > 0); //Call the next Grammar Production this.GrammarProductionNodeElement(context, subevents); #endregion } //Build a triple expressing the start of the list (which may be an empty list) INode subj, pred, obj; INode firstPred, restPred; INode b1, b2; //Subject comes from Parent ElementEvent parentElement = (ElementEvent)parent; subj = parentElement.SubjectNode; //Validate the ID (if any) if (!ID.Equals(String.Empty)) { this.ValidateID(context, ID.Substring(1), subj); } //Predicate from the Element pred = this.Resolve(context, element);//context.Handler.CreateUriNode(element.QName); if (seqNodes.Count > 0) { //Non-empty list ElementEvent node; //Get first Element from the Queue node = seqNodes.Dequeue(); //Object is first thing in the Sequence which we create a Blank Node for b1 = context.Handler.CreateBlankNode(); //Assert if (!context.Handler.HandleTriple(new Triple(subj, pred, b1))) ParserHelper.Stop(); //Reify if applicable if (!ID.Equals(String.Empty)) { //Resolve the Uri UriReferenceEvent uriref = new UriReferenceEvent(ID, String.Empty); IUriNode uri = this.Resolve(context, uriref, element.BaseUri); this.Reify(context, uri, subj, pred, b1); } //Set the first element in the list subj = b1; firstPred = context.Handler.CreateUriNode(UriFactory.Create(RdfSpecsHelper.RdfListFirst)); if (!context.Handler.HandleTriple(new Triple(subj, firstPred, node.SubjectNode))) ParserHelper.Stop(); //Middle elements of the list restPred = context.Handler.CreateUriNode(UriFactory.Create(RdfSpecsHelper.RdfListRest)); while (seqNodes.Count >= 1) { node = seqNodes.Dequeue(); //Set Node 2 to be the rest of the previous items list b2 = context.Handler.CreateBlankNode(); if (!context.Handler.HandleTriple(new Triple(b1, restPred, b2))) ParserHelper.Stop(); //Set Node 2 to be the start of it's own list if (!context.Handler.HandleTriple(new Triple(b2, firstPred, node.SubjectNode))) ParserHelper.Stop(); b1 = b2; } //Set last element of the list to have its rest as nil if (!context.Handler.HandleTriple(new Triple(b1, restPred, context.Handler.CreateUriNode(UriFactory.Create(RdfSpecsHelper.RdfListNil))))) ParserHelper.Stop(); } else { //Empty list //Object is therefore rdf:nil obj = context.Handler.CreateUriNode(UriFactory.Create(RdfSpecsHelper.RdfListNil)); //Assert if (!context.Handler.HandleTriple(new Triple(subj, pred, obj))) ParserHelper.Stop(); //Reify if applicable if (!ID.Equals(String.Empty)) { //Resolve the Uri UriReferenceEvent uriref = new UriReferenceEvent(ID, String.Empty); IUriNode uri = this.Resolve(context, uriref, element.BaseUri); this.Reify(context, uri, subj, pred, obj); } } //Check last event is an EndElementEvent next = eventlist.Dequeue(); if (!(next is EndElementEvent)) { throw ParserHelper.Error("Unexpected Event '" + next.GetType().ToString() + "', expected an EndElementEvent to terminate a Parse Type Collection Property Element!", "Parse Type Collection Property Element", next); } }
/// <summary> /// Implementation of the RDF/XML Grammar Production 'resourcePropertyElt' /// </summary> /// <param name="context">Parser Context</param> /// <param name="eventlist">Queue of Events that make up the Resource Property Element and its Children</param> /// <param name="parent">Parent Event (ie. Node) of the Property Element</param> private void GrammarProductionResourcePropertyElement(RdfXmlParserContext context, IEventQueue<IRdfXmlEvent> eventlist, IRdfXmlEvent parent) { //Tracing if (this._traceparsing) { this.ProductionTracePartial("Resource Property Element"); } //Cast to an ElementEvent //We don't validate type here since we know this will be an ElementEvent because the calling function //will have done this validation previously IRdfXmlEvent first = eventlist.Dequeue(); IRdfXmlEvent next = eventlist.Peek(); ElementEvent element = (ElementEvent)first; if (this._traceparsing) this.ProductionTracePartial(element); //Apply Namespaces this.ApplyNamespaces(context, element); //Only allowed one attribute max which must be an ID attribute String ID = String.Empty; if (element.Attributes.Count > 1) { throw ParserHelper.Error("A Resource Property Element contains too many Attributes, only rdf:ID is permitted", element); } else if (element.Attributes.Count == 1) { if (!RdfXmlSpecsHelper.IsIDAttribute(element.Attributes.First())) { throw ParserHelper.Error("A Resource Property Element was encountered with a single attribute which was not rdf:ID, only rdf:ID is permitted", element); } else { ID = element.Attributes.First().Value; } } //Next must be an ElementEvent if (!(next is ElementEvent)) { throw ParserHelper.Error("Unexpected Event '" + next.GetType().ToString() + "', expected an ElementEvent as the first Event in a Resource Property Elements Event list", next); } //Get list of Sub Events IEventQueue<IRdfXmlEvent> subevents = new EventQueue<IRdfXmlEvent>(); while (eventlist.Count > 1) { subevents.Enqueue(eventlist.Dequeue()); } this.GrammarProductionNodeElement(context, subevents); //Check Last is an EndElementEvent IRdfXmlEvent last = eventlist.Dequeue(); if (!(last is EndElementEvent)) { throw ParserHelper.Error("Unexpected Event '" + last.GetType().ToString() + "', expected an EndElement Event", last); } //Now we can generate the relevant RDF INode subj, pred, obj; //Validate the Type of the Parent if (!(parent is ElementEvent)) { throw ParserHelper.Error("Unexpected Parent Event '" + parent.GetType().ToString() + "', expected an ElementEvent", parent); } ElementEvent parentEl = (ElementEvent)parent; //Get the Subject Node from the Parent subj = parentEl.SubjectNode; //Validate the ID (if any) if (!ID.Equals(String.Empty)) { this.ValidateID(context, ID, subj); } //Create a Predicate from this Element pred = this.Resolve(context, element);//context.Handler.CreateUriNode(element.QName); //Get the Object Node from the Child Node ElementEvent child = (ElementEvent)next; obj = child.SubjectNode; //Assert the Triple if (!context.Handler.HandleTriple(new Triple(subj, pred, obj))) ParserHelper.Stop(); //Add Reification where appropriate if (element.Attributes.Count == 1) { //Must be an rdf:ID attribute as we've validated this earlier //Get the Attribute Event and generate a Uri from it AttributeEvent attr = element.Attributes.First(); UriReferenceEvent uriref = new UriReferenceEvent("#" + attr.Value, attr.SourceXml); IUriNode uri = this.Resolve(context, uriref, element.BaseUri); this.Reify(context, uri, subj, pred, obj); } }
/// <summary> /// Implementation of the RDF/XML Grammar Production 'propertyElt' /// </summary> /// <param name="context">Parser Context</param> /// <param name="eventlist">Queue of Events that make up the Property Element and its Children</param> /// <param name="parent">Parent Event (ie. Node) of the Property Element</param> private void GrammarProductionPropertyElement(RdfXmlParserContext context, IEventQueue<IRdfXmlEvent> eventlist, IRdfXmlEvent parent) { //Tracing if (this._traceparsing) { this.ProductionTracePartial("Property Element"); } //Get first thing from the Queue IRdfXmlEvent first = eventlist.Dequeue(); ElementEvent element; //Must be an ElementEvent if (!(first is ElementEvent)) { //Unexpected Event throw ParserHelper.Error("Expected an ElementEvent but encountered a '" + first.GetType().ToString() + "'", "PropertyElement", first); } //Validate the Uri element = (ElementEvent)first; if (this._traceparsing) this.ProductionTracePartial(element); this.ApplyNamespaces(context, element); if (!RdfXmlSpecsHelper.IsPropertyElementURI(element.QName)) { //Invalid Uri throw ParserHelper.Error("A Property Element was encountered with an invalid URI '" + element.QName + "'\nCore Syntax Terms, Old Syntax Terms and rdf:Description cannot be used as Property Element URIs", "PropertyElement", element); } //List Expansion if (element.QName.Equals("rdf:li")) { UriReferenceEvent u = this.ListExpand(parent); element.SetUri(u); } //Need to select what to do based on the Type of Property Element IRdfXmlEvent next = eventlist.Peek(); //This call inserts the first element back at the head of the queue //Most of the sub-productions here need this //Would ideally use Stacks instead of Queues but Queues make more sense for most of the Parsing this.QueueJump(eventlist, first); if (element.ParseType == RdfXmlParseType.None) { //A Resource/Literal Property Element if (next is ElementEvent) { //Resource this.GrammarProductionResourcePropertyElement(context, eventlist, parent); } else if (next is TextEvent) { //Literal this.GrammarProductionLiteralPropertyElement(context, eventlist, parent); } else if (next is EndElementEvent) { //An Empty Property Element this.GrammarProductionEmptyPropertyElement(context, element, parent); } else { //Error throw ParserHelper.Error("An Element which should be Parsed with the Default Parsing Rules was encountered without a valid subsequent Event - Parser cannot proceed!", "Property Element", element); } } else if (element.ParseType == RdfXmlParseType.Literal) { //A rdf:parseType="Literal" Property Element this.GrammarProductionParseTypeLiteralPropertyElement(context, eventlist, parent); } else if (element.ParseType == RdfXmlParseType.Collection) { //A rdf:parseType="Collection" Property Element this.GrammarProductionParseTypeCollectionPropertyElement(context, eventlist, parent); } else if (element.ParseType == RdfXmlParseType.Resource) { //A rdf:parseType="Resource" Property Element this.GrammarProductionParseTypeResourcePropertyElement(context, eventlist, parent); } else if (next is EndElementEvent) { //An Empty Property Element this.GrammarProductionEmptyPropertyElement(context, element, parent); } else { //Error throw ParserHelper.Error("An Element without a known Parse Type was encountered Or the Parser was unable to determine what to do based on the subsequent event - Parser cannot proceed!", "Node Element", element); } }