1 from xml
.dom
import XMLNS_NAMESPACE
, XML_NAMESPACE
2 from constants
import DOME_NS
7 fixed_ns
= {'xml': XML_NAMESPACE
, 'xmlns': XMLNS_NAMESPACE
,
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/XSL/Transform': 'xsl',
14 'http://www.w3.org/1999/XMLSchema': 'xsd',
15 'http://www.w3.org/1999/XMLSchema-instance': 'xsi',
16 'http://schemas.xmlsoap.org/soap/envelope/': 'env',
17 'http://www.w3.org/1999/xlink': 'xlink'}
19 class Namespaces(g
.GenericTreeModel
):
21 g
.GenericTreeModel
.__init
__(self
)
22 self
.uri
= dict(fixed_ns
)
25 def update_list(self
):
26 self
.list = self
.uri
.keys()
28 pre
= self
.prefix
= {}
29 for p
, u
in self
.uri
.iteritems():
32 def on_get_n_columns(self
):
35 def on_get_iter(self
, path
):
39 def on_get_value(self
, iter, column
):
41 return self
.list[iter]
42 return self
.uri
[self
.list[iter]]
44 def on_iter_nth_child(self
, iter, n
):
49 def on_get_column_type(self
, col
):
52 def on_iter_has_child(self
, iter):
55 def on_iter_next(self
, iter):
56 if iter < len(self
.list) - 1:
59 def __setitem__(self
, prefix
, uri
):
60 if prefix
in self
.uri
and self
.uri
[prefix
] == uri
:
64 if prefix
in fixed_ns
:
65 raise Exception('That namespace prefix cannot be changed')
66 if uri
in self
.prefix
:
67 raise Exception('That namespace already has a prefix (%s)' % self
.prefix
[uri
])
69 modifed
= prefix
in self
.uri
71 self
.uri
[prefix
] = uri
73 path
= (self
.list.index(prefix
),)
76 self
.emit('row-changed', path
, self
.get_iter(path
))
78 self
.emit('row-inserted', path
, self
.get_iter(path
))
80 def __delitem__(self
, iter):
81 prefix
= self
[iter][0]
82 if prefix
in fixed_ns
:
83 raise Exception('This is a built-in namespace and cannot be deleted')
84 path
= (self
.list.index(prefix
),)
87 self
.emit('row-deleted', path
)
89 def ensure_ns(self
, suggested_prefix
, uri
):
90 """Return the prefix for this URI. If none is set choose one (using suggested_prefix
93 return self
.prefix
[uri
]
95 if not suggested_prefix
:
96 suggested_prefix
= common_ns
.get(uri
, 'ns')
97 if suggested_prefix
in self
.uri
:
99 while suggested_prefix
+ `x`
in self
.uri
: x
+= 1
100 suggested_prefix
+= `x`
101 self
[suggested_prefix
] = uri
102 #print "Added", suggested_prefix, uri
103 return suggested_prefix
105 def to_xml(self
, doc
):
106 node
= doc
.createElementNS(DOME_NS
, 'dome:namespaces')
107 for prefix
, uri
in self
.uri
.iteritems():
108 if prefix
in fixed_ns
: continue
109 ns
= doc
.createElementNS(DOME_NS
, 'dome:ns')
110 ns
.setAttributeNS(None, 'prefix', prefix
)
111 ns
.setAttributeNS(None, 'uri', uri
)
115 class GUI(rox
.Dialog
):
116 def __init__(self
, model
):
117 rox
.Dialog
.__init
__(self
)
120 self
.add_button(g
.STOCK_ADD
, 1)
121 self
.add_button(g
.STOCK_DELETE
, 2)
122 self
.add_button(g
.STOCK_CLOSE
, g
.RESPONSE_OK
)
124 tree
= g
.TreeView(model
.namespaces
)
126 def response(dialog
, resp
):
128 dialog
.add_new(model
.namespaces
)
130 dialog
.delete_selected(tree
.get_selection())
133 self
.connect('response', response
)
134 self
.set_position(g
.WIN_POS_MOUSE
)
135 self
.set_title('Namespaces for ' + `model
.uri`
)
139 frame
.set_shadow_type(g
.SHADOW_IN
)
141 cell
= g
.CellRendererText()
142 column
= g
.TreeViewColumn('Prefix', cell
, text
= 0)
143 tree
.append_column(column
)
145 column
= g
.TreeViewColumn('URI', cell
, text
= 1)
146 tree
.append_column(column
)
150 self
.vbox
.pack_start(frame
, True, True)
151 self
.set_default_size(400, 200)
153 self
.set_has_separator(False)
155 def delete_selected(self
, sel
):
156 model
, iter = sel
.get_selected()
158 rox
.alert('Select a namespace binding to delete')
163 rox
.report_exception()
165 def add_new(self
, ns
):
168 hbox
= g
.HBox(False, 2)
169 d
.vbox
.pack_start(hbox
, False, True)
170 hbox
.pack_start(g
.Label('Prefix:'), False, True, 0)
172 hbox
.pack_start(prefix
, True, True, 0)
174 def check_prefix(entry
):
175 if not uri
.get_text():
176 pre
= prefix
.get_text()
177 for u
, p
in common_ns
.iteritems():
183 prefix
.connect('activate', check_prefix
)
185 hbox
= g
.HBox(False, 2)
186 d
.vbox
.pack_start(hbox
, False, True)
187 hbox
.pack_start(g
.Label('Namespace URI:'), False, True, 0)
189 hbox
.pack_start(uri
, True, True, 0)
190 uri
.set_activates_default(True)
193 d
.set_position(g
.WIN_POS_MOUSE
)
197 d
.add_button(g
.STOCK_CANCEL
, g
.RESPONSE_CANCEL
)
198 d
.add_button(g
.STOCK_OK
, g
.RESPONSE_OK
)
200 d
.set_default_response(g
.RESPONSE_OK
)
203 if d
.run() != g
.RESPONSE_OK
:
207 ns
[prefix
.get_text()] = uri
.get_text()
210 rox
.report_exception()