Fixed version number (for 0release)
[dom-editor.git] / Dome / Namespaces.py
bloba6ae08e90de4920139c69e55f3b2e043e68bd82f
1 from xml.dom import XMLNS_NAMESPACE, XML_NAMESPACE
2 from constants import DOME_NS
3 from Ft.Xml.XPath import FT_EXT_NAMESPACE
5 import rox
6 from rox import g
8 fixed_ns = {'xml': XML_NAMESPACE, 'xmlns': XMLNS_NAMESPACE,
9 'ext': FT_EXT_NAMESPACE,
10 'dome': DOME_NS}
12 common_ns = {'http://www.w3.org/1999/xhtml': 'xhtml',
13 'http://www.w3.org/1999/02/22-rdf-syntax-ns#': 'rdf',
14 'http://xmlns.4suite.org/ext': 'ft',
15 'http://www.w3.org/1999/XSL/Transform': 'xsl',
16 'http://www.w3.org/2001/XMLSchema': 'xsd',
17 'http://www.w3.org/2001/XMLSchema-instance': 'xsi',
18 'http://schemas.xmlsoap.org/soap/envelope/': 'env',
19 'http://www.w3.org/1999/xlink': 'xlink'}
21 class Namespaces(g.GenericTreeModel):
22 def __init__(self):
23 g.GenericTreeModel.__init__(self)
24 self.uri = dict(fixed_ns)
25 self.update_list()
27 def update_list(self):
28 self.list = self.uri.keys()
29 self.list.sort()
30 pre = self.prefix = {}
31 for p, u in self.uri.iteritems():
32 pre[u] = p
34 def on_get_n_columns(self):
35 return 2
37 def on_get_iter(self, path):
38 assert len(path) == 1
39 return path[0]
41 def on_get_value(self, iter, column):
42 if column == 0:
43 return self.list[iter]
44 return self.uri[self.list[iter]]
46 def on_get_flags(self):
47 return 0
49 def on_iter_nth_child(self, iter, n):
50 if iter == None:
51 return n
52 return None
54 def on_get_column_type(self, col):
55 return str
57 def on_iter_has_child(self, iter):
58 return False
60 def on_iter_next(self, iter):
61 if iter < len(self.list) - 1:
62 return iter + 1
64 def __setitem__(self, prefix, uri):
65 if prefix in self.uri and self.uri[prefix] == uri:
66 return
68 assert prefix
69 if prefix in fixed_ns:
70 raise Exception('That namespace prefix cannot be changed')
71 if uri in self.prefix:
72 raise Exception('That namespace already has a prefix (%s)' % self.prefix[uri])
74 modifed = prefix in self.uri
76 self.uri[prefix] = uri
77 self.update_list()
78 path = (self.list.index(prefix),)
80 if modifed:
81 self.emit('row-changed', path, self.get_iter(path))
82 else:
83 self.emit('row-inserted', path, self.get_iter(path))
85 def __delitem__(self, iter):
86 prefix = self[iter][0]
87 if prefix in fixed_ns:
88 raise Exception('This is a built-in namespace and cannot be deleted')
89 path = (self.list.index(prefix),)
90 del self.uri[prefix]
91 self.update_list()
92 self.emit('row-deleted', path)
94 def ensure_ns(self, suggested_prefix, uri):
95 """Return the prefix for this URI. If none is set choose one (using suggested_prefix
96 if possible)."""
97 try:
98 return self.prefix[uri]
99 except KeyError:
100 if not suggested_prefix:
101 suggested_prefix = common_ns.get(uri, 'ns')
102 if suggested_prefix in self.uri:
103 x = 1
104 while suggested_prefix + `x` in self.uri: x += 1
105 suggested_prefix += `x`
106 self[suggested_prefix] = uri
107 #print "Added", suggested_prefix, uri
108 return suggested_prefix
110 def to_xml(self, doc):
111 node = doc.createElementNS(DOME_NS, 'dome:namespaces')
112 for prefix, uri in self.uri.iteritems():
113 if prefix in fixed_ns: continue
114 ns = doc.createElementNS(DOME_NS, 'dome:ns')
115 ns.setAttributeNS(None, 'prefix', prefix)
116 ns.setAttributeNS(None, 'uri', uri)
117 node.appendChild(ns)
118 return node
120 class GUI(rox.Dialog):
121 def __init__(self, model):
122 rox.Dialog.__init__(self)
123 self.model = model
125 self.add_button(g.STOCK_ADD, 1)
126 self.add_button(g.STOCK_DELETE, 2)
127 self.add_button(g.STOCK_CLOSE, g.RESPONSE_OK)
129 tree = g.TreeView(model.namespaces)
131 def response(dialog, resp):
132 if resp == 1:
133 dialog.add_new(model.namespaces)
134 elif resp == 2:
135 dialog.delete_selected(tree.get_selection())
136 else:
137 dialog.destroy()
138 self.connect('response', response)
139 self.set_position(g.WIN_POS_MOUSE)
140 self.set_title('Namespaces for ' + `model.uri`)
142 frame = g.Frame()
143 frame.add(tree)
144 frame.set_shadow_type(g.SHADOW_IN)
146 cell = g.CellRendererText()
147 column = g.TreeViewColumn('Prefix', cell, text = 0)
148 tree.append_column(column)
150 column = g.TreeViewColumn('URI', cell, text = 1)
151 tree.append_column(column)
153 frame.show_all()
155 self.vbox.pack_start(frame, True, True)
156 self.set_default_size(400, 200)
158 self.set_has_separator(False)
160 def delete_selected(self, sel):
161 model, iter = sel.get_selected()
162 if not iter:
163 rox.alert('Select a namespace binding to delete')
164 return
165 try:
166 del model[iter]
167 except:
168 rox.report_exception()
170 def add_new(self, ns):
171 d = rox.Dialog()
173 hbox = g.HBox(False, 2)
174 d.vbox.pack_start(hbox, False, True)
175 hbox.pack_start(g.Label('Prefix:'), False, True, 0)
176 prefix = g.Entry()
177 hbox.pack_start(prefix, True, True, 0)
179 def check_prefix(entry):
180 if not uri.get_text():
181 pre = prefix.get_text()
182 for u, p in common_ns.iteritems():
183 if p == pre:
184 uri.set_text(u)
185 break
186 uri.grab_focus()
188 prefix.connect('activate', check_prefix)
190 hbox = g.HBox(False, 2)
191 d.vbox.pack_start(hbox, False, True)
192 hbox.pack_start(g.Label('Namespace URI:'), False, True, 0)
193 uri = g.Entry()
194 hbox.pack_start(uri, True, True, 0)
195 uri.set_activates_default(True)
197 d.vbox.show_all()
198 d.set_position(g.WIN_POS_MOUSE)
200 prefix.grab_focus()
202 d.add_button(g.STOCK_CANCEL, g.RESPONSE_CANCEL)
203 d.add_button(g.STOCK_OK, g.RESPONSE_OK)
205 d.set_default_response(g.RESPONSE_OK)
207 while 1:
208 if d.run() != g.RESPONSE_OK:
209 d.destroy()
210 return
211 try:
212 ns[prefix.get_text()] = uri.get_text()
213 break
214 except:
215 rox.report_exception()
216 d.destroy()