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 |
80 |
|
|
81 |
Which attributes are IDs or IDREFS is defined with the |
Which attributes are IDs or IDREFS is defined with the |
82 |
correspoding constructor arguments. |
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, ids = (), idrefs = ()): |
def __init__(self, ids = (), idrefs = (), filenames = ()): |
90 |
"""Initialize the SaxEventLister |
"""Initialize the SaxEventLister |
91 |
|
|
92 |
The ids and idrefs parameters should be lists of (element, attr) |
The ids and idrefs parameters should be lists of (element, attr) |
93 |
pairs where name is the name of an attribute as passed to the |
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 |
startElementNS method and attr is the name of an attribute as |
95 |
used in the mapping passed to startElementNS, so both name and |
used in the mapping passed to startElementNS, so both name and |
96 |
attr usually must include the namespace. |
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 |
self.ids = ids |
104 |
self.idrefs = idrefs |
self.idrefs = idrefs |
105 |
self.idremap = {} |
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() |
118 |
value = self.idremap[value] |
value = self.idremap[value] |
119 |
elif (name, attr) in self.idrefs: |
elif (name, attr) in self.idrefs: |
120 |
value = self.idremap[value] |
value = self.idremap[value] |
121 |
|
elif (name, attr) in self.filenames: |
122 |
|
value = os.path.normpath(value) |
123 |
items[i] = (attr, value) |
items[i] = (attr, value) |
124 |
#print name, attr, value |
#print name, attr, value |
125 |
self.eventlist.append(("start", name, items)) |
self.eventlist.append(("start", name, items)) |
129 |
|
|
130 |
|
|
131 |
def sax_eventlist(data = None, filename = None, |
def sax_eventlist(data = None, filename = None, |
132 |
ids = (), idrefs = ()): |
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(ids = ids, idrefs = idrefs) |
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()) |