21 |
import xml.sax.handler |
import xml.sax.handler |
22 |
from xml.sax import make_parser, ErrorHandler, SAXNotRecognizedException |
from xml.sax import make_parser, ErrorHandler, SAXNotRecognizedException |
23 |
|
|
24 |
|
import xmlsupport |
25 |
|
|
26 |
import support |
import support |
27 |
support.initthuban() |
support.initthuban() |
28 |
|
|
29 |
from Thuban.Model.save import save_session |
from Thuban.Model.save import XMLWriter, save_session |
30 |
from Thuban.Model.session import Session |
from Thuban.Model.session import Session |
31 |
from Thuban.Model.map import Map |
from Thuban.Model.map import Map |
32 |
from Thuban.Model.layer import Layer |
from Thuban.Model.layer import Layer, RasterLayer |
33 |
from Thuban.Model.proj import Projection |
from Thuban.Model.proj import Projection |
34 |
|
|
35 |
|
from Thuban.Model.classification import ClassGroupSingleton, ClassGroupRange, \ |
36 |
|
ClassGroupProperties |
37 |
|
|
38 |
|
from Thuban.Model.range import Range |
39 |
|
|
40 |
|
|
41 |
class SaxEventLister(xml.sax.handler.ContentHandler): |
class XMLWriterTest(unittest.TestCase): |
42 |
|
|
43 |
def __init__(self): |
def testEncode(self): |
44 |
self.eventlist = [] |
"""Test XMLWriter.encode""" |
45 |
|
writer = XMLWriter() |
46 |
|
eq = self.assertEquals |
47 |
|
|
48 |
def startElementNS(self, name, qname, attrs): |
eq(writer.encode("hello world"), "hello world") |
49 |
items = attrs.items() |
eq(writer.encode(unicode("hello world")), unicode("hello world")) |
|
items.sort() |
|
|
self.eventlist.append(("start", name, qname, items)) |
|
|
|
|
|
def endElementNS(self, name, qname): |
|
|
self.eventlist.append(("end", name, qname)) |
|
|
|
|
|
|
|
|
def sax_eventlist(data): |
|
|
"""Return a list of SAX event generated for the XML data. |
|
|
""" |
|
|
handler = SaxEventLister() |
|
|
parser = make_parser() |
|
|
parser.setContentHandler(handler) |
|
|
parser.setErrorHandler(ErrorHandler()) |
|
|
parser.setFeature(xml.sax.handler.feature_namespaces, 1) |
|
|
|
|
|
# |
|
|
# see comment at the end of Thuban/Model/load.py |
|
|
# |
|
|
try: |
|
|
parser.setFeature(xml.sax.handler.feature_validation, 0) |
|
|
parser.setFeature(xml.sax.handler.feature_external_ges, 0) |
|
|
parser.setFeature(xml.sax.handler.feature_external_pes, 0) |
|
|
except SAXNotRecognizedException: |
|
|
pass |
|
|
|
|
|
inpsrc = xml.sax.InputSource() |
|
|
inpsrc.setByteStream(StringIO(data)) |
|
|
parser.parse(inpsrc) |
|
50 |
|
|
51 |
return handler.eventlist |
eq(writer.encode("\x80\x90\xc2\x100"), |
52 |
|
"\xc2\x80\xc2\x90\xc3\x82\x100") |
53 |
|
eq(writer.encode(u"\x80\x90\xc2\x100"), |
54 |
|
"\xc2\x80\xc2\x90\xc3\x82\x100") |
55 |
|
eq(writer.encode(u"\xFF5E"), "\xc3\xbf5E") |
56 |
|
|
57 |
class SaveSessionTest(unittest.TestCase, support.FileTestMixin): |
eq(writer.encode('&"\'<>'), "&"'<>") |
58 |
|
eq(writer.encode(unicode('&"\'<>')), "&"'<>") |
59 |
|
|
60 |
|
class SaveSessionTest(unittest.TestCase, support.FileTestMixin, |
61 |
|
xmlsupport.ValidationTest): |
62 |
|
|
63 |
def compare_xml(self, xml1, xml2): |
def compare_xml(self, xml1, xml2): |
64 |
self.assertEquals(sax_eventlist(xml1), sax_eventlist(xml2)) |
self.assertEquals(xmlsupport.sax_eventlist(xml1), |
65 |
|
xmlsupport.sax_eventlist(xml2)) |
66 |
|
|
67 |
def testEmptySession(self): |
def testEmptySession(self): |
68 |
"""Save an empty session""" |
"""Save an empty session""" |
79 |
'<!DOCTYPE session SYSTEM "thuban.dtd">\n' |
'<!DOCTYPE session SYSTEM "thuban.dtd">\n' |
80 |
'<session title="empty session">\n</session>\n') |
'<session title="empty session">\n</session>\n') |
81 |
|
|
82 |
|
self.validate_data(written_contents) |
83 |
|
|
84 |
def testSingleLayer(self): |
def testSingleLayer(self): |
85 |
"""Save a session with a single map with a single layer""" |
"""Save a session with a single map with a single layer""" |
86 |
# deliberately put an apersand in the title :) |
# deliberately put an apersand in the title :) |
123 |
#print expected_contents |
#print expected_contents |
124 |
self.compare_xml(written_contents, expected_contents) |
self.compare_xml(written_contents, expected_contents) |
125 |
|
|
126 |
|
self.validate_data(written_contents) |
127 |
|
|
128 |
layer.SetVisible(False) |
layer.SetVisible(False) |
129 |
save_session(session, filename) |
save_session(session, filename) |
130 |
|
|
139 |
#print "********************************************" |
#print "********************************************" |
140 |
#print expected_contents |
#print expected_contents |
141 |
self.compare_xml(written_contents, expected_contents) |
self.compare_xml(written_contents, expected_contents) |
142 |
|
self.validate_data(written_contents) |
143 |
|
|
144 |
session.Destroy() |
session.Destroy() |
145 |
|
|
188 |
#print expected_contents |
#print expected_contents |
189 |
self.compare_xml(written_contents, expected_contents) |
self.compare_xml(written_contents, expected_contents) |
190 |
|
|
191 |
|
self.validate_data(written_contents) |
192 |
|
|
193 |
|
def testRasterLayer(self): |
194 |
|
# deliberately put an apersand in the title :) |
195 |
|
session = Session("single map&layer") |
196 |
|
map = Map("Test Map") |
197 |
|
session.AddMap(map) |
198 |
|
# use shapefile from the example data |
199 |
|
imgfile = os.path.join(os.path.dirname(__file__), |
200 |
|
os.pardir, "Data", "iceland", "island.tif") |
201 |
|
layer = RasterLayer("My RasterLayer", imgfile) |
202 |
|
map.AddLayer(layer) |
203 |
|
|
204 |
|
filename = self.temp_file_name("save_singlemap.thuban") |
205 |
|
save_session(session, filename) |
206 |
|
session.Destroy() |
207 |
|
|
208 |
|
file = open(filename) |
209 |
|
written_contents = file.read() |
210 |
|
file.close() |
211 |
|
expected_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
212 |
|
<!DOCTYPE session SYSTEM "thuban.dtd"> |
213 |
|
<session title="single map&layer"> |
214 |
|
<map title="Test Map"> |
215 |
|
<rasterlayer title="My RasterLayer" filename="%s" |
216 |
|
visible="true"> |
217 |
|
</rasterlayer> |
218 |
|
</map> |
219 |
|
</session>''' % os.path.join(os.path.dirname(__file__), |
220 |
|
os.pardir, "Data", "iceland", |
221 |
|
"island.tif") |
222 |
|
#print written_contents |
223 |
|
#print "********************************************" |
224 |
|
#print expected_contents |
225 |
|
self.compare_xml(written_contents, expected_contents) |
226 |
|
|
227 |
|
self.validate_data(written_contents) |
228 |
|
|
229 |
|
def testClassifiedLayer(self): |
230 |
|
"""Save a session with a single map with a single layer |
231 |
|
with a classificaton. |
232 |
|
""" |
233 |
|
# deliberately put an apersand in the title :) |
234 |
|
session = Session("single map&layer") |
235 |
|
proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"]) |
236 |
|
map = Map("Test Map", projection = proj) |
237 |
|
session.AddMap(map) |
238 |
|
# use shapefile from the example data |
239 |
|
shpfile = os.path.join(os.path.dirname(__file__), |
240 |
|
os.pardir, "Data", "iceland", "political.shp") |
241 |
|
layer = Layer("My Layer", session.OpenShapefile(shpfile)) |
242 |
|
map.AddLayer(layer) |
243 |
|
|
244 |
|
clazz = layer.GetClassification() |
245 |
|
|
246 |
|
clazz.SetField("AREA") |
247 |
|
|
248 |
|
clazz.AppendGroup(ClassGroupSingleton(42, |
249 |
|
ClassGroupProperties(), |
250 |
|
"single")) |
251 |
|
clazz.AppendGroup(ClassGroupSingleton("text", |
252 |
|
ClassGroupProperties(), |
253 |
|
"single-text")) |
254 |
|
|
255 |
|
clazz.AppendGroup(ClassGroupRange(0, 42, |
256 |
|
ClassGroupProperties(), |
257 |
|
"range")) |
258 |
|
|
259 |
|
range = ClassGroupRange(Range("[0;42]")) |
260 |
|
range.SetProperties(ClassGroupProperties()) |
261 |
|
range.SetLabel("new-range") |
262 |
|
clazz.AppendGroup(range) |
263 |
|
|
264 |
|
filename = self.temp_file_name("save_singlemap.thuban") |
265 |
|
save_session(session, filename) |
266 |
|
|
267 |
|
file = open(filename) |
268 |
|
written_contents = file.read() |
269 |
|
file.close() |
270 |
|
expected_template = '''<?xml version="1.0" encoding="UTF-8"?> |
271 |
|
<!DOCTYPE session SYSTEM "thuban.dtd"> |
272 |
|
<session title="single map&layer"> |
273 |
|
<map title="Test Map"> |
274 |
|
<projection name="Unknown"> |
275 |
|
<parameter value="zone=26"/> |
276 |
|
<parameter value="proj=utm"/> |
277 |
|
<parameter value="ellps=clrk66"/> |
278 |
|
</projection> |
279 |
|
<layer title="My Layer" filename="%s" |
280 |
|
fill="None" stroke="#000000" stroke_width="1" visible="%s"> |
281 |
|
<classification field="AREA" field_type="double"> |
282 |
|
<clnull label=""> |
283 |
|
<cldata fill="None" stroke="#000000" stroke_width="1"/> |
284 |
|
</clnull> |
285 |
|
<clpoint value="42" label="single"> |
286 |
|
<cldata fill="None" stroke="#000000" stroke_width="1"/> |
287 |
|
</clpoint> |
288 |
|
<clpoint value="text" label="single-text"> |
289 |
|
<cldata fill="None" stroke="#000000" stroke_width="1"/> |
290 |
|
</clpoint> |
291 |
|
<clrange range="[0;42[" label="range"> |
292 |
|
<cldata fill="None" stroke="#000000" stroke_width="1"/> |
293 |
|
</clrange> |
294 |
|
<clrange range="[0;42]" label="new-range"> |
295 |
|
<cldata fill="None" stroke="#000000" stroke_width="1"/> |
296 |
|
</clrange> |
297 |
|
</classification> |
298 |
|
</layer> |
299 |
|
</map> |
300 |
|
</session>''' |
301 |
|
|
302 |
|
expected_contents = expected_template % \ |
303 |
|
(os.path.join("..", "..", "Data", "iceland", "political.shp"), |
304 |
|
"true") |
305 |
|
|
306 |
|
#print written_contents |
307 |
|
#print "********************************************" |
308 |
|
#print expected_contents |
309 |
|
self.compare_xml(written_contents, expected_contents) |
310 |
|
|
311 |
|
self.validate_data(written_contents) |
312 |
|
|
313 |
|
session.Destroy() |
314 |
|
|
315 |
|
|
316 |
if __name__ == "__main__": |
if __name__ == "__main__": |