Skip to content

nathan130200/XmppSharp

Repository files navigation

XMPP#

Lightweight XMPP library.

nuget

nuget-expat

nuget-tokenizer


Supported features


Parsing

The lib uses XmlReader as a base to read and tokenize the XML stream for the XMPP protocol. It can control and maintain the XML state in a stable and secure way.

Additional parsers can be implemented along with the lib. For example, the default implementation of XmppSharp.Expat contains the basic code for XML parsing using the expat parser (basically one of the best XML parsers out there).

Consider that some parsers (like our official implementation of XmppSharp.Expat) use external libraries, requiring P/Invoking, which requires the user to have already installed the library on their computer. The most practical way to obtain these compiled libraries is using vcpkg (in the case of expat, they have releases ready for download in the official repository).

In the current version we introduced a new class XmppSharp.Dom.Document that aims to parse an XML file or even an XML string in a simple and fast way without also depending on System.Xml using fully the Xmpp Sharp APIs! It supports both reading and writing an XML document to a file or string.

Element Factory

The ElementFactory class registers and maintains the mapping of all XML elements to their respective classes. Each element is qualified by a name (Tag name) and its namespace URI, when registered the parser will obtain this information and construct the class corresponding to the XML element. If the mapping does not exist, it will use a fallback by constructing an instance of XmppSharp.Dom.Element.

Consider the example table below demonstrating how this mapping works:

Qualified Tag Name Namespace(s) Mapped Class
iq see below XmppSharp.Protocol.Core.Iq
message see below XmppSharp.Protocol.Core.Message
presence see below XmppSharp.Protocol.Core.Presence
error see below XmppSharp.Protocol.Base.StanzaError
starttls urn:ietf:params:xml:ns:xmpp-tls XmppSharp.Protocol.Tls.StartTls
success urn:ietf:params:xml:ns:xmpp-sasl XmppSharp.Protocol.Sasl.Success
stream:features http://etherx.jabber.org/streams XmppSharp.Protocol.Base.StreamFeatures
... ... ...

Note

Some cases like iq, message, presence, error (error element inside an stanza), have more than one namespace defined (because it depends on each specific piece of the protocol), but they are declared in the same way. The difference is that more than one namespace is assigned to this element, so the ElementFactory can correctly map which one it will instantiate.


Defining your XML elements is simple, just create a class that inherits the base class XmppSharp.Dom.Element or similar, call the base constructor of the class to initialize the tag name correctly. And add the attribute [XmppTag(tagName, namespace)] for each desired tag and namespace and register the type by calling ElementFactory.RegisterType (or to register an entire assembly ElementFacotry.RegisterAssembly), eg:

// define xmpp tags
[XmppTag("bind", Namespaces.Bind)]
public class Bind : Element
{
    public Bind() : base("bind", Namespaces.Bind) // call base constructor to set-up this element instance.
    {
        ...
    }
}

Notes:

  • We introduced again XMPP# tokenizer package as separated package for handling chunked xmpp bytes.