59 |
class SaxEventLister(xml.sax.handler.ContentHandler): |
class SaxEventLister(xml.sax.handler.ContentHandler): |
60 |
|
|
61 |
"""Create a normalized list representation containing the SAX events |
"""Create a normalized list representation containing the SAX events |
62 |
|
|
63 |
|
The normalization includes the following |
64 |
|
|
65 |
|
- The attribute dictionary of a staertElement event is converted to |
66 |
|
a sorted list of (key, value) pairs |
67 |
|
|
68 |
|
- ID and IDREF attribute values are normalized in such a way that |
69 |
|
two documents that only use different values for IDs can still |
70 |
|
lead to the same normalized representation. |
71 |
|
|
72 |
|
The implementation of this feature assumes that all IDs are |
73 |
|
defined before they are used. The normalized ID values are of the |
74 |
|
form 'D<NUM>' where <NUM> is a counter starting with 0, so the |
75 |
|
first ID value will become 'D0', the second 'D1', etc. |
76 |
|
|
77 |
|
Which attributes are IDs or IDREFS is defined with the |
78 |
|
correspoding constructor arguments. |
79 |
""" |
""" |
80 |
|
|
81 |
def __init__(self): |
def __init__(self, ids = (), idrefs = ()): |
82 |
|
"""Initialize the SaxEventLister |
83 |
|
|
84 |
|
The ids and idrefs parameters should be lists of (element, attr) |
85 |
|
pairs where name is the name of an attribute as passed to the |
86 |
|
startElementNS method and attr is the name of an attribute as |
87 |
|
used in the mapping passed to startElementNS, so both name and |
88 |
|
attr usually must include the namespace. |
89 |
|
""" |
90 |
self.eventlist = [] |
self.eventlist = [] |
91 |
|
self.ids = ids |
92 |
|
self.idrefs = idrefs |
93 |
|
self.idremap = {} |
94 |
|
|
95 |
def startElementNS(self, name, qname, attrs): |
def startElementNS(self, name, qname, attrs): |
96 |
items = attrs.items() |
items = attrs.items() |
97 |
items.sort() |
items.sort() |
98 |
|
for i, (attr, value) in zip(range(len(items)), items): |
99 |
|
#print '++++' |
100 |
|
#print self.idremap |
101 |
|
#print name, attr, value |
102 |
|
if (name, attr) in self.ids: |
103 |
|
newid = len(self.idremap) |
104 |
|
self.idremap[value] = "D" + str(newid) |
105 |
|
value = self.idremap[value] |
106 |
|
elif (name, attr) in self.idrefs: |
107 |
|
value = self.idremap[value] |
108 |
|
items[i] = (attr, value) |
109 |
|
#print name, attr, value |
110 |
self.eventlist.append(("start", name, qname, items)) |
self.eventlist.append(("start", name, qname, items)) |
111 |
|
|
112 |
def endElementNS(self, name, qname): |
def endElementNS(self, name, qname): |
113 |
self.eventlist.append(("end", name, qname)) |
self.eventlist.append(("end", name, qname)) |
114 |
|
|
115 |
|
|
116 |
def sax_eventlist(data = None, filename = None): |
def sax_eventlist(data = None, filename = None, |
117 |
|
ids = (), idrefs = ()): |
118 |
"""Return a list of SAX event generated for a given XML source |
"""Return a list of SAX event generated for a given XML source |
119 |
|
|
120 |
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 |
123 |
""" |
""" |
124 |
if filename is not None: |
if filename is not None: |
125 |
data = open(filename).read() |
data = open(filename).read() |
126 |
handler = SaxEventLister() |
handler = SaxEventLister(ids = ids, idrefs = idrefs) |
127 |
parser = make_parser() |
parser = make_parser() |
128 |
parser.setContentHandler(handler) |
parser.setContentHandler(handler) |
129 |
parser.setErrorHandler(ErrorHandler()) |
parser.setErrorHandler(ErrorHandler()) |