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
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
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