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/XMLSchema': 'xsd',
14 'http://www.w3.org/1999/XMLSchema-instance': 'xsi',
15 'http://schemas.xmlsoap.org/soap/envelope/': 'env'}
17 class Namespaces(g
.GenericTreeModel
):
19 g
.GenericTreeModel
.__init
__(self
)
20 self
.uri
= dict(fixed_ns
)
23 def update_list(self
):
24 self
.list = self
.uri
.keys()
26 pre
= self
.prefix
= {}
27 for p
, u
in self
.uri
.iteritems():
30 def on_get_n_columns(self
):
33 def on_get_iter(self
, path
):
37 def on_get_value(self
, iter, column
):
39 return self
.list[iter]
40 return self
.uri
[self
.list[iter]]
42 def on_iter_nth_child(self
, iter, n
):
47 def on_get_column_type(self
, col
):
50 def on_iter_has_child(self
, iter):
53 def on_iter_next(self
, iter):
54 if iter < len(self
.list) - 1:
57 def __setitem__(self
, prefix
, uri
):
58 if prefix
in self
.uri
and self
.uri
[prefix
] == uri
:
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
71 path
= (self
.list.index(prefix
),)
74 self
.emit('row-changed', path
, self
.get_iter(path
))
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
),)
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
91 return self
.prefix
[uri
]
93 if not suggested_prefix
:
94 suggested_prefix
= common_ns
.get(uri
, 'ns')
95 if suggested_prefix
in self
.uri
:
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
)
113 class GUI(rox
.Dialog
):
114 def __init__(self
, model
):
115 rox
.Dialog
.__init
__(self
)
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
):
126 dialog
.add_new(model
.namespaces
)
128 dialog
.delete_selected(tree
.get_selection())
131 self
.connect('response', response
)
132 self
.set_position(g
.WIN_POS_MOUSE
)
133 self
.set_title('Namespaces for ' + `model
.uri`
)
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
)
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()
156 rox
.alert('Select a namespace binding to delete')
161 rox
.report_exception()
163 def add_new(self
, ns
):
166 hbox
= g
.HBox(False, 2)
167 d
.vbox
.pack_start(hbox
, False, True)
168 hbox
.pack_start(g
.Label('Prefix:'), False, True, 0)
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():
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)
187 hbox
.pack_start(uri
, True, True, 0)
188 uri
.set_activates_default(True)
191 d
.set_position(g
.WIN_POS_MOUSE
)
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
)
201 if d
.run() != g
.RESPONSE_OK
:
205 ns
[prefix
.get_text()] = uri
.get_text()
208 rox
.report_exception()