Package pyxmpp2 :: Module xmppserializer :: Class XMPPSerializer
[hide private]

Class XMPPSerializer


Implementation of the XMPP serializer.

Single instance of this class should be used for a single stream and never reused. It will keep track of prefixes declared on the root element and used later.

Instance Methods [hide private]
 
__init__(self, stanza_namespace, extra_prefixes=None)
x.__init__(...) initializes x; see help(type(x)) for signature
 
add_prefix(self, namespace, prefix)
Add a new namespace prefix.
 
emit_head(self, stream_from, stream_to, stream_id=None, version=u'1.0', language=None)
Return the opening tag of the stream root element.
 
emit_tail(self)
Return the end tag of the stream root element.
unicode, unicode
_split_qname(self, name, is_element)
Split an element of attribute qname into namespace and local name.
unicode
_make_prefix(self, declared_prefixes)
Make up a new namespace prefix, which won't conflict with _prefixes and prefixes declared in the current scope.
unicode
_make_prefixed(self, name, is_element, declared_prefixes, declarations)
Return namespace-prefixed tag or attribute name.
unicode
_emit_element(self, element, level, declared_prefixes)
"Recursive XML element serializer.
unicode
emit_stanza(self, element)
"Serialize a stanza.

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Static Methods [hide private]
unicode
_make_ns_declarations(declarations, declared_prefixes)
Build namespace declarations and remove obsoleted mappings from declared_prefixes.
Instance Variables [hide private]
bool _head_emitted
True if the stream start tag has been emitted
int _next_id
the next sequence number to be used in auto-generated prefixes.
dict _prefixes
mapping (prefix -> uri) of known namespace prefixes
dict _root_prefixes
prefixes declared on the root element
unicode stanza_namespace
the default namespace of the stream
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, stanza_namespace, extra_prefixes=None)
(Constructor)

 
x.__init__(...) initializes x; see help(type(x)) for signature
Parameters:
  • stanza_namespace (unicode) - the default namespace used for XMPP stanzas. E.g. 'jabber:client' for c2s connections.
  • extra_prefixes (unicode to unicode mapping.) - mapping of namespaces to prefixes (not the other way) to be used on the stream. These prefixes will be declared on the root element and used in all descendants. That may be used to optimize the stream for size.
Overrides: object.__init__

add_prefix(self, namespace, prefix)

 

Add a new namespace prefix.

If the root element has not yet been emitted the prefix will be declared there, otherwise the prefix will be declared on the top-most element using this namespace in every stanza.

Parameters:
  • namespace (unicode) - the namespace URI
  • prefix (unicode) - the prefix string

emit_head(self, stream_from, stream_to, stream_id=None, version=u'1.0', language=None)

 
Return the opening tag of the stream root element.
Parameters:
  • stream_from (unicode) - the 'from' attribute of the stream. May be None.
  • stream_to (unicode) - the 'to' attribute of the stream. May be None.
  • version (unicode) - the 'version' of the stream.
  • language (unicode) - the 'xml:lang' of the stream

_split_qname(self, name, is_element)

 
Split an element of attribute qname into namespace and local name.
Parameters:
  • name (unicode) - element or attribute QName
  • is_element (bool) - True for an element, False for an attribute
Returns: unicode, unicode
namespace URI, local name

_make_prefix(self, declared_prefixes)

 
Make up a new namespace prefix, which won't conflict with _prefixes and prefixes declared in the current scope.
Parameters:
  • declared_prefixes (unicode to unicode dictionary) - namespace to prefix mapping for the current scope
Returns: unicode
a new prefix

_make_prefixed(self, name, is_element, declared_prefixes, declarations)

 

Return namespace-prefixed tag or attribute name.

Add appropriate declaration to declarations when neccessary.

If no prefix for an element namespace is defined, make the elements namespace default (no prefix). For attributes, make up a prefix in such case.

Parameters:
  • name (unicode) - QName ('{namespace-uri}local-name') to convert
  • is_element (bool) - True for element, False for an attribute
  • declared_prefixes (unicode to unicode dictionary) - mapping of prefixes already declared at this scope
  • declarations (unicode to unicode dictionary) - XMLNS declarations on the current element.
Returns: unicode

_make_ns_declarations(declarations, declared_prefixes)
Static Method

 
Build namespace declarations and remove obsoleted mappings from declared_prefixes.
Parameters:
  • declarations (unicode to unicode dictionary) - namespace to prefix mapping of the new declarations
  • declared_prefixes (unicode to unicode dictionary) - namespace to prefix mapping of already declared prefixes.
Returns: unicode
string of namespace declarations to be used in a start tag

_emit_element(self, element, level, declared_prefixes)

 
"Recursive XML element serializer.
Parameters:
  • element (ElementTree.Element) - the element to serialize
  • level (int) - nest level (0 - root element, 1 - stanzas, etc.)
  • declared_prefixes (unicode to unicode dictionary) - namespace to prefix mapping of already declared prefixes.
Returns: unicode
serialized element

emit_stanza(self, element)

 

"Serialize a stanza.

Must be called after emit_head.

Parameters:
Returns: unicode
serialized element