translate.misc.xmlwrapper

Package translate :: Package misc :: Module xmlwrapper |
[hide private]
[frames] | no frames]

# Source Code for Module translate.misc.xmlwrapper

      1  #!/usr/bin/env python
      2  # -*- coding: utf-8 -*-
      3  #
      4  # Copyright 2004, 2005 Zuza Software Foundation
      5  #
      6  # This file is part of translate.
      7  #
      8  # translate is free software; you can redistribute it and/or modify
      9  # it under the terms of the GNU General Public License as published by
     10  # the Free Software Foundation; either version 2 of the License, or
     11  # (at your option) any later version.
     12  #
     13  # translate is distributed in the hope that it will be useful,
     14  # but WITHOUT ANY WARRANTY; without even the implied warranty of
     15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16  # GNU General Public License for more details.
     17  #
     18  # You should have received a copy of the GNU General Public License
     19  # along with translate; if not, write to the Free Software
     20  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
     21
     22  """simpler wrapper to the elementtree XML parser"""
     23
     24  try:
     25      from xml.etree import ElementTree
     26  except ImportError:
     27      from elementtree import ElementTree
     28  # this is needed to prevent expat-version conflicts with wx >= 2.5.2.2
     29  from xml.parsers import expat
     30
     31  basicfixtag = ElementTree.fixtag
     32
     33
     34 -def makefixtagproc(namespacemap):
     35      """this constructs an alternative fixtag procedure that will use appropriate names for namespaces..."""
     36
     37      def fixtag(tag, namespaces):
     38          """given a decorated tag (of the form {uri}tag), return prefixed tag and namespace declaration, if any"""
     39          if isinstance(tag, ElementTree.QName):
     40              tag = tag.text
     41          namespace_uri, tag = tag[1:].split("}", 1)
     42          prefix = namespaces.get(namespace_uri)
     43          if prefix is None:
     44              if namespace_uri in namespacemap:
     45                  prefix = namespacemap[namespace_uri]
     46              else:
     47                  prefix = "ns%d" % len(namespaces)
     48              namespaces[namespace_uri] = prefix
     49              xmlns = ("xmlns:%s" % prefix, namespace_uri)
     50          else:
     51              xmlns = None
     52          return "%s:%s" % (prefix, tag), xmlns
     53      return fixtag
     54
     55
     56 -def splitnamespace(fulltag):
     57      if '{' in fulltag:
     58          namespace = fulltag[fulltag.find('{'):fulltag.find('}')+1]
     59      else:
     60          namespace = ""
     61      tag = fulltag.replace(namespace, "", 1)
     62      return namespace, tag
     63
     64
     65 -class XMLWrapper:
     66      """simple wrapper for xml objects"""
     67
     68 -    def __init__(self, obj):
     69          """construct object from the elementtree item"""
     70          self.obj = obj
     71          self.namespace, self.tag = splitnamespace(self.obj.tag)
     72          self.attrib = {}
     73          for fullkey, value in self.obj.attrib.iteritems():
     74              namespace, key = splitnamespace(fullkey)
     75              self.attrib[key] = value
     76
     77 -    def getchild(self, searchtag, tagclass=None):
     78          """get a child with the given tag name"""
     79          if tagclass is None:
     80              tagclass = XMLWrapper
     81          for childobj in self.obj.getiterator():
     82              # getiterator() includes self...
     83              if childobj == self.obj:
     84                  continue
     85              childns, childtag = splitnamespace(childobj.tag)
     86              if childtag == searchtag:
     87                  child = tagclass(childobj)
     88                  return child
     89          raise KeyError("could not find child with tag %r" % searchtag)
     90
     91 -    def getchildren(self, searchtag, tagclass=None, excludetags=[]):
     92          """get all children with the given tag name"""
     93          if tagclass is None:
     94              tagclass = XMLWrapper
     95          childobjects = []
     96          for childobj in self.obj.getiterator():
     97              # getiterator() includes self...
     98              if childobj == self.obj:
     99                  continue
    100              childns, childtag = splitnamespace(childobj.tag)
    101              if childtag == searchtag:
    102                  childobjects.append(childobj)
    103          children = [tagclass(childobj) for childobj in childobjects]
    104          return children
    105
    106 -    def gettext(self, searchtag):
    107          """get some contained text"""
    108          return self.getchild(searchtag).obj.text
    109
    110 -    def getxml(self, encoding=None):
    111          return ElementTree.tostring(self.obj, encoding)
    112
    113 -    def getplaintext(self, excludetags=[]):
    114          text = ""
    115          if self.obj.text != None:
    116              text += self.obj.text
    117          for child in self.obj._children:
    118              simplechild = XMLWrapper(child)
    119              if simplechild.tag not in excludetags:
    120                  text += simplechild.getplaintext(excludetags)
    121          if self.obj.tail != None:
    122              text += self.obj.tail
    123          return text
    124
    125 -    def getvalues(self, searchtag):
    126          """get some contained values..."""
    127          values = [child.obj.text for child in self.getchildren(searchtag)]
    128          return values
    129
    130 -    def __repr__(self):
    131          """return a representation of the object"""
    132          return self.tag+':'+repr(self.__dict__)
    133
    134 -    def getattr(self, attrname):
    135          """gets an attribute of the tag"""
    136          return self.attrib[attrname]
    137
    138 -    def write(self, file, encoding="UTF-8"):
    139          """writes the object as XML to a file..."""
    140          e = ElementTree.ElementTree(self.obj)
    141          e.write(file, encoding)
    142
    143
    144 -def BuildTree(xmlstring):
    145      parser = ElementTree.XMLTreeBuilder()
    146      parser.feed(xmlstring)
    147      return parser.close()
    148
    149
    150 -def MakeElement(tag, attrib={}, **extraargs):
    151      return ElementTree.Element(tag, attrib, **extraargs)
    152