示例#1
0
def pns_to_xml_utf8(model, xml_types={}, xml_type=xml_dom.Element):
    # try to decode the PNS/XML element
    try:
        attr, first, children, follow = netstring.validate(model[2], 4)
    except:
        if model[0] != model[3]:
            attr = {'pns': model[0]}
        else:
            attr = None
        e = xml_types.get(model[1],
                          xml_type)(model[1]
                                    or 'http://allegra/ pns-xml-error', attr)
        e.xml_first = model[2]
        return e, None

    # decode the attributes and set the pns attribute
    if attr:
        attr = dict(
            (tuple(netstring.decode(item)) for item in netstring.decode(attr)))
        if model[0] != model[3]:
            attr['pns'] = model[0]
    elif model[0] != model[3]:
        attr = {'pns': model[0]}
    else:
        attr = None
    e = xml_types.get(model[1], xml_type)(model[1], attr)
    if first:
        e.xml_first = first
    else:
        e.xml_first = ''
    if follow:
        e.xml_follow = follow
    return e, children
示例#2
0
def pns_to_xml_unicode (model, xml_types={}, xml_type=xml_dom.Element):
        name = unicode (model[1], 'utf-8')
        # try to decode the PNS/XML element 
        try:
                attr, first, children, follow = netstring.validate (
                        model[2], 4
                        )
        except:
                # if no PNS/XML element is encoded in the statement object,
                # consider the predicate as the element name, the object as 
                # first CDATA and the subject as only attribute if it is
                # distinct from the statement's context. in effect, translate
                # *any* PNS statement to an XML element:
                #
                # <predicate pns="subject">object</predicate>
                #
                if model[0] != model[3]:
                        attr = {u'pns': unicode (model[0], 'utf-8')}
                else:
                        attr = None
                e = xml_types.get (name, xml_type) (
                        name or u'http://allegra/ pns-xml-error', attr
                        )
                if model[2]:
                        e.xml_first = unicode (model[2], 'utf-8')
                else:
                        e.xml_first = u''
                return e, None
                
        # decode the attributes and set the pns attribute
        if attr:
                attr = dict ((
                        tuple ((
                                unicode (s, 'utf-8')
                                for s in netstring.decode (item)
                                ))
                        for item in netstring.decode (attr)
                        ))
                if model[0] != model[3]:
                        attr[u'pns'] = unicode (model[0], 'utf-8')
        elif model[0] != model[3]:
                attr = {u'pns': unicode (model[0], 'utf-8')}
        else:
                attr = None
        # decode the name and instanciate an XML element
        e = xml_types.get (name, xml_type) (name, attr)
        if first:
                e.xml_first = unicode (first, 'utf-8')
        else:
                e.xml_first = u''
        if follow:
                e.xml_follow = unicode (follow, 'utf-8')
        return e, children
示例#3
0
def pns_to_xml_unicode(model, xml_types={}, xml_type=xml_dom.Element):
    name = unicode(model[1], 'utf-8')
    # try to decode the PNS/XML element
    try:
        attr, first, children, follow = netstring.validate(model[2], 4)
    except:
        # if no PNS/XML element is encoded in the statement object,
        # consider the predicate as the element name, the object as
        # first CDATA and the subject as only attribute if it is
        # distinct from the statement's context. in effect, translate
        # *any* PNS statement to an XML element:
        #
        # <predicate pns="subject">object</predicate>
        #
        if model[0] != model[3]:
            attr = {u'pns': unicode(model[0], 'utf-8')}
        else:
            attr = None
        e = xml_types.get(name,
                          xml_type)(name or u'http://allegra/ pns-xml-error',
                                    attr)
        if model[2]:
            e.xml_first = unicode(model[2], 'utf-8')
        else:
            e.xml_first = u''
        return e, None

    # decode the attributes and set the pns attribute
    if attr:
        attr = dict((tuple((unicode(s, 'utf-8')
                            for s in netstring.decode(item)))
                     for item in netstring.decode(attr)))
        if model[0] != model[3]:
            attr[u'pns'] = unicode(model[0], 'utf-8')
    elif model[0] != model[3]:
        attr = {u'pns': unicode(model[0], 'utf-8')}
    else:
        attr = None
    # decode the name and instanciate an XML element
    e = xml_types.get(name, xml_type)(name, attr)
    if first:
        e.xml_first = unicode(first, 'utf-8')
    else:
        e.xml_first = u''
    if follow:
        e.xml_follow = unicode(follow, 'utf-8')
    return e, children
示例#4
0
def pns_to_xml_utf8 (model, xml_types={}, xml_type=xml_dom.Element):
        # try to decode the PNS/XML element 
        try:
                attr, first, children, follow = netstring.validate (
                        model[2], 4
                        )
        except:
                if model[0] != model[3]:
                        attr = {'pns': model[0]}
                else:
                        attr = None
                e = xml_types.get (model[1], xml_type) (
                        model[1] or 'http://allegra/ pns-xml-error', attr
                        )
                e.xml_first = model[2]
                return e, None
                
        # decode the attributes and set the pns attribute
        if attr:
                attr = dict ((
                        tuple (netstring.decode (item))
                        for item in netstring.decode (attr)
                        ))
                if model[0] != model[3]:
                        attr['pns'] = model[0]
        elif model[0] != model[3]:
                attr = {'pns': model[0]}
        else:
                attr = None
        e = xml_types.get (model[1], xml_type) (model[1], attr)
        if first:
                e.xml_first = first
        else:
                e.xml_first = ''
        if follow:
                e.xml_follow = follow
        return e, children