translate.storage.xml_name

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