3 from xml.sax.saxutils import XMLFilterBase, XMLGenerator
4 from xml.sax import make_parser
7 path = [os.path.curdir, 'extensions']
8 def find_file_on_path(name):
10 test = os.path.join(d, name)
11 if os.path.isfile(test):
13 raise OSError(errno.ENOENT, os.strerror(errno.ENOENT), name)
17 class ProcessImports(XMLFilterBase):
18 def setContentHandler(self, handler):
19 self.handler = handler
20 XMLFilterBase.setContentHandler(self, handler)
22 def ensure(self, name):
23 if not seen.has_key(name):
24 child = ProcessImports(make_parser())
25 child.setContentHandler(self.handler)
26 child.parse(find_file_on_path(name + '.xml'))
28 def startDocument(self):
30 def endDocument(self):
35 def startElement(self, name, attrs):
36 assert self.inimport is None
40 XMLFilterBase.startElement(self, name, attrs)
42 seen[attrs['header']] = True
45 def characters(self, content):
46 if self.inimport is not None:
47 self.inimport += content
49 XMLFilterBase.characters(self, content)
51 def endElement(self, name):
53 self.ensure(self.inimport)
56 XMLFilterBase.endElement(self, name)
59 importer = ProcessImports(make_parser())
60 importer.setContentHandler(out)
63 importer.parse(sys.argv[1])
65 importer.parse(sys.stdin)