def __init__(self, raw_buff): self.axml = AXMLParser(raw_buff) self.xmlns = False self.buff = "" while 1: _type = self.axml.next() #print "tagtype = ", _type if _type == tc.START_DOCUMENT: self.buff += "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" elif _type == tc.START_TAG: self.buff += "\n<%s%s " % (self.getPrefix(self.axml.getPrefix()), self.axml.getName()) # FIXME: use namespace if self.xmlns == False: self.buff += "xmlns:%s=\"%s\" " % (self.axml.getNamespacePrefix(0), self.axml.getNamespaceUri(0)) self.xmlns = True for i in range(0, self.axml.getAttributeCount()): self.buff += " %s%s=\"%s\" " % (self.getPrefix(self.axml.getAttributePrefix(i)), self.axml.getAttributeName(i), self.getAttributeValue(i)) self.buff += ">" elif _type == tc.END_TAG: self.buff += "</%s%s>\n" % (self.getPrefix(self.axml.getPrefix()), self.axml.getName()) elif _type == tc.TEXT: self.buff += " %s " % self.axml.getText() elif _type == tc.END_DOCUMENT: break
def __init__(self, raw_buff, debug=False): self.log = logging.getLogger("pyaxmlparser.axmlprinter") self.log.setLevel(logging.DEBUG if debug else logging.CRITICAL) self.char_range = None self.replacement = None self.axml = AXMLParser(raw_buff) self.root = None self.packer_warning = False cur = [] while self.axml.is_valid: _type = next(self.axml) if _type == const.START_TAG: name = self._fix_name(self.axml.name) uri = self.get_namespace(self.axml.namespace) tag = "{}{}".format(uri, name) comment = self.axml.comment if comment: if self.root is None: self.log.warning( "Can not attach comment with content '{}' without root!" .format(comment)) else: cur[-1].append(etree.Comment(comment)) self.log.debug("START_TAG: {} (line={})".format( tag, self.axml.m_lineNumber)) elem = etree.Element(tag, nsmap=self.axml.nsmap) for i in range(self.axml.get_attribute_count()): uri = self.get_namespace( self.axml.get_attribute_namespace(i)) name = self._fix_name(self.axml.get_attribute_name(i)) value = self._fix_value(self.get_attribute_value(i)) self.log.debug("found an attribute: {}{}='{}'".format( uri, name, value.encode("utf-8"))) if "{}{}".format(uri, name) in elem.attrib: self.log.warning( "Duplicate attribute '{}{}'! Will overwrite!". format(uri, name)) elem.set("{}{}".format(uri, name), value) if self.root is None: self.root = elem else: if not cur: # looks like we lost the root? self.log.error( "No more elements available to attach to! Is the XML malformed?" ) break cur[-1].append(elem) cur.append(elem) if _type == const.END_TAG: if not cur: self.log.warning( "Too many END_TAG! No more elements available to attach to!" ) name = self.axml.name uri = self.get_namespace(self.axml.namespace) tag = "{}{}".format(uri, name) if cur[-1].tag != tag: self.log.warning( "Closing tag '{}' does not match current stack! " "At line number: {}. Is the XML malformed?".format( self.axml.name, self.axml.m_lineNumber)) cur.pop() if _type == const.TEXT: self.log.debug("TEXT for {}".format(cur[-1])) cur[-1].text = self.axml.text if _type == const.END_DOCUMENT: # Check if all namespace mappings are closed if len(self.axml.namespaces) > 0: self.log.warning( "Not all namespace mappings were closed! Malformed AXML?" ) break