Esempio n. 1
0
    def deserialize(self, source, prefixes=None, strict=False, as_attribute=None, as_list=None,
                    as_attribute_of_element=None):
        
        if isinstance(source, basestring):
            try:
                if os.path.exists(source):
                    xtr = XmlTextReader(StreamReader(source))
            except ValueError:
                xtr = XmlTextReader(StringReader(source))
            else:
                xtr = XmlTextReader(StringReader(source))
        elif hasattr(source, 'read'):
            xtr = XmlTextReader(StringReader(source.read()))

        settings = XmlReaderSettings()
        settings.ProhibitDtd = True
        reader = XmlReader.Create(xtr, settings)
        
        document = Document()
        document.as_attribute = as_attribute or {}
        document.as_list = as_list or {}
        document.as_attribute_of_element = as_attribute_of_element or {}

        self._deserialize(reader, document)

        xtr.Close()
        reader.Close()

        return document
Esempio n. 2
0
    def deserialize(self,
                    source,
                    prefixes=None,
                    strict=False,
                    as_attribute=None,
                    as_list=None,
                    as_attribute_of_element=None):

        if isinstance(source, basestring):
            try:
                if os.path.exists(source):
                    xtr = XmlTextReader(StreamReader(source))
            except ValueError:
                xtr = XmlTextReader(StringReader(source))
            else:
                xtr = XmlTextReader(StringReader(source))
        elif hasattr(source, 'read'):
            xtr = XmlTextReader(StringReader(source.read()))

        settings = XmlReaderSettings()
        settings.ProhibitDtd = True
        reader = XmlReader.Create(xtr, settings)

        document = Document()
        document.as_attribute = as_attribute or {}
        document.as_list = as_list or {}
        document.as_attribute_of_element = as_attribute_of_element or {}

        self._deserialize(reader, document)

        xtr.Close()
        reader.Close()

        return document
Esempio n. 3
0
 def reset(self):
     if self._root:
         self._root.forget()
         self._root = None
     if self._current_el:
         self._current_el.forget()
         self._current_el = None
     self._root = Document()
     self._current_el = self._root
     self._current_level = 0
Esempio n. 4
0
 def reset(self):
     if self._root:
         self._root.forget()
         self._root = None
     if self._current_el:
         self._current_el.forget()
         self._current_el = None
     self._root = Document()
     self._current_el = self._root
     self._current_level = 0
Esempio n. 5
0
    def deserialize(self, source, prefixes=None, strict=False):
        doc = None
        if isinstance(source, basestring):
            if os.path.exists(source):
                doc = xdm.parse(source)
            else:
                doc = xdm.parseString(source)
        elif hasattr(source, 'read'):
            doc = xdm.parse(source)

        document = Document()

        self.__deserialize_fragment(doc, document)

        if doc:
            try:
                doc.unlink()
            except KeyError:
                pass

        return document
Esempio n. 6
0
    def serialize(self,
                  document,
                  indent=False,
                  encoding=ENCODING,
                  prefixes=None,
                  omit_declaration=False):
        if not isinstance(document, Document):
            root = document
            document = Document()
            document.xml_children.append(root)

        self.__serialize_element(document)

        if not omit_declaration:
            self.buffer.insert(
                0, '<?xml version="1.0" encoding="%s"?>%s' %
                (encoding, os.linesep))

        content = ''.join(self.buffer)
        self.buffer = []
        if indent:
            return content.rstrip(os.linesep).encode(encoding)

        return content.encode(encoding)
Esempio n. 7
0
 def startDocument(self):
     self._root = Document()
     self._current_el = self._root
     self._current_level = 0
     self._as_cdata = False
Esempio n. 8
0
 def __init__(self, out, encoding=ENCODING):
     xss.XMLGenerator.__init__(self, out, encoding) 
     self._root = Document()
     self._current_el = self._root
     self._current_level = 0
     self._as_cdata = False
