/usr/share/pyshared/translate/misc/ourdom.py is in translate-toolkit 1.10.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2004-2007 Zuza Software Foundation
#
# This file is part of translate.
#
# translate is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# translate is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
#
"""module that provides modified DOM functionality for our needs
Note that users of ourdom should ensure that no code might still use classes
directly from minidom, like minidom.Element, minidom.Document or methods such
as minidom.parseString, since the functionality provided here will not be in
those objects.
"""
from xml.dom import minidom
from xml.dom import expatbuilder
# helper functions we use to do xml the way we want, used by modified
# classes below
def writexml_helper(self, writer, indent="", addindent="", newl=""):
"""A replacement for writexml that formats it like typical XML files.
Nodes are intendented but text nodes, where whitespace can be
significant, are not indented."""
# indent = current indentation
# addindent = indentation to add to higher levels
# newl = newline string
writer.write(indent + "<" + self.tagName)
attrs = self._get_attributes()
a_names = attrs.keys()
a_names.sort()
for a_name in a_names:
writer.write(" %s=\"" % a_name)
minidom._write_data(writer, attrs[a_name].value)
writer.write("\"")
if self.childNodes:
# We need to write text nodes without newline and indentation, so
# we handle them differently. Note that we here assume that "empty"
# text nodes can be done away with (see the strip()). Note also that
# nested tags in a text node (like ph tags in xliff) should also not
# have newlines and indentation or an extra newline, since that will
# alter the text node.
haveText = False
for childNode in self.childNodes:
if childNode.nodeType == self.TEXT_NODE and childNode.data.strip():
haveText = True
break
if haveText:
writer.write(">")
for node in self.childNodes:
node.writexml(writer, "", "", "")
writer.write("</%s>%s" % (self.tagName, newl))
else:
# This is the normal case that we do with pretty layout
writer.write(">%s" % (newl))
for node in self.childNodes:
if node.nodeType != self.TEXT_NODE:
node.writexml(writer, (indent + addindent), addindent, newl)
writer.write("%s</%s>%s" % (indent, self.tagName, newl))
else:
writer.write("/>%s" % (newl))
def getElementsByTagName_helper(parent, name, dummy=None):
"""A reimplementation of getElementsByTagName as an iterator.
Note that this is not compatible with getElementsByTagName that returns a
list, therefore, the class below exposes this through yieldElementsByTagName"""
for node in parent.childNodes:
if (node.nodeType == minidom.Node.ELEMENT_NODE and
(name == "*" or node.tagName == name)):
yield node
if node.hasChildNodes():
for othernode in node.getElementsByTagName(name):
yield othernode
def searchElementsByTagName_helper(parent, name, onlysearch):
"""limits the search to within tags occuring in onlysearch"""
for node in parent.childNodes:
if (node.nodeType == minidom.Node.ELEMENT_NODE and
(name == "*" or node.tagName == name)):
yield node
if (node.nodeType == minidom.Node.ELEMENT_NODE and
node.tagName in onlysearch):
for node in node.searchElementsByTagName(name, onlysearch):
yield node
def getFirstElementByTagName(node, name):
results = node.yieldElementsByTagName(name)
# if isinstance(results, list):
# if len(results) == 0:
# return None
# else:
# return results[0]
try:
result = results.next()
return result
except StopIteration:
return None
def getnodetext(node):
"""returns the node's text by iterating through the child nodes"""
if node is None:
return ""
return "".join([t.data for t in node.childNodes if t.nodeType == t.TEXT_NODE])
# various modifications to minidom classes to add functionality we like
class DOMImplementation(minidom.DOMImplementation):
def _create_document(self):
return Document()
class Element(minidom.Element):
def yieldElementsByTagName(self, name):
return getElementsByTagName_helper(self, name)
def searchElementsByTagName(self, name, onlysearch):
return searchElementsByTagName_helper(self, name, onlysearch)
def writexml(self, writer, indent, addindent, newl):
return writexml_helper(self, writer, indent, addindent, newl)
class Document(minidom.Document):
implementation = DOMImplementation()
def yieldElementsByTagName(self, name):
return getElementsByTagName_helper(self, name)
def searchElementsByTagName(self, name, onlysearch):
return searchElementsByTagName_helper(self, name, onlysearch)
def createElement(self, tagName):
e = Element(tagName)
e.ownerDocument = self
return e
def createElementNS(self, namespaceURI, qualifiedName):
prefix, localName = _nssplit(qualifiedName)
e = Element(qualifiedName, namespaceURI, prefix)
e.ownerDocument = self
return e
theDOMImplementation = DOMImplementation()
# an ExpatBuilder that allows us to use the above modifications
class ExpatBuilderNS(expatbuilder.ExpatBuilderNS):
def reset(self):
"""Free all data structures used during DOM construction."""
self.document = theDOMImplementation.createDocument(
expatbuilder.EMPTY_NAMESPACE, None, None)
self.curNode = self.document
self._elem_info = self.document._elem_info
self._cdata = False
self._initNamespaces()
def start_element_handler(self, name, attributes):
# All we want to do is construct our own Element instead of
# minidom.Element, unfortunately the only way to do this is to
# copy this whole function from expatbuilder.py
if ' ' in name:
uri, localname, prefix, qname = expatbuilder._parse_ns_name(self, name)
else:
uri = expatbuilder.EMPTY_NAMESPACE
qname = name
localname = None
prefix = expatbuilder.EMPTY_PREFIX
node = Element(qname, uri, prefix, localname)
node.ownerDocument = self.document
expatbuilder._append_child(self.curNode, node)
self.curNode = node
if self._ns_ordered_prefixes:
for prefix, uri in self._ns_ordered_prefixes:
if prefix:
a = minidom.Attr(expatbuilder._intern(self,
'xmlns:' + prefix),
expatbuilder.XMLNS_NAMESPACE, prefix, "xmlns")
else:
a = minidom.Attr("xmlns", expatbuilder.XMLNS_NAMESPACE,
"xmlns", expatbuilder.EMPTY_PREFIX)
d = a.childNodes[0].__dict__
d['data'] = d['nodeValue'] = uri
d = a.__dict__
d['value'] = d['nodeValue'] = uri
d['ownerDocument'] = self.document
expatbuilder._set_attribute_node(node, a)
del self._ns_ordered_prefixes[:]
if attributes:
_attrs = node._attrs
_attrsNS = node._attrsNS
for i in range(0, len(attributes), 2):
aname = attributes[i]
value = attributes[i+1]
if ' ' in aname:
uri, localname, prefix, qname = expatbuilder._parse_ns_name(self, aname)
a = minidom.Attr(qname, uri, localname, prefix)
_attrs[qname] = a
_attrsNS[(uri, localname)] = a
else:
a = minidom.Attr(aname, expatbuilder.EMPTY_NAMESPACE,
aname, expatbuilder.EMPTY_PREFIX)
_attrs[aname] = a
_attrsNS[(expatbuilder.EMPTY_NAMESPACE, aname)] = a
d = a.childNodes[0].__dict__
d['data'] = d['nodeValue'] = value
d = a.__dict__
d['ownerDocument'] = self.document
d['value'] = d['nodeValue'] = value
d['ownerElement'] = node
if __debug__:
# This only adds some asserts to the original
# end_element_handler(), so we only define this when -O is not
# used. If changing one, be sure to check the other to see if
# it needs to be changed as well.
def end_element_handler(self, name):
curNode = self.curNode
if ' ' in name:
uri, localname, prefix, qname = expatbuilder._parse_ns_name(self, name)
assert (curNode.namespaceURI == uri
and curNode.localName == localname
and curNode.prefix == prefix), \
"element stack messed up! (namespace)"
else:
assert curNode.nodeName == name, \
"element stack messed up - bad nodeName"
assert curNode.namespaceURI == expatbuilder.EMPTY_NAMESPACE, \
"element stack messed up - bad namespaceURI"
self.curNode = curNode.parentNode
self._finish_end_element(curNode)
# parser methods that use our modified xml classes
def parse(file, parser=None, bufsize=None):
"""Parse a file into a DOM by filename or file object."""
builder = ExpatBuilderNS()
if isinstance(file, basestring):
fp = open(file, 'rb')
try:
result = builder.parseFile(fp)
finally:
fp.close()
else:
result = builder.parseFile(file)
return result
def parseString(string, parser=None):
"""Parse a file into a DOM from a string."""
builder = ExpatBuilderNS()
return builder.parseString(string)
|