1 |
bh |
1245 |
# Copyright (C) 2003 by Intevation GmbH |
2 |
|
|
# Authors: |
3 |
|
|
# Bernhard Herzog <[email protected]> |
4 |
|
|
# |
5 |
|
|
# This program is free software under the GPL (>=v2) |
6 |
|
|
# Read the file COPYING coming with the software for details. |
7 |
|
|
|
8 |
|
|
"""XML support code for the test cases""" |
9 |
|
|
|
10 |
|
|
import sys |
11 |
|
|
import os |
12 |
bh |
1257 |
from StringIO import StringIO |
13 |
|
|
import xml.sax |
14 |
|
|
import xml.sax.handler |
15 |
|
|
from xml.sax import make_parser, ErrorHandler, SAXNotRecognizedException |
16 |
|
|
|
17 |
bh |
1245 |
import support |
18 |
|
|
|
19 |
|
|
try: |
20 |
|
|
import pyRXP |
21 |
|
|
except ImportError: |
22 |
|
|
pyRXP = None |
23 |
|
|
|
24 |
|
|
class ValidationTest: |
25 |
|
|
|
26 |
|
|
"""Mix-in class for tests that want to check for validity of XML data""" |
27 |
|
|
|
28 |
|
|
# If true, at least one test case tried to validate XML data but |
29 |
|
|
# couldn't because PyRXP is not available |
30 |
|
|
validation_attempt_ignored = False |
31 |
|
|
|
32 |
|
|
def validate_data(self, data): |
33 |
|
|
"""Validate the XML data""" |
34 |
|
|
if pyRXP is not None: |
35 |
|
|
parser = pyRXP.Parser() |
36 |
|
|
try: |
37 |
|
|
parser.parse(data, eoCB = self.rxp_eo_cb) |
38 |
|
|
except pyRXP.error, val: |
39 |
|
|
raise AssertionError, str(val), sys.exc_info()[2] |
40 |
|
|
else: |
41 |
|
|
ValidationTest.validation_attempt_ignored = True |
42 |
|
|
|
43 |
|
|
def rxp_eo_cb(self, filename): |
44 |
|
|
"""Resolve an eternal entity |
45 |
|
|
|
46 |
|
|
In the Thuban test cases the only external entities that have to |
47 |
|
|
be resolved are the DTDs which now live in the resource |
48 |
|
|
directory. So, interpret any filename relative to that |
49 |
|
|
directory. |
50 |
|
|
""" |
51 |
|
|
return os.path.join(support.resource_dir(), "XML", filename) |
52 |
|
|
|
53 |
|
|
|
54 |
bh |
1257 |
# |
55 |
|
|
# Classes and functions to convert XML to a normalized list |
56 |
|
|
# representation for comparisons |
57 |
|
|
# |
58 |
bh |
1245 |
|
59 |
bh |
1257 |
class SaxEventLister(xml.sax.handler.ContentHandler): |
60 |
|
|
|
61 |
|
|
"""Create a normalized list representation containing the SAX events |
62 |
|
|
""" |
63 |
|
|
|
64 |
|
|
def __init__(self): |
65 |
|
|
self.eventlist = [] |
66 |
|
|
|
67 |
|
|
def startElementNS(self, name, qname, attrs): |
68 |
|
|
items = attrs.items() |
69 |
|
|
items.sort() |
70 |
|
|
self.eventlist.append(("start", name, qname, items)) |
71 |
|
|
|
72 |
|
|
def endElementNS(self, name, qname): |
73 |
|
|
self.eventlist.append(("end", name, qname)) |
74 |
|
|
|
75 |
|
|
|
76 |
|
|
def sax_eventlist(data = None, filename = None): |
77 |
|
|
"""Return a list of SAX event generated for a given XML source |
78 |
|
|
|
79 |
|
|
The xml source may either be a string with the actual XML, in which |
80 |
|
|
case it should be given as the keyword argument data or the name of |
81 |
|
|
an xml file given as the keyword argument filename |
82 |
|
|
""" |
83 |
|
|
if filename is not None: |
84 |
|
|
data = open(filename).read() |
85 |
|
|
handler = SaxEventLister() |
86 |
|
|
parser = make_parser() |
87 |
|
|
parser.setContentHandler(handler) |
88 |
|
|
parser.setErrorHandler(ErrorHandler()) |
89 |
|
|
parser.setFeature(xml.sax.handler.feature_namespaces, 1) |
90 |
|
|
|
91 |
|
|
# |
92 |
|
|
# see comment at the end of Thuban/Model/load.py |
93 |
|
|
# |
94 |
|
|
try: |
95 |
|
|
parser.setFeature(xml.sax.handler.feature_validation, 0) |
96 |
|
|
parser.setFeature(xml.sax.handler.feature_external_ges, 0) |
97 |
|
|
parser.setFeature(xml.sax.handler.feature_external_pes, 0) |
98 |
|
|
except SAXNotRecognizedException: |
99 |
|
|
pass |
100 |
|
|
|
101 |
|
|
inpsrc = xml.sax.InputSource() |
102 |
|
|
inpsrc.setByteStream(StringIO(data)) |
103 |
|
|
parser.parse(inpsrc) |
104 |
|
|
|
105 |
|
|
return handler.eventlist |
106 |
|
|
|
107 |
|
|
|
108 |
bh |
1245 |
def print_summary_message(): |
109 |
|
|
"""Print a summary message about validation tests |
110 |
|
|
|
111 |
|
|
Currently simply print a message about pyRXP not being available if |
112 |
|
|
a test case's attempt to validate XML was ignored because of that. |
113 |
|
|
""" |
114 |
|
|
if ValidationTest.validation_attempt_ignored: |
115 |
|
|
print "XML validation attempts ignored because pyRXP is not available" |