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 -def makefixtagproc(namespacemap):
     34      """this constructs an alternative fixtag procedure that will use appropriate names for namespaces..."""
     35      def fixtag(tag, namespaces):
     36          """given a decorated tag (of the form {uri}tag), return prefixed tag and namespace declaration, if any"""
     37          if isinstance(tag, ElementTree.QName):
     38              tag = tag.text
     39          namespace_uri, tag = tag[1:].split("}", 1)
     40          prefix = namespaces.get(namespace_uri)
     41          if prefix is None:
     42              if namespace_uri in namespacemap:
     43                  prefix = namespacemap[namespace_uri]
     44              else:
     45                  prefix = "ns%d" % len(namespaces)
     46              namespaces[namespace_uri] = prefix
     47              xmlns = ("xmlns:%s" % prefix, namespace_uri)
     48          else:
     49              xmlns = None
     50          return "%s:%s" % (prefix, tag), xmlns
     51      return fixtag
     52
     53 -def splitnamespace(fulltag):
     54      if '{' in fulltag:
     55          namespace = fulltag[fulltag.find('{'):fulltag.find('}')+1]
     56      else:
     57          namespace = ""
     58      tag = fulltag.replace(namespace, "", 1)
     59      return namespace, tag
     60
     61 -class XMLWrapper:
     62      """simple wrapper for xml objects"""
     63 -    def __init__(self, obj):
     64          """construct object from the elementtree item"""
     65          self.obj = obj
     66          self.namespace, self.tag = splitnamespace(self.obj.tag)
     67          self.attrib = {}
     68          for fullkey, value in self.obj.attrib.iteritems():
     69              namespace, key = splitnamespace(fullkey)
     70              self.attrib[key] = value
     71 -    def getchild(self, searchtag, tagclass=None):
     72          """get a child with the given tag name"""
     73          if tagclass is None:
     74              tagclass = XMLWrapper
     75          for childobj in self.obj.getiterator():
     76              # getiterator() includes self...
     77              if childobj == self.obj:
     78                  continue
     79              childns, childtag = splitnamespace(childobj.tag)
     80              if childtag == searchtag:
     81                  child = tagclass(childobj)
     82                  return child
     83          raise KeyError("could not find child with tag %r" % searchtag)
     84 -    def getchildren(self, searchtag, tagclass=None, excludetags=[]):
     85          """get all children with the given tag name"""
     86          if tagclass is None:
     87              tagclass = XMLWrapper
     88          childobjects = []
     89          for childobj in self.obj.getiterator():
     90              # getiterator() includes self...
     91              if childobj == self.obj:
     92                  continue
     93              childns, childtag = splitnamespace(childobj.tag)
     94              if childtag == searchtag:
     95                  childobjects.append(childobj)
     96          children = [tagclass(childobj) for childobj in childobjects]
     97          return children
     98 -    def gettext(self, searchtag):
     99          """get some contained text"""
    100          return self.getchild(searchtag).obj.text
    101 -    def getxml(self, encoding=None):
    102          return ElementTree.tostring(self.obj, encoding)
    103 -    def getplaintext(self, excludetags=[]):
    104          text = ""
    105          if self.obj.text != None:
    106              text += self.obj.text
    107          for child in self.obj._children:
    108              simplechild = XMLWrapper(child)
    109              if simplechild.tag not in excludetags:
    110                  text += simplechild.getplaintext(excludetags)
    111          if self.obj.tail != None:
    112              text += self.obj.tail
    113          return text
    114 -    def getvalues(self, searchtag):
    115          """get some contained values..."""
    116          values = [child.obj.text for child in self.getchildren(searchtag)]
    117          return values
    118 -    def __repr__(self):
    119          """return a representation of the object"""
    120          return self.tag+':'+repr(self.__dict__)
    121 -    def getattr(self, attrname):
    122          """gets an attribute of the tag"""
    123          return self.attrib[attrname]
    124 -    def write(self, file, encoding="UTF-8"):
    125          """writes the object as XML to a file..."""
    126          e = ElementTree.ElementTree(self.obj)
    127          e.write(file, encoding)
    128
    129 -def BuildTree(xmlstring):
    130      parser = ElementTree.XMLTreeBuilder()
    131      parser.feed(xmlstring)
    132      return parser.close()
    133
    134 -def MakeElement(tag, attrib={}, **extraargs):
    135      return ElementTree.Element(tag, attrib, **extraargs)
    136