Esempio n. 9
0
class IncrementalHandler(xss.XMLGenerator):
    def __init__(self, out, encoding=ENCODING):
        xss.XMLGenerator.__init__(self, out, encoding) 
        self._root = Document()
        self._current_el = self._root
        self._current_level = 0
        self._as_cdata = False

    def reset(self):
        if self._root:
            self._root.forget()
            self._root = None
        if self._current_el:
            self._current_el.forget()
            self._current_el = None
        self._root = Document()
        self._current_el = self._root
        self._current_level = 0

    def startDocument(self):
        self._root = Document()
        self._current_el = self._root
        self._current_level = 0
        self._as_cdata = False

    # see http://www.xml.com/pub/a/2003/03/10/python.html
    def _split_qname(self, qname):
        qname_split = qname.split(':')
        if len(qname_split) == 2:
            prefix, local = qname_split
        else:
            prefix = None
            local = qname_split
        return prefix, local

    def processingInstruction(self, target, data):
        PI(target, data, self._current_el)

    def startElementNS(self, name, qname, attrs):
        #print "$%s%s: %f" % (" " * self._current_level, name, time())
        uri, local_name = name
        prefix = None
        if uri and uri in self._current_context:
            prefix = self._current_context[uri]
        #print "$$%s%s: %f" % (" " * self._current_level, name, time())
        e = Element(local_name, prefix=prefix, namespace=uri, parent=self._current_el)
        #print "$$$%s%s: %f" % (" " * self._current_level, name, time())
        
        for name, value in iter(attrs.items()):
            (namespace, local_name) = name
            qname = attrs.getQNameByName(name)
            prefix = self._split_qname(qname)[0]
            Attribute(local_name, value, prefix, namespace, e)
        #print "$$$$%s%s: %f" % (" " * self._current_level, name, time())
        
        self._current_el = e
        self._current_level = self._current_level + 1
        #print "$$$$$%s%s: %f" % (" " * self._current_level, name, time())
        
    def endElementNS(self, name, qname):
        self._current_level = current_level = self._current_level - 1
        self._current_el = self._current_el.xml_parent

    def characters(self, content):
        self._current_el.as_cdata = self._as_cdata
        if not self._as_cdata and not self._current_el.xml_text:
            self._current_el.xml_text = content
        else:
            self._current_el.xml_children.append(content)
        self._as_cdata = False

    def comment(self, data):
        Comment(data, self._current_el)
        
    def startCDATA(self):
        self._as_cdata = True

    def endCDATA(self):
        pass

    def startDTD(self, name, public_id, system_id):
        pass

    def endDTD(self):
        pass
    
    def doc(self):
        """Returns the root Document instance of the parsed
        document. You have to call the close() method of the
        parser first.
        """
        return self._root
Esempio n. 10
0
 def __init__(self):
     self._root = Document()
     self._current_el = self._root
     self._current_level = 0
     self._as_cdata = False
Esempio n. 11
0
class IncrementalHandler(object):
    def __init__(self):
        self._root = Document()
        self._current_el = self._root
        self._current_level = 0
        self._as_cdata = False

    def reset(self):
        if self._root:
            self._root.forget()
            self._root = None
        if self._current_el:
            self._current_el.forget()
            self._current_el = None
        self._root = Document()
        self._current_el = self._root
        self._current_level = 0

    def startDocument(self):
        self._root = Document()
        self._current_el = self._root
        self._current_level = 0
        self._as_cdata = False

    def processingInstruction(self, target, data):
        PI(target, data, self._current_el)

    def startElementNS(self, name, qname, attrs):
        prefix, uri, local_name = qname.getPrefix() or None, qname.getNamespaceURI() or None, qname.getLocalPart()
        e = Element(local_name, prefix=prefix, namespace=uri, parent=self._current_el)
        
        while attrs.hasNext():
            attr = attrs.next()
            qname = attr.getName()
            prefix, uri, local_name = qname.getPrefix(), qname.getNamespaceURI(), qname.getLocalPart()
            Attribute(local_name, attr.getValue(), prefix or None, uri or None, e)
        
        self._current_el = e
        self._current_level = self._current_level + 1
        
    def endElementNS(self, name, qname):
        self._current_level = current_level = self._current_level - 1
        self._current_el = self._current_el.xml_parent

    def characters(self, content):
        self._current_el.as_cdata = self._as_cdata
        if not self._as_cdata and not self._current_el.xml_text:
            self._current_el.xml_text = content
        else:
            self._current_el.xml_children.append(content)
        self._as_cdata = False

    def comment(self, data):
        Comment(data, self._current_el)
        
    def startCDATA(self):
        self._as_cdata = True

    def endCDATA(self):
        pass

    def startDTD(self, name, public_id, system_id):
        pass

    def endDTD(self):
        pass
    
    def doc(self):
        """Returns the root Document instance of the parsed
        document. You have to call the close() method of the
        parser first.
        """
        return self._root
Esempio n. 12
0
 def startDocument(self):
     self._root = Document()
     self._current_el = self._root
     self._current_level = 0
     self._as_cdata = False
Esempio n. 13
0
 def __init__(self, out, encoding=ENCODING):
     xss.XMLGenerator.__init__(self, out, encoding)
     self._root = Document()
     self._current_el = self._root
     self._current_level = 0
     self._as_cdata = False
