Package translate :: Package storage :: Module xml_name

[hide private]
[frames] | no frames]

# Source Code for Module translate.storage.xml_name

     1  #!/usr/bin/env python
     2  # -*- coding: utf-8 -*-
     3  #
     4  # Copyright 2008 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
    23 -class XmlNamespace(object):
    24 -    def __init__(self, namespace):
    25          self._namespace = namespace
    26
    27 -    def name(self, tag):
    28          return "{%s}%s" % (self._namespace, tag)
    29
    30 -class XmlNamer(object):
    31      """Initialize me with a DOM node or a DOM document node (the
    32      toplevel node you get when parsing an XML file). Then use me
    33      to generate fully qualified XML names.
    34
    35      >>> xml = '<office:document-styles xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"></office>'
    36      >>> from lxml import etree
    37      >>> namer = XmlNamer(etree.fromstring(xml))
    38      >>> namer.name('office', 'blah')
    39      {urn:oasis:names:tc:opendocument:xmlns:office:1.0}blah
    40      >>> namer.name('office:blah')
    41      {urn:oasis:names:tc:opendocument:xmlns:office:1.0}blah
    42
    43      I can also give you XmlNamespace objects if you give me the abbreviated
    44      namespace name. These are useful if you need to reference a namespace
    45      continuously.
    46
    47      >>> office_ns = name.namespace('office')
    48      >>> office_ns.name('foo')
    49      {urn:oasis:names:tc:opendocument:xmlns:office:1.0}foo
    50      """
    51
    52 -    def __init__(self, dom_node):
    53          # Allow the user to pass a dom node of the
    54          # XML document nodle
    55          if hasattr(dom_node, 'nsmap'):
    56              self.nsmap = dom_node.nsmap
    57          else:
    58              self.nsmap = dom_node.getroot().nsmap
    59
    60 -    def name(self, namespace_shortcut, tag=None):
    61          # If the user doesn't pass an argument into 'tag'
    62          # then namespace_shortcut contains a tag of the form
    63          # 'short-namespace:tag'
    64          if tag is None:
    65              namespace_shortcut, tag = namespace_shortcut.split(':')
    66          return "{%s}%s" % (self.nsmap[namespace_shortcut], tag)
    67
    68 -    def namespace(self, namespace_shortcut):
    69          return XmlNamespace(self.nsmap[namespace_shortcut])
    70