Removed all the MD5 suck checking stuff.
[dom-editor.git] / Dome / Namespaces.py
blob51763c3a669be02504e39b6cb0eccf4e4e3982df
1 from xml.dom import XMLNS_NAMESPACE, XML_NAMESPACE
2 from constants import DOME_NS
4 import rox
5 from rox import g
7 fixed_ns = {'xml': XML_NAMESPACE, 'xmlns': XMLNS_NAMESPACE,
8 'dome': DOME_NS}
10 common_ns = {'http://www.w3.org/1999/xhtml': 'xhtml',
11 'http://www.w3.org/1999/02/22-rdf-syntax-ns#': 'rdf',
12 'http://xmlns.4suite.org/ext': 'ft',
13 'http://www.w3.org/1999/XMLSchema': 'xsd',
14 'http://www.w3.org/1999/XMLSchema-instance': 'xsi',
15 'http://schemas.xmlsoap.org/soap/envelope/': 'env'}
17 class Namespaces(g.GenericTreeModel):
18 def __init__(self):
19 g.GenericTreeModel.__init__(self)
20 self.uri = dict(fixed_ns)
21 self.update_list()
23 def update_list(self):
24 self.list = self.uri.keys()
25 self.list.sort()
26 pre = self.prefix = {}
27 for p, u in self.uri.iteritems():
28 pre[u] = p
30 def on_get_n_columns(self):
31 return 2
33 def on_get_iter(self, path):
34 assert len(path) == 1
35 return path[0]
37 def on_get_value(self, iter, column):
38 if column == 0:
39 return self.list[iter]
40 return self.uri[self.list[iter]]
42 def on_iter_nth_child(self, iter, n):
43 if iter == None:
44 return n
45 return None
47 def on_get_column_type(self, col):
48 return str
50 def on_iter_has_child(self, iter):
51 return False
53 def on_iter_next(self, iter):
54 if iter < len(self.list) - 1:
55 return iter + 1
57 def __setitem__(self, prefix, uri):
58 if prefix in self.uri and self.uri[prefix] == uri:
59 return
61 assert prefix
62 if prefix in fixed_ns:
63 raise Exception('That namespace prefix cannot be changed')
64 if uri in self.prefix:
65 raise Exception('That namespace already has a prefix (%s)' % self.prefix[uri])
67 modifed = prefix in self.uri
69 self.uri[prefix] = uri
70 self.update_list()
71 path = (self.list.index(prefix),)
73 if modifed:
74 self.emit('row-changed', path, self.get_iter(path))
75 else:
76 self.emit('row-inserted', path, self.get_iter(path))
78 def __delitem__(self, iter):
79 prefix = self[iter][0]
80 if prefix in fixed_ns:
81 raise Exception('This is a built-in namespace and cannot be deleted')
82 path = (self.list.index(prefix),)
83 del self.uri[prefix]
84 self.update_list()
85 self.emit('row-deleted', path)
87 def ensure_ns(self, suggested_prefix, uri):
88 """Return the prefix for this URI. If none is set choose one (using suggested_prefix
89 if possible)."""
90 try:
91 return self.prefix[uri]
92 except KeyError:
93 if not suggested_prefix:
94 suggested_prefix = common_ns.get(uri, 'ns')
95 if suggested_prefix in self.uri:
96 x = 1
97 while suggested_prefix + `x` in self.uri: x += 1
98 suggested_prefix += `x`
99 self[suggested_prefix] = uri
100 #print "Added", suggested_prefix, uri
101 return suggested_prefix
103 def to_xml(self, doc):
104 node = doc.createElementNS(DOME_NS, 'dome:namespaces')
105 for prefix, uri in self.uri.iteritems():
106 if prefix in fixed_ns: continue
107 ns = doc.createElementNS(DOME_NS, 'dome:ns')
108 ns.setAttributeNS(None, 'prefix', prefix)
109 ns.setAttributeNS(None, 'uri', uri)
110 node.appendChild(ns)
111 return node
113 class GUI(rox.Dialog):
114 def __init__(self, model):
115 rox.Dialog.__init__(self)
116 self.model = model
118 self.add_button(g.STOCK_ADD, 1)
119 self.add_button(g.STOCK_DELETE, 2)
120 self.add_button(g.STOCK_CLOSE, g.RESPONSE_OK)
122 tree = g.TreeView(model.namespaces)
124 def response(dialog, resp):
125 if resp == 1:
126 dialog.add_new(model.namespaces)
127 elif resp == 2:
128 dialog.delete_selected(tree.get_selection())
129 else:
130 dialog.destroy()
131 self.connect('response', response)
132 self.set_position(g.WIN_POS_MOUSE)
133 self.set_title('Namespaces for ' + `model.uri`)
135 frame = g.Frame()
136 frame.add(tree)
137 frame.set_shadow_type(g.SHADOW_IN)
139 cell = g.CellRendererText()
140 column = g.TreeViewColumn('Prefix', cell, text = 0)
141 tree.append_column(column)
143 column = g.TreeViewColumn('URI', cell, text = 1)
144 tree.append_column(column)
146 frame.show_all()
148 self.vbox.pack_start(frame, True, True)
149 self.set_default_size(400, 200)
151 self.set_has_separator(False)
153 def delete_selected(self, sel):
154 model, iter = sel.get_selected()
155 if not iter:
156 rox.alert('Select a namespace binding to delete')
157 return
158 try:
159 del model[iter]
160 except:
161 rox.report_exception()
163 def add_new(self, ns):
164 d = rox.Dialog()
166 hbox = g.HBox(False, 2)
167 d.vbox.pack_start(hbox, False, True)
168 hbox.pack_start(g.Label('Prefix:'), False, True, 0)
169 prefix = g.Entry()
170 hbox.pack_start(prefix, True, True, 0)
172 def check_prefix(entry):
173 if not uri.get_text():
174 pre = prefix.get_text()
175 for u, p in common_ns.iteritems():
176 if p == pre:
177 uri.set_text(u)
178 break
179 uri.grab_focus()
181 prefix.connect('activate', check_prefix)
183 hbox = g.HBox(False, 2)
184 d.vbox.pack_start(hbox, False, True)
185 hbox.pack_start(g.Label('Namespace URI:'), False, True, 0)
186 uri = g.Entry()
187 hbox.pack_start(uri, True, True, 0)
188 uri.set_activates_default(True)
190 d.vbox.show_all()
191 d.set_position(g.WIN_POS_MOUSE)
193 prefix.grab_focus()
195 d.add_button(g.STOCK_CANCEL, g.RESPONSE_CANCEL)
196 d.add_button(g.STOCK_OK, g.RESPONSE_OK)
198 d.set_default_response(g.RESPONSE_OK)
200 while 1:
201 if d.run() != g.RESPONSE_OK:
202 d.destroy()
203 return
204 try:
205 ns[prefix.get_text()] = uri.get_text()
206 break
207 except:
208 rox.report_exception()
209 d.destroy()