Esempio n. 14
0
class IncrementalHandler(xss.XMLGenerator):
    def __init__(self, out, encoding=ENCODING):
        xss.XMLGenerator.__init__(self, out, encoding)
        self._root = Document()
        self._current_el = self._root
        self._current_level = 0
        self._as_cdata = False

    def reset(self):
        if self._root:
            self._root.forget()
            self._root = None
        if self._current_el:
            self._current_el.forget()
            self._current_el = None
        self._root = Document()
        self._current_el = self._root
        self._current_level = 0

    def startDocument(self):
        self._root = Document()
        self._current_el = self._root
        self._current_level = 0
        self._as_cdata = False

    # see http://www.xml.com/pub/a/2003/03/10/python.html
    def _split_qname(self, qname):
        qname_split = qname.split(':')
        if len(qname_split) == 2:
            prefix, local = qname_split
        else:
            prefix = None
            local = qname_split
        return prefix, local

    def processingInstruction(self, target, data):
        PI(target, data, self._current_el)

    def startElementNS(self, name, qname, attrs):
        #print "$%s%s: %f" % (" " * self._current_level, name, time())
        uri, local_name = name
        prefix = None
        if uri and uri in self._current_context:
            prefix = self._current_context[uri]
        #print "$$%s%s: %f" % (" " * self._current_level, name, time())
        e = Element(local_name,
                    prefix=prefix,
                    namespace=uri,
                    parent=self._current_el)
        #print "$$$%s%s: %f" % (" " * self._current_level, name, time())

        for name, value in iter(attrs.items()):
            (namespace, local_name) = name
            qname = attrs.getQNameByName(name)
            prefix = self._split_qname(qname)[0]
            Attribute(local_name, value, prefix, namespace, e)
        #print "$$$$%s%s: %f" % (" " * self._current_level, name, time())

        self._current_el = e
        self._current_level = self._current_level + 1
        #print "$$$$$%s%s: %f" % (" " * self._current_level, name, time())

    def endElementNS(self, name, qname):
        self._current_level = current_level = self._current_level - 1
        self._current_el = self._current_el.xml_parent

    def characters(self, content):
        self._current_el.as_cdata = self._as_cdata
        if not self._as_cdata and not self._current_el.xml_text:
            self._current_el.xml_text = content
        else:
            self._current_el.xml_children.append(content)
        self._as_cdata = False

    def comment(self, data):
        Comment(data, self._current_el)

    def startCDATA(self):
        self._as_cdata = True

    def endCDATA(self):
        pass

    def startDTD(self, name, public_id, system_id):
        pass

    def endDTD(self):
        pass

    def doc(self):
        """Returns the root Document instance of the parsed
        document. You have to call the close() method of the
        parser first.
        """
        return self._root
Esempio n. 15
0
 def __init__(self):
     self._root = Document()
     self._current_el = self._root
     self._current_level = 0
     self._as_cdata = False
Esempio n. 16
0
class IncrementalHandler(object):
    def __init__(self):
        self._root = Document()
        self._current_el = self._root
        self._current_level = 0
        self._as_cdata = False

    def reset(self):
        if self._root:
            self._root.forget()
            self._root = None
        if self._current_el:
            self._current_el.forget()
            self._current_el = None
        self._root = Document()
        self._current_el = self._root
        self._current_level = 0

    def startDocument(self):
        self._root = Document()
        self._current_el = self._root
        self._current_level = 0
        self._as_cdata = False

    def processingInstruction(self, target, data):
        PI(target, data, self._current_el)

    def startElementNS(self, name, qname, attrs):
        prefix, uri, local_name = qname.getPrefix(
        ) or None, qname.getNamespaceURI() or None, qname.getLocalPart()
        e = Element(local_name,
                    prefix=prefix,
                    namespace=uri,
                    parent=self._current_el)

        while attrs.hasNext():
            attr = attrs.next()
            qname = attr.getName()
            prefix, uri, local_name = qname.getPrefix(), qname.getNamespaceURI(
            ), qname.getLocalPart()
            Attribute(local_name, attr.getValue(), prefix or None, uri or None,
                      e)

        self._current_el = e
        self._current_level = self._current_level + 1

    def endElementNS(self, name, qname):
        self._current_level = current_level = self._current_level - 1
        self._current_el = self._current_el.xml_parent

    def characters(self, content):
        self._current_el.as_cdata = self._as_cdata
        if not self._as_cdata and not self._current_el.xml_text:
            self._current_el.xml_text = content
        else:
            self._current_el.xml_children.append(content)
        self._as_cdata = False

    def comment(self, data):
        Comment(data, self._current_el)

    def startCDATA(self):
        self._as_cdata = True

    def endCDATA(self):
        pass

    def startDTD(self, name, public_id, system_id):
        pass

    def endDTD(self):
        pass

    def doc(self):
        """Returns the root Document instance of the parsed
        document. You have to call the close() method of the
        parser first.
        """
        return self._root