1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
#!/usr/bin/python
from xml.sax.saxutils import XMLFilterBase, XMLGenerator
from xml.sax import make_parser
import sys, os
path = [os.path.curdir, 'extensions']
def find_file_on_path(name):
for d in path:
test = os.path.join(d, name)
if os.path.isfile(test):
return test
raise OSError(errno.ENOENT, os.strerror(errno.ENOENT), name)
seen = {}
class ProcessImports(XMLFilterBase):
def setContentHandler(self, handler):
self.handler = handler
XMLFilterBase.setContentHandler(self, handler)
def ensure(self, name):
if not seen.has_key(name):
child = ProcessImports(make_parser())
child.setContentHandler(self.handler)
child.parse(find_file_on_path(name + '.xml'))
def startDocument(self):
pass
def endDocument(self):
pass
inimport = None
def startElement(self, name, attrs):
assert self.inimport is None
if name == 'import':
self.inimport = ""
return
XMLFilterBase.startElement(self, name, attrs)
if name == 'xcb':
seen[attrs['header']] = True
self.ensure('xproto')
def characters(self, content):
if self.inimport is not None:
self.inimport += content
else:
XMLFilterBase.characters(self, content)
def endElement(self, name):
if name == 'import':
self.ensure(self.inimport)
self.inimport = None
return
XMLFilterBase.endElement(self, name)
out = XMLGenerator()
importer = ProcessImports(make_parser())
importer.setContentHandler(out)
out.startDocument()
if len(sys.argv) > 1:
importer.parse(sys.argv[1])
else:
importer.parse(sys.stdin)
out.endDocument()
|