1 from xml
.dom
import XMLNS_NAMESPACE
, XML_NAMESPACE
2 from constants
import DOME_NS
3 from Ft
.Xml
.XPath
import FT_EXT_NAMESPACE
8 fixed_ns
= {'xml': XML_NAMESPACE
, 'xmlns': XMLNS_NAMESPACE
,
9 'ext': FT_EXT_NAMESPACE
,
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
):
23 g
.GenericTreeModel
.__init
__(self
)
24 self
.uri
= dict(fixed_ns
)
27 def update_list(self
):
28 self
.list = self
.uri
.keys()
30 pre
= self
.prefix
= {}
31 for p
, u
in self
.uri
.iteritems():
34 def on_get_n_columns(self
):
37 def on_get_iter(self
, path
):
41 def on_get_value(self
, iter, column
):
43 return self
.list[iter]
44 return self
.uri
[self
.list[iter]]
46 def on_get_flags(self
):
49 def on_iter_nth_child(self
, iter, n
):
54 def on_get_column_type(self
, col
):
57 def on_iter_has_child(self
, iter):
60 def on_iter_next(self
, iter):
61 if iter < len(self
.list) - 1:
64 def __setitem__(self
, prefix
, uri
):
65 if prefix
in self
.uri
and self
.uri
[prefix
] == uri
:
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
78 path
= (self
.list.index(prefix
),)
81 self
.emit('row-changed', path
, self
.get_iter(path
))
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
),)
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
98 return self
.prefix
[uri
]
100 if not suggested_prefix
:
101 suggested_prefix
= common_ns
.get(uri
, 'ns')
102 if suggested_prefix
in self
.uri
:
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
)
120 class GUI(rox
.Dialog
):
121 def __init__(self
, model
):
122 rox
.Dialog
.__init
__(self
)
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
):
133 dialog
.add_new(model
.namespaces
)
135 dialog
.delete_selected(tree
.get_selection())
138 self
.connect('response', response
)
139 self
.set_position(g
.WIN_POS_MOUSE
)
140 self
.set_title('Namespaces for ' + `model
.uri`
)
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
)
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()
163 rox
.alert('Select a namespace binding to delete')
168 rox
.report_exception()
170 def add_new(self
, ns
):
173 hbox
= g
.HBox(False, 2)
174 d
.vbox
.pack_start(hbox
, False, True)
175 hbox
.pack_start(g
.Label('Prefix:'), False, True, 0)
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():
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)
194 hbox
.pack_start(uri
, True, True, 0)
195 uri
.set_activates_default(True)
198 d
.set_position(g
.WIN_POS_MOUSE
)
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
)
208 if d
.run() != g
.RESPONSE_OK
:
212 ns
[prefix
.get_text()] = uri
.get_text()
215 rox
.report_exception()