7 |
|
|
8 |
"""XML support code for the test cases""" |
"""XML support code for the test cases""" |
9 |
|
|
10 |
|
__version__ = "$Revision$" |
11 |
|
# $Source$ |
12 |
|
# $Id$ |
13 |
|
|
14 |
import sys |
import sys |
15 |
import os |
import os |
16 |
from StringIO import StringIO |
from StringIO import StringIO |
63 |
class SaxEventLister(xml.sax.handler.ContentHandler): |
class SaxEventLister(xml.sax.handler.ContentHandler): |
64 |
|
|
65 |
"""Create a normalized list representation containing the SAX events |
"""Create a normalized list representation containing the SAX events |
66 |
|
|
67 |
|
The normalization includes the following |
68 |
|
|
69 |
|
- The attribute dictionary of a staertElement event is converted to |
70 |
|
a sorted list of (key, value) pairs |
71 |
|
|
72 |
|
- ID and IDREF attribute values are normalized in such a way that |
73 |
|
two documents that only use different values for IDs can still |
74 |
|
lead to the same normalized representation. |
75 |
|
|
76 |
|
The implementation of this feature assumes that all IDs are |
77 |
|
defined before they are used. The normalized ID values are of the |
78 |
|
form 'D<NUM>' where <NUM> is a counter starting with 0, so the |
79 |
|
first ID value will become 'D0', the second 'D1', etc. |
80 |
|
|
81 |
|
Which attributes are IDs or IDREFS is defined with the |
82 |
|
correspoding constructor arguments. |
83 |
|
|
84 |
|
- Filenames are normalized with os.path.normpath. Which attributes |
85 |
|
are filenames is defiend with the corresponding constructor |
86 |
|
argument. |
87 |
""" |
""" |
88 |
|
|
89 |
def __init__(self): |
def __init__(self, ids = (), idrefs = (), filenames = ()): |
90 |
|
"""Initialize the SaxEventLister |
91 |
|
|
92 |
|
The ids and idrefs parameters should be lists of (element, attr) |
93 |
|
pairs where element is the name of an attribute as passed to the |
94 |
|
startElementNS method and attr is the name of an attribute as |
95 |
|
used in the mapping passed to startElementNS, so both name and |
96 |
|
attr usually must include the namespace. |
97 |
|
|
98 |
|
The filenames parameter should be a sequence of the same form as |
99 |
|
ids and idrefs identifying the attributes which are filenames |
100 |
|
that should be normalized. |
101 |
|
""" |
102 |
self.eventlist = [] |
self.eventlist = [] |
103 |
|
self.ids = ids |
104 |
|
self.idrefs = idrefs |
105 |
|
self.idremap = {} |
106 |
|
self.filenames = filenames |
107 |
|
|
108 |
def startElementNS(self, name, qname, attrs): |
def startElementNS(self, name, qname, attrs): |
109 |
items = attrs.items() |
items = attrs.items() |
110 |
items.sort() |
items.sort() |
111 |
self.eventlist.append(("start", name, qname, items)) |
for i, (attr, value) in zip(range(len(items)), items): |
112 |
|
#print '++++' |
113 |
|
#print self.idremap |
114 |
|
#print name, attr, value |
115 |
|
if (name, attr) in self.ids: |
116 |
|
newid = len(self.idremap) |
117 |
|
self.idremap[value] = "D" + str(newid) |
118 |
|
value = self.idremap[value] |
119 |
|
elif (name, attr) in self.idrefs: |
120 |
|
value = self.idremap[value] |
121 |
|
elif (name, attr) in self.filenames: |
122 |
|
value = os.path.normpath(value) |
123 |
|
items[i] = (attr, value) |
124 |
|
#print name, attr, value |
125 |
|
self.eventlist.append(("start", name, items)) |
126 |
|
|
127 |
def endElementNS(self, name, qname): |
def endElementNS(self, name, qname): |
128 |
self.eventlist.append(("end", name, qname)) |
self.eventlist.append(("end", name)) |
129 |
|
|
130 |
|
|
131 |
def sax_eventlist(data = None, filename = None): |
def sax_eventlist(data = None, filename = None, |
132 |
|
ids = (), idrefs = (), filenames = ()): |
133 |
"""Return a list of SAX event generated for a given XML source |
"""Return a list of SAX event generated for a given XML source |
134 |
|
|
135 |
The xml source may either be a string with the actual XML, in which |
The xml source may either be a string with the actual XML, in which |
138 |
""" |
""" |
139 |
if filename is not None: |
if filename is not None: |
140 |
data = open(filename).read() |
data = open(filename).read() |
141 |
handler = SaxEventLister() |
handler = SaxEventLister(ids = ids, idrefs = idrefs, filenames = filenames) |
142 |
parser = make_parser() |
parser = make_parser() |
143 |
parser.setContentHandler(handler) |
parser.setContentHandler(handler) |
144 |
parser.setErrorHandler(ErrorHandler()) |
parser.setErrorHandler(ErrorHandler()) |