This module contains classes that may be used as publishing handlers in
ll.xist.xsc.Node.publish.
class Publisher(object):
A Publisher object is used for serializing an XIST tree into a byte
sequence.
def __init__(self, encoding=None, xhtml=1, validate=True, prefixes={}, prefixdefault=False, hidexmlns=(), showxmlns=()):
selfCreate a publisher. Arguments have the following meaning:
encoding(string orNone)Specifies the encoding to be used for the byte sequence. If
Noneis used the encoding in the XML declaration will be used. If there is no XML declaration, UTF-8 will be used.xhtml(int)With the parameter
xhtmlyou can specify if you want HTML output:- HTML (
xhtml==0) Elements with a empty content model will be published as
<foo>.- HTML browser compatible XML (
xhtml==1) Elements with an empty content model will be published as
<foo />and others that just happen to be empty as<foo></foo>. This is the default.- Pure XML (
xhtml==2) All empty elements will be published as
<foo/>.
- HTML (
validate(bool)Specifies whether validation should be done before publishing.
prefixes(mapping)A dictionary that specifies which namespace prefixes should be used for publishing. Keys in the dictionary are either namespace names or objects that have an
xmlnsattribute which is the namespace name. Values can be:FalseTreat elements in this namespace as if they are not in any namespace (if global attributes from this namespace are encountered, a non-empty prefix will be used nonetheless).
NoneTreat the namespace as the default namespaces (i.e. use unprefixed element names). Global attributes will again result in a non-empty prefix.
TrueThe publisher uses a unique non-empty prefix for this namespace.
- A string
Use this prefix for the namespace.
prefixdefault(string orNone)If an element or attribute is encountered whose namespace name is not in
prefixesprefixdefaultis used as the fallback.hidexmlns(list or set)hidexmlnscan be a list or set that contains namespace names for which noxmlnsattributes should be published. (This can be used to hide the namespace declarations for e.g. Java taglibs.)showxmlns(list or set)showxmlnscan be a list or set that contains namespace names for whichxmlnsattributes will be published, even if there are no elements from this namespace in the tree.
def encode(self, text):
selfEncode text with the encoding and error handling currently active
and return the resulting byte string.
def encodetext(self, text):
selfEncode test as text data. text must be a unicode
object. The publisher will apply the configured encoding, error handling
and the current text filter (which escapes characters that can't appear
in text data (like < etc.)) and returns the resulting str
object.
def pushtextfilter(self, filter):
selfPushes a new text filter function ontp the text filter stack. This
function is responsible for escaping characters that can't appear in text
data (like <)). This is used to switch on escaping of " inside
attribute values.
def poptextfilter(self):
selfPops the current text filter function from the stack.
def pusherrors(self, errors):
selfPushes a new error handling scheme onto the error handling stack.
def poperrors(self):
selfPop the current error handling scheme from the error handling stack.
def _newprefix(self):
selfdef getencoding(self):
selfReturn the encoding currently in effect.
def getnamespaceprefix(self, xmlns):
selfReturn (and register) a namespace prefix for the namespace name
xmlns. This honors the namespace configuration from self.prefixes
and self.prefixdefault. Furthermore the same prefix will be returned
from now on (except when the empty prefix becomes invalid once global
attributes are encountered)
def getobjectprefix(self, object):
selfGet and register a namespace prefix for the namespace object lives
in (specified by the xmlns attribute of object). Similar
to getnamespaceprefix this honors the namespace configuration from
self.prefixes and self.prefixdefault (except when a global
attribute requires a non-empty prefix).
def iterbytes(self, node, base=None):
selfOutput the node node. This method is a generator that will yield
the resulting XML byte sequence in fragments.
def bytes(self, node, base=None):
selfReturn a byte string in XML format for the XIST node node.
def iterstring(self, node, base=None):
selfA generator that will produce a serialized string of node.
def string(self, node, base=None):
selfReturn a unicode string for node.
def write(self, stream, node, base=None):
selfWrite node to the file-like object stream (which must
provide a write method).