1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 import qm.xmlutil
19 from qm.test.file_result_reader import FileResultReader
20 from qm.test.result import Result
21 from qm.test.file_result_stream import FileResultStream
22
23
24
25
26
28 """An 'XMLResultStream' writes out results as XML.
29
30 An 'XMLResultStream' writes out results as XML. This stream is
31 used to write out QMTest results files. The DTD is designed in
32 such a way that if QMTest is terminated in the middle of a test
33 run, the file will still be nearly valid, as long as the
34 interruption did not occur in the midst of writing out an
35 individual result. The closing tag for the results file will
36 be missing."""
37
39
40
41 super(XMLResultStream, self).__init__(arguments, **args)
42
43
44
45 self.__document = qm.xmlutil.create_dom_document(
46 public_id="QMTest/Result",
47 document_element_tag="results")
48
49 self.file.write("<?xml version='1.0' encoding='ISO-8859-1'?>\n")
50 self.file.write('<!DOCTYPE results PUBLIC "%s" "%s">\n'
51 % (qm.xmlutil.make_public_id("QMTest/Result"),
52 qm.xmlutil.make_system_id("qmtest/result.dtd")))
53
54 self.file.write("<results>\n")
55
56
58
59 element = self.__document.createElement("annotation")
60 element.setAttribute("key", key)
61 text = self.__document.createTextNode(value)
62 element.appendChild(text)
63 element.writexml(self.file, addindent = " ", newl = "\n")
64
65
67
68 element = result.MakeDomNode(self.__document)
69 element.writexml(self.file, indent = " ", addindent = " ",
70 newl = "\n")
71
72
79
80
81
83 """Reads in 'Result's from an XML-formatted results file.
84
85 To write such a file, see 'XMLResultStream'."""
86
88
89 super(XMLResultReader, self).__init__(arguments, **args)
90
91
92 tag = self.file.read(5)
93 if tag != "<?xml":
94 raise FileResultReader.InvalidFile, \
95 "file is not an XML result stream"
96 self.file.seek(0)
97
98 document = qm.xmlutil.load_xml(self.file)
99 node = document.documentElement
100 results = node.getElementsByTagName("result")
101 self.__node_iterator = iter(results)
102
103
104 self._annotations = {}
105 annotation_nodes = node.getElementsByTagName("annotation")
106 for node in annotation_nodes:
107 key = node.getAttribute("key")
108 value = qm.xmlutil.get_dom_text(node).strip()
109 self._annotations[key] = value
110
111
113
114 return self._annotations
115
116
123
124
126 """Extract a result from a DOM node.
127
128 'node' -- A DOM node corresponding to a "result" element.
129
130 returns -- A 'Result' object."""
131
132 assert node.tagName == "result"
133
134 outcome = node.getAttribute("outcome")
135
136
137 if not outcome:
138 outcome = qm.xmlutil.get_child_text(node, "outcome").strip()
139
140 test_id = node.getAttribute("id")
141 kind = node.getAttribute("kind")
142
143 result = Result(kind, test_id, outcome)
144
145 for n in node.childNodes:
146 if n.nodeType != node.ELEMENT_NODE:
147 continue
148 if n.tagName == "annotation":
149 quoted = 1
150 elif n.tagName == "property":
151
152
153 quoted = 0
154 else:
155 continue
156
157 name = n.getAttribute("name")
158
159 value = qm.xmlutil.get_dom_text(n)
160 if quoted:
161
162 value = value.strip()[1:-1]
163
164 result[name] = value
165
166 return result
167
168
169
170
171
172
173
174
175
176