/usr/lib/gdesklets/utils/DOM.py is in gdesklets 0.36.1-7.
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 | """
Class for a simple mini DOM.
This class takes an SVG string at construction time and then provides a way to
modify element properties in the DOM tree.
The DOM object can be serialized to a XML string at any time.
"""
from xml import sax
class DOM(sax.handler.ContentHandler):
def __init__(self, xml):
self.__dom = None
self.__node_stack = []
self.__current_node = None
self.__current_chars = ""
self.__id_table = {}
sax.handler.ContentHandler.__init__(self)
# silly PyXML fails for standalone="no" documents without net access,
# so rip that out
xml = self.__modify_standalone(xml)
try:
sax.parseString(xml, self)
except sax._exceptions.SAXParseException, exc:
raise UserError(_("XML parse error"),
_("An invalid graphics code was loaded into a "
"canvas."),
code = xml, lineno = exc.getLineNumber())
except:
return
self.__dom.set_id_table(self.__id_table)
#
# Rips out a standalone="yes" attribute if it's set. We don't need and don't
# want it for SVGs.
#
def __modify_standalone(self, xml):
index1 = xml.find("<")
if (xml[index1 + 1] != "?"):
# no header; nothing to do
return xml
index2 = xml.find("?>")
header = xml[index1:index2]
index3 = header.find("standalone")
if (index3 == -1):
# no "standalone setting; add one
return xml[:index1] + " standalone='yes'" + xml[index2:]
state = 0
index4 = index3 + 10
for c in header[index4:]:
index4 += 1
if (c in ("\"", "'")): state += 1
if (state == 2): break
if ("no" in header.lower()[index3:index4]):
# replace with "yes"
xml = xml[:index1 + index3] + \
xml[index1 + index3:index1 + index4].lower().replace("no", "yes") + \
xml[index1 + index4:]
return xml
def get_root(self): return self.__dom
def startElement(self, name, attrs):
parent = self.__current_node
self.__current_node = _DOMNode(name)
self.__current_chars = ""
for key, value in attrs.items():
self.__current_node[key] = value
if (parent): parent.add_child(self.__current_node)
else:
# we insert an empty <g> node here; it can be used later if needed
# TODO: not here; this is meant to be a generic DOM
self.__dom = self.__current_node
new_node = _DOMNode("g")
self.__dom.add_child(new_node)
self.__current_node = new_node
if ("id" in attrs.keys()):
self.__id_table[attrs["id"]] = self.__current_node
self.__node_stack.append(self.__current_node)
def endElement(self, name):
if (self.__current_chars):
self.__current_node["PCDATA"] = self.__current_chars
self.__node_stack.pop(-1)
if (self.__node_stack):
self.__current_node = self.__node_stack[-1]
def characters(self, content):
self.__current_chars += content
#
# Class for nodes of the DOM tree.
#
class _DOMNode:
def __init__(self, name):
self.__name = name
self.__attrs = {}
self.__children = []
self.__update_handler = None
self.__id_table = {}
#
# Sets the ID table for quickly accessing a node by its ID.
#
def set_id_table(self, table): self.__id_table = table
#
# Returns whether the given key exists.
#
def has_key(self, key): return (key in self.__attrs)
#
# Returns the node with the given ID.
#
def get(self, ident):
try:
return self.__id_table[ident]
except KeyError:
raise UserError(_("No such element: %s") % ident,
_("The element with ID <b>%s</b> does not "
"exist in the SVG image.") % ident)
#
# Sets the handler for updating the tree.
#
def set_update_handler(self, handler):
self.__update_handler = handler
#
# Updates the tree.
#
def update(self):
if (self.__update_handler): self.__update_handler()
def add_child(self, child): self.__children.append(child)
def get_children(self): return self.__children[:]
def __getitem__(self, key):
try:
return self.__attrs[key]
except KeyError:
raise UserError(_("No such property: %s") % key,
_("The SVG element <b>%s</b> does not have the "
"<b>%s</b> property.") % (self.__name, key))
def __setitem__(self, key, value): self.__attrs[key] = value
#
# Returns a XML representation of this node.
#
def __str__(self):
attrs = [ "%s=\"%s\"" % (k, v) for k, v in self.__attrs.items() ]
attrs = " ".join(attrs)
children = [ str(c) for c in self.get_children() ]
children = "\n".join(children)
contents = self.__attrs.get("PCDATA", "")
contents += children
if (contents):
out = "<%s %s>%s</%s>" % (self.__name, attrs, contents, self.__name)
else:
out = "<%s %s/>" % (self.__name, attrs)
return out
|