Esempio n. 1
0
    def start(self, tag, attrs):
        name = QNameSplit(tag)

        if self._characterBuffer is not None:
            pcdata = PCDATAElement("".join(self._characterBuffer))
            self.stack[-1]["children"].append(pcdata)
            self._characterBuffer = None

        # Need to convert a "full" namespace in an attribute QName to the form
        # "%s:%s".
        attributes_dict = {}
        for aname, avalue in attrs.items():
            anamespace, aname = QNameSplit(aname)
            if anamespace:
                anamespace = _namespace_map.get(anamespace, anamespace)
                aname = "%s:%s" % (
                    anamespace,
                    aname,
                )
            attributes_dict[aname] = avalue

        tag_namespace, tag_name = name

        if name in _elements_by_qname:
            element_class = _elements_by_qname[name]
        elif name in self.unknownElementClasses:
            element_class = self.unknownElementClasses[name]
        else:

            def element_class(*args, **kwargs):
                element = WebDAVUnknownElement(*args, **kwargs)
                element.namespace = tag_namespace
                element.name = tag_name
                return element

            self.unknownElementClasses[name] = element_class

        self.stack.append({
            "name": name,
            "class": element_class,
            "attributes": attributes_dict,
            "children": [],
        })
Esempio n. 2
0
def myfixtag(tag, namespaces):
    # given a decorated tag (of the form {uri}tag), return prefixed
    # tag and namespace declaration, if any
    if isinstance(tag, QName):
        tag = tag.text
    namespace_uri, tag = string.split(tag[1:], "}", 1)
    prefix = namespaces.get(namespace_uri)
    if prefix is None:
        prefix = _namespace_map.get(namespace_uri)
        if prefix is None:
            prefix = "ns%d" % len(namespaces)
        namespaces[namespace_uri] = prefix
        if prefix == "xml":
            xmlns = None
        else:
            xmlns = ("xmlns:%s" % prefix, namespace_uri)
    else:
        xmlns = None
    return "%s:%s" % (prefix, tag), xmlns
Esempio n. 3
0
 def fixtag(tag, namespaces):
     # given a decorated tag (of the form {uri}tag), return prefixed
     # tag and namespace declaration, if any
     if isinstance(tag, QName):
         tag = tag.text
     namespace_uri, tag = string.split(tag[1:], "}", 1)
     prefix = namespaces.get(namespace_uri)
     if prefix is None:
         prefix = _namespace_map.get(namespace_uri)
         if prefix is None:
             prefix = "ns%d" % len(namespaces)
         namespaces[namespace_uri] = prefix
         if prefix == "xml":
             xmlns = None
         else:
             xmlns = ("xmlns:%s" % prefix, namespace_uri)
     else:
         xmlns = None
     return "%s:%s" % (prefix, tag), xmlns
Esempio n. 4
0
    def start(self, tag, attrs):
        name = QNameSplit(tag)

        if self._characterBuffer is not None:
            pcdata = PCDATAElement("".join(self._characterBuffer))
            self.stack[-1]["children"].append(pcdata)
            self._characterBuffer = None

        # Need to convert a "full" namespace in an attribute QName to the form
        # "%s:%s".
        attributes_dict = {}
        for aname, avalue in attrs.items():
            anamespace, aname = QNameSplit(aname)
            if anamespace:
                anamespace = _namespace_map.get(anamespace, anamespace)
                aname = "%s:%s" % (anamespace, aname,)
            attributes_dict[aname] = avalue

        tag_namespace, tag_name = name

        if name in _elements_by_qname:
            element_class = _elements_by_qname[name]
        elif name in self.unknownElementClasses:
            element_class = self.unknownElementClasses[name]
        else:
            def element_class(*args, **kwargs):
                element = WebDAVUnknownElement(*args, **kwargs)
                element.namespace = tag_namespace
                element.name      = tag_name
                return element
            self.unknownElementClasses[name] = element_class

        self.stack.append({
            "name"       : name,
            "class"      : element_class,
            "attributes" : attributes_dict,
            "children"   : [],
        })
Esempio n. 5
0
 def add_qname(qname):
     # calculate serialized qname representation
     try:
         qname = _tounicode(qname)
         if qname[:1] == "{":
             uri, tag = qname[1:].rsplit("}", 1)
             prefix = namespaces.get(uri)
             if prefix is None:
                 prefix = _namespace_map.get(uri)
                 if prefix is None:
                     prefix = "ns%d" % len(namespaces)
                 else:
                     prefix = _tounicode(prefix)
                 if prefix != "xml":
                     namespaces[uri] = prefix
             if prefix:
                 qnames[qname] = "%s:%s" % (prefix, tag)
             else:
                 qnames[qname] = tag  # default element
         else:
             qnames[qname] = qname
     except TypeError:
         _raise_serialization_error(qname)