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 |
|
|
38 |
from Thuban.Model.range import Range |
from Thuban.Model.range import Range |
39 |
|
|
40 |
|
|
|
class SaxEventLister(xml.sax.handler.ContentHandler): |
|
|
|
|
|
def __init__(self): |
|
|
self.eventlist = [] |
|
|
|
|
|
def startElementNS(self, name, qname, attrs): |
|
|
items = attrs.items() |
|
|
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) |
|
|
|
|
|
return handler.eventlist |
|
|
|
|
41 |
class XMLWriterTest(unittest.TestCase): |
class XMLWriterTest(unittest.TestCase): |
42 |
|
|
43 |
def testEncode(self): |
def testEncode(self): |
44 |
"""Test XMLWriter.encode""" |
"""Test XMLWriter.encode""" |
45 |
writer = XMLWriter() |
writer = XMLWriter() |
46 |
|
eq = self.assertEquals |
47 |
|
|
48 |
writer.encode("hello world") |
eq(writer.encode("hello world"), "hello world") |
49 |
writer.encode(unicode("hello world")) |
eq(writer.encode(unicode("hello world")), unicode("hello world")) |
50 |
|
|
51 |
writer.encode("\x80\x90\xc2\x100") |
eq(writer.encode("\x80\x90\xc2\x100"), |
52 |
writer.encode(u"\x80\x90\xc2\x100") |
"\xc2\x80\xc2\x90\xc3\x82\x100") |
53 |
writer.encode(u"\xFF5E") |
eq(writer.encode(u"\x80\x90\xc2\x100"), |
54 |
|
"\xc2\x80\xc2\x90\xc3\x82\x100") |
55 |
self.assertEquals(writer.encode('&"\'<>'), |
eq(writer.encode(u"\xFF5E"), "\xc3\xbf5E") |
|
"&"'<>") |
|
|
self.assertEquals(writer.encode(unicode('&"\'<>')), |
|
|
"&"'<>") |
|
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): |
def testRasterLayer(self): |
194 |
# deliberately put an apersand in the title :) |
# deliberately put an apersand in the title :) |
195 |
session = Session("single map&layer") |
session = Session("single map&layer") |
200 |
os.pardir, "Data", "iceland", "island.tif") |
os.pardir, "Data", "iceland", "island.tif") |
201 |
layer = RasterLayer("My RasterLayer", imgfile) |
layer = RasterLayer("My RasterLayer", imgfile) |
202 |
map.AddLayer(layer) |
map.AddLayer(layer) |
203 |
|
|
204 |
filename = self.temp_file_name("save_singlemap.thuban") |
filename = self.temp_file_name("save_singlemap.thuban") |
205 |
save_session(session, filename) |
save_session(session, filename) |
206 |
session.Destroy() |
session.Destroy() |
207 |
|
|
208 |
file = open(filename) |
file = open(filename) |
209 |
written_contents = file.read() |
written_contents = file.read() |
210 |
file.close() |
file.close() |
224 |
#print expected_contents |
#print expected_contents |
225 |
self.compare_xml(written_contents, expected_contents) |
self.compare_xml(written_contents, expected_contents) |
226 |
|
|
227 |
|
self.validate_data(written_contents) |
228 |
|
|
229 |
def testClassifiedLayer(self): |
def testClassifiedLayer(self): |
230 |
"""Save a session with a single map with a single layer |
"""Save a session with a single map with a single layer |
231 |
with a classificaton. |
with a classificaton. |
297 |
</classification> |
</classification> |
298 |
</layer> |
</layer> |
299 |
</map> |
</map> |
300 |
</session>''' |
</session>''' |
301 |
|
|
302 |
expected_contents = expected_template % \ |
expected_contents = expected_template % \ |
303 |
(os.path.join("..", "..", "Data", "iceland", "political.shp"), |
(os.path.join("..", "..", "Data", "iceland", "political.shp"), |
304 |
"true") |
"true") |
308 |
#print expected_contents |
#print expected_contents |
309 |
self.compare_xml(written_contents, expected_contents) |
self.compare_xml(written_contents, expected_contents) |
310 |
|
|
311 |
|
self.validate_data(written_contents) |
312 |
|
|
313 |
session.Destroy() |
session.Destroy() |
314 |
|
|
315 |
|
|